Skip to content

Commit b6d7cf6

Browse files
committed
migrate EKS client from e2e to AWS SDK V2
Signed-off-by: Pankaj Walke <[email protected]>
1 parent abacc75 commit b6d7cf6

23 files changed

+151
-120
lines changed

go.mod

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ require (
1212
github.com/aws/aws-lambda-go v1.41.0
1313
github.com/aws/aws-sdk-go v1.55.5
1414
github.com/aws/aws-sdk-go-v2 v1.36.3
15-
github.com/aws/aws-sdk-go-v2 v1.36.3
1615
github.com/aws/aws-sdk-go-v2/config v1.27.11
1716
github.com/aws/aws-sdk-go-v2/credentials v1.17.11
1817
github.com/aws/aws-sdk-go-v2/service/autoscaling v1.52.4
19-
github.com/aws/aws-sdk-go-v2/service/iam v1.32.0
2018
github.com/aws/aws-sdk-go-v2/service/eks v1.64.0
19+
github.com/aws/aws-sdk-go-v2/service/iam v1.32.0
2120
github.com/aws/aws-sdk-go-v2/service/s3 v1.53.1
2221
github.com/aws/aws-sdk-go-v2/service/sts v1.28.6
2322
github.com/aws/smithy-go v1.22.2
@@ -83,8 +82,6 @@ require (
8382
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.1 // indirect
8483
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.34 // indirect
8584
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.34 // indirect
86-
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.34 // indirect
87-
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.34 // indirect
8885
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect
8986
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.5 // indirect
9087
github.com/aws/aws-sdk-go-v2/service/cloudformation v1.50.0 // indirect

pkg/cloud/services/eks/addons.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ func (s *Service) translateAPIToAddon(addons []ekscontrolplanev1.Addon) []*eksad
210210
return converted
211211
}
212212

213+
// WaitUntilAddonDeleted is blocking function to wait until EKS Nodegroup is Deleted.
213214
func (k *EKSClient) WaitUntilAddonDeleted(ctx context.Context, input *eks.DescribeAddonInput) error {
214215
waiter := eks.NewAddonDeletedWaiter(k, func(o *eks.AddonDeletedWaiterOptions) {
215216
o.LogWaitAttempts = true

pkg/cloud/services/eks/cluster.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,7 @@ func getKeyArn(encryptionConfig ekstypes.EncryptionConfig) string {
722722
return ""
723723
}
724724

725+
// WaitUntilClusterActive is blocking function to wait until EKS Cluster is Active.
725726
func (k *EKSClient) WaitUntilClusterActive(ctx context.Context, input *eks.DescribeClusterInput) error {
726727
waiter := eks.NewClusterActiveWaiter(k, func(o *eks.ClusterActiveWaiterOptions) {
727728
o.LogWaitAttempts = true
@@ -730,12 +731,14 @@ func (k *EKSClient) WaitUntilClusterActive(ctx context.Context, input *eks.Descr
730731
return waiter.Wait(ctx, input, maxActiveUpdateDeleteWait)
731732
}
732733

734+
// WaitUntilClusterDeleted is blocking function to wait until EKS Cluster is Deleted.
733735
func (k *EKSClient) WaitUntilClusterDeleted(ctx context.Context, input *eks.DescribeClusterInput) error {
734736
waiter := eks.NewClusterDeletedWaiter(k)
735737

736738
return waiter.Wait(ctx, input, maxActiveUpdateDeleteWait)
737739
}
738740

741+
// WaitUntilClusterUpdating is blocking function to wait until EKS Cluster is Updating.
739742
func (k *EKSClient) WaitUntilClusterUpdating(ctx context.Context, input *eks.DescribeClusterInput) error {
740743
waiter := eks.NewClusterActiveWaiter(k, func(o *eks.ClusterActiveWaiterOptions) {
741744
o.LogWaitAttempts = true

pkg/cloud/services/eks/eks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
)
3434

3535
const (
36-
// Maximum duration for waiting on EKS cluster state
36+
// Maximum duration for waiting on EKS cluster state.
3737
maxActiveUpdateDeleteWait = 15 * time.Minute
3838
)
3939

pkg/cloud/services/eks/nodegroup.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,7 @@ func (s *NodegroupService) waitForNodegroupActive(ctx context.Context) (*ekstype
625625
return ng, nil
626626
}
627627

628+
// WaitUntilNodegroupDeleted is blocking function to wait until EKS Nodegroup is Deleted.
628629
func (k *EKSClient) WaitUntilNodegroupDeleted(ctx context.Context, input *eks.DescribeNodegroupInput) error {
629630
waiter := eks.NewNodegroupDeletedWaiter(k)
630631
err := waiter.Wait(ctx, input, maxActiveUpdateDeleteWait)
@@ -634,6 +635,7 @@ func (k *EKSClient) WaitUntilNodegroupDeleted(ctx context.Context, input *eks.De
634635
return nil
635636
}
636637

638+
// WaitUntilNodegroupActive is blocking function to wait until EKS Nodegroup is Active.
637639
func (k *EKSClient) WaitUntilNodegroupActive(ctx context.Context, input *eks.DescribeNodegroupInput) error {
638640
waiter := eks.NewNodegroupActiveWaiter(k, func(o *eks.NodegroupActiveWaiterOptions) {
639641
o.LogWaitAttempts = true

pkg/cloud/services/eks/oidc.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ func (s *Service) reconcileOIDCProvider(ctx context.Context, cluster *ekstypes.C
9393
}
9494

9595
func (s *Service) reconcileTrustPolicy(ctx context.Context) error {
96-
9796
clusterKey := client.ObjectKey{
9897
Name: s.scope.Name(),
9998
Namespace: s.scope.Namespace(),

pkg/cloud/services/eks/service.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ type EKSAPI interface {
7575
WaitUntilAddonDeleted(ctx context.Context, params *eks.DescribeAddonInput) error
7676
}
7777

78+
// EKSClient is a wrapper over eks.Client for implementing custom methods of EKSAPI.
7879
type EKSClient struct {
7980
*eks.Client
8081
}

pkg/eks/identityprovider/plan_test.go

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -259,49 +259,3 @@ func createDesiredIdentityProviderWithDifferentClientID(name string, tags infrav
259259
p.ClientID = "clientId2"
260260
return p
261261
}
262-
263-
// // identityProviderConfigMatcher Custom matcher for AssociateIdentityProviderConfigInput.
264-
// // As the AWS SDK V2 types have nested pointer objects and reflect.DeepEqual doesn't compare values of nested objects.
265-
// // Adopted from https://pkg.go.dev/github.com/golang/mock/gomock#Eq.
266-
// type identityProviderConfigMatcher struct {
267-
// want interface{}
268-
// }
269-
270-
// func (m identityProviderConfigMatcher) Matches(got interface{}) bool {
271-
// if m.want == nil || got == nil {
272-
// return reflect.DeepEqual(m.want, got)
273-
// }
274-
275-
// oidc, ok := got.(*eks.AssociateIdentityProviderConfigInput)
276-
// if !ok {
277-
// return false
278-
// }
279-
// oidc2, ok := m.want.(*eks.AssociateIdentityProviderConfigInput)
280-
// if !ok {
281-
// return false
282-
// }
283-
// config := oidc.Oidc
284-
// config2 := oidc2.Oidc
285-
// oidc.Oidc = nil
286-
// oidc2.Oidc = nil
287-
288-
// if !reflect.DeepEqual(oidc, oidc) {
289-
// return false
290-
// }
291-
292-
// claims := config.RequiredClaims
293-
// claims2 := config2.RequiredClaims
294-
// config.RequiredClaims = nil
295-
// config2.RequiredClaims = nil
296-
297-
// if !reflect.DeepEqual(config, config2) {
298-
// return false
299-
// }
300-
301-
// return cmp.Equal(claims, claims2)
302-
303-
// }
304-
305-
// func (m identityProviderConfigMatcher) String() string {
306-
// return fmt.Sprintf("is equal to %v (%T)", m.want, m.want)
307-
// }

test/e2e/shared/aws.go

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ import (
3434

3535
awsv2 "github.com/aws/aws-sdk-go-v2/aws"
3636
"github.com/aws/aws-sdk-go-v2/config"
37-
iam "github.com/aws/aws-sdk-go-v2/service/iam"
37+
awscredsv2 "github.com/aws/aws-sdk-go-v2/credentials"
38+
"github.com/aws/aws-sdk-go-v2/service/eks"
39+
ekstypes "github.com/aws/aws-sdk-go-v2/service/eks/types"
40+
"github.com/aws/aws-sdk-go-v2/service/iam"
3841
iamtypes "github.com/aws/aws-sdk-go-v2/service/iam/types"
3942
"github.com/aws/aws-sdk-go/aws"
4043
"github.com/aws/aws-sdk-go/aws/client"
@@ -46,7 +49,6 @@ import (
4649
"github.com/aws/aws-sdk-go/service/ec2"
4750
"github.com/aws/aws-sdk-go/service/ecrpublic"
4851
"github.com/aws/aws-sdk-go/service/efs"
49-
"github.com/aws/aws-sdk-go/service/eks"
5052
"github.com/aws/aws-sdk-go/service/elb"
5153
"github.com/aws/aws-sdk-go/service/servicequotas"
5254
"github.com/aws/aws-sdk-go/service/sts"
@@ -434,6 +436,55 @@ func NewAWSSessionWithKey(accessKey *iamtypes.AccessKey) client.ConfigProvider {
434436
return sess
435437
}
436438

439+
func NewAWSSessionV2() *awsv2.Config {
440+
By("Getting an AWS IAM session - from environment")
441+
region, err := credentials.ResolveRegion("")
442+
Expect(err).NotTo(HaveOccurred())
443+
optFns := []func(*config.LoadOptions) error{
444+
config.WithRegion(region),
445+
config.WithClientLogMode(awsv2.LogSigning),
446+
}
447+
cfg, err := config.LoadDefaultConfig(context.Background(), optFns...)
448+
Expect(err).NotTo(HaveOccurred())
449+
_, err = cfg.Credentials.Retrieve(context.Background())
450+
Expect(err).NotTo(HaveOccurred())
451+
return &cfg
452+
}
453+
454+
func NewAWSSessionRepoWithKeyV2(accessKey *iamtypes.AccessKey) *awsv2.Config {
455+
By("Getting an AWS IAM session - from access key")
456+
region, err := credentials.ResolveRegion("us-east-1")
457+
Expect(err).NotTo(HaveOccurred())
458+
staticCredProvider := awscredsv2.NewStaticCredentialsProvider(awsv2.ToString(accessKey.AccessKeyId), awsv2.ToString(accessKey.SecretAccessKey), "")
459+
optFns := []func(*config.LoadOptions) error{
460+
config.WithRegion(region),
461+
config.WithClientLogMode(awsv2.LogSigning),
462+
config.WithCredentialsProvider(staticCredProvider),
463+
}
464+
cfg, err := config.LoadDefaultConfig(context.Background(), optFns...)
465+
Expect(err).NotTo(HaveOccurred())
466+
_, err = cfg.Credentials.Retrieve(context.Background())
467+
Expect(err).NotTo(HaveOccurred())
468+
return &cfg
469+
}
470+
471+
func NewAWSSessionWithKeyV2(accessKey *iamtypes.AccessKey) *awsv2.Config {
472+
By("Getting an AWS IAM session - from access key")
473+
region, err := credentials.ResolveRegion("")
474+
Expect(err).NotTo(HaveOccurred())
475+
staticCredProvider := awscredsv2.NewStaticCredentialsProvider(awsv2.ToString(accessKey.AccessKeyId), awsv2.ToString(accessKey.SecretAccessKey), "")
476+
optFns := []func(*config.LoadOptions) error{
477+
config.WithRegion(region),
478+
config.WithClientLogMode(awsv2.LogSigning),
479+
config.WithCredentialsProvider(staticCredProvider),
480+
}
481+
cfg, err := config.LoadDefaultConfig(context.Background(), optFns...)
482+
Expect(err).NotTo(HaveOccurred())
483+
_, err = cfg.Credentials.Retrieve(context.Background())
484+
Expect(err).NotTo(HaveOccurred())
485+
return &cfg
486+
}
487+
437488
// createCloudFormationStack ensures the cloudformation stack is up to date.
438489
func createCloudFormationStack(ctx context.Context, cfg awsv2.Config, prov client.ConfigProvider, t *cfn_bootstrap.Template, tags map[string]string) error {
439490
By(fmt.Sprintf("Creating AWS CloudFormation stack for AWS IAM resources: stack-name=%s", t.Spec.StackName))
@@ -1051,7 +1102,7 @@ func (s *ServiceQuota) updateServiceQuotaRequestStatus(serviceQuotasClient *serv
10511102
}
10521103

10531104
// DumpEKSClusters dumps the EKS clusters in the environment.
1054-
func DumpEKSClusters(_ context.Context, e2eCtx *E2EContext) {
1105+
func DumpEKSClusters(ctx context.Context, e2eCtx *E2EContext) {
10551106
name := "no-bootstrap-cluster"
10561107
if e2eCtx.Environment.BootstrapClusterProxy != nil {
10571108
name = e2eCtx.Environment.BootstrapClusterProxy.GetName()
@@ -1063,36 +1114,36 @@ func DumpEKSClusters(_ context.Context, e2eCtx *E2EContext) {
10631114
fmt.Fprintf(GinkgoWriter, "Folder created for eks clusters: %q\n", logPath)
10641115

10651116
input := &eks.ListClustersInput{}
1066-
var eksClient *eks.EKS
1067-
if e2eCtx.BootstrapUserAWSSession == nil && e2eCtx.AWSSession != nil {
1068-
eksClient = eks.New(e2eCtx.AWSSession)
1069-
} else if e2eCtx.BootstrapUserAWSSession != nil {
1070-
eksClient = eks.New(e2eCtx.BootstrapUserAWSSession)
1117+
var eksClient *eks.Client
1118+
if e2eCtx.BootstrapUserAWSSessionV2 == nil && e2eCtx.AWSSessionV2 != nil {
1119+
eksClient = eks.NewFromConfig(*e2eCtx.AWSSessionV2)
1120+
} else if e2eCtx.BootstrapUserAWSSessionV2 != nil {
1121+
eksClient = eks.NewFromConfig(*e2eCtx.BootstrapUserAWSSessionV2)
10711122
} else {
10721123
Fail("Couldn't list EKS clusters: no AWS client was set up (please look at previous errors)")
10731124
return
10741125
}
10751126

1076-
output, err := eksClient.ListClusters(input)
1127+
output, err := eksClient.ListClusters(ctx, input)
10771128
if err != nil {
10781129
fmt.Fprintf(GinkgoWriter, "Couldn't list EKS clusters: err=%s\n", err)
10791130
return
10801131
}
10811132

10821133
for _, clusterName := range output.Clusters {
10831134
describeInput := &eks.DescribeClusterInput{
1084-
Name: clusterName,
1135+
Name: aws.String(clusterName),
10851136
}
1086-
describeOutput, err := eksClient.DescribeCluster(describeInput)
1137+
describeOutput, err := eksClient.DescribeCluster(ctx, describeInput)
10871138
if err != nil {
1088-
fmt.Fprintf(GinkgoWriter, "Couldn't describe EKS clusters: name=%q err=%s\n", *clusterName, err)
1139+
fmt.Fprintf(GinkgoWriter, "Couldn't describe EKS clusters: name=%q err=%s\n", clusterName, err)
10891140
continue
10901141
}
10911142
dumpEKSCluster(describeOutput.Cluster, logPath)
10921143
}
10931144
}
10941145

1095-
func dumpEKSCluster(cluster *eks.Cluster, logPath string) {
1146+
func dumpEKSCluster(cluster *ekstypes.Cluster, logPath string) {
10961147
clusterYAML, err := yaml.Marshal(cluster)
10971148
if err != nil {
10981149
fmt.Fprintf(GinkgoWriter, "Couldn't marshal cluster to yaml: name=%q err=%s\n", *cluster.Name, err)

test/e2e/shared/context.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ type E2EContext struct {
6969
AWSConfig awsv2.Config
7070
// BootstrapUserAWSSession is the AWS session for the bootstrap user.
7171
BootstrapUserAWSSession client.ConfigProvider
72-
// IsManaged indicates that this is for the managed part of the provider.
72+
// BootstrapUserAWSSessionV2 is the AWS SDK V2 session for the bootstrap user.
73+
BootstrapUserAWSSessionV2 *awsv2.Config
74+
// IsManaged indicates that this is for the managed part of the provider. This is until the V2 migration is done.
7375
IsManaged bool
7476
// CloudFormationTemplate is the rendered template created for the test.
7577
CloudFormationTemplate *cloudformation.Template

0 commit comments

Comments
 (0)