Skip to content

Commit 0a61ead

Browse files
authored
Log IMDS response full text when status code not in 200 range (#645)
* Log IMDS response full text when status code not in 200 range * Refactor the nil check; use info level logging
1 parent 2048e92 commit 0a61ead

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

pkg/ec2metadata/ec2metadata.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -195,19 +195,24 @@ func (e *Service) GetRebalanceRecommendationEvent() (rebalanceRec *RebalanceReco
195195

196196
// GetMetadataInfo generic function for retrieving ec2 metadata
197197
func (e *Service) GetMetadataInfo(path string) (info string, err error) {
198+
metadataInfo := ""
198199
resp, err := e.Request(path)
199-
if resp != nil && (resp.StatusCode < 200 || resp.StatusCode >= 300) {
200-
return "", fmt.Errorf("Metadata request received http status code: %d", resp.StatusCode)
201-
}
202200
if err != nil {
203201
return "", fmt.Errorf("Unable to parse metadata response: %w", err)
204202
}
205-
defer resp.Body.Close()
206-
body, err := ioutil.ReadAll(resp.Body)
207-
if err != nil {
208-
return "", fmt.Errorf("Unable to parse http response: %w", err)
203+
if resp != nil {
204+
defer resp.Body.Close()
205+
body, err := ioutil.ReadAll(resp.Body)
206+
if err != nil {
207+
return "", fmt.Errorf("Unable to parse http response. Status code: %d. %w", resp.StatusCode, err)
208+
}
209+
metadataInfo = string(body)
210+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
211+
log.Info().Msgf("Metadata response status code: %d. Body: %s", resp.StatusCode, metadataInfo)
212+
return "", fmt.Errorf("Metadata request received http status code: %d", resp.StatusCode)
213+
}
209214
}
210-
return string(body), nil
215+
return metadataInfo, nil
211216
}
212217

213218
// Request sends an http request to IMDSv1 or v2 at the specified path

0 commit comments

Comments
 (0)