Skip to content

Commit 38efa9f

Browse files
committed
CORS-4053: Migrate AWS SDK to v2 in cluster/aws
** The IAM, EC2, and Route53 clients were moved to SDK v2. This included migration from session to config (and use of endpoints).
1 parent 88ba667 commit 38efa9f

File tree

1 file changed

+67
-32
lines changed

1 file changed

+67
-32
lines changed

pkg/asset/cluster/aws/aws.go

Lines changed: 67 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,21 @@ package aws
44
import (
55
"context"
66
"fmt"
7-
8-
"github.com/aws/aws-sdk-go/aws"
9-
"github.com/aws/aws-sdk-go/service/ec2"
10-
"github.com/aws/aws-sdk-go/service/iam"
11-
"github.com/aws/aws-sdk-go/service/route53"
7+
"strings"
8+
9+
"github.com/aws/aws-sdk-go-v2/aws"
10+
configv2 "github.com/aws/aws-sdk-go-v2/config"
11+
"github.com/aws/aws-sdk-go-v2/service/ec2"
12+
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
13+
"github.com/aws/aws-sdk-go-v2/service/iam"
14+
iamtypes "github.com/aws/aws-sdk-go-v2/service/iam/types"
15+
"github.com/aws/aws-sdk-go-v2/service/route53"
16+
r53types "github.com/aws/aws-sdk-go-v2/service/route53/types"
1217
"github.com/pkg/errors"
1318
"github.com/sirupsen/logrus"
1419
"k8s.io/apimachinery/pkg/util/sets"
1520

1621
"github.com/openshift/installer/pkg/asset/installconfig"
17-
awsic "github.com/openshift/installer/pkg/asset/installconfig/aws"
1822
"github.com/openshift/installer/pkg/types"
1923
awstypes "github.com/openshift/installer/pkg/types/aws"
2024
)
@@ -68,39 +72,54 @@ func tagSharedVPCResources(ctx context.Context, clusterID string, installConfig
6872
return err
6973
}
7074

71-
ids := make([]*string, 0, len(privateSubnets)+len(publicSubnets)+len(edgeSubnets))
75+
ids := make([]string, 0, len(privateSubnets)+len(publicSubnets)+len(edgeSubnets))
7276
for id := range privateSubnets {
73-
ids = append(ids, aws.String(id))
77+
ids = append(ids, id)
7478
}
7579
for id := range publicSubnets {
76-
ids = append(ids, aws.String(id))
80+
ids = append(ids, id)
7781
}
7882
for id := range edgeSubnets {
79-
ids = append(ids, aws.String(id))
83+
ids = append(ids, id)
8084
}
8185

82-
session, err := installConfig.AWS.Session(ctx)
86+
tagKey, tagValue := sharedTag(clusterID)
87+
88+
cfg, err := configv2.LoadDefaultConfig(ctx, configv2.WithRegion(installConfig.Config.Platform.AWS.Region))
8389
if err != nil {
84-
return errors.Wrap(err, "could not create AWS session")
90+
return fmt.Errorf("failed to load AWS config: %w", err)
8591
}
8692

87-
tagKey, tagValue := sharedTag(clusterID)
93+
ec2Client := ec2.NewFromConfig(cfg, func(options *ec2.Options) {
94+
options.Region = installConfig.Config.Platform.AWS.Region
95+
for _, endpoint := range installConfig.Config.AWS.ServiceEndpoints {
96+
if strings.EqualFold(endpoint.Name, "ec2") {
97+
options.BaseEndpoint = aws.String(endpoint.URL)
98+
}
99+
}
100+
})
88101

89-
ec2Client := ec2.New(session, aws.NewConfig().WithRegion(installConfig.Config.Platform.AWS.Region))
90-
if _, err = ec2Client.CreateTagsWithContext(ctx, &ec2.CreateTagsInput{
102+
if _, err = ec2Client.CreateTags(ctx, &ec2.CreateTagsInput{
91103
Resources: ids,
92-
Tags: []*ec2.Tag{{Key: &tagKey, Value: &tagValue}},
104+
Tags: []ec2types.Tag{{Key: &tagKey, Value: &tagValue}},
93105
}); err != nil {
94106
return errors.Wrap(err, "could not add tags to subnets")
95107
}
96108

97109
if zone := installConfig.Config.AWS.HostedZone; zone != "" {
98-
r53cfg := awsic.GetR53ClientCfg(session, installConfig.Config.AWS.HostedZoneRole)
99-
route53Client := route53.New(session, r53cfg)
100-
if _, err := route53Client.ChangeTagsForResourceWithContext(ctx, &route53.ChangeTagsForResourceInput{
101-
ResourceType: aws.String("hostedzone"),
110+
route53Client := route53.NewFromConfig(cfg, func(options *route53.Options) {
111+
options.Region = installConfig.Config.Platform.AWS.Region
112+
for _, endpoint := range installConfig.Config.AWS.ServiceEndpoints {
113+
if strings.EqualFold(endpoint.Name, "route53") {
114+
options.BaseEndpoint = aws.String(endpoint.URL)
115+
}
116+
}
117+
})
118+
119+
if _, err := route53Client.ChangeTagsForResource(ctx, &route53.ChangeTagsForResourceInput{
120+
ResourceType: r53types.TagResourceTypeHostedzone,
102121
ResourceId: aws.String(zone),
103-
AddTags: []*route53.Tag{{Key: &tagKey, Value: &tagValue}},
122+
AddTags: []r53types.Tag{{Key: &tagKey, Value: &tagValue}},
104123
}); err != nil {
105124
return errors.Wrap(err, "could not add tags to hosted zone")
106125
}
@@ -145,18 +164,26 @@ func tagSharedIAMRoles(ctx context.Context, clusterID string, installConfig *ins
145164

146165
logrus.Debugf("Tagging shared instance roles: %v", sets.List(iamRoles))
147166

148-
session, err := installConfig.AWS.Session(ctx)
167+
tagKey, tagValue := sharedTag(clusterID)
168+
169+
cfg, err := configv2.LoadDefaultConfig(ctx, configv2.WithRegion(installConfig.Config.Platform.AWS.Region))
149170
if err != nil {
150-
return fmt.Errorf("could not create AWS session: %w", err)
171+
return fmt.Errorf("failed to load AWS config: %w", err)
151172
}
152173

153-
tagKey, tagValue := sharedTag(clusterID)
174+
iamClient := iam.NewFromConfig(cfg, func(options *iam.Options) {
175+
options.Region = installConfig.Config.Platform.AWS.Region
176+
for _, endpoint := range installConfig.Config.AWS.ServiceEndpoints {
177+
if strings.EqualFold(endpoint.Name, "iam") {
178+
options.BaseEndpoint = aws.String(endpoint.URL)
179+
}
180+
}
181+
})
154182

155-
iamClient := iam.New(session, aws.NewConfig().WithRegion(installConfig.Config.Platform.AWS.Region))
156183
for role := range iamRoles {
157-
if _, err := iamClient.TagRoleWithContext(ctx, &iam.TagRoleInput{
184+
if _, err := iamClient.TagRole(ctx, &iam.TagRoleInput{
158185
RoleName: aws.String(role),
159-
Tags: []*iam.Tag{
186+
Tags: []iamtypes.Tag{
160187
{Key: aws.String(tagKey), Value: aws.String(tagValue)},
161188
},
162189
}); err != nil {
@@ -207,17 +234,25 @@ func tagSharedIAMProfiles(ctx context.Context, clusterID string, installConfig *
207234

208235
logrus.Debugf("Tagging shared instance profiles: %v", sets.List(iamProfileNames))
209236

210-
session, err := installConfig.AWS.Session(ctx)
237+
cfg, err := configv2.LoadDefaultConfig(ctx, configv2.WithRegion(installConfig.Config.Platform.AWS.Region))
211238
if err != nil {
212-
return errors.Wrap(err, "could not create AWS session")
239+
return fmt.Errorf("failed loading default config: %w", err)
213240
}
214-
iamClient := iam.New(session, aws.NewConfig().WithRegion(installConfig.Config.AWS.Region))
241+
242+
iamClient := iam.NewFromConfig(cfg, func(options *iam.Options) {
243+
options.Region = installConfig.Config.Platform.AWS.Region
244+
for _, endpoint := range installConfig.Config.AWS.ServiceEndpoints {
245+
if strings.EqualFold(endpoint.Name, "iam") {
246+
options.BaseEndpoint = aws.String(endpoint.URL)
247+
}
248+
}
249+
})
215250

216251
tagKey, tagValue := sharedTag(clusterID)
217252
for name := range iamProfileNames {
218-
if _, err := iamClient.TagInstanceProfileWithContext(ctx, &iam.TagInstanceProfileInput{
253+
if _, err := iamClient.TagInstanceProfile(ctx, &iam.TagInstanceProfileInput{
219254
InstanceProfileName: aws.String(name),
220-
Tags: []*iam.Tag{
255+
Tags: []iamtypes.Tag{
221256
{Key: aws.String(tagKey), Value: aws.String(tagValue)},
222257
},
223258
}); err != nil {

0 commit comments

Comments
 (0)