Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use AWS SDK v2 #478

Merged
merged 2 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions LICENSES
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,28 @@ github.com/Azure/go-autorest/autorest/to,https://github.com/Azure/go-autorest/bl
github.com/Azure/go-autorest/autorest/validation,https://github.com/Azure/go-autorest/blob/autorest/validation/v0.3.1/autorest/validation/LICENSE,Apache-2.0
github.com/Azure/go-autorest/logger,https://github.com/Azure/go-autorest/blob/logger/v0.2.1/logger/LICENSE,Apache-2.0
github.com/Azure/go-autorest/tracing,https://github.com/Azure/go-autorest/blob/tracing/v0.6.0/tracing/LICENSE,Apache-2.0
github.com/aws/aws-sdk-go,https://github.com/aws/aws-sdk-go/blob/v1.44.4/LICENSE.txt,Apache-2.0
github.com/aws/aws-sdk-go/internal/sync/singleflight,https://github.com/aws/aws-sdk-go/blob/v1.44.4/internal/sync/singleflight/LICENSE,BSD-3-Clause
github.com/aws/aws-sdk-go-v2,https://github.com/aws/aws-sdk-go-v2/blob/v1.24.0/LICENSE.txt,Apache-2.0
github.com/aws/aws-sdk-go-v2/config,https://github.com/aws/aws-sdk-go-v2/blob/config/v1.26.2/config/LICENSE.txt,Apache-2.0
github.com/aws/aws-sdk-go-v2/credentials,https://github.com/aws/aws-sdk-go-v2/blob/credentials/v1.16.13/credentials/LICENSE.txt,Apache-2.0
github.com/aws/aws-sdk-go-v2/feature/ec2/imds,https://github.com/aws/aws-sdk-go-v2/blob/feature/ec2/imds/v1.14.10/feature/ec2/imds/LICENSE.txt,Apache-2.0
github.com/aws/aws-sdk-go-v2/internal/configsources,https://github.com/aws/aws-sdk-go-v2/blob/internal/configsources/v1.2.9/internal/configsources/LICENSE.txt,Apache-2.0
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2,https://github.com/aws/aws-sdk-go-v2/blob/internal/endpoints/v2.5.9/internal/endpoints/v2/LICENSE.txt,Apache-2.0
github.com/aws/aws-sdk-go-v2/internal/ini,https://github.com/aws/aws-sdk-go-v2/blob/internal/ini/v1.7.2/internal/ini/LICENSE.txt,Apache-2.0
github.com/aws/aws-sdk-go-v2/internal/sync/singleflight,https://github.com/aws/aws-sdk-go-v2/blob/v1.24.0/internal/sync/singleflight/LICENSE,BSD-3-Clause
github.com/aws/aws-sdk-go-v2/service/autoscaling,https://github.com/aws/aws-sdk-go-v2/blob/service/autoscaling/v1.36.6/service/autoscaling/LICENSE.txt,Apache-2.0
github.com/aws/aws-sdk-go-v2/service/ec2,https://github.com/aws/aws-sdk-go-v2/blob/service/ec2/v1.142.0/service/ec2/LICENSE.txt,Apache-2.0
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding,https://github.com/aws/aws-sdk-go-v2/blob/service/internal/accept-encoding/v1.10.4/service/internal/accept-encoding/LICENSE.txt,Apache-2.0
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url,https://github.com/aws/aws-sdk-go-v2/blob/service/internal/presigned-url/v1.10.9/service/internal/presigned-url/LICENSE.txt,Apache-2.0
github.com/aws/aws-sdk-go-v2/service/sso,https://github.com/aws/aws-sdk-go-v2/blob/service/sso/v1.18.5/service/sso/LICENSE.txt,Apache-2.0
github.com/aws/aws-sdk-go-v2/service/ssooidc,https://github.com/aws/aws-sdk-go-v2/blob/service/ssooidc/v1.21.5/service/ssooidc/LICENSE.txt,Apache-2.0
github.com/aws/aws-sdk-go-v2/service/sts,https://github.com/aws/aws-sdk-go-v2/blob/service/sts/v1.26.6/service/sts/LICENSE.txt,Apache-2.0
github.com/aws/smithy-go,https://github.com/aws/smithy-go/blob/v1.19.0/LICENSE,Apache-2.0
github.com/aws/smithy-go/internal/sync/singleflight,https://github.com/aws/smithy-go/blob/v1.19.0/internal/sync/singleflight/LICENSE,BSD-3-Clause
github.com/dimchansky/utfbom,https://github.com/dimchansky/utfbom/blob/v1.1.1/LICENSE,Apache-2.0
github.com/golang-jwt/jwt/v4,https://github.com/golang-jwt/jwt/blob/v4.2.0/LICENSE,MIT
github.com/jmespath/go-jmespath,https://github.com/jmespath/go-jmespath/blob/v0.4.0/LICENSE,Apache-2.0
github.com/mitchellh/go-homedir,https://github.com/mitchellh/go-homedir/blob/v1.1.0/LICENSE,MIT
github.com/nginxinc/nginx-asg-sync/cmd/sync,https://github.com/nginxinc/nginx-asg-sync/blob/HEAD/LICENSE,BSD-2-Clause
github.com/nginxinc/nginx-plus-go-client/client,https://github.com/nginxinc/nginx-plus-go-client/blob/v0.10.0/LICENSE,Apache-2.0
golang.org/x/crypto/pkcs12,https://cs.opensource.google/go/x/crypto/+/v0.1.0:LICENSE,BSD-3-Clause
github.com/nginxinc/nginx-plus-go-client/client,https://github.com/nginxinc/nginx-plus-go-client/blob/v1.2.0/LICENSE,Apache-2.0
golang.org/x/crypto/pkcs12,https://cs.opensource.google/go/x/crypto/+/v0.17.0:LICENSE,BSD-3-Clause
gopkg.in/yaml.v2,https://github.com/go-yaml/yaml/blob/v2.4.0/LICENSE,Apache-2.0
72 changes: 38 additions & 34 deletions cmd/sync/aws.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package main

import (
"context"
"fmt"
"net/http"
"reflect"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/autoscaling/autoscalingiface"
"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/feature/ec2/imds"
"github.com/aws/aws-sdk-go-v2/service/autoscaling"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
yaml "gopkg.in/yaml.v2"
)

// AWSClient allows you to get the list of IP addresses of instances of an Auto Scaling group. It implements the CloudProvider interface
type AWSClient struct {
svcEC2 ec2iface.EC2API
svcAutoscaling autoscalingiface.AutoScalingAPI
svcEC2 *ec2.Client
svcAutoscaling *autoscaling.Client
config *awsConfig
}

Expand All @@ -33,23 +33,21 @@ func NewAWSClient(data []byte) (*AWSClient, error) {

if cfg.Region == "self" {
httpClient := &http.Client{Timeout: connTimeoutInSecs * time.Second}
params := &aws.Config{HTTPClient: httpClient}

metaSession, err := session.NewSession(params)
conf, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
return nil, err
}

metaClient := ec2metadata.New(metaSession)
if !metaClient.Available() {
return nil, fmt.Errorf("ec2metadata service is unavailable")
}
client := imds.NewFromConfig(conf, func(o *imds.Options) {
o.HTTPClient = httpClient
})

region, err := metaClient.Region()
response, err := client.GetRegion(context.TODO(), &imds.GetRegionInput{})
if err != nil {
return nil, fmt.Errorf("unable to retrieve region from ec2metadata: %w", err)
}
cfg.Region = region
cfg.Region = response.Region
}

awsClient.config = cfg
Expand Down Expand Up @@ -85,15 +83,21 @@ func (client *AWSClient) GetUpstreams() []Upstream {
// configure configures the AWSClient with necessary parameters
func (client *AWSClient) configure() error {
httpClient := &http.Client{Timeout: connTimeoutInSecs * time.Second}
cfg := &aws.Config{Region: aws.String(client.config.Region), HTTPClient: httpClient}

session, err := session.NewSession(cfg)
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
return err
}

client.svcEC2 = ec2.New(session)
client.svcAutoscaling = autoscaling.New(session)
client.svcEC2 = ec2.NewFromConfig(cfg, func(o *ec2.Options) {
o.Region = client.config.Region
o.HTTPClient = httpClient
})

client.svcAutoscaling = autoscaling.NewFromConfig(cfg, func(o *autoscaling.Options) {
o.Region = client.config.Region
o.HTTPClient = httpClient
})

return nil
}
Expand All @@ -117,17 +121,17 @@ func parseAWSConfig(data []byte) (*awsConfig, error) {
// CheckIfScalingGroupExists checks if the Auto Scaling group exists
func (client *AWSClient) CheckIfScalingGroupExists(name string) (bool, error) {
params := &ec2.DescribeInstancesInput{
Filters: []*ec2.Filter{
Filters: []types.Filter{
{
Name: aws.String("tag:aws:autoscaling:groupName"),
Values: []*string{
aws.String(name),
Values: []string{
name,
},
},
},
}

response, err := client.svcEC2.DescribeInstances(params)
response, err := client.svcEC2.DescribeInstances(context.Background(), params)
if err != nil {
return false, fmt.Errorf("couldn't check if an AutoScaling group exists: %w", err)
}
Expand All @@ -145,17 +149,17 @@ func (client *AWSClient) GetPrivateIPsForScalingGroup(name string) ([]string, er
}
}
params := &ec2.DescribeInstancesInput{
Filters: []*ec2.Filter{
Filters: []types.Filter{
{
Name: aws.String("tag:aws:autoscaling:groupName"),
Values: []*string{
aws.String(name),
Values: []string{
name,
},
},
},
}

response, err := client.svcEC2.DescribeInstances(params)
response, err := client.svcEC2.DescribeInstances(context.Background(), params)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -193,18 +197,18 @@ func (client *AWSClient) getInstancesInService(insIDtoIP map[string]string) ([]s
const maxItems = 50
var result []string
keys := reflect.ValueOf(insIDtoIP).MapKeys()
instanceIds := make([]*string, len(keys))
instanceIds := make([]string, len(keys))

for i := 0; i < len(keys); i++ {
instanceIds[i] = aws.String(keys[i].String())
instanceIds[i] = keys[i].String()
}

batches := prepareBatches(maxItems, instanceIds)
for _, batch := range batches {
params := &autoscaling.DescribeAutoScalingInstancesInput{
InstanceIds: batch,
}
response, err := client.svcAutoscaling.DescribeAutoScalingInstances(params)
response, err := client.svcAutoscaling.DescribeAutoScalingInstances(context.Background(), params)
if err != nil {
return nil, err
}
Expand All @@ -219,8 +223,8 @@ func (client *AWSClient) getInstancesInService(insIDtoIP map[string]string) ([]s
return result, nil
}

func prepareBatches(maxItems int, items []*string) [][]*string {
var batches [][]*string
func prepareBatches(maxItems int, items []string) [][]string {
var batches [][]string

min := func(a, b int) int {
if a <= b {
Expand Down
4 changes: 2 additions & 2 deletions cmd/sync/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ func areEqualUpstreamsAWS(u1 awsUpstream, u2 Upstream) bool {
func TestPrepareBatches(t *testing.T) {
const maxItems = 3
ids := []string{"i-394ujfs", "i-dfdinf", "i-fsfsf", "i-8hr83hfwif", "i-nsnsnan"}
instanceIds := make([]*string, len(ids))
instanceIds := make([]string, len(ids))

for i := 0; i < len(ids); i++ {
instanceIds[i] = &ids[i]
instanceIds[i] = ids[i]
}

batches := prepareBatches(maxItems, instanceIds)
Expand Down
16 changes: 15 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ go 1.21.3
require (
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible
github.com/Azure/go-autorest/autorest/azure/auth v0.5.12
github.com/aws/aws-sdk-go v1.50.0
github.com/aws/aws-sdk-go-v2 v1.24.0
github.com/aws/aws-sdk-go-v2/config v1.26.2
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.14.10
github.com/aws/aws-sdk-go-v2/service/autoscaling v1.36.6
github.com/aws/aws-sdk-go-v2/service/ec2 v1.142.0
github.com/nginxinc/nginx-plus-go-client v1.2.0
gopkg.in/yaml.v2 v2.4.0
)
Expand All @@ -20,6 +24,16 @@ require (
github.com/Azure/go-autorest/autorest/validation v0.3.1 // indirect
github.com/Azure/go-autorest/logger v0.2.1 // indirect
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.16.13 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.9 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.5.9 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.7.2 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.10.4 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.10.9 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.18.5 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.21.5 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.26.6 // indirect
github.com/aws/smithy-go v1.19.0 // indirect
github.com/dimchansky/utfbom v1.1.1 // indirect
github.com/golang-jwt/jwt/v4 v4.2.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
Expand Down
34 changes: 32 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,45 @@ github.com/Azure/go-autorest/logger v0.2.1 h1:IG7i4p/mDa2Ce4TRyAO8IHnVhAVF3RFU+Z
github.com/Azure/go-autorest/logger v0.2.1/go.mod h1:T9E3cAhj2VqvPOtCYAvby9aBXkZmbF5NWuPV8+WeEW8=
github.com/Azure/go-autorest/tracing v0.6.0 h1:TYi4+3m5t6K48TGI9AUdb+IzbnSxvnvUMfuitfgcfuo=
github.com/Azure/go-autorest/tracing v0.6.0/go.mod h1:+vhtPC754Xsa23ID7GlGsrdKBpUA79WCAKPPZVC2DeU=
github.com/aws/aws-sdk-go v1.50.0 h1:HBtrLeO+QyDKnc3t1+5DR1RxodOHCGr8ZcrHudpv7jI=
github.com/aws/aws-sdk-go v1.50.0/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
github.com/aws/aws-sdk-go-v2 v1.24.0 h1:890+mqQ+hTpNuw0gGP6/4akolQkSToDJgHfQE7AwGuk=
github.com/aws/aws-sdk-go-v2 v1.24.0/go.mod h1:LNh45Br1YAkEKaAqvmE1m8FUx6a5b/V0oAKV7of29b4=
github.com/aws/aws-sdk-go-v2/config v1.26.2 h1:+RWLEIWQIGgrz2pBPAUoGgNGs1TOyF4Hml7hCnYj2jc=
github.com/aws/aws-sdk-go-v2/config v1.26.2/go.mod h1:l6xqvUxt0Oj7PI/SUXYLNyZ9T/yBPn3YTQcJLLOdtR8=
github.com/aws/aws-sdk-go-v2/credentials v1.16.13 h1:WLABQ4Cp4vXtXfOWOS3MEZKr6AAYUpMczLhgKtAjQ/8=
github.com/aws/aws-sdk-go-v2/credentials v1.16.13/go.mod h1:Qg6x82FXwW0sJHzYruxGiuApNo31UEtJvXVSZAXeWiw=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.14.10 h1:w98BT5w+ao1/r5sUuiH6JkVzjowOKeOJRHERyy1vh58=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.14.10/go.mod h1:K2WGI7vUvkIv1HoNbfBA1bvIZ+9kL3YVmWxeKuLQsiw=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.9 h1:v+HbZaCGmOwnTTVS86Fleq0vPzOd7tnJGbFhP0stNLs=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.9/go.mod h1:Xjqy+Nyj7VDLBtCMkQYOw1QYfAEZCVLrfI0ezve8wd4=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.5.9 h1:N94sVhRACtXyVcjXxrwK1SKFIJrA9pOJ5yu2eSHnmls=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.5.9/go.mod h1:hqamLz7g1/4EJP+GH5NBhcUMLjW+gKLQabgyz6/7WAU=
github.com/aws/aws-sdk-go-v2/internal/ini v1.7.2 h1:GrSw8s0Gs/5zZ0SX+gX4zQjRnRsMJDJ2sLur1gRBhEM=
github.com/aws/aws-sdk-go-v2/internal/ini v1.7.2/go.mod h1:6fQQgfuGmw8Al/3M2IgIllycxV7ZW7WCdVSqfBeUiCY=
github.com/aws/aws-sdk-go-v2/service/autoscaling v1.36.6 h1:xLETNIzlbzqb/ZFir6l1AQKjDJ96dQf/ekNysJHoxqo=
github.com/aws/aws-sdk-go-v2/service/autoscaling v1.36.6/go.mod h1:ldeYLrGhWz2aMgCEL7He3+YbJAG5xn1K/fFFKRkyzd0=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.142.0 h1:VrFC1uEZjX4ghkm/et8ATVGb1mT75Iv8aPKPjUE+F8A=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.142.0/go.mod h1:qjhtI9zjpUHRc6khtrIM9fb48+ii6+UikL3/b+MKYn0=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.10.4 h1:/b31bi3YVNlkzkBrm9LfpaKoaYZUxIAj4sHfOTmLfqw=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.10.4/go.mod h1:2aGXHFmbInwgP9ZfpmdIfOELL79zhdNYNmReK8qDfdQ=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.10.9 h1:Nf2sHxjMJR8CSImIVCONRi4g0Su3J+TSTbS7G0pUeMU=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.10.9/go.mod h1:idky4TER38YIjr2cADF1/ugFMKvZV7p//pVeV5LZbF0=
github.com/aws/aws-sdk-go-v2/service/sso v1.18.5 h1:ldSFWz9tEHAwHNmjx2Cvy1MjP5/L9kNoR0skc6wyOOM=
github.com/aws/aws-sdk-go-v2/service/sso v1.18.5/go.mod h1:CaFfXLYL376jgbP7VKC96uFcU8Rlavak0UlAwk1Dlhc=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.21.5 h1:2k9KmFawS63euAkY4/ixVNsYYwrwnd5fIvgEKkfZFNM=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.21.5/go.mod h1:W+nd4wWDVkSUIox9bacmkBP5NMFQeTJ/xqNabpzSR38=
github.com/aws/aws-sdk-go-v2/service/sts v1.26.6 h1:HJeiuZ2fldpd0WqngyMR6KW7ofkXNLyOaHwEIGm39Cs=
github.com/aws/aws-sdk-go-v2/service/sts v1.26.6/go.mod h1:XX5gh4CB7wAs4KhcF46G6C8a2i7eupU19dcAAE+EydU=
github.com/aws/smithy-go v1.19.0 h1:KWFKQV80DpP3vJrrA9sVAHQ5gc2z8i4EzrLhLlWXcBM=
github.com/aws/smithy-go v1.19.0/go.mod h1:NukqUGpCZIILqqiV0NIjeFh24kd/FAa4beRb6nbIUPE=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dimchansky/utfbom v1.1.1 h1:vV6w1AhK4VMnhBno/TPVCoK9U/LP0PkLCS9tbxHdi/U=
github.com/dimchansky/utfbom v1.1.1/go.mod h1:SxdoEBH5qIqFocHMyGOXVAybYJdr71b1Q/j0mACtrfE=
github.com/golang-jwt/jwt/v4 v4.0.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
github.com/golang-jwt/jwt/v4 v4.2.0 h1:besgBTC8w8HjP6NzQdxwKH9Z5oQMZ24ThTrHp3cZ8eU=
github.com/golang-jwt/jwt/v4 v4.2.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
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=
Expand Down
Loading