@@ -194,7 +194,7 @@ func (e *Service) GetRebalanceRecommendationEvent() (rebalanceRec *RebalanceReco
194194}
195195
196196// GetMetadataInfo generic function for retrieving ec2 metadata
197- func (e * Service ) GetMetadataInfo (path string ) (info string , err error ) {
197+ func (e * Service ) GetMetadataInfo (path string , allowMissing bool ) (info string , err error ) {
198198 metadataInfo := ""
199199 resp , err := e .Request (path )
200200 if err != nil {
@@ -208,8 +208,12 @@ func (e *Service) GetMetadataInfo(path string) (info string, err error) {
208208 }
209209 metadataInfo = string (body )
210210 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 )
211+ if resp .StatusCode != 404 || ! allowMissing {
212+ log .Info ().Msgf ("Metadata response status code: %d. Body: %s" , resp .StatusCode , metadataInfo )
213+ return "" , fmt .Errorf ("Metadata request received http status code: %d" , resp .StatusCode )
214+ } else {
215+ return "" , nil
216+ }
213217 }
214218 }
215219 return metadataInfo , nil
@@ -327,26 +331,26 @@ func retry(attempts int, sleep time.Duration, httpReq func() (*http.Response, er
327331// GetNodeMetadata attempts to gather additional ec2 instance information from the metadata service
328332func (e * Service ) GetNodeMetadata () NodeMetadata {
329333 metadata := NodeMetadata {}
330- identityDoc , err := e .GetMetadataInfo (IdentityDocPath )
334+ identityDoc , err := e .GetMetadataInfo (IdentityDocPath , false )
331335 if err != nil {
332336 log .Err (err ).Msg ("Unable to fetch metadata from IMDS" )
333337 return metadata
334338 }
335339 err = json .NewDecoder (strings .NewReader (identityDoc )).Decode (& metadata )
336340 if err != nil {
337341 log .Warn ().Msg ("Unable to fetch instance identity document from ec2 metadata" )
338- metadata .InstanceID , _ = e .GetMetadataInfo (InstanceIDPath )
339- metadata .InstanceType , _ = e .GetMetadataInfo (InstanceTypePath )
340- metadata .LocalIP , _ = e .GetMetadataInfo (LocalIPPath )
341- metadata .AvailabilityZone , _ = e .GetMetadataInfo (AZPlacementPath )
342+ metadata .InstanceID , _ = e .GetMetadataInfo (InstanceIDPath , false )
343+ metadata .InstanceType , _ = e .GetMetadataInfo (InstanceTypePath , false )
344+ metadata .LocalIP , _ = e .GetMetadataInfo (LocalIPPath , false )
345+ metadata .AvailabilityZone , _ = e .GetMetadataInfo (AZPlacementPath , false )
342346 if len (metadata .AvailabilityZone ) > 1 {
343347 metadata .Region = metadata .AvailabilityZone [0 : len (metadata .AvailabilityZone )- 1 ]
344348 }
345349 }
346- metadata .InstanceLifeCycle , _ = e .GetMetadataInfo (InstanceLifeCycle )
347- metadata .LocalHostname , _ = e .GetMetadataInfo (LocalHostnamePath )
348- metadata .PublicHostname , _ = e .GetMetadataInfo (PublicHostnamePath )
349- metadata .PublicIP , _ = e .GetMetadataInfo (PublicIPPath )
350+ metadata .InstanceLifeCycle , _ = e .GetMetadataInfo (InstanceLifeCycle , false )
351+ metadata .LocalHostname , _ = e .GetMetadataInfo (LocalHostnamePath , false )
352+ metadata .PublicHostname , _ = e .GetMetadataInfo (PublicHostnamePath , true )
353+ metadata .PublicIP , _ = e .GetMetadataInfo (PublicIPPath , true )
350354
351355 log .Info ().Interface ("metadata" , metadata ).Msg ("Startup Metadata Retrieved" )
352356
0 commit comments