Skip to content

Commit

Permalink
use service level endpoint resolver instead of global endpoint resolv…
Browse files Browse the repository at this point in the history
…er which was deprecated
  • Loading branch information
TiberiuGC committed Jun 3, 2024
1 parent bb87f30 commit e64db43
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 66 deletions.
11 changes: 3 additions & 8 deletions pkg/eks/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,9 @@ func newAWSProvider(spec *api.ProviderConfig, configurationLoader AWSConfigurati

provider.asg = autoscaling.NewFromConfig(cfg)
provider.cloudwatchlogs = cloudwatchlogs.NewFromConfig(cfg)
provider.cloudtrail = cloudtrail.NewFromConfig(cfg)

if endpoint, ok := os.LookupEnv("AWS_CLOUDTRAIL_ENDPOINT"); ok {
logger.Debug("Setting CloudTrail endpoint to %s", endpoint)
provider.cloudtrail = cloudtrail.NewFromConfig(cfg, func(o *cloudtrail.Options) {
o.BaseEndpoint = &endpoint
})
}
provider.cloudtrail = cloudtrail.NewFromConfig(cfg, func(o *cloudtrail.Options) {
o.BaseEndpoint = getBaseEndpoint(cloudtrail.ServiceID, "AWS_CLOUDTRAIL_ENDPOINT")
})

return provider, nil
}
Expand Down
53 changes: 0 additions & 53 deletions pkg/eks/apiv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,12 @@ package eks
import (
"context"
"fmt"
"os"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
middlewarev2 "github.com/aws/aws-sdk-go-v2/aws/middleware"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
"github.com/aws/aws-sdk-go-v2/service/cloudformation"
"github.com/aws/aws-sdk-go-v2/service/cloudtrail"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/eks"
"github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing"
"github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2"
"github.com/aws/aws-sdk-go-v2/service/iam"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/aws/smithy-go/middleware"
"github.com/gofrs/flock"
"github.com/kris-nova/logger"
Expand Down Expand Up @@ -55,10 +46,6 @@ func newV2Config(pc *api.ProviderConfig, credentialsCacheFilePath string, config
}
options = append(options, config.WithClientLogMode(clientLogMode))

if endpointResolver := makeEndpointResolverFunc(); endpointResolver != nil {
options = append(options, config.WithEndpointResolverWithOptions(endpointResolver))
}

if !pc.Profile.SourceIsEnvVar {
options = append(options, config.WithSharedConfigProfile(pc.Profile.Name))
}
Expand Down Expand Up @@ -99,43 +86,3 @@ func newV2Config(pc *api.ProviderConfig, credentialsCacheFilePath string, config
}
return cfg, nil
}

func makeEndpointResolverFunc() aws.EndpointResolverWithOptionsFunc {
serviceIDEnvMap := map[string]string{
cloudformation.ServiceID: "AWS_CLOUDFORMATION_ENDPOINT",
eks.ServiceID: "AWS_EKS_ENDPOINT",
ec2.ServiceID: "AWS_EC2_ENDPOINT",
elasticloadbalancing.ServiceID: "AWS_ELB_ENDPOINT",
elasticloadbalancingv2.ServiceID: "AWS_ELBV2_ENDPOINT",
sts.ServiceID: "AWS_STS_ENDPOINT",
iam.ServiceID: "AWS_IAM_ENDPOINT",
cloudtrail.ServiceID: "AWS_CLOUDTRAIL_ENDPOINT",
}

hasCustomEndpoint := false
for service, envName := range serviceIDEnvMap {
if endpoint, ok := os.LookupEnv(envName); ok {
logger.Debug(
"Setting %s endpoint to %s", service, endpoint)
hasCustomEndpoint = true
}
}

if !hasCustomEndpoint {
return nil
}

return func(service, region string, options ...interface{}) (aws.Endpoint, error) {
if envName, ok := serviceIDEnvMap[service]; ok {
if ok {
if endpoint, ok := os.LookupEnv(envName); ok {
return aws.Endpoint{
URL: endpoint,
SigningRegion: region,
}, nil
}
}
}
return aws.Endpoint{}, &aws.EndpointNotFoundError{}
}
}
33 changes: 28 additions & 5 deletions pkg/eks/services_v2.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eks

import (
"os"
"sync"

"github.com/aws/aws-sdk-go-v2/aws"
Expand All @@ -14,6 +15,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/outposts"
"github.com/aws/aws-sdk-go-v2/service/ssm"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/kris-nova/logger"

api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/awsapi"
Expand Down Expand Up @@ -45,6 +47,7 @@ func (s *ServicesV2) STS() awsapi.STS {
defer s.mu.Unlock()
if s.sts == nil {
s.sts = sts.NewFromConfig(s.config, func(o *sts.Options) {
o.BaseEndpoint = getBaseEndpoint(sts.ServiceID, "AWS_STS_ENDPOINT")
// Disable retryer for STS
// (see https://github.com/eksctl-io/eksctl/issues/705)
o.Retryer = aws.NopRetryer{}
Expand Down Expand Up @@ -75,6 +78,7 @@ func (s *ServicesV2) CloudFormation() awsapi.CloudFormation {
defer s.mu.Unlock()
if s.cloudformation == nil {
s.cloudformation = cloudformation.NewFromConfig(s.config, func(o *cloudformation.Options) {
o.BaseEndpoint = getBaseEndpoint(cloudformation.ServiceID, "AWS_CLOUDFORMATION_ENDPOINT")
// Use adaptive mode for retrying CloudFormation requests to mimic
// the logic used for AWS SDK v1.
o.Retryer = retry.NewAdaptiveMode(func(o *retry.AdaptiveModeOptions) {
Expand All @@ -94,7 +98,9 @@ func (s *ServicesV2) ELB() awsapi.ELB {
s.mu.Lock()
defer s.mu.Unlock()
if s.elasticloadbalancing == nil {
s.elasticloadbalancing = elasticloadbalancing.NewFromConfig(s.config)
s.elasticloadbalancing = elasticloadbalancing.NewFromConfig(s.config, func(o *elasticloadbalancing.Options) {
o.BaseEndpoint = getBaseEndpoint(elasticloadbalancing.ServiceID, "AWS_ELB_ENDPOINT")
})
}
return s.elasticloadbalancing
}
Expand All @@ -104,7 +110,9 @@ func (s *ServicesV2) ELBV2() awsapi.ELBV2 {
s.mu.Lock()
defer s.mu.Unlock()
if s.elasticloadbalancingV2 == nil {
s.elasticloadbalancingV2 = elasticloadbalancingv2.NewFromConfig(s.config)
s.elasticloadbalancingV2 = elasticloadbalancingv2.NewFromConfig(s.config, func(o *elasticloadbalancingv2.Options) {
o.BaseEndpoint = getBaseEndpoint(elasticloadbalancingv2.ServiceID, "AWS_ELBV2_ENDPOINT")
})
}
return s.elasticloadbalancingV2
}
Expand All @@ -124,7 +132,9 @@ func (s *ServicesV2) IAM() awsapi.IAM {
s.mu.Lock()
defer s.mu.Unlock()
if s.iam == nil {
s.iam = iam.NewFromConfig(s.config)
s.iam = iam.NewFromConfig(s.config, func(o *iam.Options) {
o.BaseEndpoint = getBaseEndpoint(iam.ServiceID, "AWS_IAM_ENDPOINT")
})
}
return s.iam
}
Expand All @@ -134,7 +144,9 @@ func (s *ServicesV2) EC2() awsapi.EC2 {
s.mu.Lock()
defer s.mu.Unlock()
if s.ec2 == nil {
s.ec2 = ec2.NewFromConfig(s.config)
s.ec2 = ec2.NewFromConfig(s.config, func(o *ec2.Options) {
o.BaseEndpoint = getBaseEndpoint(ec2.ServiceID, "AWS_EC2_ENDPOINT")
})
}
return s.ec2
}
Expand All @@ -144,7 +156,9 @@ func (s *ServicesV2) EKS() awsapi.EKS {
s.mu.Lock()
defer s.mu.Unlock()
if s.eks == nil {
s.eks = eks.NewFromConfig(s.config)
s.eks = eks.NewFromConfig(s.config, func(o *eks.Options) {
o.BaseEndpoint = getBaseEndpoint(eks.ServiceID, "AWS_EKS_ENDPOINT")
})
}
return s.eks
}
Expand All @@ -166,3 +180,12 @@ func (s *ServicesV2) AWSConfig() aws.Config {
func (s *ServicesV2) CredentialsProvider() aws.CredentialsProvider {
return s.config.Credentials
}

func getBaseEndpoint(serviceID, endpoint string) *string {
if endpoint, ok := os.LookupEnv(endpoint); ok {
logger.Debug(
"Setting %s endpoint to %s", serviceID, endpoint)
return aws.String(endpoint)
}
return nil
}

0 comments on commit e64db43

Please sign in to comment.