@@ -4,17 +4,23 @@ package aws
4
4
import (
5
5
"context"
6
6
"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"
12
19
"github.com/pkg/errors"
13
20
"github.com/sirupsen/logrus"
14
21
"k8s.io/apimachinery/pkg/util/sets"
15
22
16
23
"github.com/openshift/installer/pkg/asset/installconfig"
17
- awsic "github.com/openshift/installer/pkg/asset/installconfig/aws"
18
24
"github.com/openshift/installer/pkg/types"
19
25
awstypes "github.com/openshift/installer/pkg/types/aws"
20
26
)
@@ -68,39 +74,60 @@ func tagSharedVPCResources(ctx context.Context, clusterID string, installConfig
68
74
return err
69
75
}
70
76
71
- ids := make ([]* string , 0 , len (privateSubnets )+ len (publicSubnets )+ len (edgeSubnets ))
77
+ ids := make ([]string , 0 , len (privateSubnets )+ len (publicSubnets )+ len (edgeSubnets ))
72
78
for id := range privateSubnets {
73
- ids = append (ids , aws . String ( id ) )
79
+ ids = append (ids , id )
74
80
}
75
81
for id := range publicSubnets {
76
- ids = append (ids , aws . String ( id ) )
82
+ ids = append (ids , id )
77
83
}
78
84
for id := range edgeSubnets {
79
- ids = append (ids , aws . String ( id ) )
85
+ ids = append (ids , id )
80
86
}
81
87
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 ))
83
91
if err != nil {
84
- return errors . Wrap ( err , "could not create AWS session" )
92
+ return fmt . Errorf ( "failed to load AWS config: %w" , err )
85
93
}
86
94
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
+ })
88
103
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 {
91
105
Resources : ids ,
92
- Tags : []* ec2 .Tag {{Key : & tagKey , Value : & tagValue }},
106
+ Tags : []ec2types .Tag {{Key : & tagKey , Value : & tagValue }},
93
107
}); err != nil {
94
108
return errors .Wrap (err , "could not add tags to subnets" )
95
109
}
96
110
97
111
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
+ stsSvc := sts .NewFromConfig (cfg )
113
+ creds := stscreds .NewAssumeRoleProvider (stsSvc , installConfig .Config .AWS .HostedZoneRole )
114
+ // The credentials for this config are set after the other uses. In the event that more
115
+ // clients use the config, a new config should be created.
116
+ cfg .Credentials = aws .NewCredentialsCache (creds )
117
+
118
+ route53Client := route53 .NewFromConfig (cfg , func (options * route53.Options ) {
119
+ options .Region = installConfig .Config .Platform .AWS .Region
120
+ for _ , endpoint := range installConfig .Config .AWS .ServiceEndpoints {
121
+ if strings .EqualFold (endpoint .Name , "route53" ) {
122
+ options .BaseEndpoint = aws .String (endpoint .URL )
123
+ }
124
+ }
125
+ })
126
+
127
+ if _ , err := route53Client .ChangeTagsForResource (ctx , & route53.ChangeTagsForResourceInput {
128
+ ResourceType : r53types .TagResourceTypeHostedzone ,
102
129
ResourceId : aws .String (zone ),
103
- AddTags : []* route53 .Tag {{Key : & tagKey , Value : & tagValue }},
130
+ AddTags : []r53types .Tag {{Key : & tagKey , Value : & tagValue }},
104
131
}); err != nil {
105
132
return errors .Wrap (err , "could not add tags to hosted zone" )
106
133
}
@@ -145,18 +172,26 @@ func tagSharedIAMRoles(ctx context.Context, clusterID string, installConfig *ins
145
172
146
173
logrus .Debugf ("Tagging shared instance roles: %v" , sets .List (iamRoles ))
147
174
148
- session , err := installConfig .AWS .Session (ctx )
175
+ tagKey , tagValue := sharedTag (clusterID )
176
+
177
+ cfg , err := configv2 .LoadDefaultConfig (ctx , configv2 .WithRegion (installConfig .Config .Platform .AWS .Region ))
149
178
if err != nil {
150
- return fmt .Errorf ("could not create AWS session : %w" , err )
179
+ return fmt .Errorf ("failed to load AWS config : %w" , err )
151
180
}
152
181
153
- tagKey , tagValue := sharedTag (clusterID )
182
+ iamClient := iam .NewFromConfig (cfg , func (options * iam.Options ) {
183
+ options .Region = installConfig .Config .Platform .AWS .Region
184
+ for _ , endpoint := range installConfig .Config .AWS .ServiceEndpoints {
185
+ if strings .EqualFold (endpoint .Name , "iam" ) {
186
+ options .BaseEndpoint = aws .String (endpoint .URL )
187
+ }
188
+ }
189
+ })
154
190
155
- iamClient := iam .New (session , aws .NewConfig ().WithRegion (installConfig .Config .Platform .AWS .Region ))
156
191
for role := range iamRoles {
157
- if _ , err := iamClient .TagRoleWithContext (ctx , & iam.TagRoleInput {
192
+ if _ , err := iamClient .TagRole (ctx , & iam.TagRoleInput {
158
193
RoleName : aws .String (role ),
159
- Tags : []* iam .Tag {
194
+ Tags : []iamtypes .Tag {
160
195
{Key : aws .String (tagKey ), Value : aws .String (tagValue )},
161
196
},
162
197
}); err != nil {
@@ -207,17 +242,25 @@ func tagSharedIAMProfiles(ctx context.Context, clusterID string, installConfig *
207
242
208
243
logrus .Debugf ("Tagging shared instance profiles: %v" , sets .List (iamProfileNames ))
209
244
210
- session , err := installConfig .AWS .Session ( ctx )
245
+ cfg , err := configv2 . LoadDefaultConfig ( ctx , configv2 . WithRegion ( installConfig .Config . Platform . AWS .Region ) )
211
246
if err != nil {
212
- return errors . Wrap ( err , "could not create AWS session" )
247
+ return fmt . Errorf ( "failed loading default config: %w" , err )
213
248
}
214
- iamClient := iam .New (session , aws .NewConfig ().WithRegion (installConfig .Config .AWS .Region ))
249
+
250
+ iamClient := iam .NewFromConfig (cfg , func (options * iam.Options ) {
251
+ options .Region = installConfig .Config .Platform .AWS .Region
252
+ for _ , endpoint := range installConfig .Config .AWS .ServiceEndpoints {
253
+ if strings .EqualFold (endpoint .Name , "iam" ) {
254
+ options .BaseEndpoint = aws .String (endpoint .URL )
255
+ }
256
+ }
257
+ })
215
258
216
259
tagKey , tagValue := sharedTag (clusterID )
217
260
for name := range iamProfileNames {
218
- if _ , err := iamClient .TagInstanceProfileWithContext (ctx , & iam.TagInstanceProfileInput {
261
+ if _ , err := iamClient .TagInstanceProfile (ctx , & iam.TagInstanceProfileInput {
219
262
InstanceProfileName : aws .String (name ),
220
- Tags : []* iam .Tag {
263
+ Tags : []iamtypes .Tag {
221
264
{Key : aws .String (tagKey ), Value : aws .String (tagValue )},
222
265
},
223
266
}); err != nil {
0 commit comments