Skip to content

Commit

Permalink
migrate aws package ec2 client to aws-sdk-go-v2
Browse files Browse the repository at this point in the history
Signed-off-by: Noor Malik <[email protected]>
  • Loading branch information
demonCoder95 committed Sep 1, 2024
1 parent 6d70589 commit 5a4f1b4
Show file tree
Hide file tree
Showing 11 changed files with 274 additions and 200 deletions.
14 changes: 14 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ require (

require (
github.com/Masterminds/sprig/v3 v3.2.3
github.com/aws/aws-sdk-go-v2 v1.30.4
github.com/aws/aws-sdk-go-v2/config v1.27.31
github.com/aws/aws-sdk-go-v2/service/ec2 v1.177.0
github.com/luci/go-render v0.0.0-20160219211803-9a04cc21af0f
github.com/samber/lo v1.46.0
sigs.k8s.io/aws-iam-authenticator v0.6.22
Expand All @@ -49,6 +52,17 @@ require (
github.com/Masterminds/semver/v3 v3.2.1 // indirect
github.com/alecthomas/units v0.0.0-20240626203959-61d1e3462e30 // indirect
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.17.30 // 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.5 // indirect
github.com/aws/smithy-go v1.20.4 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
Expand Down
28 changes: 28 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,34 @@ github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3d
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
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.31 h1:kxBoRsjhT3pq0cKthgj6RU6bXTm/2SgdoUMyrVw0rAI=
github.com/aws/aws-sdk-go-v2/config v1.27.31/go.mod h1:z04nZdSWFPaDwK3DdJOG2r+scLQzMYuJeW0CujEm9FM=
github.com/aws/aws-sdk-go-v2/credentials v1.17.30 h1:aau/oYFtibVovr2rDt8FHlU17BTicFEMAi29V1U+L5Q=
github.com/aws/aws-sdk-go-v2/credentials v1.17.30/go.mod h1:BPJ/yXV92ZVq6G8uYvbU0gSl8q94UB63nMT5ctNO38g=
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.177.0 h1:LAdDRIj5BEZM9fLDTUWUyPzWvv5A++nCEps/RGmZNOo=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.177.0/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.5 h1:OMsEmCyz2i89XwRwPouAJvhj81wINh+4UK+k/0Yo/q8=
github.com/aws/aws-sdk-go-v2/service/sts v1.30.5/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 v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
Expand Down
62 changes: 34 additions & 28 deletions pkg/aws/instance_info.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package aws

import (
"context"
"fmt"

"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"
log "github.com/sirupsen/logrus"
)

Expand All @@ -21,9 +22,9 @@ const (

type Instance struct {
InstanceType string
VCPU int64
VCPU int32
Memory int64
InstanceStorageDevices int64
InstanceStorageDevices int32
InstanceStorageDeviceSize int64
Architecture string
}
Expand All @@ -32,7 +33,7 @@ func (i Instance) AvailableStorage(instanceStorageScaleFactor float64, rootVolum
if i.InstanceStorageDevices == 0 {
return int64(float64(rootVolumeSize) * rootVolumeScaleFactor)
}
return int64(instanceStorageScaleFactor * float64(i.InstanceStorageDevices*i.InstanceStorageDeviceSize))
return int64(instanceStorageScaleFactor * float64(int64(i.InstanceStorageDevices)*i.InstanceStorageDeviceSize))
}

func (i Instance) MemoryFraction(percent int64) int64 {
Expand All @@ -51,14 +52,23 @@ func NewInstanceTypes(instanceData []Instance) *InstanceTypes {
return &InstanceTypes{instances: result}
}

func NewInstanceTypesFromAWS(ec2client ec2iface.EC2API) (*InstanceTypes, error) {
func NewInstanceTypesFromAWS(ec2client EC2API) (*InstanceTypes, error) {
instances := make(map[string]Instance)

var innerErr error
err := ec2client.DescribeInstanceTypesPages(&ec2.DescribeInstanceTypesInput{}, func(output *ec2.DescribeInstanceTypesOutput, _ bool) bool {

paginator := ec2.NewDescribeInstanceTypesPaginator(ec2client, &ec2.DescribeInstanceTypesInput{})

pagination:
for paginator.HasMorePages() {
output, err := paginator.NextPage(context.TODO())
if err != nil {
return nil, err
}
for _, instanceType := range output.InstanceTypes {
var (
deviceCount, deviceSize int64
deviceCount int32
deviceSize int64
)

if instanceType.InstanceStorageInfo != nil {
Expand All @@ -67,12 +77,12 @@ func NewInstanceTypesFromAWS(ec2client ec2iface.EC2API) (*InstanceTypes, error)
case 0:
// do nothing
case 1:
deviceCount = aws.Int64Value(storageDisks[0].Count)
deviceSize = aws.Int64Value(storageDisks[0].SizeInGB) * gigabyte
deviceCount = aws.ToInt32(storageDisks[0].Count)
deviceSize = aws.ToInt64(storageDisks[0].SizeInGB) * gigabyte
default:
// doesn't happen at the moment, raise an error so we can decide how to handle this
innerErr = fmt.Errorf("invalid number of disk sets (%d) for %s, expecting 0 or 1", len(storageDisks), aws.StringValue(instanceType.InstanceType))
return false
innerErr = fmt.Errorf("invalid number of disk sets (%d) for %s, expecting 0 or 1", len(storageDisks), string(instanceType.InstanceType))
break pagination
}
}

Expand All @@ -84,24 +94,20 @@ func NewInstanceTypesFromAWS(ec2client ec2iface.EC2API) (*InstanceTypes, error)
}

info := Instance{
InstanceType: aws.StringValue(instanceType.InstanceType),
VCPU: aws.Int64Value(instanceType.VCpuInfo.DefaultVCpus),
Memory: aws.Int64Value(instanceType.MemoryInfo.SizeInMiB) * mebibyte,
InstanceType: string(instanceType.InstanceType),
VCPU: aws.ToInt32(instanceType.VCpuInfo.DefaultVCpus),
Memory: aws.ToInt64(instanceType.MemoryInfo.SizeInMiB) * mebibyte,
InstanceStorageDevices: deviceCount,
InstanceStorageDeviceSize: deviceSize,
Architecture: cpuArch,
}
instances[info.InstanceType] = info
}
return true
})

}
if innerErr != nil {
return nil, innerErr
}
if err != nil {
return nil, err
}

log.Debugf("Loaded %d instance types from AWS", len(instances))
return &InstanceTypes{instances: instances}, nil
}
Expand Down Expand Up @@ -155,27 +161,27 @@ func (types *InstanceTypes) SyntheticInstanceInfo(instanceTypes []string) (Insta

// getCompatibleCPUArchitecture returns a single compatible CPU architecture. It's either `amd64` or `arm64`.
// Other intance types might return 32-bit or macos specific types which will be ignored.
func getCompatibleCPUArchitecture(instanceType *ec2.InstanceTypeInfo) (string, error) {
supportedArchitectures := aws.StringValueSlice(instanceType.ProcessorInfo.SupportedArchitectures)
func getCompatibleCPUArchitecture(instanceType types.InstanceTypeInfo) (string, error) {
supportedArchitectures := instanceType.ProcessorInfo.SupportedArchitectures

if contains(supportedArchitectures, archX86_64) {
if contains(supportedArchitectures, types.ArchitectureTypeX8664) {
return archAMD64, nil
}
if contains(supportedArchitectures, archARM64) {
if contains(supportedArchitectures, types.ArchitectureTypeArm64) {
return archARM64, nil
}

return "", fmt.Errorf("didn't find compatible cpu architecture within '%v'", supportedArchitectures)
}

func min(a int64, b int64) int64 {
func min[T int64 | int32](a T, b T) T {
if a < b {
return a
}
return b
}

func contains(s []string, e string) bool {
func contains[T string | types.ArchitectureType](s []T, e T) bool {
for _, a := range s {
if a == e {
return true
Expand Down
Loading

0 comments on commit 5a4f1b4

Please sign in to comment.