Skip to content

Commit dd6e683

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 dd6e683

File tree

2 files changed

+78
-33
lines changed

2 files changed

+78
-33
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ require (
4040
github.com/aws/aws-sdk-go v1.55.5
4141
github.com/aws/aws-sdk-go-v2 v1.36.0
4242
github.com/aws/aws-sdk-go-v2/config v1.27.11
43+
github.com/aws/aws-sdk-go-v2/credentials v1.17.11
4344
github.com/aws/aws-sdk-go-v2/service/ec2 v1.159.0
4445
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2 v1.43.11
4546
github.com/aws/aws-sdk-go-v2/service/iam v1.32.0
@@ -172,7 +173,6 @@ require (
172173
github.com/PaesslerAG/jsonpath v0.1.1 // indirect
173174
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
174175
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.2 // indirect
175-
github.com/aws/aws-sdk-go-v2/credentials v1.17.11 // indirect
176176
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.1 // indirect
177177
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.31 // indirect
178178
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.31 // indirect

pkg/asset/cluster/aws/aws.go

Lines changed: 77 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,23 @@ 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/credentials/stscreds"
12+
"github.com/aws/aws-sdk-go-v2/service/ec2"
13+
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
14+
"github.com/aws/aws-sdk-go-v2/service/iam"
15+
iamtypes "github.com/aws/aws-sdk-go-v2/service/iam/types"
16+
"github.com/aws/aws-sdk-go-v2/service/route53"
17+
r53types "github.com/aws/aws-sdk-go-v2/service/route53/types"
18+
"github.com/aws/aws-sdk-go-v2/service/sts"
1219
"github.com/pkg/errors"
1320
"github.com/sirupsen/logrus"
1421
"k8s.io/apimachinery/pkg/util/sets"
1522

1623
"github.com/openshift/installer/pkg/asset/installconfig"
17-
awsic "github.com/openshift/installer/pkg/asset/installconfig/aws"
1824
"github.com/openshift/installer/pkg/types"
1925
awstypes "github.com/openshift/installer/pkg/types/aws"
2026
)
@@ -68,39 +74,62 @@ func tagSharedVPCResources(ctx context.Context, clusterID string, installConfig
6874
return err
6975
}
7076

71-
ids := make([]*string, 0, len(privateSubnets)+len(publicSubnets)+len(edgeSubnets))
77+
ids := make([]string, 0, len(privateSubnets)+len(publicSubnets)+len(edgeSubnets))
7278
for id := range privateSubnets {
73-
ids = append(ids, aws.String(id))
79+
ids = append(ids, id)
7480
}
7581
for id := range publicSubnets {
76-
ids = append(ids, aws.String(id))
82+
ids = append(ids, id)
7783
}
7884
for id := range edgeSubnets {
79-
ids = append(ids, aws.String(id))
85+
ids = append(ids, id)
8086
}
8187

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

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

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

97111
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"),
112+
if installConfig.Config.AWS.HostedZoneRole != "" {
113+
stsSvc := sts.NewFromConfig(cfg)
114+
creds := stscreds.NewAssumeRoleProvider(stsSvc, installConfig.Config.AWS.HostedZoneRole)
115+
// The credentials for this config are set after the other uses. In the event that more
116+
// clients use the config, a new config should be created.
117+
cfg.Credentials = aws.NewCredentialsCache(creds)
118+
}
119+
120+
route53Client := route53.NewFromConfig(cfg, func(options *route53.Options) {
121+
options.Region = installConfig.Config.Platform.AWS.Region
122+
for _, endpoint := range installConfig.Config.AWS.ServiceEndpoints {
123+
if strings.EqualFold(endpoint.Name, "route53") {
124+
options.BaseEndpoint = aws.String(endpoint.URL)
125+
}
126+
}
127+
})
128+
129+
if _, err := route53Client.ChangeTagsForResource(ctx, &route53.ChangeTagsForResourceInput{
130+
ResourceType: r53types.TagResourceTypeHostedzone,
102131
ResourceId: aws.String(zone),
103-
AddTags: []*route53.Tag{{Key: &tagKey, Value: &tagValue}},
132+
AddTags: []r53types.Tag{{Key: &tagKey, Value: &tagValue}},
104133
}); err != nil {
105134
return errors.Wrap(err, "could not add tags to hosted zone")
106135
}
@@ -145,18 +174,26 @@ func tagSharedIAMRoles(ctx context.Context, clusterID string, installConfig *ins
145174

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

148-
session, err := installConfig.AWS.Session(ctx)
177+
tagKey, tagValue := sharedTag(clusterID)
178+
179+
cfg, err := configv2.LoadDefaultConfig(ctx, configv2.WithRegion(installConfig.Config.Platform.AWS.Region))
149180
if err != nil {
150-
return fmt.Errorf("could not create AWS session: %w", err)
181+
return fmt.Errorf("failed to load AWS config: %w", err)
151182
}
152183

153-
tagKey, tagValue := sharedTag(clusterID)
184+
iamClient := iam.NewFromConfig(cfg, func(options *iam.Options) {
185+
options.Region = installConfig.Config.Platform.AWS.Region
186+
for _, endpoint := range installConfig.Config.AWS.ServiceEndpoints {
187+
if strings.EqualFold(endpoint.Name, "iam") {
188+
options.BaseEndpoint = aws.String(endpoint.URL)
189+
}
190+
}
191+
})
154192

155-
iamClient := iam.New(session, aws.NewConfig().WithRegion(installConfig.Config.Platform.AWS.Region))
156193
for role := range iamRoles {
157-
if _, err := iamClient.TagRoleWithContext(ctx, &iam.TagRoleInput{
194+
if _, err := iamClient.TagRole(ctx, &iam.TagRoleInput{
158195
RoleName: aws.String(role),
159-
Tags: []*iam.Tag{
196+
Tags: []iamtypes.Tag{
160197
{Key: aws.String(tagKey), Value: aws.String(tagValue)},
161198
},
162199
}); err != nil {
@@ -207,17 +244,25 @@ func tagSharedIAMProfiles(ctx context.Context, clusterID string, installConfig *
207244

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

210-
session, err := installConfig.AWS.Session(ctx)
247+
cfg, err := configv2.LoadDefaultConfig(ctx, configv2.WithRegion(installConfig.Config.Platform.AWS.Region))
211248
if err != nil {
212-
return errors.Wrap(err, "could not create AWS session")
249+
return fmt.Errorf("failed to load AWS config: %w", err)
213250
}
214-
iamClient := iam.New(session, aws.NewConfig().WithRegion(installConfig.Config.AWS.Region))
251+
252+
iamClient := iam.NewFromConfig(cfg, func(options *iam.Options) {
253+
options.Region = installConfig.Config.Platform.AWS.Region
254+
for _, endpoint := range installConfig.Config.AWS.ServiceEndpoints {
255+
if strings.EqualFold(endpoint.Name, "iam") {
256+
options.BaseEndpoint = aws.String(endpoint.URL)
257+
}
258+
}
259+
})
215260

216261
tagKey, tagValue := sharedTag(clusterID)
217262
for name := range iamProfileNames {
218-
if _, err := iamClient.TagInstanceProfileWithContext(ctx, &iam.TagInstanceProfileInput{
263+
if _, err := iamClient.TagInstanceProfile(ctx, &iam.TagInstanceProfileInput{
219264
InstanceProfileName: aws.String(name),
220-
Tags: []*iam.Tag{
265+
Tags: []iamtypes.Tag{
221266
{Key: aws.String(tagKey), Value: aws.String(tagValue)},
222267
},
223268
}); err != nil {

0 commit comments

Comments
 (0)