Skip to content

Commit e64db43

Browse files
committed
use service level endpoint resolver instead of global endpoint resolver which was deprecated
1 parent bb87f30 commit e64db43

3 files changed

Lines changed: 31 additions & 66 deletions

File tree

pkg/eks/api.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,9 @@ func newAWSProvider(spec *api.ProviderConfig, configurationLoader AWSConfigurati
200200

201201
provider.asg = autoscaling.NewFromConfig(cfg)
202202
provider.cloudwatchlogs = cloudwatchlogs.NewFromConfig(cfg)
203-
provider.cloudtrail = cloudtrail.NewFromConfig(cfg)
204-
205-
if endpoint, ok := os.LookupEnv("AWS_CLOUDTRAIL_ENDPOINT"); ok {
206-
logger.Debug("Setting CloudTrail endpoint to %s", endpoint)
207-
provider.cloudtrail = cloudtrail.NewFromConfig(cfg, func(o *cloudtrail.Options) {
208-
o.BaseEndpoint = &endpoint
209-
})
210-
}
203+
provider.cloudtrail = cloudtrail.NewFromConfig(cfg, func(o *cloudtrail.Options) {
204+
o.BaseEndpoint = getBaseEndpoint(cloudtrail.ServiceID, "AWS_CLOUDTRAIL_ENDPOINT")
205+
})
211206

212207
return provider, nil
213208
}

pkg/eks/apiv2.go

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,12 @@ package eks
33
import (
44
"context"
55
"fmt"
6-
"os"
76
"time"
87

98
"github.com/aws/aws-sdk-go-v2/aws"
109
middlewarev2 "github.com/aws/aws-sdk-go-v2/aws/middleware"
1110
"github.com/aws/aws-sdk-go-v2/config"
1211
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
13-
"github.com/aws/aws-sdk-go-v2/service/cloudformation"
14-
"github.com/aws/aws-sdk-go-v2/service/cloudtrail"
15-
"github.com/aws/aws-sdk-go-v2/service/ec2"
16-
"github.com/aws/aws-sdk-go-v2/service/eks"
17-
"github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing"
18-
"github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2"
19-
"github.com/aws/aws-sdk-go-v2/service/iam"
20-
"github.com/aws/aws-sdk-go-v2/service/sts"
2112
"github.com/aws/smithy-go/middleware"
2213
"github.com/gofrs/flock"
2314
"github.com/kris-nova/logger"
@@ -55,10 +46,6 @@ func newV2Config(pc *api.ProviderConfig, credentialsCacheFilePath string, config
5546
}
5647
options = append(options, config.WithClientLogMode(clientLogMode))
5748

58-
if endpointResolver := makeEndpointResolverFunc(); endpointResolver != nil {
59-
options = append(options, config.WithEndpointResolverWithOptions(endpointResolver))
60-
}
61-
6249
if !pc.Profile.SourceIsEnvVar {
6350
options = append(options, config.WithSharedConfigProfile(pc.Profile.Name))
6451
}
@@ -99,43 +86,3 @@ func newV2Config(pc *api.ProviderConfig, credentialsCacheFilePath string, config
9986
}
10087
return cfg, nil
10188
}
102-
103-
func makeEndpointResolverFunc() aws.EndpointResolverWithOptionsFunc {
104-
serviceIDEnvMap := map[string]string{
105-
cloudformation.ServiceID: "AWS_CLOUDFORMATION_ENDPOINT",
106-
eks.ServiceID: "AWS_EKS_ENDPOINT",
107-
ec2.ServiceID: "AWS_EC2_ENDPOINT",
108-
elasticloadbalancing.ServiceID: "AWS_ELB_ENDPOINT",
109-
elasticloadbalancingv2.ServiceID: "AWS_ELBV2_ENDPOINT",
110-
sts.ServiceID: "AWS_STS_ENDPOINT",
111-
iam.ServiceID: "AWS_IAM_ENDPOINT",
112-
cloudtrail.ServiceID: "AWS_CLOUDTRAIL_ENDPOINT",
113-
}
114-
115-
hasCustomEndpoint := false
116-
for service, envName := range serviceIDEnvMap {
117-
if endpoint, ok := os.LookupEnv(envName); ok {
118-
logger.Debug(
119-
"Setting %s endpoint to %s", service, endpoint)
120-
hasCustomEndpoint = true
121-
}
122-
}
123-
124-
if !hasCustomEndpoint {
125-
return nil
126-
}
127-
128-
return func(service, region string, options ...interface{}) (aws.Endpoint, error) {
129-
if envName, ok := serviceIDEnvMap[service]; ok {
130-
if ok {
131-
if endpoint, ok := os.LookupEnv(envName); ok {
132-
return aws.Endpoint{
133-
URL: endpoint,
134-
SigningRegion: region,
135-
}, nil
136-
}
137-
}
138-
}
139-
return aws.Endpoint{}, &aws.EndpointNotFoundError{}
140-
}
141-
}

pkg/eks/services_v2.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package eks
22

33
import (
4+
"os"
45
"sync"
56

67
"github.com/aws/aws-sdk-go-v2/aws"
@@ -14,6 +15,7 @@ import (
1415
"github.com/aws/aws-sdk-go-v2/service/outposts"
1516
"github.com/aws/aws-sdk-go-v2/service/ssm"
1617
"github.com/aws/aws-sdk-go-v2/service/sts"
18+
"github.com/kris-nova/logger"
1719

1820
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
1921
"github.com/weaveworks/eksctl/pkg/awsapi"
@@ -45,6 +47,7 @@ func (s *ServicesV2) STS() awsapi.STS {
4547
defer s.mu.Unlock()
4648
if s.sts == nil {
4749
s.sts = sts.NewFromConfig(s.config, func(o *sts.Options) {
50+
o.BaseEndpoint = getBaseEndpoint(sts.ServiceID, "AWS_STS_ENDPOINT")
4851
// Disable retryer for STS
4952
// (see https://github.com/eksctl-io/eksctl/issues/705)
5053
o.Retryer = aws.NopRetryer{}
@@ -75,6 +78,7 @@ func (s *ServicesV2) CloudFormation() awsapi.CloudFormation {
7578
defer s.mu.Unlock()
7679
if s.cloudformation == nil {
7780
s.cloudformation = cloudformation.NewFromConfig(s.config, func(o *cloudformation.Options) {
81+
o.BaseEndpoint = getBaseEndpoint(cloudformation.ServiceID, "AWS_CLOUDFORMATION_ENDPOINT")
7882
// Use adaptive mode for retrying CloudFormation requests to mimic
7983
// the logic used for AWS SDK v1.
8084
o.Retryer = retry.NewAdaptiveMode(func(o *retry.AdaptiveModeOptions) {
@@ -94,7 +98,9 @@ func (s *ServicesV2) ELB() awsapi.ELB {
9498
s.mu.Lock()
9599
defer s.mu.Unlock()
96100
if s.elasticloadbalancing == nil {
97-
s.elasticloadbalancing = elasticloadbalancing.NewFromConfig(s.config)
101+
s.elasticloadbalancing = elasticloadbalancing.NewFromConfig(s.config, func(o *elasticloadbalancing.Options) {
102+
o.BaseEndpoint = getBaseEndpoint(elasticloadbalancing.ServiceID, "AWS_ELB_ENDPOINT")
103+
})
98104
}
99105
return s.elasticloadbalancing
100106
}
@@ -104,7 +110,9 @@ func (s *ServicesV2) ELBV2() awsapi.ELBV2 {
104110
s.mu.Lock()
105111
defer s.mu.Unlock()
106112
if s.elasticloadbalancingV2 == nil {
107-
s.elasticloadbalancingV2 = elasticloadbalancingv2.NewFromConfig(s.config)
113+
s.elasticloadbalancingV2 = elasticloadbalancingv2.NewFromConfig(s.config, func(o *elasticloadbalancingv2.Options) {
114+
o.BaseEndpoint = getBaseEndpoint(elasticloadbalancingv2.ServiceID, "AWS_ELBV2_ENDPOINT")
115+
})
108116
}
109117
return s.elasticloadbalancingV2
110118
}
@@ -124,7 +132,9 @@ func (s *ServicesV2) IAM() awsapi.IAM {
124132
s.mu.Lock()
125133
defer s.mu.Unlock()
126134
if s.iam == nil {
127-
s.iam = iam.NewFromConfig(s.config)
135+
s.iam = iam.NewFromConfig(s.config, func(o *iam.Options) {
136+
o.BaseEndpoint = getBaseEndpoint(iam.ServiceID, "AWS_IAM_ENDPOINT")
137+
})
128138
}
129139
return s.iam
130140
}
@@ -134,7 +144,9 @@ func (s *ServicesV2) EC2() awsapi.EC2 {
134144
s.mu.Lock()
135145
defer s.mu.Unlock()
136146
if s.ec2 == nil {
137-
s.ec2 = ec2.NewFromConfig(s.config)
147+
s.ec2 = ec2.NewFromConfig(s.config, func(o *ec2.Options) {
148+
o.BaseEndpoint = getBaseEndpoint(ec2.ServiceID, "AWS_EC2_ENDPOINT")
149+
})
138150
}
139151
return s.ec2
140152
}
@@ -144,7 +156,9 @@ func (s *ServicesV2) EKS() awsapi.EKS {
144156
s.mu.Lock()
145157
defer s.mu.Unlock()
146158
if s.eks == nil {
147-
s.eks = eks.NewFromConfig(s.config)
159+
s.eks = eks.NewFromConfig(s.config, func(o *eks.Options) {
160+
o.BaseEndpoint = getBaseEndpoint(eks.ServiceID, "AWS_EKS_ENDPOINT")
161+
})
148162
}
149163
return s.eks
150164
}
@@ -166,3 +180,12 @@ func (s *ServicesV2) AWSConfig() aws.Config {
166180
func (s *ServicesV2) CredentialsProvider() aws.CredentialsProvider {
167181
return s.config.Credentials
168182
}
183+
184+
func getBaseEndpoint(serviceID, endpoint string) *string {
185+
if endpoint, ok := os.LookupEnv(endpoint); ok {
186+
logger.Debug(
187+
"Setting %s endpoint to %s", serviceID, endpoint)
188+
return aws.String(endpoint)
189+
}
190+
return nil
191+
}

0 commit comments

Comments
 (0)