Skip to content

Commit

Permalink
FEAT. Migrate to aws sdk v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Fred78290 committed Aug 12, 2024
1 parent d7688bb commit 24c624e
Show file tree
Hide file tree
Showing 6 changed files with 326 additions and 319 deletions.
16 changes: 15 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ toolchain go1.22.3
require (
github.com/alecthomas/kingpin v2.2.6+incompatible
github.com/apache/cloudstack-go/v2 v2.16.1
github.com/aws/aws-sdk-go v1.55.3
github.com/aws/aws-sdk-go-v2 v1.30.3
github.com/aws/aws-sdk-go-v2/config v1.27.27
github.com/aws/aws-sdk-go-v2/credentials v1.17.27
github.com/aws/aws-sdk-go-v2/service/ec2 v1.174.0
github.com/aws/aws-sdk-go-v2/service/route53 v1.42.3
github.com/aws/smithy-go v1.20.3
github.com/drone/envsubst v1.0.3
github.com/gophercloud/gophercloud/v2 v2.1.0
github.com/linki/instrumented_http v0.3.0
Expand All @@ -31,6 +36,15 @@ require (
require (
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.11 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.15 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.15 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.3 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.17 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.22.4 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.4 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.30.3 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
32 changes: 30 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,36 @@ github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 h1:s6gZFSlWYmbqAu
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
github.com/apache/cloudstack-go/v2 v2.16.1 h1:2wOE4RKEjWPRZNO7ZNnZYmR3JJ+JJPQwhoc7W1fkiK4=
github.com/apache/cloudstack-go/v2 v2.16.1/go.mod h1:cZsgFe+VmrgLBm7QjeHTJBXYe8E5+yGYkdfwGb+Pu9c=
github.com/aws/aws-sdk-go v1.55.3 h1:0B5hOX+mIx7I5XPOrjrHlKSDQV/+ypFZpIHOx5LOk3E=
github.com/aws/aws-sdk-go v1.55.3/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU=
github.com/aws/aws-sdk-go-v2 v1.30.3 h1:jUeBtG0Ih+ZIFH0F4UkmL9w3cSpaMv9tYYDbzILP8dY=
github.com/aws/aws-sdk-go-v2 v1.30.3/go.mod h1:nIQjQVp5sfpQcTc9mPSr1B0PaWK5ByX9MOoDadSN4lc=
github.com/aws/aws-sdk-go-v2/config v1.27.27 h1:HdqgGt1OAP0HkEDDShEl0oSYa9ZZBSOmKpdpsDMdO90=
github.com/aws/aws-sdk-go-v2/config v1.27.27/go.mod h1:MVYamCg76dFNINkZFu4n4RjDixhVr51HLj4ErWzrVwg=
github.com/aws/aws-sdk-go-v2/credentials v1.17.27 h1:2raNba6gr2IfA0eqqiP2XiQ0UVOpGPgDSi0I9iAP+UI=
github.com/aws/aws-sdk-go-v2/credentials v1.17.27/go.mod h1:gniiwbGahQByxan6YjQUMcW4Aov6bLC3m+evgcoN4r4=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.11 h1:KreluoV8FZDEtI6Co2xuNk/UqI9iwMrOx/87PBNIKqw=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.11/go.mod h1:SeSUYBLsMYFoRvHE0Tjvn7kbxaUhl75CJi1sbfhMxkU=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.15 h1:SoNJ4RlFEQEbtDcCEt+QG56MY4fm4W8rYirAmq+/DdU=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.15/go.mod h1:U9ke74k1n2bf+RIgoX1SXFed1HLs51OgUSs+Ph0KJP8=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.15 h1:C6WHdGnTDIYETAm5iErQUiVNsclNx9qbJVPIt03B6bI=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.15/go.mod h1:ZQLZqhcu+JhSrA9/NXRm8SkDvsycE+JkV3WGY41e+IM=
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 h1:hT8rVHwugYE2lEfdFE0QWVo81lF7jMrYJVDWI+f+VxU=
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0/go.mod h1:8tu/lYfQfFe6IGnaOdrpVgEL2IrrDOf6/m9RQum4NkY=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.174.0 h1:xY25etxTt0mYfWVWv8DjIUcJQyX2cwBeInpIvg6Sbsw=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.174.0/go.mod h1:o6QDjdVKpP5EF0dp/VlvqckzuSDATr1rLdHt3A5m0YY=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.3 h1:dT3MqvGhSoaIhRseqw2I0yH81l7wiR2vjs57O51EAm8=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.3/go.mod h1:GlAeCkHwugxdHaueRr4nhPuY+WW+gR8UjlcqzPr1SPI=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.17 h1:HGErhhrxZlQ044RiM+WdoZxp0p+EGM62y3L6pwA4olE=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.17/go.mod h1:RkZEx4l0EHYDJpWppMJ3nD9wZJAa8/0lq9aVC+r2UII=
github.com/aws/aws-sdk-go-v2/service/route53 v1.42.3 h1:MmLCRqP4U4Cw9gJ4bNrCG0mWqEtBlmAVleyelcHARMU=
github.com/aws/aws-sdk-go-v2/service/route53 v1.42.3/go.mod h1:AMPjK2YnRh0YgOID3PqhJA1BRNfXDfGOnSsKHtAe8yA=
github.com/aws/aws-sdk-go-v2/service/sso v1.22.4 h1:BXx0ZIxvrJdSgSvKTZ+yRBeSqqgPM89VPlulEcl37tM=
github.com/aws/aws-sdk-go-v2/service/sso v1.22.4/go.mod h1:ooyCOXjvJEsUw7x+ZDHeISPMhtwI3ZCB7ggFMcFfWLU=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.4 h1:yiwVzJW2ZxZTurVbYWA7QOrAaCYQR72t0wrSBfoesUE=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.4/go.mod h1:0oxfLkpz3rQ/CHlx5hB7H69YUpFiI1tql6Q6Ne+1bCw=
github.com/aws/aws-sdk-go-v2/service/sts v1.30.3 h1:ZsDKRLXGWHk8WdtyYMoGNO7bTudrvuKpDKgMVRlepGE=
github.com/aws/aws-sdk-go-v2/service/sts v1.30.3/go.mod h1:zwySh8fpFyXp9yOr/KVzxOl8SRqgf/IDw5aUt9UKFcQ=
github.com/aws/smithy-go v1.20.3 h1:ryHwveWzPV5BIof6fyDvor6V3iUL7nTfiTKXHiW05nE=
github.com/aws/smithy-go v1.20.3/go.mod h1:krry+ya/rV9RDcV/Q16kpu6ypI4K2czasz0NC3qS14E=
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/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
Expand Down
125 changes: 66 additions & 59 deletions providers/aws/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import (
"github.com/Fred78290/kubernetes-cloud-autoscaler/providers"
"github.com/Fred78290/kubernetes-cloud-autoscaler/utils"

"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-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/aws/smithy-go/logging"
glog "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
Expand All @@ -35,11 +36,11 @@ const (
)

type MetadataOptionsConfiguration struct {
HttpEndpoint *string `default:"enabled" json:"http-endpoint" type:"string" enum:"InstanceMetadataEndpointState"`
HttpProtocolIpv6 *string `default:"disabled" json:"http-protocol-ipv6" type:"string" enum:"InstanceMetadataProtocolState"`
HttpPutResponseHopLimit *int64 `default:"2" json:"http-put-responsehop-limit" type:"integer"`
HttpTokens *string `default:"optional" json:"http-tokens" type:"string" enum:"HttpTokensState"`
InstanceMetadataTags *string `default:"enabled" json:"instance-metadata-tags" type:"string" enum:"InstanceMetadataTagsState"`
HttpEndpoint types.InstanceMetadataEndpointState `default:"enabled" json:"http-endpoint" type:"string" enum:"InstanceMetadataEndpointState"`
HttpProtocolIpv6 types.InstanceMetadataProtocolState `default:"disabled" json:"http-protocol-ipv6" type:"string" enum:"InstanceMetadataProtocolState"`
HttpPutResponseHopLimit int32 `default:"2" json:"http-put-responsehop-limit" type:"integer"`
HttpTokens types.HttpTokensState `default:"required" json:"http-tokens" type:"string" enum:"HttpTokensState"`
InstanceMetadataTags types.InstanceMetadataTagsState `default:"enabled" json:"instance-metadata-tags" type:"string" enum:"InstanceMetadataTagsState"`
}

// Configuration declares aws connection info
Expand All @@ -54,12 +55,12 @@ type Configuration struct {
ImageID string `json:"ami"`
IamRole string `json:"iam-role-arn"`
KeyName string `json:"keyName"`
VolumeType string `default:"gp3" json:"volume-type"`
VolumeType types.VolumeType `default:"gp3" json:"volume-type"`
Tags []Tag `json:"tags,omitempty"`
Network Network `json:"network"`
AvailableGPUTypes map[string]string `json:"gpu-types"`
MetadataOptions MetadataOptionsConfiguration `json:"metadata-options"`
ec2Client *ec2.EC2
ec2Client *ec2.Client
}

// Tag aws tag
Expand Down Expand Up @@ -124,7 +125,7 @@ type awsWrapper struct {
type awsHandler struct {
*awsWrapper
instanceName string
instanceType string
instanceType types.InstanceType
controlPlane bool
nodeIndex int
runningInstance *Ec2Instance
Expand Down Expand Up @@ -179,29 +180,29 @@ func (handler *awsHandler) UpdateMacAddressTable() error {
}

func (handler *awsHandler) GenerateProviderID() string {
if handler.runningInstance.Zone == nil || handler.runningInstance.InstanceID == nil {
if handler.runningInstance.Zone == "" || handler.runningInstance.InstanceID == "" {
return ""
}

if isNullOrEmpty(*handler.runningInstance.Zone) || isNullOrEmpty(*handler.runningInstance.InstanceID) {
if isNullOrEmpty(handler.runningInstance.Zone) || isNullOrEmpty(handler.runningInstance.InstanceID) {
return ""
}

return fmt.Sprintf("aws://%s/%s", *handler.runningInstance.Zone, *handler.runningInstance.InstanceID)
return fmt.Sprintf("aws://%s/%s", handler.runningInstance.Zone, handler.runningInstance.InstanceID)
}

func (handler *awsHandler) GetTopologyLabels() map[string]string {
return map[string]string{
constantes.NodeLabelTopologyRegion: *handler.runningInstance.Region,
constantes.NodeLabelTopologyZone: *handler.runningInstance.Zone,
constantes.NodeLabelVMWareCSIRegion: *handler.runningInstance.Region,
constantes.NodeLabelVMWareCSIZone: *handler.runningInstance.Zone,
constantes.NodeLabelTopologyRegion: handler.runningInstance.Region,
constantes.NodeLabelTopologyZone: handler.runningInstance.Zone,
constantes.NodeLabelVMWareCSIRegion: handler.runningInstance.Region,
constantes.NodeLabelVMWareCSIZone: handler.runningInstance.Zone,
}
}

func (handler *awsHandler) encodeCloudInit(object any) (*string, error) {
func (handler *awsHandler) encodeCloudInit(object any) (string, error) {
if object == nil {
return nil, nil
return "", nil
}

var out bytes.Buffer
Expand All @@ -214,33 +215,34 @@ func (handler *awsHandler) encodeCloudInit(object any) (*string, error) {
wr.Close()

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

result := base64.StdEncoding.EncodeToString(out.Bytes())

return aws.String(result), nil
return result, nil
}

// InstanceCreate will create a named VM not powered
// memory and disk are in megabytes
func (handler *awsHandler) InstanceCreate(input *providers.InstanceCreateInput) (string, error) {
var err error
var userData *string
func (handler *awsHandler) InstanceCreate(input *providers.InstanceCreateInput) (instanceID string, err error) {
var userData string

if userData, err = handler.encodeCloudInit(input.CloudInit); err != nil {
return "", err
return
}

if handler.runningInstance, err = handler.newEc2Instance(handler.instanceName, handler.nodeIndex); err != nil {
return "", err
return
}

if err = handler.runningInstance.Create(input.NodeGroup, handler.instanceType, userData, handler.VolumeType, input.Machine.GetDiskSize(), handler.desiredENI); err != nil {
return "", err
return
}

return *handler.runningInstance.InstanceID, nil
instanceID = handler.runningInstance.InstanceID

return
}

func (handler *awsHandler) InstanceWaitReady(callback providers.CallbackWaitSSHReady) (string, error) {
Expand All @@ -264,7 +266,7 @@ func (handler *awsHandler) InstanceID() (string, error) {
return "", fmt.Errorf(constantes.ErrInstanceIsNotAttachedToCloudProvider)
}

return *handler.runningInstance.InstanceID, nil
return handler.runningInstance.InstanceID, nil
}

func (handler *awsHandler) InstanceAutoStart() error {
Expand Down Expand Up @@ -335,13 +337,13 @@ func (handler *awsHandler) InstanceMaxPods(desiredMaxPods int) (int, error) {
if client, err := handler.createClient(); err != nil {
return 0, err
} else {
input := ec2.DescribeInstanceTypesInput{
InstanceTypes: []*string{
aws.String(handler.instanceType),
input := &ec2.DescribeInstanceTypesInput{
InstanceTypes: []types.InstanceType{
handler.instanceType,
},
}

if result, err := client.DescribeInstanceTypes(&input); err != nil {
if result, err := client.DescribeInstanceTypes(context.TODO(), input); err != nil {
return 0, err
} else {
networkInfos := result.InstanceTypes[0].NetworkInfo
Expand Down Expand Up @@ -388,13 +390,13 @@ func (handler *awsHandler) UnregisterDNS(address string) error {

func (handler *awsHandler) UUID(instanceName string) (string, error) {
if handler.runningInstance != nil && handler.runningInstance.InstanceName == instanceName {
return *handler.runningInstance.InstanceID, nil
return handler.runningInstance.InstanceID, nil
}

if ec2, err := handler.GetEc2Instance(instanceName); err != nil {
return "", err
} else {
return *ec2.InstanceID, nil
return ec2.InstanceID, nil
}
}

Expand All @@ -420,15 +422,15 @@ func (wrapper *awsWrapper) AttachInstance(instanceName string, controlPlane bool
}
}

func (wrapper *awsWrapper) CreateInstance(instanceName, instanceType string, controlPlane bool, nodeIndex int) (providers.ProviderHandler, error) {
func (wrapper *awsWrapper) CreateInstance(instanceName string, instanceType string, controlPlane bool, nodeIndex int) (providers.ProviderHandler, error) {
if wrapper.InstanceExists(instanceName) {
glog.Warnf(constantes.ErrVMAlreadyExists, instanceName)
return nil, fmt.Errorf(constantes.ErrVMAlreadyExists, instanceName)
}

return &awsHandler{
awsWrapper: wrapper,
instanceType: instanceType,
instanceType: types.InstanceType(instanceType),
instanceName: instanceName,
controlPlane: controlPlane,
nodeIndex: nodeIndex,
Expand All @@ -443,29 +445,34 @@ func (wrapper *awsWrapper) GetAvailableGpuTypes() map[string]string {
return wrapper.AvailableGPUTypes
}

func (wrapper *awsWrapper) createClient() (*ec2.EC2, error) {
func (wrapper *awsWrapper) createClient() (*ec2.Client, error) {
if wrapper.ec2Client == nil {
var err error
var sess *session.Session
var cfg aws.Config

if sess, err = newSessionWithOptions(wrapper.AccessKey, wrapper.SecretKey, wrapper.Token, wrapper.Filename, wrapper.Profile, wrapper.Region); err != nil {
if cfg, err = newSessionWithOptions(wrapper.AccessKey, wrapper.SecretKey, wrapper.Token, wrapper.Filename, wrapper.Profile, wrapper.Region); err != nil {
return nil, err
}

// Create EC2 service client
if glog.GetLevel() >= glog.DebugLevel {
wrapper.ec2Client = ec2.New(sess, aws.NewConfig().WithLogger(wrapper).WithLogLevel(aws.LogDebugWithHTTPBody).WithLogLevel(aws.LogDebugWithSigning))
} else {
wrapper.ec2Client = ec2.New(sess)
}
wrapper.ec2Client = ec2.NewFromConfig(cfg, func(o *ec2.Options) {
if glog.GetLevel() >= glog.TraceLevel {
o.Logger = wrapper
o.ClientLogMode = aws.LogSigning | aws.LogRequest | aws.LogResponse
}
})
}

return wrapper.ec2Client, nil
}

// Log logging
func (wrapper *awsWrapper) Log(args ...any) {
glog.Infoln(args...)
func (wrapper *awsWrapper) Logf(classification logging.Classification, format string, v ...interface{}) {
if classification == logging.Warn {
glog.Warnf(format, v...)
} else {
glog.Infof(format, v...)
}
}

func (wrapper *awsWrapper) GetFileName() string {
Expand All @@ -485,12 +492,12 @@ func (wrapper *awsWrapper) AmiExists(ami string) bool {
return false
} else {
input := ec2.DescribeImagesInput{
ImageIds: []*string{
aws.String(ami),
ImageIds: []string{
ami,
},
}

if _, err = client.DescribeImages(&input); err != nil {
if _, err = client.DescribeImages(context.Background(), &input); err != nil {
return false
}
}
Expand All @@ -506,11 +513,11 @@ func (wrapper *awsWrapper) GetEc2Instance(instanceName string) (*Ec2Instance, er
var result *ec2.DescribeInstancesOutput

input := &ec2.DescribeInstancesInput{
Filters: []*ec2.Filter{
Filters: []types.Filter{
{
Name: aws.String("tag:Name"),
Values: []*string{
aws.String(instanceName),
Values: []string{
instanceName,
},
},
},
Expand All @@ -519,7 +526,7 @@ func (wrapper *awsWrapper) GetEc2Instance(instanceName string) (*Ec2Instance, er
ctx := context.NewContext(wrapper.Timeout)
defer ctx.Cancel()

if result, err = client.DescribeInstancesWithContext(ctx, input); err != nil {
if result, err = client.DescribeInstances(ctx, input); err != nil {
return nil, err
}

Expand All @@ -544,9 +551,9 @@ func (wrapper *awsWrapper) GetEc2Instance(instanceName string) (*Ec2Instance, er
client: client,
InstanceName: instanceName,
PrivateDNSName: *instance.PrivateDnsName,
InstanceID: instance.InstanceId,
Region: &wrapper.Region,
Zone: instance.Placement.AvailabilityZone,
InstanceID: *instance.InstanceId,
Region: wrapper.Region,
Zone: *instance.Placement.AvailabilityZone,
AddressIP: address,
}, nil
}
Expand All @@ -561,7 +568,7 @@ func (wrapper *awsWrapper) UUID(instanceName string) (string, error) {
if ec2, err := wrapper.GetEc2Instance(instanceName); err != nil {
return "", err
} else {
return *ec2.InstanceID, nil
return ec2.InstanceID, nil
}
}

Expand Down
Loading

0 comments on commit 24c624e

Please sign in to comment.