Skip to content

Commit 8d4c7f2

Browse files
authored
Merge pull request #5518 from Danil-Grigorev/migrate-gc-to-aws-sdk-v2
🌱 Migrate gc to aws sdk v2
2 parents d28ca77 + ae57778 commit 8d4c7f2

File tree

13 files changed

+631
-64
lines changed

13 files changed

+631
-64
lines changed

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@ require (
77
github.com/apparentlymart/go-cidr v1.1.0
88
github.com/aws/amazon-vpc-cni-k8s v1.15.5
99
github.com/aws/aws-lambda-go v1.41.0
10-
github.com/aws/aws-sdk-go v1.55.5
10+
github.com/aws/aws-sdk-go v1.55.7
1111
github.com/aws/aws-sdk-go-v2 v1.36.5
1212
github.com/aws/aws-sdk-go-v2/config v1.27.11
1313
github.com/aws/aws-sdk-go-v2/credentials v1.17.11
1414
github.com/aws/aws-sdk-go-v2/service/autoscaling v1.52.4
15+
github.com/aws/aws-sdk-go-v2/service/ec2 v1.159.0
1516
github.com/aws/aws-sdk-go-v2/service/eks v1.64.0
1617
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing v1.29.6
17-
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2 v1.34.0
18+
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2 v1.45.2
1819
github.com/aws/aws-sdk-go-v2/service/iam v1.32.0
1920
github.com/aws/aws-sdk-go-v2/service/resourcegroupstaggingapi v1.26.6
2021
github.com/aws/aws-sdk-go-v2/service/s3 v1.53.1
22+
github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.28.6
2123
github.com/aws/aws-sdk-go-v2/service/ssm v1.59.1
2224
github.com/aws/aws-sdk-go-v2/service/sts v1.28.6
2325
github.com/aws/smithy-go v1.22.4
@@ -89,14 +91,12 @@ require (
8991
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect
9092
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.36 // indirect
9193
github.com/aws/aws-sdk-go-v2/service/cloudformation v1.50.0 // indirect
92-
github.com/aws/aws-sdk-go-v2/service/ec2 v1.159.0
9394
github.com/aws/aws-sdk-go-v2/service/eventbridge v1.39.3
9495
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.2 // indirect
9596
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.3.7 // indirect
9697
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.7 // indirect
9798
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.17.5 // indirect
9899
github.com/aws/aws-sdk-go-v2/service/organizations v1.27.3 // indirect
99-
github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.28.6
100100
github.com/aws/aws-sdk-go-v2/service/servicequotas v1.21.4 // indirect
101101
github.com/aws/aws-sdk-go-v2/service/sqs v1.38.8
102102
github.com/aws/aws-sdk-go-v2/service/sso v1.20.5 // indirect

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ github.com/aws/amazon-vpc-cni-k8s v1.15.5 h1:/mqTXB4HoGYg4CiU4Gco9iEvZ+V/309Na4H
4646
github.com/aws/amazon-vpc-cni-k8s v1.15.5/go.mod h1:jV4wNtmgT2Ra1/oZU99DPOFsCUKnf0mYfIyzDyAUVAY=
4747
github.com/aws/aws-lambda-go v1.41.0 h1:l/5fyVb6Ud9uYd411xdHZzSf2n86TakxzpvIoz7l+3Y=
4848
github.com/aws/aws-lambda-go v1.41.0/go.mod h1:jwFe2KmMsHmffA1X2R09hH6lFzJQxzI8qK17ewzbQMM=
49-
github.com/aws/aws-sdk-go v1.55.5 h1:KKUZBfBoyqy5d3swXyiC7Q76ic40rYcbqH7qjh59kzU=
50-
github.com/aws/aws-sdk-go v1.55.5/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU=
49+
github.com/aws/aws-sdk-go v1.55.7 h1:UJrkFq7es5CShfBwlWAC8DA077vp8PyVbQd3lqLiztE=
50+
github.com/aws/aws-sdk-go v1.55.7/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU=
5151
github.com/aws/aws-sdk-go-v2 v1.36.5 h1:0OF9RiEMEdDdZEMqF9MRjevyxAQcf6gY+E7vwBILFj0=
5252
github.com/aws/aws-sdk-go-v2 v1.36.5/go.mod h1:EYrzvCCN9CMUTa5+6lf6MM4tq3Zjp8UhSGR/cBsjai0=
5353
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.2 h1:x6xsQXGSmW6frevwDA+vi/wqhp1ct18mVXYN08/93to=
@@ -76,8 +76,8 @@ github.com/aws/aws-sdk-go-v2/service/eks v1.64.0 h1:EYeOThTRysemFtC6J6h6b7dNg3jN
7676
github.com/aws/aws-sdk-go-v2/service/eks v1.64.0/go.mod h1:v1xXy6ea0PHtWkjFUvAUh6B/5wv7UF909Nru0dOIJDk=
7777
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing v1.29.6 h1:9grU/+HRwLXJV8XUjEPThJj/H+0oHkeNBFpSSfZekeg=
7878
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing v1.29.6/go.mod h1:N4fs285CsnBHlAkzBpQapefR/noggTyF09fWs72EzB4=
79-
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2 v1.34.0 h1:8rDRtPOu3ax8jEctw7G926JQlnFdhZZA4KJzQ+4ks3Q=
80-
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2 v1.34.0/go.mod h1:L5bVuO4PeXuDuMYZfL3IW69E6mz6PDCYpp6IKDlcLMA=
79+
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2 v1.45.2 h1:vX70Z4lNSr7XsioU0uJq5yvxgI50sB66MvD+V/3buS4=
80+
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2 v1.45.2/go.mod h1:xnCC3vFBfOKpU6PcsCKL2ktgBTZfOwTGxj6V8/X3IS4=
8181
github.com/aws/aws-sdk-go-v2/service/eventbridge v1.39.3 h1:T6L7fsONflMeXuvsT8qZ247hA8ShBB0jF9yUEhW4JqI=
8282
github.com/aws/aws-sdk-go-v2/service/eventbridge v1.39.3/go.mod h1:sIrUII6Z+hAVAgcpmsc2e9HvEr++m/v8aBPT7s4ZYUk=
8383
github.com/aws/aws-sdk-go-v2/service/iam v1.32.0 h1:ZNlfPdw849gBo/lvLFbEEvpTJMij0LXqiNWZ+lIamlU=

pkg/cloud/convertersv2/tags.go

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package convertersv2 provides conversion functions for AWS SDK V2 types to CAPA types.
18+
package convertersv2
19+
20+
import (
21+
"sort"
22+
23+
"github.com/aws/aws-sdk-go-v2/aws"
24+
autoscalingtypes "github.com/aws/aws-sdk-go-v2/service/autoscaling/types"
25+
v2ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
26+
elbv1types "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing/types"
27+
elbv2types "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2/types"
28+
iamtypes "github.com/aws/aws-sdk-go-v2/service/iam/types"
29+
secretsmanagertypes "github.com/aws/aws-sdk-go-v2/service/secretsmanager/types"
30+
ssmtypes "github.com/aws/aws-sdk-go-v2/service/ssm/types"
31+
32+
infrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
33+
)
34+
35+
// TagsToMap converts a []v2ec2types.Tag into a infrav1.Tags.
36+
func TagsToMap(src []v2ec2types.Tag) infrav1.Tags {
37+
tags := make(infrav1.Tags, len(src))
38+
39+
for _, t := range src {
40+
tags[*t.Key] = *t.Value
41+
}
42+
43+
return tags
44+
}
45+
46+
// MapPtrToMap converts a [string]*string into a infrav1.Tags.
47+
func MapPtrToMap(src map[string]*string) infrav1.Tags {
48+
tags := make(infrav1.Tags, len(src))
49+
50+
for k, v := range src {
51+
tags[k] = *v
52+
}
53+
54+
return tags
55+
}
56+
57+
// MapToTags converts a infrav1.Tags to a []v2ec2types.Tag.
58+
func MapToTags(src infrav1.Tags) []v2ec2types.Tag {
59+
tags := make([]v2ec2types.Tag, 0, len(src))
60+
61+
for k, v := range src {
62+
tag := v2ec2types.Tag{
63+
Key: aws.String(k),
64+
Value: aws.String(v),
65+
}
66+
67+
tags = append(tags, tag)
68+
}
69+
70+
// Sort so that unit tests can expect a stable order
71+
sort.Slice(tags, func(i, j int) bool { return *tags[i].Key < *tags[j].Key })
72+
73+
return tags
74+
}
75+
76+
// ELBTagsToMap converts a []elbv1types.Tag into a infrav1.Tags.
77+
func ELBTagsToMap(src []elbv1types.Tag) infrav1.Tags {
78+
tags := make(infrav1.Tags, len(src))
79+
80+
for _, t := range src {
81+
tags[*t.Key] = *t.Value
82+
}
83+
84+
return tags
85+
}
86+
87+
// V2TagsToMap converts a []elbv2types.Tag into a infrav1.Tags.
88+
func V2TagsToMap(src []elbv2types.Tag) infrav1.Tags {
89+
tags := make(infrav1.Tags, len(src))
90+
91+
for _, t := range src {
92+
tags[*t.Key] = *t.Value
93+
}
94+
95+
return tags
96+
}
97+
98+
// MapToELBTags converts a infrav1.Tags to a []elbv1types.Tag.
99+
func MapToELBTags(src infrav1.Tags) []elbv1types.Tag {
100+
tags := make([]elbv1types.Tag, 0, len(src))
101+
102+
for k, v := range src {
103+
tag := elbv1types.Tag{
104+
Key: aws.String(k),
105+
Value: aws.String(v),
106+
}
107+
108+
tags = append(tags, tag)
109+
}
110+
111+
// Sort so that unit tests can expect a stable order
112+
sort.Slice(tags, func(i, j int) bool { return *tags[i].Key < *tags[j].Key })
113+
114+
return tags
115+
}
116+
117+
// MapToV2Tags converts a infrav1.Tags to a []elbv2types.Tag.
118+
func MapToV2Tags(src infrav1.Tags) []elbv2types.Tag {
119+
tags := make([]elbv2types.Tag, 0, len(src))
120+
121+
for k, v := range src {
122+
tag := elbv2types.Tag{
123+
Key: aws.String(k),
124+
Value: aws.String(v),
125+
}
126+
127+
tags = append(tags, tag)
128+
}
129+
130+
// Sort so that unit tests can expect a stable order
131+
sort.Slice(tags, func(i, j int) bool { return *tags[i].Key < *tags[j].Key })
132+
133+
return tags
134+
}
135+
136+
// MapToSecretsManagerTags converts a infrav1.Tags to a []secretsmanagertypes.Tag.
137+
func MapToSecretsManagerTags(src infrav1.Tags) []secretsmanagertypes.Tag {
138+
tags := make([]secretsmanagertypes.Tag, 0, len(src))
139+
140+
for k, v := range src {
141+
tag := secretsmanagertypes.Tag{
142+
Key: aws.String(k),
143+
Value: aws.String(v),
144+
}
145+
146+
tags = append(tags, tag)
147+
}
148+
149+
// Sort so that unit tests can expect a stable order
150+
sort.Slice(tags, func(i, j int) bool { return *tags[i].Key < *tags[j].Key })
151+
152+
return tags
153+
}
154+
155+
// MapToSSMTags converts a infrav1.Tags to a []ssm.Tag.
156+
func MapToSSMTags(src infrav1.Tags) []ssmtypes.Tag {
157+
tags := make([]ssmtypes.Tag, 0, len(src))
158+
159+
for k, v := range src {
160+
tag := ssmtypes.Tag{
161+
Key: aws.String(k),
162+
Value: aws.String(v),
163+
}
164+
165+
tags = append(tags, tag)
166+
}
167+
168+
// Sort so that unit tests can expect a stable order
169+
sort.Slice(tags, func(i, j int) bool { return *tags[i].Key < *tags[j].Key })
170+
171+
return tags
172+
}
173+
174+
// MapToIAMTags converts a infrav1.Tags to a []iamtypes.Tag.
175+
func MapToIAMTags(src infrav1.Tags) []iamtypes.Tag {
176+
tags := make([]iamtypes.Tag, 0, len(src))
177+
178+
for k, v := range src {
179+
tag := iamtypes.Tag{
180+
Key: aws.String(k),
181+
Value: aws.String(v),
182+
}
183+
184+
tags = append(tags, tag)
185+
}
186+
187+
// Sort so that unit tests can expect a stable order
188+
sort.Slice(tags, func(i, j int) bool { return *tags[i].Key < *tags[j].Key })
189+
190+
return tags
191+
}
192+
193+
// ASGTagsToMap converts a []autoscalingtypes.TagDescription into a infrav1.Tags.
194+
func ASGTagsToMap(src []autoscalingtypes.TagDescription) infrav1.Tags {
195+
tags := make(infrav1.Tags, len(src))
196+
197+
for _, t := range src {
198+
tags[*t.Key] = *t.Value
199+
}
200+
201+
return tags
202+
}

pkg/cloud/endpointsv2/endpoints.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"net/url"
2424
"strings"
2525

26+
"github.com/aws/aws-sdk-go-v2/service/ec2"
2627
"github.com/aws/aws-sdk-go-v2/service/eks"
2728
elb "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing"
2829
elbv2 "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2"
@@ -171,6 +172,28 @@ func (s *ELBV2EndpointResolver) ResolveEndpoint(ctx context.Context, params elbv
171172
return elbv2.NewDefaultEndpointResolverV2().ResolveEndpoint(ctx, params)
172173
}
173174

175+
// EC2EndpointResolver implements EndpointResolverV2 interface for EC2.
176+
type EC2EndpointResolver struct {
177+
*MultiServiceEndpointResolver
178+
}
179+
180+
// ResolveEndpoint for ELBV2.
181+
func (s *EC2EndpointResolver) ResolveEndpoint(ctx context.Context, params ec2.EndpointParameters) (smithyendpoints.Endpoint, error) {
182+
// If custom endpoint not found, return default endpoint for the service
183+
log := logger.FromContext(ctx)
184+
endpoint, ok := s.endpoints[ec2.ServiceID]
185+
186+
if !ok {
187+
log.Debug("Custom endpoint not found, using default endpoint")
188+
return ec2.NewDefaultEndpointResolverV2().ResolveEndpoint(ctx, params)
189+
}
190+
191+
log.Debug("Custom endpoint found, using custom endpoint", "endpoint", endpoint.URL)
192+
params.Endpoint = &endpoint.URL
193+
params.Region = &endpoint.SigningRegion
194+
return ec2.NewDefaultEndpointResolverV2().ResolveEndpoint(ctx, params)
195+
}
196+
174197
// RGAPIEndpointResolver implements EndpointResolverV2 interface for RGAPI.
175198
type RGAPIEndpointResolver struct {
176199
*MultiServiceEndpointResolver

0 commit comments

Comments
 (0)