1
1
package main
2
2
3
3
import (
4
+ "context"
4
5
"fmt"
5
6
"net/http"
6
7
"reflect"
7
8
"time"
8
9
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"
16
16
yaml "gopkg.in/yaml.v2"
17
17
)
18
18
19
19
// AWSClient allows you to get the list of IP addresses of instances of an Auto Scaling group. It implements the CloudProvider interface
20
20
type AWSClient struct {
21
- svcEC2 ec2iface. EC2API
22
- svcAutoscaling autoscalingiface. AutoScalingAPI
21
+ svcEC2 * ec2. Client
22
+ svcAutoscaling * autoscaling. Client
23
23
config * awsConfig
24
24
}
25
25
@@ -33,23 +33,21 @@ func NewAWSClient(data []byte) (*AWSClient, error) {
33
33
34
34
if cfg .Region == "self" {
35
35
httpClient := & http.Client {Timeout : connTimeoutInSecs * time .Second }
36
- params := & aws.Config {HTTPClient : httpClient }
37
36
38
- metaSession , err := session . NewSession ( params )
37
+ conf , err := config . LoadDefaultConfig ( context . TODO () )
39
38
if err != nil {
40
39
return nil , err
41
40
}
42
41
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
+ })
47
45
48
- region , err := metaClient . Region ( )
46
+ response , err := client . GetRegion ( context . TODO (), & imds. GetRegionInput {} )
49
47
if err != nil {
50
48
return nil , fmt .Errorf ("unable to retrieve region from ec2metadata: %w" , err )
51
49
}
52
- cfg .Region = region
50
+ cfg .Region = response . Region
53
51
}
54
52
55
53
awsClient .config = cfg
@@ -85,15 +83,21 @@ func (client *AWSClient) GetUpstreams() []Upstream {
85
83
// configure configures the AWSClient with necessary parameters
86
84
func (client * AWSClient ) configure () error {
87
85
httpClient := & http.Client {Timeout : connTimeoutInSecs * time .Second }
88
- cfg := & aws.Config {Region : aws .String (client .config .Region ), HTTPClient : httpClient }
89
86
90
- session , err := session . NewSession ( cfg )
87
+ cfg , err := config . LoadDefaultConfig ( context . TODO () )
91
88
if err != nil {
92
89
return err
93
90
}
94
91
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
+ })
97
101
98
102
return nil
99
103
}
@@ -117,17 +121,17 @@ func parseAWSConfig(data []byte) (*awsConfig, error) {
117
121
// CheckIfScalingGroupExists checks if the Auto Scaling group exists
118
122
func (client * AWSClient ) CheckIfScalingGroupExists (name string ) (bool , error ) {
119
123
params := & ec2.DescribeInstancesInput {
120
- Filters : []* ec2 .Filter {
124
+ Filters : []types .Filter {
121
125
{
122
126
Name : aws .String ("tag:aws:autoscaling:groupName" ),
123
- Values : []* string {
124
- aws . String ( name ) ,
127
+ Values : []string {
128
+ name ,
125
129
},
126
130
},
127
131
},
128
132
}
129
133
130
- response , err := client .svcEC2 .DescribeInstances (params )
134
+ response , err := client .svcEC2 .DescribeInstances (context . Background (), params )
131
135
if err != nil {
132
136
return false , fmt .Errorf ("couldn't check if an AutoScaling group exists: %w" , err )
133
137
}
@@ -145,17 +149,17 @@ func (client *AWSClient) GetPrivateIPsForScalingGroup(name string) ([]string, er
145
149
}
146
150
}
147
151
params := & ec2.DescribeInstancesInput {
148
- Filters : []* ec2 .Filter {
152
+ Filters : []types .Filter {
149
153
{
150
154
Name : aws .String ("tag:aws:autoscaling:groupName" ),
151
- Values : []* string {
152
- aws . String ( name ) ,
155
+ Values : []string {
156
+ name ,
153
157
},
154
158
},
155
159
},
156
160
}
157
161
158
- response , err := client .svcEC2 .DescribeInstances (params )
162
+ response , err := client .svcEC2 .DescribeInstances (context . Background (), params )
159
163
if err != nil {
160
164
return nil , err
161
165
}
@@ -193,18 +197,18 @@ func (client *AWSClient) getInstancesInService(insIDtoIP map[string]string) ([]s
193
197
const maxItems = 50
194
198
var result []string
195
199
keys := reflect .ValueOf (insIDtoIP ).MapKeys ()
196
- instanceIds := make ([]* string , len (keys ))
200
+ instanceIds := make ([]string , len (keys ))
197
201
198
202
for i := 0 ; i < len (keys ); i ++ {
199
- instanceIds [i ] = aws . String ( keys [i ].String () )
203
+ instanceIds [i ] = keys [i ].String ()
200
204
}
201
205
202
206
batches := prepareBatches (maxItems , instanceIds )
203
207
for _ , batch := range batches {
204
208
params := & autoscaling.DescribeAutoScalingInstancesInput {
205
209
InstanceIds : batch ,
206
210
}
207
- response , err := client .svcAutoscaling .DescribeAutoScalingInstances (params )
211
+ response , err := client .svcAutoscaling .DescribeAutoScalingInstances (context . Background (), params )
208
212
if err != nil {
209
213
return nil , err
210
214
}
@@ -219,8 +223,8 @@ func (client *AWSClient) getInstancesInService(insIDtoIP map[string]string) ([]s
219
223
return result , nil
220
224
}
221
225
222
- func prepareBatches (maxItems int , items []* string ) [][]* string {
223
- var batches [][]* string
226
+ func prepareBatches (maxItems int , items []string ) [][]string {
227
+ var batches [][]string
224
228
225
229
min := func (a , b int ) int {
226
230
if a <= b {
0 commit comments