Skip to content

Commit 86d7d58

Browse files
committed
kg/destroy/aws/aws.go:
** Remove session from the imports. Added the agent handler to the configurations.
1 parent 3e5b4da commit 86d7d58

File tree

1 file changed

+49
-79
lines changed

1 file changed

+49
-79
lines changed

pkg/destroy/aws/aws.go

Lines changed: 49 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
awsv2 "github.com/aws/aws-sdk-go-v2/aws"
1010
"github.com/aws/aws-sdk-go-v2/aws/arn"
11+
"github.com/aws/aws-sdk-go-v2/aws/middleware"
1112
configv2 "github.com/aws/aws-sdk-go-v2/config"
1213
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
1314
ec2v2 "github.com/aws/aws-sdk-go-v2/service/ec2"
@@ -22,12 +23,6 @@ import (
2223
"github.com/aws/aws-sdk-go-v2/service/s3"
2324
s3types "github.com/aws/aws-sdk-go-v2/service/s3/types"
2425
"github.com/aws/aws-sdk-go-v2/service/sts"
25-
"github.com/aws/aws-sdk-go/aws"
26-
"github.com/aws/aws-sdk-go/aws/awserr"
27-
"github.com/aws/aws-sdk-go/aws/endpoints"
28-
"github.com/aws/aws-sdk-go/aws/request"
29-
"github.com/aws/aws-sdk-go/aws/session"
30-
"github.com/aws/aws-sdk-go/service/s3/s3manager"
3126
"github.com/pkg/errors"
3227
"github.com/sirupsen/logrus"
3328
utilerrors "k8s.io/apimachinery/pkg/util/errors"
@@ -73,11 +68,6 @@ type ClusterUninstaller struct {
7368
HostedZoneRole string
7469
endpoints []awstypes.ServiceEndpoint
7570

76-
// Session is the AWS session to be used for deletion. If nil, a
77-
// new session will be created based on the usual credential
78-
// configuration (AWS_PROFILE, AWS_ACCESS_KEY_ID, etc.).
79-
Session *session.Session
80-
8171
EC2Client *ec2v2.Client
8272
EFSClient *efs.Client
8373
ELBClient *elb.Client
@@ -88,20 +78,24 @@ type ClusterUninstaller struct {
8878
S3Client *s3.Client
8979
}
9080

81+
const (
82+
endpointUSEast1 = "us-east-1"
83+
endpointCNNorth1 = "cn-north-1"
84+
endpointCNNorthWest1 = "cn-northwest-1"
85+
endpointISOEast1 = "us-iso-east-1"
86+
endpointISOWest1 = "us-iso-west-1"
87+
endpointISOBEast1 = "us-isob-east-1"
88+
endpointUSGovEast1 = "us-gov-east-1"
89+
endpointUSGovWest1 = "us-gov-west-1"
90+
)
91+
9192
// New returns an AWS destroyer from ClusterMetadata.
9293
func New(logger logrus.FieldLogger, metadata *types.ClusterMetadata) (providers.Destroyer, error) {
9394
filters := make([]Filter, 0, len(metadata.ClusterPlatformMetadata.AWS.Identifier))
9495
for _, filter := range metadata.ClusterPlatformMetadata.AWS.Identifier {
9596
filters = append(filters, filter)
9697
}
9798
region := metadata.ClusterPlatformMetadata.AWS.Region
98-
session, err := awssession.GetSessionWithOptions(
99-
awssession.WithRegion(region),
100-
awssession.WithServiceEndpoints(region, metadata.ClusterPlatformMetadata.AWS.ServiceEndpoints),
101-
)
102-
if err != nil {
103-
return nil, err
104-
}
10599

106100
ctx := context.Background()
107101
ec2Client, err := awssession.NewEC2Client(ctx, awssession.EndpointOptions{
@@ -122,44 +116,56 @@ func New(logger logrus.FieldLogger, metadata *types.ClusterMetadata) (providers.
122116

123117
// FIXME: remove this code when the elb and elbv2 clients are "fixed" or figured out
124118
elbCfg, err := awssession.GetConfigWithOptions(ctx, configv2.WithRegion(region))
119+
if err != nil {
120+
return nil, fmt.Errorf("failed to create AWS config for elb client: %w", err)
121+
}
125122
elbclient := elb.NewFromConfig(elbCfg, func(options *elb.Options) {
126123
options.Region = region
127124
for _, endpoint := range metadata.AWS.ServiceEndpoints {
128125
if strings.EqualFold(endpoint.Name, "elb") {
129-
options.BaseEndpoint = aws.String(endpoint.URL)
126+
options.BaseEndpoint = awsv2.String(endpoint.URL)
130127
}
131128
}
132129
})
133130

134131
// FIXME: remove this code when the elb and elbv2 clients are "fixed" or figured out
135132
elbv2Cfg, err := awssession.GetConfigWithOptions(ctx, configv2.WithRegion(region))
133+
if err != nil {
134+
return nil, fmt.Errorf("failed to create AWS config for elbv2 client: %w", err)
135+
}
136136
elbv2client := elbv2.NewFromConfig(elbv2Cfg, func(options *elbv2.Options) {
137137
options.Region = region
138138
for _, endpoint := range metadata.AWS.ServiceEndpoints {
139139
if strings.EqualFold(endpoint.Name, "elbv2") {
140-
options.BaseEndpoint = aws.String(endpoint.URL)
140+
options.BaseEndpoint = awsv2.String(endpoint.URL)
141141
}
142142
}
143143
})
144144

145145
// FIXME: remove this code when the s3client is made
146146
s3Cfg, err := awssession.GetConfigWithOptions(ctx, configv2.WithRegion(region))
147+
if err != nil {
148+
return nil, fmt.Errorf("failed to create AWS config for S3 client: %w", err)
149+
}
147150
s3Client := s3.NewFromConfig(s3Cfg, func(options *s3.Options) {
148151
options.Region = region
149152
for _, endpoint := range metadata.AWS.ServiceEndpoints {
150153
if strings.EqualFold(endpoint.Name, "s3") {
151-
options.BaseEndpoint = aws.String(endpoint.URL)
154+
options.BaseEndpoint = awsv2.String(endpoint.URL)
152155
}
153156
}
154157
})
155158

156159
// FIXME: remove this code when the EFS client is made
157160
efsCfg, err := awssession.GetConfigWithOptions(ctx, configv2.WithRegion(region))
161+
if err != nil {
162+
return nil, fmt.Errorf("failed to create AWS config for EFS client: %w", err)
163+
}
158164
efsClient := efs.NewFromConfig(efsCfg, func(options *efs.Options) {
159165
options.Region = region
160166
for _, endpoint := range metadata.AWS.ServiceEndpoints {
161167
if strings.EqualFold(endpoint.Name, "efs") {
162-
options.BaseEndpoint = aws.String(endpoint.URL)
168+
options.BaseEndpoint = awsv2.String(endpoint.URL)
163169
}
164170
}
165171
})
@@ -178,7 +184,6 @@ func New(logger logrus.FieldLogger, metadata *types.ClusterMetadata) (providers.
178184
Logger: logger,
179185
ClusterID: metadata.InfraID,
180186
ClusterDomain: metadata.AWS.ClusterDomain,
181-
Session: session,
182187
HostedZoneRole: metadata.AWS.HostedZoneRole,
183188
endpoints: metadata.AWS.ServiceEndpoints,
184189
EC2Client: ec2Client,
@@ -231,19 +236,6 @@ func (o *ClusterUninstaller) RunWithContext(ctx context.Context) ([]string, erro
231236
return nil, err
232237
}
233238

234-
awsSession := o.Session
235-
if awsSession == nil {
236-
// Relying on appropriate AWS ENV vars (eg AWS_PROFILE, AWS_ACCESS_KEY_ID, etc)
237-
awsSession, err = session.NewSession(aws.NewConfig().WithRegion(o.Region))
238-
if err != nil {
239-
return nil, err
240-
}
241-
}
242-
awsSession.Handlers.Build.PushBackNamed(request.NamedHandler{
243-
Name: "openshiftInstaller.OpenshiftInstallerUserAgentHandler",
244-
Fn: request.MakeAddToUserAgentHandler("OpenShift/4.x Destroyer", version.Raw),
245-
})
246-
247239
baseTaggingClient, err := createResourceTaggingClient(o.Region, o.endpoints)
248240
if err != nil {
249241
return nil, err
@@ -272,21 +264,21 @@ func (o *ClusterUninstaller) RunWithContext(ctx context.Context) ([]string, erro
272264
}
273265

274266
switch o.Region {
275-
case endpoints.CnNorth1RegionID, endpoints.CnNorthwest1RegionID:
267+
case endpointCNNorth1, endpointCNNorthWest1:
276268
break
277-
case endpoints.UsIsoEast1RegionID, endpoints.UsIsoWest1RegionID, endpoints.UsIsobEast1RegionID:
269+
case endpointISOEast1, endpointISOWest1, endpointISOBEast1:
278270
break
279-
case endpoints.UsGovEast1RegionID, endpoints.UsGovWest1RegionID:
280-
if o.Region != endpoints.UsGovWest1RegionID {
281-
tagClient, err := createResourceTaggingClient(endpoints.UsGovWest1RegionID, o.endpoints)
271+
case endpointUSGovEast1, endpointUSGovWest1:
272+
if o.Region != endpointUSGovWest1 {
273+
tagClient, err := createResourceTaggingClient(endpointUSGovWest1, o.endpoints)
282274
if err != nil {
283-
return nil, fmt.Errorf("failed to create resource tagging client for usgov-west-1: %w", err)
275+
return nil, fmt.Errorf("failed to create resource tagging client for us-gov-west-1: %w", err)
284276
}
285277
tagClients = append(tagClients, tagClient)
286278
}
287279
default:
288-
if o.Region != endpoints.UsEast1RegionID {
289-
tagClient, err := createResourceTaggingClientWithConfig(endpoints.UsEast1RegionID, o.endpoints)
280+
if o.Region != endpointUSEast1 {
281+
tagClient, err := createResourceTaggingClient(endpointUSEast1, o.endpoints)
290282
if err != nil {
291283
return nil, fmt.Errorf("failed to create resource tagging client for default us-east-1: %w", err)
292284
}
@@ -490,7 +482,7 @@ func findResourcesByTag(
490482
tagFilters := make([]tagtypes.TagFilter, 0, len(filter))
491483
for key, value := range filter {
492484
tagFilters = append(tagFilters, tagtypes.TagFilter{
493-
Key: aws.String(key),
485+
Key: awsv2.String(key),
494486
Values: []string{value},
495487
})
496488
}
@@ -573,7 +565,7 @@ func tagMatch(filters []Filter, tags map[string]string) bool {
573565
// Terraform-managed zone's privateID.
574566
func getPublicHostedZone(ctx context.Context, client *route53.Client, privateID string, logger logrus.FieldLogger) (string, error) {
575567
response, err := client.GetHostedZone(ctx, &route53.GetHostedZoneInput{
576-
Id: aws.String(privateID),
568+
Id: awsv2.String(privateID),
577569
})
578570
if err != nil {
579571
return "", err
@@ -615,7 +607,7 @@ func findAncestorPublicRoute53(ctx context.Context, client *route53.Client, dnsN
615607
// It returns "", when no public route53 zone could be found.
616608
func findPublicRoute53(ctx context.Context, client *route53.Client, dnsName string, logger logrus.FieldLogger) (string, error) {
617609
request := &route53.ListHostedZonesByNameInput{
618-
DNSName: aws.String(dnsName),
610+
DNSName: awsv2.String(dnsName),
619611
}
620612
for i := 0; true; i++ {
621613
logger.Debugf("listing AWS hosted zones %q (page %d)", dnsName, i)
@@ -695,7 +687,7 @@ func deleteRoute53(ctx context.Context, client *route53.Client, arn arn.ARN, log
695687
publicEntries := map[string]route53types.ResourceRecordSet{}
696688
if len(publicZoneID) != 0 {
697689

698-
paginator := route53.NewListResourceRecordSetsPaginator(client, &route53.ListResourceRecordSetsInput{HostedZoneId: aws.String(publicZoneID)})
690+
paginator := route53.NewListResourceRecordSetsPaginator(client, &route53.ListResourceRecordSetsInput{HostedZoneId: awsv2.String(publicZoneID)})
699691
for paginator.HasMorePages() {
700692
page, err := paginator.NextPage(ctx)
701693
if err != nil {
@@ -711,7 +703,7 @@ func deleteRoute53(ctx context.Context, client *route53.Client, arn arn.ARN, log
711703
}
712704

713705
var lastError error
714-
paginator := route53.NewListResourceRecordSetsPaginator(client, &route53.ListResourceRecordSetsInput{HostedZoneId: aws.String(id)})
706+
paginator := route53.NewListResourceRecordSetsPaginator(client, &route53.ListResourceRecordSetsInput{HostedZoneId: awsv2.String(id)})
715707
for paginator.HasMorePages() {
716708
page, err := paginator.NextPage(ctx)
717709
if err != nil {
@@ -752,7 +744,7 @@ func deleteRoute53(ctx context.Context, client *route53.Client, arn arn.ARN, log
752744
}
753745

754746
_, err = client.DeleteHostedZone(ctx, &route53.DeleteHostedZoneInput{
755-
Id: aws.String(id),
747+
Id: awsv2.String(id),
756748
})
757749
if err != nil {
758750
if strings.Contains(HandleErrorCode(err), "NoSuchHostedZone") {
@@ -768,7 +760,7 @@ func deleteRoute53(ctx context.Context, client *route53.Client, arn arn.ARN, log
768760
func deleteRoute53RecordSet(ctx context.Context, client *route53.Client, zoneID string, recordSet *route53types.ResourceRecordSet, logger logrus.FieldLogger) error {
769761
logger = logger.WithField("record set", fmt.Sprintf("%s %s", recordSet.Type, *recordSet.Name))
770762
_, err := client.ChangeResourceRecordSets(ctx, &route53.ChangeResourceRecordSetsInput{
771-
HostedZoneId: aws.String(zoneID),
763+
HostedZoneId: awsv2.String(zoneID),
772764
ChangeBatch: &route53types.ChangeBatch{
773765
Changes: []route53types.Change{
774766
{
@@ -829,28 +821,6 @@ func deleteS3(ctx context.Context, client *s3.Client, arn arn.ARN, logger logrus
829821
return nil
830822
}
831823

832-
func isBucketNotFound(err interface{}) bool {
833-
switch s3Err := err.(type) {
834-
case awserr.Error:
835-
if s3Err.Code() == "NoSuchBucket" {
836-
return true
837-
}
838-
origErr := s3Err.OrigErr()
839-
if origErr != nil {
840-
return isBucketNotFound(origErr)
841-
}
842-
case s3manager.Error:
843-
if s3Err.OrigErr != nil {
844-
return isBucketNotFound(s3Err.OrigErr)
845-
}
846-
case s3manager.Errors:
847-
if len(s3Err) == 1 {
848-
return isBucketNotFound(s3Err[0])
849-
}
850-
}
851-
return false
852-
}
853-
854824
func deleteElasticFileSystem(ctx context.Context, client *efs.Client, arn arn.ARN, logger logrus.FieldLogger) error {
855825
resourceType, id, err := splitSlash("resource", arn.Resource)
856826
if err != nil {
@@ -892,7 +862,7 @@ func deleteFileSystem(ctx context.Context, client *efs.Client, fsid string, logg
892862
}
893863
}
894864

895-
_, err = client.DeleteFileSystem(ctx, &efs.DeleteFileSystemInput{FileSystemId: aws.String(fsid)})
865+
_, err = client.DeleteFileSystem(ctx, &efs.DeleteFileSystemInput{FileSystemId: awsv2.String(fsid)})
896866
if err != nil {
897867
if strings.Contains(HandleErrorCode(err), "FileSystemNotFound") {
898868
return nil
@@ -906,12 +876,12 @@ func deleteFileSystem(ctx context.Context, client *efs.Client, fsid string, logg
906876

907877
func getAccessPoints(ctx context.Context, client *efs.Client, apID string) ([]string, error) {
908878
var accessPointIDs []string
909-
paginator := efs.NewDescribeAccessPointsPaginator(client, &efs.DescribeAccessPointsInput{FileSystemId: aws.String(apID)})
879+
paginator := efs.NewDescribeAccessPointsPaginator(client, &efs.DescribeAccessPointsInput{FileSystemId: awsv2.String(apID)})
910880

911881
for paginator.HasMorePages() {
912882
page, err := paginator.NextPage(ctx)
913883
if err != nil {
914-
return nil, fmt.Errorf("describing access points failed: %w", err)
884+
return nil, fmt.Errorf("describing access points: %w", err)
915885
}
916886

917887
for _, ap := range page.AccessPoints {
@@ -932,7 +902,7 @@ func getMountTargets(ctx context.Context, client *efs.Client, fsid string) ([]st
932902
// Number of Mount Targets should be equal to nr. of subnets that can access the volume, i.e. relatively small.
933903
rsp, err := client.DescribeMountTargets(
934904
ctx,
935-
&efs.DescribeMountTargetsInput{FileSystemId: aws.String(fsid)},
905+
&efs.DescribeMountTargetsInput{FileSystemId: awsv2.String(fsid)},
936906
)
937907
if err != nil {
938908
return nil, err
@@ -951,7 +921,7 @@ func getMountTargets(ctx context.Context, client *efs.Client, fsid string) ([]st
951921

952922
func deleteAccessPoint(ctx context.Context, client *efs.Client, id string, logger logrus.FieldLogger) error {
953923
logger = logger.WithField("AccessPoint ID", id)
954-
_, err := client.DeleteAccessPoint(ctx, &efs.DeleteAccessPointInput{AccessPointId: aws.String(id)})
924+
_, err := client.DeleteAccessPoint(ctx, &efs.DeleteAccessPointInput{AccessPointId: awsv2.String(id)})
955925
if err != nil {
956926
if strings.Contains(HandleErrorCode(err), "AccessPointNotFound") {
957927
return nil
@@ -965,7 +935,7 @@ func deleteAccessPoint(ctx context.Context, client *efs.Client, id string, logge
965935

966936
func deleteMountTarget(ctx context.Context, client *efs.Client, id string, logger logrus.FieldLogger) error {
967937
logger = logger.WithField("Mount Target ID", id)
968-
_, err := client.DeleteMountTarget(ctx, &efs.DeleteMountTargetInput{MountTargetId: aws.String(id)})
938+
_, err := client.DeleteMountTarget(ctx, &efs.DeleteMountTargetInput{MountTargetId: awsv2.String(id)})
969939
if err != nil {
970940
if strings.Contains(HandleErrorCode(err), "MountTargetNotFound") {
971941
return nil

0 commit comments

Comments
 (0)