|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 6 | + "github.com/aws/aws-sdk-go-v2/service/acm" |
| 7 | + "github.com/aws/aws-sdk-go-v2/service/ec2" |
| 8 | + "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2" |
| 9 | + "github.com/aws/aws-sdk-go-v2/service/resourcegroupstaggingapi" |
| 10 | + "github.com/aws/aws-sdk-go-v2/service/shield" |
| 11 | + "github.com/aws/aws-sdk-go-v2/service/wafregional" |
| 12 | + "github.com/aws/aws-sdk-go-v2/service/wafv2" |
| 13 | + "sigs.k8s.io/aws-load-balancer-controller/pkg/aws/endpoints" |
| 14 | +) |
| 15 | + |
| 16 | +type defaultAWSClientsProvider struct { |
| 17 | + ec2Client *ec2.Client |
| 18 | + elbv2Client *elasticloadbalancingv2.Client |
| 19 | + acmClient *acm.Client |
| 20 | + wafv2Client *wafv2.Client |
| 21 | + wafRegionClient *wafregional.Client |
| 22 | + shieldClient *shield.Client |
| 23 | + rgtClient *resourcegroupstaggingapi.Client |
| 24 | +} |
| 25 | + |
| 26 | +func NewDefaultAWSClientsProvider(cfg aws.Config, endpointsResolver *endpoints.Resolver) (*defaultAWSClientsProvider, error) { |
| 27 | + ec2CustomEndpoint := endpointsResolver.EndpointFor(ec2.ServiceID) |
| 28 | + elbv2CustomEndpoint := endpointsResolver.EndpointFor(elasticloadbalancingv2.ServiceID) |
| 29 | + acmCustomEndpoint := endpointsResolver.EndpointFor(acm.ServiceID) |
| 30 | + wafv2CustomEndpoint := endpointsResolver.EndpointFor(wafv2.ServiceID) |
| 31 | + wafregionalCustomEndpoint := endpointsResolver.EndpointFor(wafregional.ServiceID) |
| 32 | + shieldCustomEndpoint := endpointsResolver.EndpointFor(shield.ServiceID) |
| 33 | + rgtCustomEndpoint := endpointsResolver.EndpointFor(resourcegroupstaggingapi.ServiceID) |
| 34 | + |
| 35 | + ec2Client := ec2.NewFromConfig(cfg, func(o *ec2.Options) { |
| 36 | + if ec2CustomEndpoint != nil { |
| 37 | + o.BaseEndpoint = ec2CustomEndpoint |
| 38 | + } |
| 39 | + }) |
| 40 | + elbv2Client := elasticloadbalancingv2.NewFromConfig(cfg, func(o *elasticloadbalancingv2.Options) { |
| 41 | + if elbv2CustomEndpoint != nil { |
| 42 | + o.BaseEndpoint = elbv2CustomEndpoint |
| 43 | + } |
| 44 | + }) |
| 45 | + acmClient := acm.NewFromConfig(cfg, func(o *acm.Options) { |
| 46 | + if acmCustomEndpoint != nil { |
| 47 | + o.BaseEndpoint = acmCustomEndpoint |
| 48 | + } |
| 49 | + }) |
| 50 | + wafv2Client := wafv2.NewFromConfig(cfg, func(o *wafv2.Options) { |
| 51 | + if wafv2CustomEndpoint != nil { |
| 52 | + o.BaseEndpoint = wafv2CustomEndpoint |
| 53 | + } |
| 54 | + }) |
| 55 | + wafregionalClient := wafregional.NewFromConfig(cfg, func(o *wafregional.Options) { |
| 56 | + o.Region = cfg.Region |
| 57 | + o.BaseEndpoint = wafregionalCustomEndpoint |
| 58 | + }) |
| 59 | + sheildClient := shield.NewFromConfig(cfg, func(o *shield.Options) { |
| 60 | + o.Region = "us-east-1" |
| 61 | + o.BaseEndpoint = shieldCustomEndpoint |
| 62 | + }) |
| 63 | + rgtClient := resourcegroupstaggingapi.NewFromConfig(cfg, func(o *resourcegroupstaggingapi.Options) { |
| 64 | + if rgtCustomEndpoint != nil { |
| 65 | + o.BaseEndpoint = rgtCustomEndpoint |
| 66 | + } |
| 67 | + }) |
| 68 | + |
| 69 | + return &defaultAWSClientsProvider{ |
| 70 | + ec2Client: ec2Client, |
| 71 | + elbv2Client: elbv2Client, |
| 72 | + acmClient: acmClient, |
| 73 | + wafv2Client: wafv2Client, |
| 74 | + wafRegionClient: wafregionalClient, |
| 75 | + shieldClient: sheildClient, |
| 76 | + rgtClient: rgtClient, |
| 77 | + }, nil |
| 78 | +} |
| 79 | + |
| 80 | +// DO NOT REMOVE operationName as parameter, this is on purpose |
| 81 | +// to retain the default behavior for OSS controller to use the default client for each aws service |
| 82 | +// for our internal controller, we will choose different client based on operationName |
| 83 | +func (p *defaultAWSClientsProvider) GetEC2Client(ctx context.Context, operationName string) (*ec2.Client, error) { |
| 84 | + return p.ec2Client, nil |
| 85 | +} |
| 86 | + |
| 87 | +func (p *defaultAWSClientsProvider) GetELBv2Client(ctx context.Context, operationName string) (*elasticloadbalancingv2.Client, error) { |
| 88 | + return p.elbv2Client, nil |
| 89 | +} |
| 90 | + |
| 91 | +func (p *defaultAWSClientsProvider) GetACMClient(ctx context.Context, operationName string) (*acm.Client, error) { |
| 92 | + return p.acmClient, nil |
| 93 | +} |
| 94 | + |
| 95 | +func (p *defaultAWSClientsProvider) GetWAFv2Client(ctx context.Context, operationName string) (*wafv2.Client, error) { |
| 96 | + return p.wafv2Client, nil |
| 97 | +} |
| 98 | + |
| 99 | +func (p *defaultAWSClientsProvider) GetWAFRegionClient(ctx context.Context, operationName string) (*wafregional.Client, error) { |
| 100 | + return p.wafRegionClient, nil |
| 101 | +} |
| 102 | + |
| 103 | +func (p *defaultAWSClientsProvider) GetShieldClient(ctx context.Context, operationName string) (*shield.Client, error) { |
| 104 | + return p.shieldClient, nil |
| 105 | +} |
| 106 | + |
| 107 | +func (p *defaultAWSClientsProvider) GetRGTClient(ctx context.Context, operationName string) (*resourcegroupstaggingapi.Client, error) { |
| 108 | + return p.rgtClient, nil |
| 109 | +} |
0 commit comments