@@ -17,14 +17,14 @@ import (
17
17
yaml "gopkg.in/yaml.v2"
18
18
)
19
19
20
- // AWSClient allows you to get the list of IP addresses of instances of an Auto Scaling group. It implements the CloudProvider interface
20
+ // AWSClient allows you to get the list of IP addresses of instances of an Auto Scaling group. It implements the CloudProvider interface.
21
21
type AWSClient struct {
22
22
svcEC2 * ec2.Client
23
23
svcAutoscaling * autoscaling.Client
24
24
config * awsConfig
25
25
}
26
26
27
- // NewAWSClient creates and configures an AWSClient
27
+ // NewAWSClient creates and configures an AWSClient.
28
28
func NewAWSClient (data []byte ) (* AWSClient , error ) {
29
29
awsClient := & AWSClient {}
30
30
cfg , err := parseAWSConfig (data )
@@ -37,7 +37,7 @@ func NewAWSClient(data []byte) (*AWSClient, error) {
37
37
38
38
conf , loadErr := config .LoadDefaultConfig (context .TODO ())
39
39
if loadErr != nil {
40
- return nil , loadErr
40
+ return nil , fmt . Errorf ( "unable to load default AWS config: %w" , loadErr )
41
41
}
42
42
43
43
client := imds .NewFromConfig (conf , func (o * imds.Options ) {
@@ -61,10 +61,10 @@ func NewAWSClient(data []byte) (*AWSClient, error) {
61
61
return awsClient , nil
62
62
}
63
63
64
- // GetUpstreams returns the Upstreams list
64
+ // GetUpstreams returns the Upstreams list.
65
65
func (client * AWSClient ) GetUpstreams () []Upstream {
66
- var upstreams []Upstream
67
- for i := 0 ; i < len (client .config .Upstreams ); i ++ {
66
+ upstreams := make ( []Upstream , 0 , len ( client . config . Upstreams ))
67
+ for i := range len (client .config .Upstreams ) {
68
68
u := Upstream {
69
69
Name : client .config .Upstreams [i ].Name ,
70
70
Port : client .config .Upstreams [i ].Port ,
@@ -81,13 +81,13 @@ func (client *AWSClient) GetUpstreams() []Upstream {
81
81
return upstreams
82
82
}
83
83
84
- // configure configures the AWSClient with necessary parameters
84
+ // configure configures the AWSClient with necessary parameters.
85
85
func (client * AWSClient ) configure () error {
86
86
httpClient := & http.Client {Timeout : connTimeoutInSecs * time .Second }
87
87
88
88
cfg , err := config .LoadDefaultConfig (context .TODO ())
89
89
if err != nil {
90
- return err
90
+ return fmt . Errorf ( "unable to load default AWS config: %w" , err )
91
91
}
92
92
93
93
client .svcEC2 = ec2 .NewFromConfig (cfg , func (o * ec2.Options ) {
@@ -103,12 +103,12 @@ func (client *AWSClient) configure() error {
103
103
return nil
104
104
}
105
105
106
- // parseAWSConfig parses and validates AWSClient config
106
+ // parseAWSConfig parses and validates AWSClient config.
107
107
func parseAWSConfig (data []byte ) (* awsConfig , error ) {
108
108
cfg := & awsConfig {}
109
109
err := yaml .Unmarshal (data , cfg )
110
110
if err != nil {
111
- return nil , err
111
+ return nil , fmt . Errorf ( "error unmarshalling AWS config: %w" , err )
112
112
}
113
113
114
114
err = validateAWSConfig (cfg )
@@ -119,7 +119,7 @@ func parseAWSConfig(data []byte) (*awsConfig, error) {
119
119
return cfg , nil
120
120
}
121
121
122
- // CheckIfScalingGroupExists checks if the Auto Scaling group exists
122
+ // CheckIfScalingGroupExists checks if the Auto Scaling group exists.
123
123
func (client * AWSClient ) CheckIfScalingGroupExists (name string ) (bool , error ) {
124
124
params := & ec2.DescribeInstancesInput {
125
125
Filters : []types.Filter {
@@ -140,7 +140,7 @@ func (client *AWSClient) CheckIfScalingGroupExists(name string) (bool, error) {
140
140
return len (response .Reservations ) > 0 , nil
141
141
}
142
142
143
- // GetPrivateIPsForScalingGroup returns the list of IP addresses of instances of the Auto Scaling group
143
+ // GetPrivateIPsForScalingGroup returns the list of IP addresses of instances of the Auto Scaling group.
144
144
func (client * AWSClient ) GetPrivateIPsForScalingGroup (name string ) ([]string , error ) {
145
145
var onlyInService bool
146
146
for _ , u := range client .GetUpstreams () {
@@ -162,7 +162,7 @@ func (client *AWSClient) GetPrivateIPsForScalingGroup(name string) ([]string, er
162
162
163
163
response , err := client .svcEC2 .DescribeInstances (context .Background (), params )
164
164
if err != nil {
165
- return nil , err
165
+ return nil , fmt . Errorf ( "couldn't describe instances: %w" , err )
166
166
}
167
167
168
168
if len (response .Reservations ) == 0 {
@@ -193,14 +193,14 @@ func (client *AWSClient) GetPrivateIPsForScalingGroup(name string) ([]string, er
193
193
return result , nil
194
194
}
195
195
196
- // getInstancesInService returns the list of instances that have LifecycleState == InService
196
+ // getInstancesInService returns the list of instances that have LifecycleState == InService.
197
197
func (client * AWSClient ) getInstancesInService (insIDtoIP map [string ]string ) ([]string , error ) {
198
198
const maxItems = 50
199
199
var result []string
200
200
keys := reflect .ValueOf (insIDtoIP ).MapKeys ()
201
201
instanceIDs := make ([]string , len (keys ))
202
202
203
- for i := 0 ; i < len (keys ); i ++ {
203
+ for i := range len (keys ) {
204
204
instanceIDs [i ] = keys [i ].String ()
205
205
}
206
206
@@ -211,7 +211,7 @@ func (client *AWSClient) getInstancesInService(insIDtoIP map[string]string) ([]s
211
211
}
212
212
response , err := client .svcAutoscaling .DescribeAutoScalingInstances (context .Background (), params )
213
213
if err != nil {
214
- return nil , err
214
+ return nil , fmt . Errorf ( "couldn't describe AutoScaling instances: %w" , err )
215
215
}
216
216
217
217
for _ , ins := range response .AutoScalingInstances {
0 commit comments