Skip to content

Commit fef7353

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

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
@@ -218,7 +218,7 @@ func (e *Service) GetASGTargetLifecycleState() (state string, err error) {
218218
}
219219

220220
// GetMetadataInfo generic function for retrieving ec2 metadata
221-
func (e *Service) GetMetadataInfo(path string) (info string, err error) {
221+
func (e *Service) GetMetadataInfo(path string, allowMissing bool) (info string, err error) {
222222
metadataInfo := ""
223223
resp, err := e.Request(path)
224224
if err != nil {
@@ -232,8 +232,12 @@ func (e *Service) GetMetadataInfo(path string) (info string, err error) {
232232
}
233233
metadataInfo = string(body)
234234
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
235-
log.Info().Msgf("Metadata response status code: %d. Body: %s", resp.StatusCode, metadataInfo)
236-
return "", fmt.Errorf("Metadata request received http status code: %d", resp.StatusCode)
235+
if resp.StatusCode != 404 || !allowMissing {
236+
log.Info().Msgf("Metadata response status code: %d. Body: %s", resp.StatusCode, metadataInfo)
237+
return "", fmt.Errorf("Metadata request received http status code: %d", resp.StatusCode)
238+
} else {
239+
return "", nil
240+
}
237241
}
238242
}
239243
return metadataInfo, nil
@@ -351,26 +355,26 @@ func retry(attempts int, sleep time.Duration, httpReq func() (*http.Response, er
351355
// GetNodeMetadata attempts to gather additional ec2 instance information from the metadata service
352356
func (e *Service) GetNodeMetadata() NodeMetadata {
353357
metadata := NodeMetadata{}
354-
identityDoc, err := e.GetMetadataInfo(IdentityDocPath)
358+
identityDoc, err := e.GetMetadataInfo(IdentityDocPath, false)
355359
if err != nil {
356360
log.Err(err).Msg("Unable to fetch metadata from IMDS")
357361
return metadata
358362
}
359363
err = json.NewDecoder(strings.NewReader(identityDoc)).Decode(&metadata)
360364
if err != nil {
361365
log.Warn().Msg("Unable to fetch instance identity document from ec2 metadata")
362-
metadata.InstanceID, _ = e.GetMetadataInfo(InstanceIDPath)
363-
metadata.InstanceType, _ = e.GetMetadataInfo(InstanceTypePath)
364-
metadata.LocalIP, _ = e.GetMetadataInfo(LocalIPPath)
365-
metadata.AvailabilityZone, _ = e.GetMetadataInfo(AZPlacementPath)
366+
metadata.InstanceID, _ = e.GetMetadataInfo(InstanceIDPath, false)
367+
metadata.InstanceType, _ = e.GetMetadataInfo(InstanceTypePath, false)
368+
metadata.LocalIP, _ = e.GetMetadataInfo(LocalIPPath, false)
369+
metadata.AvailabilityZone, _ = e.GetMetadataInfo(AZPlacementPath, false)
366370
if len(metadata.AvailabilityZone) > 1 {
367371
metadata.Region = metadata.AvailabilityZone[0 : len(metadata.AvailabilityZone)-1]
368372
}
369373
}
370-
metadata.InstanceLifeCycle, _ = e.GetMetadataInfo(InstanceLifeCycle)
371-
metadata.LocalHostname, _ = e.GetMetadataInfo(LocalHostnamePath)
372-
metadata.PublicHostname, _ = e.GetMetadataInfo(PublicHostnamePath)
373-
metadata.PublicIP, _ = e.GetMetadataInfo(PublicIPPath)
374+
metadata.InstanceLifeCycle, _ = e.GetMetadataInfo(InstanceLifeCycle, false)
375+
metadata.LocalHostname, _ = e.GetMetadataInfo(LocalHostnamePath, false)
376+
metadata.PublicHostname, _ = e.GetMetadataInfo(PublicHostnamePath, true)
377+
metadata.PublicIP, _ = e.GetMetadataInfo(PublicIPPath, true)
374378

375379
log.Info().Interface("metadata", metadata).Msg("Startup Metadata Retrieved")
376380

pkg/ec2metadata/ec2metadata_test.go

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

612-
_, err := imds.GetMetadataInfo(requestPath)
612+
_, err := imds.GetMetadataInfo(requestPath, false)
613613

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

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

@@ -643,7 +643,7 @@ func TestGetMetadataServiceSuccess(t *testing.T) {
643643
// Use URL from our local test server
644644
imds := ec2metadata.New(server.URL, 1)
645645

646-
resp, err := imds.GetMetadataInfo(requestPath)
646+
resp, err := imds.GetMetadataInfo(requestPath, false)
647647

648648
h.Ok(t, err)
649649
h.Equals(t, `x1.32xlarge`, resp)

0 commit comments

Comments
 (0)