@@ -194,7 +194,7 @@ func (e *Service) GetRebalanceRecommendationEvent() (rebalanceRec *RebalanceReco
194
194
}
195
195
196
196
// 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 ) {
198
198
metadataInfo := ""
199
199
resp , err := e .Request (path )
200
200
if err != nil {
@@ -208,8 +208,12 @@ func (e *Service) GetMetadataInfo(path string) (info string, err error) {
208
208
}
209
209
metadataInfo = string (body )
210
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 )
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
+ }
213
217
}
214
218
}
215
219
return metadataInfo , nil
@@ -327,26 +331,26 @@ func retry(attempts int, sleep time.Duration, httpReq func() (*http.Response, er
327
331
// GetNodeMetadata attempts to gather additional ec2 instance information from the metadata service
328
332
func (e * Service ) GetNodeMetadata () NodeMetadata {
329
333
metadata := NodeMetadata {}
330
- identityDoc , err := e .GetMetadataInfo (IdentityDocPath )
334
+ identityDoc , err := e .GetMetadataInfo (IdentityDocPath , false )
331
335
if err != nil {
332
336
log .Err (err ).Msg ("Unable to fetch metadata from IMDS" )
333
337
return metadata
334
338
}
335
339
err = json .NewDecoder (strings .NewReader (identityDoc )).Decode (& metadata )
336
340
if err != nil {
337
341
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 )
342
346
if len (metadata .AvailabilityZone ) > 1 {
343
347
metadata .Region = metadata .AvailabilityZone [0 : len (metadata .AvailabilityZone )- 1 ]
344
348
}
345
349
}
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 )
350
354
351
355
log .Info ().Interface ("metadata" , metadata ).Msg ("Startup Metadata Retrieved" )
352
356
0 commit comments