Skip to content

Commit

Permalink
Get region from AWS SDK if NoIID = true
Browse files Browse the repository at this point in the history
  • Loading branch information
yhlee-aws committed Nov 1, 2018
1 parent 426cc36 commit 8d7d0d2
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
13 changes: 11 additions & 2 deletions agent/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,17 @@ func NewConfig(ec2client ec2.EC2MetadataClient) (*Config, error) {
config.Merge(userDataConfig(ec2client))

if config.AWSRegion == "" {
// Get it from metadata only if we need to (network io)
config.Merge(ec2MetadataConfig(ec2client))
if config.NoIID {
// get it from AWS SDK if we don't have instance identity document
awsRegion, err := ec2client.Region()
if err != nil {
errs = append(errs, err)
}
config.AWSRegion = awsRegion
} else {
// Get it from metadata only if we need to (network io)
config.Merge(ec2MetadataConfig(ec2client))
}
}

return config, config.mergeDefaultConfig(errs)
Expand Down
19 changes: 19 additions & 0 deletions agent/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,25 @@ func TestBrokenEC2MetadataEndpoint(t *testing.T) {
assert.Zero(t, config.APIEndpoint, "Endpoint env variable not set; endpoint should be blank")
}

func TestGetRegionWithNoIID(t *testing.T) {
defer setTestEnv("AWS_DEFAULT_REGION", "")()
ctrl := gomock.NewController(t)
mockEc2Metadata := mock_ec2.NewMockEC2MetadataClient(ctrl)

userDataResponse := `{ "ECSAgentConfiguration":{
"Cluster":"arn:aws:ecs:us-east-1:123456789012:cluster/my-cluster",
"APIEndpoint":"https://some-endpoint.com",
"NoIID":true
}}`
mockEc2Metadata.EXPECT().GetUserData().Return(userDataResponse, nil)
mockEc2Metadata.EXPECT().Region().Return("us-east-1", nil)

config, err := NewConfig(mockEc2Metadata)
assert.NoError(t, err)
assert.Equal(t, config.AWSRegion, "us-east-1", "Wrong region")
assert.Equal(t, config.APIEndpoint, "https://some-endpoint.com", "Endpoint env variable not set; endpoint should be blank")
}

func TestEnvironmentConfig(t *testing.T) {
defer setTestRegion()()
defer setTestEnv("ECS_CLUSTER", "myCluster")()
Expand Down
4 changes: 4 additions & 0 deletions agent/ec2/blackhole_ec2_metadata_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,7 @@ func (blackholeMetadataClient) GetDynamicData(path string) (string, error) {
func (blackholeMetadataClient) GetUserData() (string, error) {
return "", errors.New("blackholed")
}

func (blackholeMetadataClient) Region() (string, error) {
return "", errors.New("blackholed")
}
7 changes: 7 additions & 0 deletions agent/ec2/ec2_metadata_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type HttpClient interface {
GetDynamicData(string) (string, error)
GetInstanceIdentityDocument() (ec2metadata.EC2InstanceIdentityDocument, error)
GetUserData() (string, error)
Region() (string, error)
}

// EC2MetadataClient is the client used to get metadata from instance metadata service
Expand All @@ -68,6 +69,7 @@ type EC2MetadataClient interface {
PrimaryENIMAC() (string, error)
InstanceID() (string, error)
GetUserData() (string, error)
Region() (string, error)
}

type ec2MetadataClientImpl struct {
Expand Down Expand Up @@ -155,3 +157,8 @@ func (c *ec2MetadataClientImpl) InstanceID() (string, error) {
func (c *ec2MetadataClientImpl) GetUserData() (string, error) {
return c.client.GetUserData()
}

// Region returns the region the instance is running in.
func (c *ec2MetadataClientImpl) Region() (string, error) {
return c.client.Region()
}
26 changes: 26 additions & 0 deletions agent/ec2/mocks/ec2_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8d7d0d2

Please sign in to comment.