Skip to content

Commit e48dd7b

Browse files
authored
Migrate/sdk v2 (#169)
* Initial iteration of AWS SDK GO V2 by michaelhtm * Add region to GetCallerIdentity request, handle AwsErrors with APIError * Address PR comments * Fix how we inject user Agent Now using a custom client to inject the user agent
1 parent 8d1fd6c commit e48dd7b

15 files changed

+213
-179
lines changed

go.mod

+13-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ go 1.22.0
55
toolchain go1.22.3
66

77
require (
8-
github.com/aws/aws-sdk-go v1.49.0
8+
github.com/aws/aws-sdk-go-v2 v1.32.7
9+
github.com/aws/aws-sdk-go-v2/config v1.28.6
10+
github.com/aws/aws-sdk-go-v2/credentials v1.17.47
11+
github.com/aws/aws-sdk-go-v2/service/sts v1.33.2
12+
github.com/aws/smithy-go v1.22.2
913
github.com/cenkalti/backoff/v4 v4.3.0
1014
github.com/go-logr/logr v1.4.2
1115
github.com/google/go-cmp v0.6.0
@@ -25,6 +29,14 @@ require (
2529
)
2630

2731
require (
32+
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.21 // indirect
33+
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.26 // indirect
34+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.26 // indirect
35+
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1 // indirect
36+
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.1 // indirect
37+
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.6 // indirect
38+
github.com/aws/aws-sdk-go-v2/service/sso v1.24.7 // indirect
39+
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.6 // indirect
2840
github.com/beorn7/perks v1.0.1 // indirect
2941
github.com/cespare/xxhash/v2 v2.3.0 // indirect
3042
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
@@ -45,7 +57,6 @@ require (
4557
github.com/google/uuid v1.6.0 // indirect
4658
github.com/imdario/mergo v0.3.12 // indirect
4759
github.com/itchyny/timefmt-go v0.1.3 // indirect
48-
github.com/jmespath/go-jmespath v0.4.0 // indirect
4960
github.com/josharian/intern v1.0.0 // indirect
5061
github.com/json-iterator/go v1.1.12 // indirect
5162
github.com/mailru/easyjson v0.7.7 // indirect

go.sum

+26-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
1-
github.com/aws/aws-sdk-go v1.49.0 h1:g9BkW1fo9GqKfwg2+zCD+TW/D36Ux+vtfJ8guF4AYmY=
2-
github.com/aws/aws-sdk-go v1.49.0/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
1+
github.com/aws/aws-sdk-go-v2 v1.32.7 h1:ky5o35oENWi0JYWUZkB7WYvVPP+bcRF5/Iq7JWSb5Rw=
2+
github.com/aws/aws-sdk-go-v2 v1.32.7/go.mod h1:P5WJBrYqqbWVaOxgH0X/FYYD47/nooaPOZPlQdmiN2U=
3+
github.com/aws/aws-sdk-go-v2/config v1.28.6 h1:D89IKtGrs/I3QXOLNTH93NJYtDhm8SYa9Q5CsPShmyo=
4+
github.com/aws/aws-sdk-go-v2/config v1.28.6/go.mod h1:GDzxJ5wyyFSCoLkS+UhGB0dArhb9mI+Co4dHtoTxbko=
5+
github.com/aws/aws-sdk-go-v2/credentials v1.17.47 h1:48bA+3/fCdi2yAwVt+3COvmatZ6jUDNkDTIsqDiMUdw=
6+
github.com/aws/aws-sdk-go-v2/credentials v1.17.47/go.mod h1:+KdckOejLW3Ks3b0E3b5rHsr2f9yuORBum0WPnE5o5w=
7+
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.21 h1:AmoU1pziydclFT/xRV+xXE/Vb8fttJCLRPv8oAkprc0=
8+
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.21/go.mod h1:AjUdLYe4Tgs6kpH4Bv7uMZo7pottoyHMn4eTcIcneaY=
9+
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.26 h1:I/5wmGMffY4happ8NOCuIUEWGUvvFp5NSeQcXl9RHcI=
10+
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.26/go.mod h1:FR8f4turZtNy6baO0KJ5FJUmXH/cSkI9fOngs0yl6mA=
11+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.26 h1:zXFLuEuMMUOvEARXFUVJdfqZ4bvvSgdGRq/ATcrQxzM=
12+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.26/go.mod h1:3o2Wpy0bogG1kyOPrgkXA8pgIfEEv0+m19O9D5+W8y8=
13+
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1 h1:VaRN3TlFdd6KxX1x3ILT5ynH6HvKgqdiXoTxAF4HQcQ=
14+
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1/go.mod h1:FbtygfRFze9usAadmnGJNc8KsP346kEe+y2/oyhGAGc=
15+
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.1 h1:iXtILhvDxB6kPvEXgsDhGaZCSC6LQET5ZHSdJozeI0Y=
16+
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.1/go.mod h1:9nu0fVANtYiAePIBh2/pFUSwtJ402hLnp854CNoDOeE=
17+
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.6 h1:50+XsN70RS7dwJ2CkVNXzj7U2L1HKP8nqTd3XWEXBN4=
18+
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.6/go.mod h1:WqgLmwY7so32kG01zD8CPTJWVWM+TzJoOVHwTg4aPug=
19+
github.com/aws/aws-sdk-go-v2/service/sso v1.24.7 h1:rLnYAfXQ3YAccocshIH5mzNNwZBkBo+bP6EhIxak6Hw=
20+
github.com/aws/aws-sdk-go-v2/service/sso v1.24.7/go.mod h1:ZHtuQJ6t9A/+YDuxOLnbryAmITtr8UysSny3qcyvJTc=
21+
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.6 h1:JnhTZR3PiYDNKlXy50/pNeix9aGMo6lLpXwJ1mw8MD4=
22+
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.6/go.mod h1:URronUEGfXZN1VpdktPSD1EkAL9mfrV+2F4sjH38qOY=
23+
github.com/aws/aws-sdk-go-v2/service/sts v1.33.2 h1:s4074ZO1Hk8qv65GqNXqDjmkf4HSQqJukaLuuW0TpDA=
24+
github.com/aws/aws-sdk-go-v2/service/sts v1.33.2/go.mod h1:mVggCnIWoM09jP71Wh+ea7+5gAp53q+49wDFs1SW5z8=
25+
github.com/aws/smithy-go v1.22.2 h1:6D9hW43xKFrRx/tXXfAlIZc4JI+yQe6snnWcQyxSyLQ=
26+
github.com/aws/smithy-go v1.22.2/go.mod h1:irrKGvNn1InZwb2d7fkIRNucdfwR8R+Ts3wxYa/cJHg=
327
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
428
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
529
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
@@ -61,10 +85,6 @@ github.com/itchyny/timefmt-go v0.1.3 h1:7M3LGVDsqcd0VZH2U+x393obrzZisp7C0uEe921i
6185
github.com/itchyny/timefmt-go v0.1.3/go.mod h1:0osSSCQSASBJMsIZnhAaF1C2fCBTJZXrnj37mG8/c+A=
6286
github.com/jaypipes/envutil v1.0.0 h1:u6Vwy9HwruFihoZrL0bxDLCa/YNadGVwKyPElNmZWow=
6387
github.com/jaypipes/envutil v1.0.0/go.mod h1:vgIRDly+xgBq0eeZRcflOHMMobMwgC6MkMbxo/Nw65M=
64-
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
65-
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
66-
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
67-
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
6888
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
6989
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
7090
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=

mocks/pkg/types/aws_resource.go

+14-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mocks/pkg/types/aws_resource_manager_factory.go

+5-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mocks/pkg/types/service_controller.go

+15-15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/config/config.go

+17-14
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@
1414
package config
1515

1616
import (
17+
"context"
1718
"errors"
1819
"fmt"
1920
"net/url"
2021
"strconv"
2122
"strings"
2223
"time"
2324

24-
"github.com/aws/aws-sdk-go/aws"
25-
"github.com/aws/aws-sdk-go/aws/session"
26-
"github.com/aws/aws-sdk-go/service/sts"
25+
"github.com/aws/aws-sdk-go-v2/aws"
26+
"github.com/aws/aws-sdk-go-v2/config"
27+
"github.com/aws/aws-sdk-go-v2/service/sts"
2728
"github.com/jaypipes/envutil"
2829
flag "github.com/spf13/pflag"
2930
"go.uber.org/zap/zapcore"
@@ -257,19 +258,21 @@ func (cfg *Config) SetupLogger() {
257258

258259
// SetAWSAccountID uses sts GetCallerIdentity API to find AWS AccountId and set
259260
// in Config
260-
func (cfg *Config) SetAWSAccountID() error {
261-
awsCfg := aws.Config{}
261+
func (cfg *Config) SetAWSAccountID(ctx context.Context) error {
262+
awsCfg, err := config.LoadDefaultConfig(
263+
ctx,
264+
config.WithRegion(cfg.Region),
265+
)
266+
if err != nil {
267+
return fmt.Errorf("unable to create awsCfg for SetAccountID: %v", err)
268+
}
262269
if cfg.IdentityEndpointURL != "" {
263-
awsCfg.Endpoint = aws.String(cfg.IdentityEndpointURL)
270+
awsCfg.BaseEndpoint = aws.String(cfg.IdentityEndpointURL)
264271
}
265272

266273
// use sts to find AWS AccountId
267-
session, err := session.NewSession(&awsCfg)
268-
if err != nil {
269-
return fmt.Errorf("unable to create session: %v", err)
270-
}
271-
client := sts.New(session)
272-
res, err := client.GetCallerIdentity(&sts.GetCallerIdentityInput{})
274+
client := sts.NewFromConfig(awsCfg)
275+
res, err := client.GetCallerIdentity(ctx, &sts.GetCallerIdentityInput{})
273276
if err != nil {
274277
return fmt.Errorf("unable to get caller identity: %v", err)
275278
}
@@ -278,7 +281,7 @@ func (cfg *Config) SetAWSAccountID() error {
278281
}
279282

280283
// Validate ensures the options are valid
281-
func (cfg *Config) Validate(options ...Option) error {
284+
func (cfg *Config) Validate(ctx context.Context, options ...Option) error {
282285
merged := mergeOptions(options)
283286
if len(merged.gvks) > 0 {
284287
err := cfg.validateReconcileConfigResources(merged.gvks)
@@ -317,7 +320,7 @@ func (cfg *Config) Validate(options ...Option) error {
317320
}
318321
}
319322

320-
if err := cfg.SetAWSAccountID(); err != nil {
323+
if err := cfg.SetAWSAccountID(ctx); err != nil {
321324
return fmt.Errorf("unable to determine account ID: %v", err)
322325
}
323326

pkg/errors/error.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
package errors
1515

1616
import (
17+
"errors"
1718
"fmt"
1819

19-
"github.com/aws/aws-sdk-go/aws/awserr"
20+
"github.com/aws/smithy-go"
2021
)
2122

2223
var (
@@ -68,15 +69,18 @@ var (
6869

6970
// AWSError returns the type conversion for the supplied error to an aws-sdk-go
7071
// Error interface
71-
func AWSError(err error) (awserr.Error, bool) {
72-
awsErr, ok := err.(awserr.Error)
72+
func AWSError(err error) (smithy.APIError, bool) {
73+
var awsErr smithy.APIError
74+
ok := errors.As(err, &awsErr)
75+
7376
return awsErr, ok
7477
}
7578

7679
// AWSRequestFailure returns the type conversion for the supplied error to an
7780
// aws-sdk-go RequestFailure interface
78-
func AWSRequestFailure(err error) (awserr.RequestFailure, bool) {
79-
awsRF, ok := err.(awserr.RequestFailure)
81+
func AWSRequestFailure(err error) (smithy.APIError, bool) {
82+
var awsRF smithy.APIError
83+
ok := errors.As(err, &awsRF)
8084
return awsRF, ok
8185
}
8286

@@ -95,7 +99,7 @@ func HTTPStatusCode(err error) int {
9599
if !ok {
96100
return -1
97101
}
98-
return awsRF.StatusCode()
102+
return int(awsRF.ErrorFault())
99103
}
100104

101105
// TerminalError defines an error that should be considered terminal, and placed

pkg/runtime/adoption_reconciler.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"context"
1818
"fmt"
1919

20-
"github.com/aws/aws-sdk-go/aws/arn"
20+
"github.com/aws/aws-sdk-go-v2/aws/arn"
2121
"github.com/go-logr/logr"
2222
"github.com/pkg/errors"
2323
corev1 "k8s.io/api/core/v1"
@@ -149,15 +149,15 @@ func (r *adoptionReconciler) reconcile(ctx context.Context, req ctrlrt.Request)
149149
endpointURL := r.getEndpointURL(res)
150150
gvk := targetDescriptor.GroupVersionKind()
151151

152-
sess, err := r.sc.NewSession(region, &endpointURL, roleARN, gvk)
152+
awsconfig, err := r.sc.NewAWSConfig(ctx, region, &endpointURL, roleARN, gvk)
153153
if err != nil {
154154
return err
155155
}
156156

157157
ackrtlog.InfoAdoptedResource(r.log, res, "starting adoption reconciliation")
158158

159159
rm, err := rmf.ManagerFor(
160-
r.cfg, r.log, r.metrics, r, sess, acctID, region, roleARN,
160+
r.cfg, awsconfig, r.log, r.metrics, r, acctID, region, roleARN,
161161
)
162162
if err != nil {
163163
return err

0 commit comments

Comments
 (0)