Skip to content

Commit 4c40431

Browse files
authored
Detect Graviton 2 instance as arm64 Fix #1113 (#1116)
* Detect Graviton 2 instance as arm64 Fix #1113 - The original PR #695 only handles a1, which is the only arm instance type at that time. Now there are more instance types like c6g etc. - Tweak the unit test a bit so instance types using same ami are merged into one test case. * Add more generic instance types in AMI unit test
1 parent bbd651c commit 4c40431

File tree

2 files changed

+25
-29
lines changed

2 files changed

+25
-29
lines changed

ecs-cli/modules/clients/aws/amimetadata/client.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,13 @@ func (c *metadataClient) parameterValueFor(ssmParamName string) (*AMIMetadata, e
103103
return metadata, err
104104
}
105105

106+
// See: https://aws.amazon.com/ec2/instance-types/
107+
// a1 is the first generation of graviton processors.
108+
// t4g, m6g, c6g, r6g are using graviton 2.
109+
// The d suffix is for disk optimized and applies to all except a1 and t4g, e.g. m6gd.medium.
110+
// Invalid instance type like t4gd.nano will trigger validation error in API so we don't do validation here.
106111
func isARM64Instance(instanceType string) bool {
107-
r := regexp.MustCompile("a1\\.(medium|\\d*x?large)")
112+
r := regexp.MustCompile("(a1|.\\dgd?)\\.(medium|\\d*x?large|metal)")
108113
if r.MatchString(instanceType) {
109114
return true
110115
}

ecs-cli/modules/clients/aws/amimetadata/client_test.go

+19-28
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ type Configurer func(ssmClient *mock_ssmiface.MockSSMAPI) *mock_ssmiface.MockSSM
1515

1616
func TestMetadataClient_GetRecommendedECSLinuxAMI(t *testing.T) {
1717
tests := []struct {
18-
instanceType string
18+
instanceTypes []string
1919
configureMock Configurer
2020
expectedErr error
2121
}{
2222
{
2323
// validate that we use the ARM64 optimized AMI for Arm instances
24-
"a1.medium",
24+
[]string{"a1.medium", "m6g.medium", "c6gd.16xlarge", "m6g.metal"},
2525
func(ssmClient *mock_ssmiface.MockSSMAPI) *mock_ssmiface.MockSSMAPI {
2626
ssmClient.EXPECT().GetParameter(gomock.Any()).Do(func(input *ssm.GetParameterInput) {
2727
assert.Equal(t, amazonLinux2ARM64RecommendedParameterName, *input.Name)
@@ -32,18 +32,7 @@ func TestMetadataClient_GetRecommendedECSLinuxAMI(t *testing.T) {
3232
},
3333
{
3434
// validate that we use GPU optimized AMI for GPU instances
35-
"p2.large",
36-
func(ssmClient *mock_ssmiface.MockSSMAPI) *mock_ssmiface.MockSSMAPI {
37-
ssmClient.EXPECT().GetParameter(gomock.Any()).Do(func(input *ssm.GetParameterInput) {
38-
assert.Equal(t, amazonLinux2X86GPURecommendedParameterName, *input.Name)
39-
}).Return(emptySSMParameterOutput(), nil)
40-
return ssmClient
41-
},
42-
nil,
43-
},
44-
{
45-
// validate that we use GPU optimized AMI for GPU instances
46-
"g4dn.xlarge",
35+
[]string{"p2.large", "g4dn.xlarge"},
4736
func(ssmClient *mock_ssmiface.MockSSMAPI) *mock_ssmiface.MockSSMAPI {
4837
ssmClient.EXPECT().GetParameter(gomock.Any()).Do(func(input *ssm.GetParameterInput) {
4938
assert.Equal(t, amazonLinux2X86GPURecommendedParameterName, *input.Name)
@@ -54,7 +43,7 @@ func TestMetadataClient_GetRecommendedECSLinuxAMI(t *testing.T) {
5443
},
5544
{
5645
// validate that we use the generic AMI for other instances
57-
"t2.micro",
46+
[]string{"t2.micro", "m5ad.large", "c4.large", "i3.2xlarge"},
5847
func(ssmClient *mock_ssmiface.MockSSMAPI) *mock_ssmiface.MockSSMAPI {
5948
ssmClient.EXPECT().GetParameter(gomock.Any()).Do(func(input *ssm.GetParameterInput) {
6049
assert.Equal(t, amazonLinux2X86RecommendedParameterName, *input.Name)
@@ -65,7 +54,7 @@ func TestMetadataClient_GetRecommendedECSLinuxAMI(t *testing.T) {
6554
},
6655
{
6756
// validate that we throw an error if the AMI is not available in a region
68-
"t2.micro",
57+
[]string{"t2.micro"},
6958
func(ssmClient *mock_ssmiface.MockSSMAPI) *mock_ssmiface.MockSSMAPI {
7059
ssmClient.EXPECT().GetParameter(gomock.Any()).Do(func(input *ssm.GetParameterInput) {
7160
assert.Equal(t, amazonLinux2X86RecommendedParameterName, *input.Name)
@@ -79,7 +68,7 @@ func TestMetadataClient_GetRecommendedECSLinuxAMI(t *testing.T) {
7968
},
8069
{
8170
// validate that we throw unexpected errors
82-
"t2.micro",
71+
[]string{"t2.micro"},
8372
func(ssmClient *mock_ssmiface.MockSSMAPI) *mock_ssmiface.MockSSMAPI {
8473
ssmClient.EXPECT().GetParameter(gomock.Any()).Do(func(input *ssm.GetParameterInput) {
8574
assert.Equal(t, amazonLinux2X86RecommendedParameterName, *input.Name)
@@ -91,19 +80,21 @@ func TestMetadataClient_GetRecommendedECSLinuxAMI(t *testing.T) {
9180
}
9281

9382
for _, test := range tests {
94-
m := newMockSSMAPI(t)
95-
test.configureMock(m)
83+
for _, instanceType := range test.instanceTypes {
84+
m := newMockSSMAPI(t)
85+
test.configureMock(m)
9686

97-
c := metadataClient{
98-
m,
99-
"us-east-1",
100-
}
101-
_, actualErr := c.GetRecommendedECSLinuxAMI(test.instanceType)
87+
c := metadataClient{
88+
m,
89+
"us-east-1",
90+
}
91+
_, actualErr := c.GetRecommendedECSLinuxAMI(instanceType)
10292

103-
if test.expectedErr == nil {
104-
assert.NoError(t, actualErr)
105-
} else {
106-
assert.EqualError(t, actualErr, test.expectedErr.Error())
93+
if test.expectedErr == nil {
94+
assert.NoError(t, actualErr)
95+
} else {
96+
assert.EqualError(t, actualErr, test.expectedErr.Error())
97+
}
10798
}
10899
}
109100
}

0 commit comments

Comments
 (0)