Skip to content

Commit

Permalink
Merge pull request #1034 from kubernetes-sigs/upgrade-to-aws-sdk-v2
Browse files Browse the repository at this point in the history
Upgrade to AWS SDK for Go v2
  • Loading branch information
k8s-ci-robot committed Aug 19, 2024
2 parents 5d1eaa5 + 85fcf5b commit bbc69a3
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 59 deletions.
16 changes: 14 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ module sigs.k8s.io/zeitgeist
go 1.22.5

require (
github.com/aws/aws-sdk-go v1.55.5
github.com/aws/aws-sdk-go-v2 v1.30.4
github.com/aws/aws-sdk-go-v2/config v1.27.28
github.com/aws/aws-sdk-go-v2/service/ec2 v1.175.1
github.com/blang/semver/v4 v4.0.0
github.com/google/go-containerregistry v0.20.2
github.com/maxbrunsfeld/counterfeiter/v6 v6.8.1
Expand All @@ -26,6 +28,17 @@ require (
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/Microsoft/hcsshim v0.11.4 // indirect
github.com/ProtonMail/go-crypto v1.0.0 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.17.28 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.12 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.16 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.16 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.4 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.18 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.22.5 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.5 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.30.4 // indirect
github.com/aws/smithy-go v1.20.4 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
Expand Down Expand Up @@ -72,7 +85,6 @@ require (
github.com/imdario/mergo v0.3.16 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
Expand Down
34 changes: 28 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,34 @@ github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFI
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/aws/aws-sdk-go v1.55.5 h1:KKUZBfBoyqy5d3swXyiC7Q76ic40rYcbqH7qjh59kzU=
github.com/aws/aws-sdk-go v1.55.5/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU=
github.com/aws/aws-sdk-go-v2 v1.30.4 h1:frhcagrVNrzmT95RJImMHgabt99vkXGslubDaDagTk8=
github.com/aws/aws-sdk-go-v2 v1.30.4/go.mod h1:CT+ZPWXbYrci8chcARI3OmI/qgd+f6WtuLOoaIA8PR0=
github.com/aws/aws-sdk-go-v2/config v1.27.28 h1:OTxWGW/91C61QlneCtnD62NLb4W616/NM1jA8LhJqbg=
github.com/aws/aws-sdk-go-v2/config v1.27.28/go.mod h1:uzVRVtJSU5EFv6Fu82AoVFKozJi2ZCY6WRCXj06rbvs=
github.com/aws/aws-sdk-go-v2/credentials v1.17.28 h1:m8+AHY/ND8CMHJnPoH7PJIRakWGa4gbfbxuY9TGTUXM=
github.com/aws/aws-sdk-go-v2/credentials v1.17.28/go.mod h1:6TF7dSc78ehD1SL6KpRIPKMA1GyyWflIkjqg+qmf4+c=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.12 h1:yjwoSyDZF8Jth+mUk5lSPJCkMC0lMy6FaCD51jm6ayE=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.12/go.mod h1:fuR57fAgMk7ot3WcNQfb6rSEn+SUffl7ri+aa8uKysI=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.16 h1:TNyt/+X43KJ9IJJMjKfa3bNTiZbUP7DeCxfbTROESwY=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.16/go.mod h1:2DwJF39FlNAUiX5pAc0UNeiz16lK2t7IaFcm0LFHEgc=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.16 h1:jYfy8UPmd+6kJW5YhY0L1/KftReOGxI/4NtVSTh9O/I=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.16/go.mod h1:7ZfEPZxkW42Afq4uQB8H2E2e6ebh6mXTueEpYzjCzcs=
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1 h1:VaRN3TlFdd6KxX1x3ILT5ynH6HvKgqdiXoTxAF4HQcQ=
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1/go.mod h1:FbtygfRFze9usAadmnGJNc8KsP346kEe+y2/oyhGAGc=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.175.1 h1:7B5ppg4i5N2B6t+aH77WLbAu8sD98MLlzruWzq5scyY=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.175.1/go.mod h1:ISODge3zgdwOEa4Ou6WM9PKbxJWJ15DYKnr2bfmCAIA=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.4 h1:KypMCbLPPHEmf9DgMGw51jMj77VfGPAN2Kv4cfhlfgI=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.4/go.mod h1:Vz1JQXliGcQktFTN/LN6uGppAIRoLBR2bMvIMP0gOjc=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.18 h1:tJ5RnkHCiSH0jyd6gROjlJtNwov0eGYNz8s8nFcR0jQ=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.18/go.mod h1:++NHzT+nAF7ZPrHPsA+ENvsXkOO8wEu+C6RXltAG4/c=
github.com/aws/aws-sdk-go-v2/service/sso v1.22.5 h1:zCsFCKvbj25i7p1u94imVoO447I/sFv8qq+lGJhRN0c=
github.com/aws/aws-sdk-go-v2/service/sso v1.22.5/go.mod h1:ZeDX1SnKsVlejeuz41GiajjZpRSWR7/42q/EyA/QEiM=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.5 h1:SKvPgvdvmiTWoi0GAJ7AsJfOz3ngVkD/ERbs5pUnHNI=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.5/go.mod h1:20sz31hv/WsPa3HhU3hfrIet2kxM4Pe0r20eBZ20Tac=
github.com/aws/aws-sdk-go-v2/service/sts v1.30.4 h1:iAckBT2OeEK/kBDyN/jDtpEExhjeeA/Im2q4X0rJZT8=
github.com/aws/aws-sdk-go-v2/service/sts v1.30.4/go.mod h1:vmSqFK+BVIwVpDAGZB3CoCXHzurt4qBE8lf+I/kRTh0=
github.com/aws/smithy-go v1.20.4 h1:2HK1zBdPgRbjFOHlfeQZfpC4r72MOb9bZkiFwggKO+4=
github.com/aws/smithy-go v1.20.4/go.mod h1:irrKGvNn1InZwb2d7fkIRNucdfwR8R+Ts3wxYa/cJHg=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
Expand Down Expand Up @@ -205,10 +231,6 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
Expand Down
9 changes: 7 additions & 2 deletions remote/dependency/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ limitations under the License.
package dependency

import (
"context"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/mitchellh/mapstructure"
log "github.com/sirupsen/logrus"

Expand All @@ -38,7 +39,11 @@ func init() {

type RemoteClient struct {
LocalClient deppkg.Client
AWSEC2Client ec2iface.EC2API
AWSEC2Client EC2DescribeImagesAPI
}

type EC2DescribeImagesAPI interface {
DescribeImages(ctx context.Context, params *ec2.DescribeImagesInput, optFns ...func(*ec2.Options)) (*ec2.DescribeImagesOutput, error)
}

func NewRemoteClient() (deppkg.Client, error) {
Expand Down
16 changes: 8 additions & 8 deletions remote/dependency/dependency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,34 @@ limitations under the License.
package dependency

import (
"context"
"os"
"path/filepath"
"reflect"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/stretchr/testify/require"

deppkg "sigs.k8s.io/zeitgeist/dependency"
)

type mockedReceiveMsgs struct {
ec2iface.EC2API
type mockedEc2DescribeImagesAPI struct {
Resp ec2.DescribeImagesOutput
}

func (m mockedReceiveMsgs) DescribeImages(_ *ec2.DescribeImagesInput) (*ec2.DescribeImagesOutput, error) {
func (m mockedEc2DescribeImagesAPI) DescribeImages(_ context.Context, _ *ec2.DescribeImagesInput, _ ...func(*ec2.Options)) (*ec2.DescribeImagesOutput, error) {
// Only need to return mocked response output
return &m.Resp, nil
}

func TestRemoteSuccess(t *testing.T) {
var client RemoteClient
client.AWSEC2Client = mockedReceiveMsgs{
client.AWSEC2Client = mockedEc2DescribeImagesAPI{
Resp: ec2.DescribeImagesOutput{
Images: []*ec2.Image{
Images: []types.Image{
{
CreationDate: aws.String("2019-05-10T13:17:12.000Z"),
ImageId: aws.String("ami-09bbefc07310f7914"),
Expand Down
40 changes: 23 additions & 17 deletions upstream/ami.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ limitations under the License.
package upstream

import (
"context"
"fmt"
"sort"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
log "github.com/sirupsen/logrus"
)

Expand All @@ -41,7 +42,11 @@ type AMI struct {
Name string

// ServiceClient is the AWS client to talk to AWS API
ServiceClient ec2iface.EC2API
ServiceClient EC2DescribeImagesAPI
}

type EC2DescribeImagesAPI interface {
DescribeImages(ctx context.Context, params *ec2.DescribeImagesInput, optFns ...func(*ec2.Options)) (*ec2.DescribeImagesOutput, error)
}

// NewAWSClient return a new aws service client for ec2
Expand All @@ -50,13 +55,14 @@ type AMI struct {
// `~/.aws/config` and `~/.aws/credentials` files, and support environment variables.
// See AWS documentation for more details:
// https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/sessions.html
func NewAWSClient() *ec2.EC2 {
func NewAWSClient() *ec2.Client {
// Create a new session based on shared / env credentials
s := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
cfg, err := config.LoadDefaultConfig(context.Background())
if err != nil {
log.Fatal("failed to load aws config", err)
}

return ec2.New(s)
return ec2.NewFromConfig(cfg)
}

// LatestVersion returns the latest version of an AMI.
Expand All @@ -68,19 +74,19 @@ func (upstream AMI) LatestVersion() (string, error) {
log.Debug("Using AMI upstream")

// Generate filters based on configuration
var filters []*ec2.Filter
filters = append(filters, &ec2.Filter{
var filters []types.Filter
filters = append(filters, types.Filter{
Name: aws.String("name"),
Values: []*string{aws.String(upstream.Name)},
Values: []string{upstream.Name},
})

input := &ec2.DescribeImagesInput{
Owners: []*string{aws.String(upstream.Owner)},
Owners: []string{upstream.Owner},
Filters: filters,
}

// Do the actual API call
result, err := upstream.ServiceClient.DescribeImages(input)
result, err := upstream.ServiceClient.DescribeImages(context.Background(), input)
if err != nil {
return "", err
}
Expand All @@ -89,14 +95,14 @@ func (upstream AMI) LatestVersion() (string, error) {

// Sort images by creation time, so we can return the latest
sort.Slice(images, func(i, j int) bool { return *images[i].CreationDate > *images[j].CreationDate })
log.Debugf("Matched AMIs:\n%s", images)
log.Debugf("Matched AMIs:\n%v", images)

if len(images) < 1 {
return "", fmt.Errorf("no AMI found for upstream %s", upstream.Name)
}

latestImage := images[0]
log.Debugf("Latest AMI: %s\n", latestImage)
log.Debugf("Latest AMI ID: %v\n", *latestImage.ImageId)

return *latestImage.ImageId, nil
}
60 changes: 36 additions & 24 deletions upstream/ami_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,34 @@ limitations under the License.
package upstream

import (
"context"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/stretchr/testify/require"
)

/*
type mockedReceiveMsgs struct {
ec2iface.EC2API
ec2.Client
Resp ec2.DescribeImagesOutput
}
*/

func (m mockedReceiveMsgs) DescribeImages(_ *ec2.DescribeImagesInput) (*ec2.DescribeImagesOutput, error) {
// Only need to return mocked response output
return &m.Resp, nil
type mockEc2Api func(ctx context.Context, params *ec2.DescribeImagesInput, optFns ...func(*ec2.Options)) (*ec2.DescribeImagesOutput, error)

func (m mockEc2Api) DescribeImages(ctx context.Context, params *ec2.DescribeImagesInput, optFns ...func(*ec2.Options)) (*ec2.DescribeImagesOutput, error) {
return m(ctx, params, optFns...)
}

func TestGetAMI(t *testing.T) {
testCases := []struct {
Name string
Input AMI
Resp ec2.DescribeImagesOutput
Client func(t *testing.T) mockEc2Api
Expected string
ExpectedError bool
}{
Expand All @@ -49,19 +54,23 @@ func TestGetAMI(t *testing.T) {
Owner: "amazon",
Name: "amazon-eks-node-1.13-*",
},
Resp: ec2.DescribeImagesOutput{
Images: []*ec2.Image{
{
CreationDate: aws.String("2019-05-10T13:17:12.000Z"),
ImageId: aws.String("ami-123oldimage"),
Name: aws.String("amazon-eks-node-1.13-honk"),
},
{
CreationDate: aws.String("2019-05-12T13:17:12.000Z"),
ImageId: aws.String("ami-honk"),
Name: aws.String("amazon-eks-node-1.13-old"),
},
},
Client: func(_ *testing.T) mockEc2Api {
return mockEc2Api(func(_ context.Context, _ *ec2.DescribeImagesInput, _ ...func(*ec2.Options)) (*ec2.DescribeImagesOutput, error) {
return &ec2.DescribeImagesOutput{
Images: []types.Image{
{
CreationDate: aws.String("2019-05-10T13:17:12.000Z"),
ImageId: aws.String("ami-123oldimage"),
Name: aws.String("amazon-eks-node-1.13-honk"),
},
{
CreationDate: aws.String("2019-05-12T13:17:12.000Z"),
ImageId: aws.String("ami-honk"),
Name: aws.String("amazon-eks-node-1.13-old"),
},
},
}, nil
})
},
Expected: "ami-honk",
ExpectedError: false,
Expand All @@ -72,8 +81,12 @@ func TestGetAMI(t *testing.T) {
Owner: "honk",
Name: "this-ami-doesnt-exist-zeitgeist",
},
Resp: ec2.DescribeImagesOutput{
Images: []*ec2.Image{},
Client: func(_ *testing.T) mockEc2Api {
return mockEc2Api(func(_ context.Context, _ *ec2.DescribeImagesInput, _ ...func(*ec2.Options)) (*ec2.DescribeImagesOutput, error) {
return &ec2.DescribeImagesOutput{
Images: []types.Image{},
}, nil
})
},
Expected: "no AMI found for upstream this-ami-doesnt-exist-zeitgeist",
ExpectedError: true,
Expand All @@ -82,8 +95,7 @@ func TestGetAMI(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
tc.Input.ServiceClient = mockedReceiveMsgs{Resp: tc.Resp}

tc.Input.ServiceClient = tc.Client(t)
latestImage, err := tc.Input.LatestVersion()
if tc.ExpectedError {
require.Error(t, err)
Expand Down

0 comments on commit bbc69a3

Please sign in to comment.