Skip to content

Commit 1a6a211

Browse files
committed
removed remaining aws sdk go v1 usage
1 parent 4ba6a52 commit 1a6a211

File tree

10 files changed

+75
-71
lines changed

10 files changed

+75
-71
lines changed

cmd/ecr-credential-provider/main.go

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ import (
2727
"strings"
2828
"time"
2929

30-
"github.com/aws/aws-sdk-go/aws"
31-
"github.com/aws/aws-sdk-go/aws/session"
32-
"github.com/aws/aws-sdk-go/service/ecr"
33-
"github.com/aws/aws-sdk-go/service/ecrpublic"
30+
"github.com/aws/aws-sdk-go-v2/aws"
31+
"github.com/aws/aws-sdk-go-v2/config"
32+
"github.com/aws/aws-sdk-go-v2/service/ecr"
33+
"github.com/aws/aws-sdk-go-v2/service/ecrpublic"
3434
"github.com/spf13/cobra"
3535

3636
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -46,67 +46,60 @@ var ecrPrivateHostPattern = regexp.MustCompile(`^(\d{12})\.dkr[\.\-]ecr(\-fips)?
4646

4747
// ECR abstracts the calls we make to aws-sdk for testing purposes
4848
type ECR interface {
49-
GetAuthorizationToken(input *ecr.GetAuthorizationTokenInput) (*ecr.GetAuthorizationTokenOutput, error)
49+
GetAuthorizationToken(ctx context.Context, input *ecr.GetAuthorizationTokenInput, optFns ...func(*ecr.Options)) (*ecr.GetAuthorizationTokenOutput, error)
5050
}
5151

5252
// ECRPublic abstracts the calls we make to aws-sdk for testing purposes
5353
type ECRPublic interface {
54-
GetAuthorizationToken(input *ecrpublic.GetAuthorizationTokenInput) (*ecrpublic.GetAuthorizationTokenOutput, error)
54+
GetAuthorizationToken(ctx context.Context, input *ecrpublic.GetAuthorizationTokenInput, optFns ...func(*ecrpublic.Options)) (*ecrpublic.GetAuthorizationTokenOutput, error)
5555
}
5656

5757
type ecrPlugin struct {
5858
ecr ECR
5959
ecrPublic ECRPublic
6060
}
6161

62-
func defaultECRProvider(region string) (*ecr.ECR, error) {
63-
cfg := aws.Config{}
64-
if region != "" {
65-
klog.Warningf("No region found in the image reference, the default region will be used. Please refer to AWS SDK documentation for configuration purpose.")
66-
cfg.Region = aws.String(region)
67-
}
68-
sess, err := session.NewSessionWithOptions(session.Options{
69-
Config: cfg,
70-
SharedConfigState: session.SharedConfigEnable,
71-
})
62+
func defaultECRProvider(ctx context.Context, region string) (*ecr.Client, error) {
63+
cfg, err := config.LoadDefaultConfig(ctx)
7264
if err != nil {
7365
return nil, err
7466
}
67+
if region != "" {
68+
klog.Warningf("No region found in the image reference, the default region will be used. Please refer to AWS SDK documentation for configuration purpose.")
69+
cfg.Region = region
70+
}
7571

76-
return ecr.New(sess), nil
72+
return ecr.NewFromConfig(cfg), nil
7773
}
7874

79-
func publicECRProvider() (*ecrpublic.ECRPublic, error) {
75+
func publicECRProvider(ctx context.Context) (*ecrpublic.Client, error) {
8076
// ECR public registries are only in one region and only accessible from regions
8177
// in the "aws" partition.
82-
sess, err := session.NewSessionWithOptions(session.Options{
83-
Config: aws.Config{Region: aws.String(ecrPublicRegion)},
84-
SharedConfigState: session.SharedConfigEnable,
85-
})
78+
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(ecrPublicRegion))
8679
if err != nil {
8780
return nil, err
8881
}
8982

90-
return ecrpublic.New(sess), nil
83+
return ecrpublic.NewFromConfig(cfg), nil
9184
}
9285

9386
type credsData struct {
9487
authToken *string
9588
expiresAt *time.Time
9689
}
9790

98-
func (e *ecrPlugin) getPublicCredsData() (*credsData, error) {
91+
func (e *ecrPlugin) getPublicCredsData(ctx context.Context) (*credsData, error) {
9992
klog.Infof("Getting creds for public registry")
10093
var err error
10194

10295
if e.ecrPublic == nil {
103-
e.ecrPublic, err = publicECRProvider()
96+
e.ecrPublic, err = publicECRProvider(ctx)
10497
}
10598
if err != nil {
10699
return nil, err
107100
}
108101

109-
output, err := e.ecrPublic.GetAuthorizationToken(&ecrpublic.GetAuthorizationTokenInput{})
102+
output, err := e.ecrPublic.GetAuthorizationToken(ctx, &ecrpublic.GetAuthorizationTokenInput{})
110103
if err != nil {
111104
return nil, err
112105
}
@@ -125,18 +118,18 @@ func (e *ecrPlugin) getPublicCredsData() (*credsData, error) {
125118
}, nil
126119
}
127120

128-
func (e *ecrPlugin) getPrivateCredsData(imageHost string, image string) (*credsData, error) {
121+
func (e *ecrPlugin) getPrivateCredsData(ctx context.Context, imageHost string, image string) (*credsData, error) {
129122
klog.Infof("Getting creds for private image %s", image)
130123
var err error
131124

132125
if e.ecr == nil {
133126
region := parseRegionFromECRPrivateHost(imageHost)
134-
e.ecr, err = defaultECRProvider(region)
127+
e.ecr, err = defaultECRProvider(ctx, region)
135128
if err != nil {
136129
return nil, err
137130
}
138131
}
139-
output, err := e.ecr.GetAuthorizationToken(&ecr.GetAuthorizationTokenInput{})
132+
output, err := e.ecr.GetAuthorizationToken(ctx, &ecr.GetAuthorizationTokenInput{})
140133
if err != nil {
141134
return nil, err
142135
}
@@ -162,9 +155,9 @@ func (e *ecrPlugin) GetCredentials(ctx context.Context, image string, args []str
162155
}
163156

164157
if imageHost == ecrPublicHost {
165-
creds, err = e.getPublicCredsData()
158+
creds, err = e.getPublicCredsData(ctx)
166159
} else {
167-
creds, err = e.getPrivateCredsData(imageHost, image)
160+
creds, err = e.getPrivateCredsData(ctx, imageHost, image)
168161
}
169162

170163
if err != nil {
@@ -175,7 +168,7 @@ func (e *ecrPlugin) GetCredentials(ctx context.Context, image string, args []str
175168
return nil, errors.New("authorization token in response was nil")
176169
}
177170

178-
decodedToken, err := base64.StdEncoding.DecodeString(aws.StringValue(creds.authToken))
171+
decodedToken, err := base64.StdEncoding.DecodeString(aws.ToString(creds.authToken))
179172
if err != nil {
180173
return nil, err
181174
}

cmd/ecr-credential-provider/main_test.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ import (
2424
"testing"
2525
"time"
2626

27-
"github.com/aws/aws-sdk-go/aws"
28-
"github.com/aws/aws-sdk-go/service/ecr"
29-
"github.com/aws/aws-sdk-go/service/ecrpublic"
27+
"github.com/aws/aws-sdk-go-v2/aws"
28+
"github.com/aws/aws-sdk-go-v2/service/ecr"
29+
ecrtypes "github.com/aws/aws-sdk-go-v2/service/ecr/types"
30+
"github.com/aws/aws-sdk-go-v2/service/ecrpublic"
31+
ecrpublictypes "github.com/aws/aws-sdk-go-v2/service/ecrpublic/types"
3032
"github.com/golang/mock/gomock"
3133
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3234
"k8s.io/cloud-provider-aws/pkg/mocks"
@@ -35,13 +37,13 @@ import (
3537

3638
func generatePrivateGetAuthorizationTokenOutput(user string, password string, proxy string, expiration *time.Time) *ecr.GetAuthorizationTokenOutput {
3739
creds := []byte(fmt.Sprintf("%s:%s", user, password))
38-
data := &ecr.AuthorizationData{
40+
data := &ecrtypes.AuthorizationData{
3941
AuthorizationToken: aws.String(base64.StdEncoding.EncodeToString(creds)),
4042
ExpiresAt: expiration,
4143
ProxyEndpoint: aws.String(proxy),
4244
}
4345
output := &ecr.GetAuthorizationTokenOutput{
44-
AuthorizationData: []*ecr.AuthorizationData{data},
46+
AuthorizationData: []ecrtypes.AuthorizationData{*data},
4547
}
4648
return output
4749
}
@@ -109,7 +111,7 @@ func Test_GetCredentials_Private(t *testing.T) {
109111
{
110112
name: "empty authorization token",
111113
image: "123456789123.dkr.ecr.us-west-2.amazonaws.com",
112-
getAuthorizationTokenOutput: &ecr.GetAuthorizationTokenOutput{AuthorizationData: []*ecr.AuthorizationData{{}}},
114+
getAuthorizationTokenOutput: &ecr.GetAuthorizationTokenOutput{AuthorizationData: []ecrtypes.AuthorizationData{{}}},
113115
getAuthorizationTokenError: nil,
114116
expectedError: errors.New("authorization token in response was nil"),
115117
},
@@ -124,7 +126,7 @@ func Test_GetCredentials_Private(t *testing.T) {
124126
name: "invalid authorization token",
125127
image: "123456789123.dkr.ecr.us-west-2.amazonaws.com",
126128
getAuthorizationTokenOutput: &ecr.GetAuthorizationTokenOutput{
127-
AuthorizationData: []*ecr.AuthorizationData{
129+
AuthorizationData: []ecrtypes.AuthorizationData{
128130
{AuthorizationToken: aws.String(base64.StdEncoding.EncodeToString([]byte(fmt.Sprint("foo"))))},
129131
},
130132
},
@@ -136,7 +138,7 @@ func Test_GetCredentials_Private(t *testing.T) {
136138
for _, testcase := range testcases {
137139
t.Run(testcase.name, func(t *testing.T) {
138140
p := &ecrPlugin{ecr: mockECR}
139-
mockECR.EXPECT().GetAuthorizationToken(gomock.Any()).Return(testcase.getAuthorizationTokenOutput, testcase.getAuthorizationTokenError)
141+
mockECR.EXPECT().GetAuthorizationToken(gomock.Any(), gomock.Any()).Return(testcase.getAuthorizationTokenOutput, testcase.getAuthorizationTokenError)
140142

141143
creds, err := p.GetCredentials(context.TODO(), testcase.image, testcase.args)
142144

@@ -163,7 +165,7 @@ func Test_GetCredentials_Private(t *testing.T) {
163165

164166
func generatePublicGetAuthorizationTokenOutput(user string, password string, proxy string, expiration *time.Time) *ecrpublic.GetAuthorizationTokenOutput {
165167
creds := []byte(fmt.Sprintf("%s:%s", user, password))
166-
data := &ecrpublic.AuthorizationData{
168+
data := &ecrpublictypes.AuthorizationData{
167169
AuthorizationToken: aws.String(base64.StdEncoding.EncodeToString(creds)),
168170
ExpiresAt: expiration,
169171
}
@@ -211,7 +213,7 @@ func Test_GetCredentials_Public(t *testing.T) {
211213
{
212214
name: "empty authorization token",
213215
image: "public.ecr.aws",
214-
getAuthorizationTokenOutput: &ecrpublic.GetAuthorizationTokenOutput{AuthorizationData: &ecrpublic.AuthorizationData{}},
216+
getAuthorizationTokenOutput: &ecrpublic.GetAuthorizationTokenOutput{AuthorizationData: &ecrpublictypes.AuthorizationData{}},
215217
getAuthorizationTokenError: nil,
216218
expectedError: errors.New("authorization token in response was nil"),
217219
},
@@ -226,7 +228,7 @@ func Test_GetCredentials_Public(t *testing.T) {
226228
name: "invalid authorization token",
227229
image: "public.ecr.aws",
228230
getAuthorizationTokenOutput: &ecrpublic.GetAuthorizationTokenOutput{
229-
AuthorizationData: &ecrpublic.AuthorizationData{
231+
AuthorizationData: &ecrpublictypes.AuthorizationData{
230232
AuthorizationToken: aws.String(base64.StdEncoding.EncodeToString([]byte(fmt.Sprint("foo")))),
231233
},
232234
},
@@ -238,7 +240,7 @@ func Test_GetCredentials_Public(t *testing.T) {
238240
for _, testcase := range testcases {
239241
t.Run(testcase.name, func(t *testing.T) {
240242
p := &ecrPlugin{ecrPublic: mockECRPublic}
241-
mockECRPublic.EXPECT().GetAuthorizationToken(gomock.Any()).Return(testcase.getAuthorizationTokenOutput, testcase.getAuthorizationTokenError)
243+
mockECRPublic.EXPECT().GetAuthorizationToken(gomock.Any(), gomock.Any()).Return(testcase.getAuthorizationTokenOutput, testcase.getAuthorizationTokenError)
242244

243245
creds, err := p.GetCredentials(context.TODO(), testcase.image, testcase.args)
244246

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ module k8s.io/cloud-provider-aws
33
go 1.24.4
44

55
require (
6-
github.com/aws/aws-sdk-go v1.55.7
76
github.com/aws/aws-sdk-go-v2 v1.36.5
87
github.com/aws/aws-sdk-go-v2/config v1.29.14
98
github.com/aws/aws-sdk-go-v2/service/autoscaling v1.53.3
9+
github.com/aws/aws-sdk-go-v2/service/ecr v1.45.1
10+
github.com/aws/aws-sdk-go-v2/service/ecrpublic v1.33.2
1011
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing v1.29.3
1112
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2 v1.45.2
1213
github.com/aws/aws-sdk-go-v2/service/kms v1.41.0
@@ -79,7 +80,6 @@ require (
7980
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect
8081
github.com/imdario/mergo v0.3.15 // indirect
8182
github.com/inconshreveable/mousetrap v1.1.0 // indirect
82-
github.com/jmespath/go-jmespath v0.4.0 // indirect
8383
github.com/josharian/intern v1.0.0 // indirect
8484
github.com/json-iterator/go v1.1.12 // indirect
8585
github.com/mailru/easyjson v0.7.7 // indirect

go.sum

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230321174746-8dcc6526cfb1 h
1111
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230321174746-8dcc6526cfb1/go.mod h1:pSwJ0fSY5KhvocuWSx4fz3BA8OrA1bQn+K1Eli3BRwM=
1212
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a h1:idn718Q4B6AGu/h5Sxe66HYVdqdGu2l9Iebqhi/AEoA=
1313
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
14-
github.com/aws/aws-sdk-go v1.55.7 h1:UJrkFq7es5CShfBwlWAC8DA077vp8PyVbQd3lqLiztE=
15-
github.com/aws/aws-sdk-go v1.55.7/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU=
1614
github.com/aws/aws-sdk-go-v2 v1.36.5 h1:0OF9RiEMEdDdZEMqF9MRjevyxAQcf6gY+E7vwBILFj0=
1715
github.com/aws/aws-sdk-go-v2 v1.36.5/go.mod h1:EYrzvCCN9CMUTa5+6lf6MM4tq3Zjp8UhSGR/cBsjai0=
1816
github.com/aws/aws-sdk-go-v2/config v1.29.14 h1:f+eEi/2cKCg9pqKBoAIwRGzVb70MRKqWX4dg1BDcSJM=
@@ -31,6 +29,10 @@ github.com/aws/aws-sdk-go-v2/service/autoscaling v1.53.3 h1:spHCGHuTPi/QaPd6tADK
3129
github.com/aws/aws-sdk-go-v2/service/autoscaling v1.53.3/go.mod h1:6U/Xm5bBkZGCTxH3NE9+hPKEpCFCothGn/gwytsr1Mk=
3230
github.com/aws/aws-sdk-go-v2/service/ec2 v1.218.0 h1:QPYsTfcPpPhkF+37pxLcl3xbQz2SRxsShQNB6VCkvLo=
3331
github.com/aws/aws-sdk-go-v2/service/ec2 v1.218.0/go.mod h1:ouvGEfHbLaIlWwpDpOVWPWR+YwO0HDv3vm5tYLq8ImY=
32+
github.com/aws/aws-sdk-go-v2/service/ecr v1.45.1 h1:Bwzh202Aq7/MYnAjXA9VawCf6u+hjwMdoYmZ4HYsdf8=
33+
github.com/aws/aws-sdk-go-v2/service/ecr v1.45.1/go.mod h1:xZzWl9AXYa6zsLLH41HBFW8KRKJRIzlGmvSM0mVMIX4=
34+
github.com/aws/aws-sdk-go-v2/service/ecrpublic v1.33.2 h1:XJ/AEFYj9VFPJdF+VFi4SUPEDfz1akHwxxm07JfZJcs=
35+
github.com/aws/aws-sdk-go-v2/service/ecrpublic v1.33.2/go.mod h1:JUBHdhvKbbKmhaHjLsKJAWnQL80T6nURmhB/LEprV+4=
3436
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing v1.29.3 h1:DpyV8LeDf0y7iDaGZ3h1Y+Nh5IaBOR+xj44vVgEEegY=
3537
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing v1.29.3/go.mod h1:H232HdqVlSUoqy0cMJYW1TKjcxvGFGFZ20xQG8fOAPw=
3638
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2 v1.45.2 h1:vX70Z4lNSr7XsioU0uJq5yvxgI50sB66MvD+V/3buS4=
@@ -149,10 +151,6 @@ github.com/imdario/mergo v0.3.15 h1:M8XP7IuFNsqUx6VPK2P9OSmsYsI/YFaGil0uD21V3dM=
149151
github.com/imdario/mergo v0.3.15/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
150152
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
151153
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
152-
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
153-
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
154-
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
155-
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
156154
github.com/jonboulle/clockwork v0.2.2 h1:UOGuzwb1PwsrDAObMuhUnj0p5ULPj8V/xJ7Kx9qUBdQ=
157155
github.com/jonboulle/clockwork v0.2.2/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8=
158156
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=

pkg/mocks/mock_ecr.go

Lines changed: 21 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/providers/v1/aws.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ import (
3232
"github.com/aws/aws-sdk-go-v2/aws"
3333
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
3434
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
35+
"github.com/aws/aws-sdk-go-v2/service/autoscaling"
3536
"github.com/aws/aws-sdk-go-v2/service/ec2"
3637
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
3738
elb "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing"
3839
elbtypes "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing/types"
3940
elbv2 "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2"
4041
elbv2types "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2/types"
41-
"github.com/aws/aws-sdk-go-v2/service/autoscaling"
4242
"github.com/aws/aws-sdk-go-v2/service/kms"
4343
"github.com/aws/smithy-go"
4444
"gopkg.in/gcfg.v1"
@@ -1013,7 +1013,7 @@ func newAWSCloud2(cfg config.CloudConfig, awsServices Services, provider config.
10131013
kms: kms,
10141014
cfg: &cfg,
10151015
region: regionName,
1016-
1016+
10171017
attaching: make(map[types.NodeName]map[mountDevice]EBSVolumeID),
10181018
deviceAllocators: make(map[types.NodeName]DeviceAllocator),
10191019
}

pkg/providers/v1/aws_sdk_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func TestClientsEndpointOverride(t *testing.T) {
138138
assert.True(t, reqInfo.usedCustomEndpoint, "KMS: custom endpoint was not used")
139139
assert.True(t, strings.Contains(reqInfo.credential, "custom-service"), "KMS: signing name was not properly overridden")
140140
assert.True(t, strings.Contains(reqInfo.credential, "custom-region"), "KMS: signing region was not properly overridden")
141-
141+
142142
// Test Autoscaling client
143143
reqInfo = requestInfo{}
144144
autoscalingClient, err := mockProvider.Autoscaling(context.TODO(), "us-west-2", nil)
@@ -149,7 +149,7 @@ func TestClientsEndpointOverride(t *testing.T) {
149149
assert.True(t, reqInfo.usedCustomEndpoint, "Autoscaling: custom endpoint was not used")
150150
assert.True(t, strings.Contains(reqInfo.credential, "custom-service"), "Autoscaling: signing name was not properly overridden")
151151
assert.True(t, strings.Contains(reqInfo.credential, "custom-region"), "Autoscaling: signing region was not properly overridden")
152-
152+
153153
})
154154

155155
// When the signing name is overridden but not the signing region, the signing name should be

0 commit comments

Comments
 (0)