@@ -17,14 +17,14 @@ import (
1717 yaml "gopkg.in/yaml.v2"
1818)
1919
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.
2121type AWSClient struct {
2222 svcEC2 * ec2.Client
2323 svcAutoscaling * autoscaling.Client
2424 config * awsConfig
2525}
2626
27- // NewAWSClient creates and configures an AWSClient
27+ // NewAWSClient creates and configures an AWSClient.
2828func NewAWSClient (data []byte ) (* AWSClient , error ) {
2929 awsClient := & AWSClient {}
3030 cfg , err := parseAWSConfig (data )
@@ -37,7 +37,7 @@ func NewAWSClient(data []byte) (*AWSClient, error) {
3737
3838 conf , loadErr := config .LoadDefaultConfig (context .TODO ())
3939 if loadErr != nil {
40- return nil , loadErr
40+ return nil , fmt . Errorf ( "unable to load default AWS config: %w" , loadErr )
4141 }
4242
4343 client := imds .NewFromConfig (conf , func (o * imds.Options ) {
@@ -61,10 +61,10 @@ func NewAWSClient(data []byte) (*AWSClient, error) {
6161 return awsClient , nil
6262}
6363
64- // GetUpstreams returns the Upstreams list
64+ // GetUpstreams returns the Upstreams list.
6565func (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 ) {
6868 u := Upstream {
6969 Name : client .config .Upstreams [i ].Name ,
7070 Port : client .config .Upstreams [i ].Port ,
@@ -81,13 +81,13 @@ func (client *AWSClient) GetUpstreams() []Upstream {
8181 return upstreams
8282}
8383
84- // configure configures the AWSClient with necessary parameters
84+ // configure configures the AWSClient with necessary parameters.
8585func (client * AWSClient ) configure () error {
8686 httpClient := & http.Client {Timeout : connTimeoutInSecs * time .Second }
8787
8888 cfg , err := config .LoadDefaultConfig (context .TODO ())
8989 if err != nil {
90- return err
90+ return fmt . Errorf ( "unable to load default AWS config: %w" , err )
9191 }
9292
9393 client .svcEC2 = ec2 .NewFromConfig (cfg , func (o * ec2.Options ) {
@@ -103,12 +103,12 @@ func (client *AWSClient) configure() error {
103103 return nil
104104}
105105
106- // parseAWSConfig parses and validates AWSClient config
106+ // parseAWSConfig parses and validates AWSClient config.
107107func parseAWSConfig (data []byte ) (* awsConfig , error ) {
108108 cfg := & awsConfig {}
109109 err := yaml .Unmarshal (data , cfg )
110110 if err != nil {
111- return nil , err
111+ return nil , fmt . Errorf ( "error unmarshalling AWS config: %w" , err )
112112 }
113113
114114 err = validateAWSConfig (cfg )
@@ -119,7 +119,7 @@ func parseAWSConfig(data []byte) (*awsConfig, error) {
119119 return cfg , nil
120120}
121121
122- // CheckIfScalingGroupExists checks if the Auto Scaling group exists
122+ // CheckIfScalingGroupExists checks if the Auto Scaling group exists.
123123func (client * AWSClient ) CheckIfScalingGroupExists (name string ) (bool , error ) {
124124 params := & ec2.DescribeInstancesInput {
125125 Filters : []types.Filter {
@@ -140,7 +140,7 @@ func (client *AWSClient) CheckIfScalingGroupExists(name string) (bool, error) {
140140 return len (response .Reservations ) > 0 , nil
141141}
142142
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.
144144func (client * AWSClient ) GetPrivateIPsForScalingGroup (name string ) ([]string , error ) {
145145 var onlyInService bool
146146 for _ , u := range client .GetUpstreams () {
@@ -162,7 +162,7 @@ func (client *AWSClient) GetPrivateIPsForScalingGroup(name string) ([]string, er
162162
163163 response , err := client .svcEC2 .DescribeInstances (context .Background (), params )
164164 if err != nil {
165- return nil , err
165+ return nil , fmt . Errorf ( "couldn't describe instances: %w" , err )
166166 }
167167
168168 if len (response .Reservations ) == 0 {
@@ -193,14 +193,14 @@ func (client *AWSClient) GetPrivateIPsForScalingGroup(name string) ([]string, er
193193 return result , nil
194194}
195195
196- // getInstancesInService returns the list of instances that have LifecycleState == InService
196+ // getInstancesInService returns the list of instances that have LifecycleState == InService.
197197func (client * AWSClient ) getInstancesInService (insIDtoIP map [string ]string ) ([]string , error ) {
198198 const maxItems = 50
199199 var result []string
200200 keys := reflect .ValueOf (insIDtoIP ).MapKeys ()
201201 instanceIDs := make ([]string , len (keys ))
202202
203- for i := 0 ; i < len (keys ); i ++ {
203+ for i := range len (keys ) {
204204 instanceIDs [i ] = keys [i ].String ()
205205 }
206206
@@ -211,7 +211,7 @@ func (client *AWSClient) getInstancesInService(insIDtoIP map[string]string) ([]s
211211 }
212212 response , err := client .svcAutoscaling .DescribeAutoScalingInstances (context .Background (), params )
213213 if err != nil {
214- return nil , err
214+ return nil , fmt . Errorf ( "couldn't describe AutoScaling instances: %w" , err )
215215 }
216216
217217 for _ , ins := range response .AutoScalingInstances {
0 commit comments