diff --git a/pkg/ec2metadata/ec2metadata.go b/pkg/ec2metadata/ec2metadata.go index 84d453c7..bb8767d7 100644 --- a/pkg/ec2metadata/ec2metadata.go +++ b/pkg/ec2metadata/ec2metadata.go @@ -195,19 +195,24 @@ func (e *Service) GetRebalanceRecommendationEvent() (rebalanceRec *RebalanceReco // GetMetadataInfo generic function for retrieving ec2 metadata func (e *Service) GetMetadataInfo(path string) (info string, err error) { + metadataInfo := "" resp, err := e.Request(path) - if resp != nil && (resp.StatusCode < 200 || resp.StatusCode >= 300) { - return "", fmt.Errorf("Metadata request received http status code: %d", resp.StatusCode) - } if err != nil { return "", fmt.Errorf("Unable to parse metadata response: %w", err) } - defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return "", fmt.Errorf("Unable to parse http response: %w", err) + if resp != nil { + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return "", fmt.Errorf("Unable to parse http response. Status code: %d. %w", resp.StatusCode, err) + } + metadataInfo = string(body) + if resp.StatusCode < 200 || resp.StatusCode >= 300 { + log.Info().Msgf("Metadata response status code: %d. Body: %s", resp.StatusCode, metadataInfo) + return "", fmt.Errorf("Metadata request received http status code: %d", resp.StatusCode) + } } - return string(body), nil + return metadataInfo, nil } // Request sends an http request to IMDSv1 or v2 at the specified path