@@ -139,16 +139,16 @@ func New(metadataURL string, tries int) *Service {
139139func (e * Service ) GetScheduledMaintenanceEvents () ([]ScheduledEventDetail , error ) {
140140 resp , err := e .Request (ScheduledEventPath )
141141 if resp != nil && (resp .StatusCode < 200 || resp .StatusCode >= 300 ) {
142- return nil , fmt .Errorf ("Metadata request received http status code: %d" , resp .StatusCode )
142+ return nil , fmt .Errorf ("metadata request received http status code: %d" , resp .StatusCode )
143143 }
144144 if err != nil {
145- return nil , fmt .Errorf ("Unable to parse metadata response: %w" , err )
145+ return nil , fmt .Errorf ("unable to parse metadata response: %w" , err )
146146 }
147- defer resp .Body .Close ()
147+ defer func () { _ = resp .Body .Close () } ()
148148 var scheduledEvents []ScheduledEventDetail
149149 err = json .NewDecoder (resp .Body ).Decode (& scheduledEvents )
150150 if err != nil {
151- return nil , fmt .Errorf ("Could not decode json retrieved from imds: %w" , err )
151+ return nil , fmt .Errorf ("could not decode json retrieved from imds: %w" , err )
152152 }
153153 return scheduledEvents , nil
154154}
@@ -160,16 +160,16 @@ func (e *Service) GetSpotITNEvent() (instanceAction *InstanceAction, err error)
160160 if resp != nil && resp .StatusCode == 404 {
161161 return nil , nil
162162 } else if resp != nil && (resp .StatusCode < 200 || resp .StatusCode >= 300 ) {
163- return nil , fmt .Errorf ("Metadata request received http status code: %d" , resp .StatusCode )
163+ return nil , fmt .Errorf ("metadata request received http status code: %d" , resp .StatusCode )
164164 }
165165 if err != nil {
166- return nil , fmt .Errorf ("Unable to parse metadata response: %w" , err )
166+ return nil , fmt .Errorf ("unable to parse metadata response: %w" , err )
167167 }
168- defer resp .Body .Close ()
168+ defer func () { _ = resp .Body .Close () } ()
169169
170170 err = json .NewDecoder (resp .Body ).Decode (& instanceAction )
171171 if err != nil {
172- return nil , fmt .Errorf ("Could not decode instance action response: %w" , err )
172+ return nil , fmt .Errorf ("could not decode instance action response: %w" , err )
173173 }
174174 return instanceAction , nil
175175}
@@ -181,16 +181,16 @@ func (e *Service) GetRebalanceRecommendationEvent() (rebalanceRec *RebalanceReco
181181 if resp != nil && resp .StatusCode == 404 {
182182 return nil , nil
183183 } else if resp != nil && (resp .StatusCode < 200 || resp .StatusCode >= 300 ) {
184- return nil , fmt .Errorf ("Metadata request received http status code: %d" , resp .StatusCode )
184+ return nil , fmt .Errorf ("metadata request received http status code: %d" , resp .StatusCode )
185185 }
186186 if err != nil {
187- return nil , fmt .Errorf ("Unable to parse metadata response: %w" , err )
187+ return nil , fmt .Errorf ("unable to parse metadata response: %w" , err )
188188 }
189- defer resp .Body .Close ()
189+ defer func () { _ = resp .Body .Close () } ()
190190
191191 err = json .NewDecoder (resp .Body ).Decode (& rebalanceRec )
192192 if err != nil {
193- return nil , fmt .Errorf ("Could not decode rebalance recommendation response: %w" , err )
193+ return nil , fmt .Errorf ("could not decode rebalance recommendation response: %w" , err )
194194 }
195195 return rebalanceRec , nil
196196}
@@ -203,16 +203,16 @@ func (e *Service) GetASGTargetLifecycleState() (state string, err error) {
203203 if resp != nil && resp .StatusCode == 404 {
204204 return "" , nil
205205 } else if resp != nil && (resp .StatusCode < 200 || resp .StatusCode >= 300 ) {
206- return "" , fmt .Errorf ("Metadata request received http status code: %d" , resp .StatusCode )
206+ return "" , fmt .Errorf ("metadata request received http status code: %d" , resp .StatusCode )
207207 }
208208 if err != nil {
209- return "" , fmt .Errorf ("Unable to parse metadata response: %w" , err )
209+ return "" , fmt .Errorf ("unable to parse metadata response: %w" , err )
210210 }
211- defer resp .Body .Close ()
211+ defer func () { _ = resp .Body .Close () } ()
212212
213213 body , err := io .ReadAll (resp .Body )
214214 if err != nil {
215- return "" , fmt .Errorf ("Unable to parse http response. Status code: %d. %w" , resp .StatusCode , err )
215+ return "" , fmt .Errorf ("unable to parse http response. Status code: %d. %w" , resp .StatusCode , err )
216216 }
217217 return string (body ), nil
218218}
@@ -222,19 +222,19 @@ func (e *Service) GetMetadataInfo(path string, allowMissing bool) (info string,
222222 metadataInfo := ""
223223 resp , err := e .Request (path )
224224 if err != nil {
225- return "" , fmt .Errorf ("Unable to parse metadata response: %w" , err )
225+ return "" , fmt .Errorf ("unable to parse metadata response: %w" , err )
226226 }
227227 if resp != nil {
228- defer resp .Body .Close ()
228+ defer func () { _ = resp .Body .Close () } ()
229229 body , err := io .ReadAll (resp .Body )
230230 if err != nil {
231- return "" , fmt .Errorf ("Unable to parse http response. Status code: %d. %w" , resp .StatusCode , err )
231+ return "" , fmt .Errorf ("unable to parse http response. Status code: %d. %w" , resp .StatusCode , err )
232232 }
233233 metadataInfo = string (body )
234234 if resp .StatusCode < 200 || resp .StatusCode >= 300 {
235235 if resp .StatusCode != 404 || ! allowMissing {
236236 log .Info ().Msgf ("Metadata response status code: %d. Body: %s" , resp .StatusCode , metadataInfo )
237- return "" , fmt .Errorf ("Metadata request received http status code: %d" , resp .StatusCode )
237+ return "" , fmt .Errorf ("metadata request received http status code: %d" , resp .StatusCode )
238238 } else {
239239 return "" , nil
240240 }
@@ -249,7 +249,7 @@ func (e *Service) GetMetadataInfo(path string, allowMissing bool) (info string,
249249func (e * Service ) Request (contextPath string ) (* http.Response , error ) {
250250 req , err := http .NewRequest (http .MethodGet , e .metadataURL + contextPath , nil )
251251 if err != nil {
252- return nil , fmt .Errorf ("Unable to construct an http get request to IDMS for %s: %w" , e .metadataURL + contextPath , err )
252+ return nil , fmt .Errorf ("unable to construct an http get request to IDMS for %s: %w" , e .metadataURL + contextPath , err )
253253 }
254254 var resp * http.Response
255255 for i := 0 ; i < tokenRetryAttempts ; i ++ {
@@ -274,7 +274,7 @@ func (e *Service) Request(contextPath string) (*http.Response, error) {
274274 }
275275 resp , err = retry (e .tries , 2 * time .Second , httpReq )
276276 if err != nil {
277- return nil , fmt .Errorf ("Unable to get a response from IMDS: %w" , err )
277+ return nil , fmt .Errorf ("unable to get a response from IMDS: %w" , err )
278278 }
279279 if resp != nil && resp .StatusCode == 401 {
280280 e .Lock ()
@@ -297,7 +297,7 @@ func (e *Service) Request(contextPath string) (*http.Response, error) {
297297func (e * Service ) getV2Token () (string , int , error ) {
298298 req , err := http .NewRequest (http .MethodPut , e .metadataURL + tokenRefreshPath , nil )
299299 if err != nil {
300- return "" , - 1 , fmt .Errorf ("Unable to construct http put request to retrieve imdsv2 token: %w" , err )
300+ return "" , - 1 , fmt .Errorf ("unable to construct http put request to retrieve imdsv2 token: %w" , err )
301301 }
302302 req .Header .Add (tokenTTLHeader , strconv .Itoa (tokenTTL ))
303303 httpReq := func () (* http.Response , error ) {
@@ -308,13 +308,13 @@ func (e *Service) getV2Token() (string, int, error) {
308308 if err != nil {
309309 return "" , - 1 , err
310310 }
311- defer resp .Body .Close ()
311+ defer func () { _ = resp .Body .Close () } ()
312312 if resp .StatusCode < 200 || resp .StatusCode >= 300 {
313- return "" , - 1 , fmt .Errorf ("Received an http status code %d" , resp .StatusCode )
313+ return "" , - 1 , fmt .Errorf ("received an http status code %d" , resp .StatusCode )
314314 }
315315 token , err := io .ReadAll (resp .Body )
316316 if err != nil {
317- return "" , - 1 , fmt .Errorf ("Unable to read token response from IMDSv2: %w" , err )
317+ return "" , - 1 , fmt .Errorf ("unable to read token response from IMDSv2: %w" , err )
318318 }
319319 ttl , err := ttlHeaderToInt (resp )
320320 if err != nil {
@@ -327,7 +327,7 @@ func (e *Service) getV2Token() (string, int, error) {
327327func ttlHeaderToInt (resp * http.Response ) (int , error ) {
328328 ttl := resp .Header .Get (tokenTTLHeader )
329329 if ttl == "" {
330- return - 1 , fmt .Errorf ("No token TTL header found" )
330+ return - 1 , fmt .Errorf ("no token TTL header found" )
331331 }
332332 ttlInt , err := strconv .Atoi (ttl )
333333 if err != nil {
0 commit comments