Skip to content

Commit 0752e31

Browse files
committed
assume public ip and hostname ec2 metadata can be absent
Signed-off-by: Xabier Napal <[email protected]>
1 parent eb3c7d0 commit 0752e31

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

pkg/ec2metadata/ec2metadata.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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
328332
func (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

pkg/ec2metadata/ec2metadata_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ func TestGetMetadataServiceRequest404(t *testing.T) {
524524
// Use URL from our local test server
525525
imds := ec2metadata.New(server.URL, 1)
526526

527-
_, err := imds.GetMetadataInfo(requestPath)
527+
_, err := imds.GetMetadataInfo(requestPath, false)
528528

529529
h.Assert(t, err != nil, "Expected error to be nil but it was not")
530530
}
@@ -533,7 +533,7 @@ func TestGetMetadataServiceRequestFailure(t *testing.T) {
533533
// Use URL from our local test server
534534
imds := ec2metadata.New("/some-path-that-will-error", 1)
535535

536-
_, err := imds.GetMetadataInfo("/latest/meta-data/instance-type")
536+
_, err := imds.GetMetadataInfo("/latest/meta-data/instance-type", false)
537537
h.Assert(t, err != nil, "Error expected because no server should be running")
538538
}
539539

@@ -558,7 +558,7 @@ func TestGetMetadataServiceSuccess(t *testing.T) {
558558
// Use URL from our local test server
559559
imds := ec2metadata.New(server.URL, 1)
560560

561-
resp, err := imds.GetMetadataInfo(requestPath)
561+
resp, err := imds.GetMetadataInfo(requestPath, false)
562562

563563
h.Ok(t, err)
564564
h.Equals(t, `x1.32xlarge`, resp)

0 commit comments

Comments
 (0)