Skip to content

Commit 06e0a71

Browse files
authored
Merge pull request kubernetes#5103 from towca/jtuznik/custom-fix
CA: GCE: fix custom machine type parsing
2 parents e478ee2 + ba2c17a commit 06e0a71

File tree

2 files changed

+107
-15
lines changed

2 files changed

+107
-15
lines changed

cluster-autoscaler/cloudprovider/gce/machine_types.go

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,23 @@ func NewCustomMachineType(name string) (MachineType, error) {
8383
return MachineType{}, fmt.Errorf("%q is not a valid custom machine type", name)
8484
}
8585

86+
// Identify the "custom" part of the name, assume the next part is the CPU count, and the one after that is the memory amount.
87+
// This should work if the type name has a "custom-*-*" infix, regardless of the rest of the name.
8688
parts := strings.Split(name, "-")
87-
var cpuPart, memPart string
88-
if len(parts) == 3 {
89-
cpuPart = parts[1]
90-
memPart = parts[2]
91-
} else if len(parts) == 4 {
92-
cpuPart = parts[2]
93-
memPart = parts[3]
94-
} else {
89+
customPartIndex := -1
90+
for i, part := range parts {
91+
if part == "custom" {
92+
customPartIndex = i
93+
break
94+
}
95+
}
96+
if customPartIndex == -1 || customPartIndex+2 >= len(parts) {
9597
return MachineType{}, fmt.Errorf("unable to parse custom machine type %q", name)
9698
}
99+
cpuPart := parts[customPartIndex+1]
100+
memPart := parts[customPartIndex+2]
97101

98-
cpu, err := strconv.ParseInt(cpuPart, 10, 64)
102+
cpu, err := parseCustomCpu(name, cpuPart)
99103
if err != nil {
100104
return MachineType{}, fmt.Errorf("unable to parse CPU %q from machine type %q: %v", cpuPart, name, err)
101105
}
@@ -110,3 +114,19 @@ func NewCustomMachineType(name string) (MachineType, error) {
110114
Memory: memBytes * units.MiB,
111115
}, nil
112116
}
117+
118+
func parseCustomCpu(machineType string, cpuPart string) (int64, error) {
119+
// We need to identify the family because some e2 machine types have special names.
120+
family, err := GetMachineFamily(machineType)
121+
if err != nil {
122+
return 0, fmt.Errorf("unable to get family while parsing custom machine type %q: %v", machineType, err)
123+
}
124+
125+
// There are e2-custom-micro-*, e2-custom-small-*, e2-custom-medium-* custom machine types in the e2 family. They all have 2 guestCpus
126+
// in the API.
127+
if family == "e2" && (cpuPart == "micro" || cpuPart == "small" || cpuPart == "medium") {
128+
return 2, nil
129+
}
130+
131+
return strconv.ParseInt(cpuPart, 10, 64)
132+
}

cluster-autoscaler/cloudprovider/gce/machine_types_test.go

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func TestNewCustomMachineType(t *testing.T) {
3030
testCases := []struct {
3131
name string
3232
expectCustom bool
33+
expectErr bool
3334
expectCPU int64
3435
expectMemory int64
3536
}{
@@ -39,33 +40,84 @@ func TestNewCustomMachineType(t *testing.T) {
3940
expectCPU: 2,
4041
expectMemory: 2816 * units.MiB,
4142
},
43+
{
44+
name: "custom-2-2816-ext",
45+
expectCustom: true,
46+
expectCPU: 2,
47+
expectMemory: 2816 * units.MiB,
48+
},
4249
{
4350
name: "n2-custom-2-2816",
4451
expectCustom: true,
4552
expectCPU: 2,
4653
expectMemory: 2816 * units.MiB,
4754
},
4855
{
49-
name: "other-a2-2816",
56+
name: "n2-custom-2-2816-ext",
57+
expectCustom: true,
58+
expectCPU: 2,
59+
expectMemory: 2816 * units.MiB,
60+
},
61+
{
62+
name: "e2-custom-medium-2816",
63+
expectCustom: true,
64+
expectCPU: 2,
65+
expectMemory: 2816 * units.MiB,
66+
},
67+
{
68+
name: "e2-custom-micro-2048",
69+
expectCustom: true,
70+
expectCPU: 2,
71+
expectMemory: 2048 * units.MiB,
72+
},
73+
{
74+
name: "e2-custom-small-2048",
75+
expectCustom: true,
76+
expectCPU: 2,
77+
expectMemory: 2048 * units.MiB,
78+
},
79+
{
80+
name: "e2-custom",
81+
expectCustom: true,
82+
expectErr: true,
83+
},
84+
{
85+
name: "e2-custom-8",
86+
expectCustom: true,
87+
expectErr: true,
88+
},
89+
{
90+
name: "e2-custom-abc-2048",
91+
expectCustom: true,
92+
expectErr: true,
93+
},
94+
{
95+
name: "other-a2-2816",
96+
expectCustom: false,
97+
expectErr: true,
5098
},
5199
{
52-
name: "other-2-2816",
100+
name: "other-2-2816",
101+
expectCustom: false,
102+
expectErr: true,
53103
},
54104
{
55-
name: "n1-standard-8",
105+
name: "n1-standard-8",
106+
expectCustom: false,
107+
expectErr: true,
56108
},
57109
}
58110

59111
for _, tc := range testCases {
60112
t.Run(tc.name, func(t *testing.T) {
61113
assert.Equal(t, tc.expectCustom, IsCustomMachine(tc.name))
62114
m, err := NewCustomMachineType(tc.name)
63-
if tc.expectCustom {
115+
if tc.expectErr {
116+
assert.Error(t, err)
117+
} else {
64118
assert.NoError(t, err)
65119
assert.Equal(t, tc.expectCPU, m.CPU)
66120
assert.Equal(t, tc.expectMemory, m.Memory)
67-
} else {
68-
assert.Error(t, err)
69121
}
70122
})
71123
}
@@ -89,10 +141,30 @@ func TestGetMachineFamily(t *testing.T) {
89141
machineType: "n2-custom-2-2816",
90142
wantFamily: "n2",
91143
},
144+
"custom machine type with family prefix and ext suffix": {
145+
machineType: "n2-custom-2-2816-ext",
146+
wantFamily: "n2",
147+
},
92148
"custom machine type without family prefix": {
93149
machineType: "custom-2-2816",
94150
wantFamily: "n1",
95151
},
152+
"custom machine type without family prefix with ext suffix": {
153+
machineType: "custom-2-2816-ext",
154+
wantFamily: "n1",
155+
},
156+
"e2 custom medium type": {
157+
machineType: "e2-custom-medium-2816",
158+
wantFamily: "e2",
159+
},
160+
"e2 custom small type": {
161+
machineType: "e2-custom-small-2048",
162+
wantFamily: "e2",
163+
},
164+
"e2 custom micro type": {
165+
machineType: "e2-custom-micro-2048",
166+
wantFamily: "e2",
167+
},
96168
"invalid machine type": {
97169
machineType: "nodashes",
98170
wantErr: cmpopts.AnyError,

0 commit comments

Comments
 (0)