Allow AWS instance types with multiple architectures

Older AWS instance types support both "i386" and "x86_64" architectures:
```
$ aws ec2 describe-instance-types --instance-types t2.micro
{
    "InstanceTypes": [
            "InstanceType": "t2.micro",
            "ProcessorInfo": {
                "SupportedArchitectures": [
                    "i386",
                    "x86_64"
                ],
```
This commit is contained in:
Ciprian Hacman 2021-05-12 11:13:06 +03:00
parent abc6bc087c
commit bdd63c917f
6 changed files with 69 additions and 26 deletions

View File

@ -102,8 +102,8 @@ go_test(
"docker_test.go",
"networking_test.go",
"new_cluster_test.go",
"populatecluster_test.go",
"populateinstancegroup_test.go",
"populate_cluster_spec_test.go",
"populate_instancegroup_spec_test.go",
"subnets_test.go",
"template_functions_test.go",
"urls_test.go",

View File

@ -327,16 +327,23 @@ func (c *MockAWSCloud) DescribeInstanceType(instanceType string) (*ec2.InstanceT
}
switch instanceType {
case "c5.large", "m3.medium", "m4.large", "m5.large", "m5.xlarge", "t2.micro", "t2.medium", "t3.medium", "t3.large", "c4.large":
case "c5.large", "m3.medium", "m4.large", "m5.large", "m5.xlarge", "t3.micro", "t3.medium", "t3.large", "c4.large":
info.ProcessorInfo = &ec2.ProcessorInfo{
SupportedArchitectures: []*string{
aws.String("x86_64"),
aws.String(ec2.ArchitectureTypeX8664),
},
}
case "a1.large":
info.ProcessorInfo = &ec2.ProcessorInfo{
SupportedArchitectures: []*string{
aws.String("arm64"),
aws.String(ec2.ArchitectureTypeArm64),
},
}
case "t2.micro", "t2.medium":
info.ProcessorInfo = &ec2.ProcessorInfo{
SupportedArchitectures: []*string{
aws.String(ec2.ArchitectureTypeI386),
aws.String(ec2.ArchitectureTypeX8664),
},
}
}

View File

@ -272,15 +272,19 @@ func MachineArchitecture(cloud fi.Cloud, machineType string) (architectures.Arch
if info.ProcessorInfo == nil || len(info.ProcessorInfo.SupportedArchitectures) == 0 {
return "", fmt.Errorf("error finding architecture info for instance type %q", machineType)
}
arch := fi.StringValue(info.ProcessorInfo.SupportedArchitectures[0])
switch arch {
case ec2.ArchitectureTypeX8664:
return architectures.ArchitectureAmd64, nil
case ec2.ArchitectureTypeArm64:
return architectures.ArchitectureArm64, nil
default:
return "", fmt.Errorf("unsupported architecture for instance type %q: %s", machineType, arch)
var unsupported []string
for _, arch := range info.ProcessorInfo.SupportedArchitectures {
// Return the first found supported architecture, in order of popularity
switch fi.StringValue(arch) {
case ec2.ArchitectureTypeX8664:
return architectures.ArchitectureAmd64, nil
case ec2.ArchitectureTypeArm64:
return architectures.ArchitectureArm64, nil
default:
unsupported = append(unsupported, fi.StringValue(arch))
}
}
return "", fmt.Errorf("unsupported architecture for instance type %q: %v", machineType, unsupported)
default:
// No other clouds are known to support any other architectures at this time
return architectures.ArchitectureAmd64, nil

View File

@ -22,6 +22,7 @@ import (
"testing"
kopsapi "k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/util/pkg/architectures"
)
func buildMinimalNodeInstanceGroup(subnets ...string) *kopsapi.InstanceGroup {
@ -77,3 +78,47 @@ func expectErrorFromPopulateInstanceGroup(t *testing.T, cluster *kopsapi.Cluster
t.Fatalf("Expected error %q, got %q", message, actualMessage)
}
}
func TestMachineArchitecture(t *testing.T) {
tests := []struct {
machineType string
arch architectures.Architecture
err error
}{
{
machineType: "t2.micro",
arch: architectures.ArchitectureAmd64,
err: nil,
},
{
machineType: "t3.micro",
arch: architectures.ArchitectureAmd64,
err: nil,
},
{
machineType: "a1.large",
arch: architectures.ArchitectureArm64,
err: nil,
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("%s-%s", test.machineType, test.arch), func(t *testing.T) {
_, cluster := buildMinimalCluster()
cloud, err := BuildCloud(cluster)
if err != nil {
t.Fatalf("error from BuildCloud: %v", err)
}
arch, err := MachineArchitecture(cloud, test.machineType)
if err != test.err {
t.Errorf("actual error %q differs from expected error %q", err, test.err)
return
}
if arch != test.arch {
t.Errorf("actual architecture %q differs from expected architecture %q", arch, test.arch)
return
}
})
}
}

View File

@ -1,13 +0,0 @@
> kops create cluster mixed.awsdata.com --zones us-east-1c --topology private --networking weave
> kops edit cluster mixed.awsdata.com
# Changed nodes to public here:
```
topology:
dns:
type: Public
masters: private
nodes: public
```