11package main
22
33import (
4+ "context"
45 "fmt"
56 "net/http"
67 "reflect"
78 "time"
89
9- "github.com/aws/aws-sdk-go/aws"
10- "github.com/aws/aws-sdk-go/aws/ec2metadata"
11- "github.com/aws/aws-sdk-go/aws/session"
12- "github.com/aws/aws-sdk-go/service/autoscaling"
13- "github.com/aws/aws-sdk-go/service/autoscaling/autoscalingiface"
14- "github.com/aws/aws-sdk-go/service/ec2"
15- "github.com/aws/aws-sdk-go/service/ec2/ec2iface"
10+ "github.com/aws/aws-sdk-go-v2/aws"
11+ "github.com/aws/aws-sdk-go-v2/config"
12+ "github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
13+ "github.com/aws/aws-sdk-go-v2/service/autoscaling"
14+ "github.com/aws/aws-sdk-go-v2/service/ec2"
15+ "github.com/aws/aws-sdk-go-v2/service/ec2/types"
1616 yaml "gopkg.in/yaml.v2"
1717)
1818
1919// AWSClient allows you to get the list of IP addresses of instances of an Auto Scaling group. It implements the CloudProvider interface
2020type AWSClient struct {
21- svcEC2 ec2iface. EC2API
22- svcAutoscaling autoscalingiface. AutoScalingAPI
21+ svcEC2 * ec2. Client
22+ svcAutoscaling * autoscaling. Client
2323 config * awsConfig
2424}
2525
@@ -33,23 +33,21 @@ func NewAWSClient(data []byte) (*AWSClient, error) {
3333
3434 if cfg .Region == "self" {
3535 httpClient := & http.Client {Timeout : connTimeoutInSecs * time .Second }
36- params := & aws.Config {HTTPClient : httpClient }
3736
38- metaSession , err := session . NewSession ( params )
37+ conf , err := config . LoadDefaultConfig ( context . TODO () )
3938 if err != nil {
4039 return nil , err
4140 }
4241
43- metaClient := ec2metadata .New (metaSession )
44- if ! metaClient .Available () {
45- return nil , fmt .Errorf ("ec2metadata service is unavailable" )
46- }
42+ client := imds .NewFromConfig (conf , func (o * imds.Options ) {
43+ o .HTTPClient = httpClient
44+ })
4745
48- region , err := metaClient . Region ( )
46+ response , err := client . GetRegion ( context . TODO (), & imds. GetRegionInput {} )
4947 if err != nil {
5048 return nil , fmt .Errorf ("unable to retrieve region from ec2metadata: %w" , err )
5149 }
52- cfg .Region = region
50+ cfg .Region = response . Region
5351 }
5452
5553 awsClient .config = cfg
@@ -85,15 +83,21 @@ func (client *AWSClient) GetUpstreams() []Upstream {
8583// configure configures the AWSClient with necessary parameters
8684func (client * AWSClient ) configure () error {
8785 httpClient := & http.Client {Timeout : connTimeoutInSecs * time .Second }
88- cfg := & aws.Config {Region : aws .String (client .config .Region ), HTTPClient : httpClient }
8986
90- session , err := session . NewSession ( cfg )
87+ cfg , err := config . LoadDefaultConfig ( context . TODO () )
9188 if err != nil {
9289 return err
9390 }
9491
95- client .svcEC2 = ec2 .New (session )
96- client .svcAutoscaling = autoscaling .New (session )
92+ client .svcEC2 = ec2 .NewFromConfig (cfg , func (o * ec2.Options ) {
93+ o .Region = client .config .Region
94+ o .HTTPClient = httpClient
95+ })
96+
97+ client .svcAutoscaling = autoscaling .NewFromConfig (cfg , func (o * autoscaling.Options ) {
98+ o .Region = client .config .Region
99+ o .HTTPClient = httpClient
100+ })
97101
98102 return nil
99103}
@@ -117,17 +121,17 @@ func parseAWSConfig(data []byte) (*awsConfig, error) {
117121// CheckIfScalingGroupExists checks if the Auto Scaling group exists
118122func (client * AWSClient ) CheckIfScalingGroupExists (name string ) (bool , error ) {
119123 params := & ec2.DescribeInstancesInput {
120- Filters : []* ec2 .Filter {
124+ Filters : []types .Filter {
121125 {
122126 Name : aws .String ("tag:aws:autoscaling:groupName" ),
123- Values : []* string {
124- aws . String ( name ) ,
127+ Values : []string {
128+ name ,
125129 },
126130 },
127131 },
128132 }
129133
130- response , err := client .svcEC2 .DescribeInstances (params )
134+ response , err := client .svcEC2 .DescribeInstances (context . Background (), params )
131135 if err != nil {
132136 return false , fmt .Errorf ("couldn't check if an AutoScaling group exists: %w" , err )
133137 }
@@ -145,17 +149,17 @@ func (client *AWSClient) GetPrivateIPsForScalingGroup(name string) ([]string, er
145149 }
146150 }
147151 params := & ec2.DescribeInstancesInput {
148- Filters : []* ec2 .Filter {
152+ Filters : []types .Filter {
149153 {
150154 Name : aws .String ("tag:aws:autoscaling:groupName" ),
151- Values : []* string {
152- aws . String ( name ) ,
155+ Values : []string {
156+ name ,
153157 },
154158 },
155159 },
156160 }
157161
158- response , err := client .svcEC2 .DescribeInstances (params )
162+ response , err := client .svcEC2 .DescribeInstances (context . Background (), params )
159163 if err != nil {
160164 return nil , err
161165 }
@@ -193,18 +197,18 @@ func (client *AWSClient) getInstancesInService(insIDtoIP map[string]string) ([]s
193197 const maxItems = 50
194198 var result []string
195199 keys := reflect .ValueOf (insIDtoIP ).MapKeys ()
196- instanceIds := make ([]* string , len (keys ))
200+ instanceIds := make ([]string , len (keys ))
197201
198202 for i := 0 ; i < len (keys ); i ++ {
199- instanceIds [i ] = aws . String ( keys [i ].String () )
203+ instanceIds [i ] = keys [i ].String ()
200204 }
201205
202206 batches := prepareBatches (maxItems , instanceIds )
203207 for _ , batch := range batches {
204208 params := & autoscaling.DescribeAutoScalingInstancesInput {
205209 InstanceIds : batch ,
206210 }
207- response , err := client .svcAutoscaling .DescribeAutoScalingInstances (params )
211+ response , err := client .svcAutoscaling .DescribeAutoScalingInstances (context . Background (), params )
208212 if err != nil {
209213 return nil , err
210214 }
@@ -219,8 +223,8 @@ func (client *AWSClient) getInstancesInService(insIDtoIP map[string]string) ([]s
219223 return result , nil
220224}
221225
222- func prepareBatches (maxItems int , items []* string ) [][]* string {
223- var batches [][]* string
226+ func prepareBatches (maxItems int , items []string ) [][]string {
227+ var batches [][]string
224228
225229 min := func (a , b int ) int {
226230 if a <= b {
0 commit comments