Skip to content

Commit 2b6e4f7

Browse files
authored
[cluster-autoscaler-1.34] allow clusterapi provider to process managed labels (#8753)
* [cluster-autoscaler-1.34] allow clusterapi provider to process managed labels * update UT
1 parent cb43228 commit 2b6e4f7

File tree

7 files changed

+188
-39
lines changed

7 files changed

+188
-39
lines changed

cluster-autoscaler/cloudprovider/clusterapi/clusterapi_autodiscovery_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -204,17 +204,17 @@ func Test_allowedByAutoDiscoverySpec(t *testing.T) {
204204
shouldMatch bool
205205
}{{
206206
name: "no clustername, namespace, or label selector specified should match any MachineSet",
207-
testSpec: createTestSpec(RandomString(6), RandomString(6), RandomString(6), 1, false, nil, nil),
207+
testSpec: createTestSpec(RandomString(6), RandomString(6), RandomString(6), 1, false, nil, nil, map[string]string{}),
208208
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{labelSelector: labels.NewSelector()},
209209
shouldMatch: true,
210210
}, {
211211
name: "no clustername, namespace, or label selector specified should match any MachineDeployment",
212-
testSpec: createTestSpec(RandomString(6), RandomString(6), RandomString(6), 1, true, nil, nil),
212+
testSpec: createTestSpec(RandomString(6), RandomString(6), RandomString(6), 1, true, nil, nil, map[string]string{}),
213213
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{labelSelector: labels.NewSelector()},
214214
shouldMatch: true,
215215
}, {
216216
name: "clustername specified does not match MachineSet, namespace matches, no labels specified",
217-
testSpec: createTestSpec("default", RandomString(6), RandomString(6), 1, false, nil, nil),
217+
testSpec: createTestSpec("default", RandomString(6), RandomString(6), 1, false, nil, nil, map[string]string{}),
218218
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{
219219
clusterName: "foo",
220220
namespace: "default",
@@ -223,7 +223,7 @@ func Test_allowedByAutoDiscoverySpec(t *testing.T) {
223223
shouldMatch: false,
224224
}, {
225225
name: "clustername specified does not match MachineDeployment, namespace matches, no labels specified",
226-
testSpec: createTestSpec("default", RandomString(6), RandomString(6), 1, true, nil, nil),
226+
testSpec: createTestSpec("default", RandomString(6), RandomString(6), 1, true, nil, nil, map[string]string{}),
227227
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{
228228
clusterName: "foo",
229229
namespace: "default",
@@ -232,7 +232,7 @@ func Test_allowedByAutoDiscoverySpec(t *testing.T) {
232232
shouldMatch: false,
233233
}, {
234234
name: "namespace specified does not match MachineSet, clusterName matches, no labels specified",
235-
testSpec: createTestSpec(RandomString(6), "foo", RandomString(6), 1, false, nil, nil),
235+
testSpec: createTestSpec(RandomString(6), "foo", RandomString(6), 1, false, nil, nil, map[string]string{}),
236236
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{
237237
clusterName: "foo",
238238
namespace: "default",
@@ -241,7 +241,7 @@ func Test_allowedByAutoDiscoverySpec(t *testing.T) {
241241
shouldMatch: false,
242242
}, {
243243
name: "clustername specified does not match MachineDeployment, namespace matches, no labels specified",
244-
testSpec: createTestSpec(RandomString(6), "foo", RandomString(6), 1, true, nil, nil),
244+
testSpec: createTestSpec(RandomString(6), "foo", RandomString(6), 1, true, nil, nil, map[string]string{}),
245245
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{
246246
clusterName: "foo",
247247
namespace: "default",
@@ -250,7 +250,7 @@ func Test_allowedByAutoDiscoverySpec(t *testing.T) {
250250
shouldMatch: false,
251251
}, {
252252
name: "namespace and clusterName matches MachineSet, no labels specified",
253-
testSpec: createTestSpec("default", "foo", RandomString(6), 1, false, nil, nil),
253+
testSpec: createTestSpec("default", "foo", RandomString(6), 1, false, nil, nil, map[string]string{}),
254254
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{
255255
clusterName: "foo",
256256
namespace: "default",
@@ -259,7 +259,7 @@ func Test_allowedByAutoDiscoverySpec(t *testing.T) {
259259
shouldMatch: true,
260260
}, {
261261
name: "namespace and clusterName matches MachineDeployment, no labels specified",
262-
testSpec: createTestSpec("default", "foo", RandomString(6), 1, true, nil, nil),
262+
testSpec: createTestSpec("default", "foo", RandomString(6), 1, true, nil, nil, map[string]string{}),
263263
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{
264264
clusterName: "foo",
265265
namespace: "default",
@@ -268,7 +268,7 @@ func Test_allowedByAutoDiscoverySpec(t *testing.T) {
268268
shouldMatch: true,
269269
}, {
270270
name: "namespace and clusterName matches MachineSet, does not match label selector",
271-
testSpec: createTestSpec("default", "foo", RandomString(6), 1, false, nil, nil),
271+
testSpec: createTestSpec("default", "foo", RandomString(6), 1, false, nil, nil, map[string]string{}),
272272
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{
273273
clusterName: "foo",
274274
namespace: "default",
@@ -277,7 +277,7 @@ func Test_allowedByAutoDiscoverySpec(t *testing.T) {
277277
shouldMatch: false,
278278
}, {
279279
name: "namespace and clusterName matches MachineDeployment, does not match label selector",
280-
testSpec: createTestSpec("default", "foo", RandomString(6), 1, true, nil, nil),
280+
testSpec: createTestSpec("default", "foo", RandomString(6), 1, true, nil, nil, map[string]string{}),
281281
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{
282282
clusterName: "foo",
283283
namespace: "default",
@@ -286,7 +286,7 @@ func Test_allowedByAutoDiscoverySpec(t *testing.T) {
286286
shouldMatch: false,
287287
}, {
288288
name: "namespace, clusterName, and label selector matches MachineSet",
289-
testSpec: createTestSpec("default", "foo", RandomString(6), 1, false, nil, nil),
289+
testSpec: createTestSpec("default", "foo", RandomString(6), 1, false, nil, nil, map[string]string{}),
290290
additionalLabels: map[string]string{"color": "green"},
291291
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{
292292
clusterName: "foo",
@@ -296,7 +296,7 @@ func Test_allowedByAutoDiscoverySpec(t *testing.T) {
296296
shouldMatch: true,
297297
}, {
298298
name: "namespace, clusterName, and label selector matches MachineDeployment",
299-
testSpec: createTestSpec("default", "foo", RandomString(6), 1, true, nil, nil),
299+
testSpec: createTestSpec("default", "foo", RandomString(6), 1, true, nil, nil, map[string]string{}),
300300
additionalLabels: map[string]string{"color": "green"},
301301
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{
302302
clusterName: "foo",

cluster-autoscaler/cloudprovider/clusterapi/clusterapi_controller_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,23 +1288,23 @@ func Test_machineController_allowedByAutoDiscoverySpecs(t *testing.T) {
12881288
shouldMatch bool
12891289
}{{
12901290
name: "autodiscovery specs includes permissive spec that should match any MachineSet",
1291-
testSpec: createTestSpec(RandomString(6), RandomString(6), RandomString(6), 1, false, nil, nil),
1291+
testSpec: createTestSpec(RandomString(6), RandomString(6), RandomString(6), 1, false, nil, nil, map[string]string{}),
12921292
autoDiscoverySpecs: []*clusterAPIAutoDiscoveryConfig{
12931293
{labelSelector: labels.NewSelector()},
12941294
{clusterName: "foo", namespace: "bar", labelSelector: labels.Nothing()},
12951295
},
12961296
shouldMatch: true,
12971297
}, {
12981298
name: "autodiscovery specs includes permissive spec that should match any MachineDeployment",
1299-
testSpec: createTestSpec(RandomString(6), RandomString(6), RandomString(6), 1, true, nil, nil),
1299+
testSpec: createTestSpec(RandomString(6), RandomString(6), RandomString(6), 1, true, nil, nil, map[string]string{}),
13001300
autoDiscoverySpecs: []*clusterAPIAutoDiscoveryConfig{
13011301
{labelSelector: labels.NewSelector()},
13021302
{clusterName: "foo", namespace: "bar", labelSelector: labels.Nothing()},
13031303
},
13041304
shouldMatch: true,
13051305
}, {
13061306
name: "autodiscovery specs includes a restrictive spec that should match specific MachineSet",
1307-
testSpec: createTestSpec("default", "foo", RandomString(6), 1, false, nil, nil),
1307+
testSpec: createTestSpec("default", "foo", RandomString(6), 1, false, nil, nil, map[string]string{}),
13081308
additionalLabels: map[string]string{"color": "green"},
13091309
autoDiscoverySpecs: []*clusterAPIAutoDiscoveryConfig{
13101310
{clusterName: "foo", namespace: "default", labelSelector: labels.SelectorFromSet(labels.Set{"color": "green"})},
@@ -1313,7 +1313,7 @@ func Test_machineController_allowedByAutoDiscoverySpecs(t *testing.T) {
13131313
shouldMatch: true,
13141314
}, {
13151315
name: "autodiscovery specs includes a restrictive spec that should match specific MachineDeployment",
1316-
testSpec: createTestSpec("default", "foo", RandomString(6), 1, true, nil, nil),
1316+
testSpec: createTestSpec("default", "foo", RandomString(6), 1, true, nil, nil, map[string]string{}),
13171317
additionalLabels: map[string]string{"color": "green"},
13181318
autoDiscoverySpecs: []*clusterAPIAutoDiscoveryConfig{
13191319
{clusterName: "foo", namespace: "default", labelSelector: labels.SelectorFromSet(labels.Set{"color": "green"})},
@@ -1322,7 +1322,7 @@ func Test_machineController_allowedByAutoDiscoverySpecs(t *testing.T) {
13221322
shouldMatch: true,
13231323
}, {
13241324
name: "autodiscovery specs does not include any specs that should match specific MachineSet",
1325-
testSpec: createTestSpec("default", "foo", RandomString(6), 1, false, nil, nil),
1325+
testSpec: createTestSpec("default", "foo", RandomString(6), 1, false, nil, nil, map[string]string{}),
13261326
additionalLabels: map[string]string{"color": "green"},
13271327
autoDiscoverySpecs: []*clusterAPIAutoDiscoveryConfig{
13281328
{clusterName: "test", namespace: "default", labelSelector: labels.SelectorFromSet(labels.Set{"color": "blue"})},
@@ -1331,7 +1331,7 @@ func Test_machineController_allowedByAutoDiscoverySpecs(t *testing.T) {
13311331
shouldMatch: false,
13321332
}, {
13331333
name: "autodiscovery specs does not include any specs that should match specific MachineDeployment",
1334-
testSpec: createTestSpec("default", "foo", RandomString(6), 1, true, nil, nil),
1334+
testSpec: createTestSpec("default", "foo", RandomString(6), 1, true, nil, nil, map[string]string{}),
13351335
additionalLabels: map[string]string{"color": "green"},
13361336
autoDiscoverySpecs: []*clusterAPIAutoDiscoveryConfig{
13371337
{clusterName: "test", namespace: "default", labelSelector: labels.SelectorFromSet(labels.Set{"color": "blue"})},

cluster-autoscaler/cloudprovider/clusterapi/clusterapi_nodegroup_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,6 +1494,7 @@ func TestNodeGroupTemplateNodeInfo(t *testing.T) {
14941494

14951495
type testCaseConfig struct {
14961496
nodeLabels map[string]string
1497+
managedLabels map[string]string
14971498
includeNodes bool
14981499
expectedErr error
14991500
expectedCapacity map[corev1.ResourceName]int64
@@ -1618,6 +1619,37 @@ func TestNodeGroupTemplateNodeInfo(t *testing.T) {
16181619
},
16191620
},
16201621
},
1622+
{
1623+
name: "When the NodeGroup can scale from zero, and the scalable resource contains managed labels",
1624+
nodeGroupAnnotations: map[string]string{
1625+
memoryKey: "2048Mi",
1626+
cpuKey: "2",
1627+
gpuTypeKey: gpuapis.ResourceNvidiaGPU,
1628+
gpuCountKey: "1",
1629+
},
1630+
config: testCaseConfig{
1631+
expectedErr: nil,
1632+
nodeLabels: map[string]string{
1633+
"kubernetes.io/os": "linux",
1634+
"kubernetes.io/arch": "amd64",
1635+
},
1636+
managedLabels: map[string]string{
1637+
"node-role.kubernetes.io/test": "test",
1638+
},
1639+
expectedCapacity: map[corev1.ResourceName]int64{
1640+
corev1.ResourceCPU: 2,
1641+
corev1.ResourceMemory: 2048 * 1024 * 1024,
1642+
corev1.ResourcePods: 110,
1643+
gpuapis.ResourceNvidiaGPU: 1,
1644+
},
1645+
expectedNodeLabels: map[string]string{
1646+
"kubernetes.io/os": "linux",
1647+
"kubernetes.io/arch": "amd64",
1648+
"kubernetes.io/hostname": "random value",
1649+
"node-role.kubernetes.io/test": "test",
1650+
},
1651+
},
1652+
},
16211653
}
16221654

16231655
test := func(t *testing.T, testConfig *TestConfig, config testCaseConfig) {
@@ -1704,6 +1736,7 @@ func TestNodeGroupTemplateNodeInfo(t *testing.T) {
17041736
WithNamespace(testNamespace).
17051737
WithNodeCount(10).
17061738
WithAnnotations(cloudprovider.JoinStringMaps(enableScaleAnnotations, tc.nodeGroupAnnotations)).
1739+
WithManagedLabels(tc.config.managedLabels).
17071740
Build()
17081741
test(t, testConfig, tc.config)
17091742
})
@@ -1714,6 +1747,7 @@ func TestNodeGroupTemplateNodeInfo(t *testing.T) {
17141747
WithNamespace(testNamespace).
17151748
WithNodeCount(10).
17161749
WithAnnotations(cloudprovider.JoinStringMaps(enableScaleAnnotations, tc.nodeGroupAnnotations)).
1750+
WithManagedLabels(tc.config.managedLabels).
17171751
Build()
17181752
test(t, testConfig, tc.config)
17191753
})

cluster-autoscaler/cloudprovider/clusterapi/clusterapi_test_framework.go

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,26 @@ const (
5353
)
5454

5555
type testConfigBuilder struct {
56-
scalableType scalableTestType
57-
namespace string
58-
clusterName string
59-
namePrefix string
60-
nodeCount int
61-
annotations map[string]string
62-
capacity map[string]string
56+
scalableType scalableTestType
57+
namespace string
58+
clusterName string
59+
namePrefix string
60+
nodeCount int
61+
annotations map[string]string
62+
capacity map[string]string
63+
managedLabels map[string]string
6364
}
6465

6566
// NewTestConfigBuilder returns a builder for dynamically constructing mock ClusterAPI resources for testing.
6667
func NewTestConfigBuilder() *testConfigBuilder {
6768
return &testConfigBuilder{
68-
namespace: RandomString(6),
69-
clusterName: RandomString(6),
70-
namePrefix: RandomString(6),
71-
nodeCount: 0,
72-
annotations: nil,
73-
capacity: nil,
69+
namespace: RandomString(6),
70+
clusterName: RandomString(6),
71+
namePrefix: RandomString(6),
72+
nodeCount: 0,
73+
annotations: nil,
74+
capacity: nil,
75+
managedLabels: nil,
7476
}
7577
}
7678

@@ -91,6 +93,7 @@ func (b *testConfigBuilder) Build() *TestConfig {
9193
isMachineDeployment,
9294
b.annotations,
9395
b.capacity,
96+
b.managedLabels,
9497
)[0],
9598
)[0]
9699
}
@@ -111,6 +114,7 @@ func (b *testConfigBuilder) BuildMultiple(configCount int) []*TestConfig {
111114
isMachineDeployment,
112115
b.annotations,
113116
b.capacity,
117+
b.managedLabels,
114118
)...,
115119
)
116120
}
@@ -171,6 +175,19 @@ func (b *testConfigBuilder) WithCapacity(c map[string]string) *testConfigBuilder
171175
return b
172176
}
173177

178+
func (b *testConfigBuilder) WithManagedLabels(l map[string]string) *testConfigBuilder {
179+
if l == nil {
180+
// explicitly setting managed labels to nil
181+
b.managedLabels = nil
182+
} else {
183+
if b.managedLabels == nil {
184+
b.managedLabels = map[string]string{}
185+
}
186+
maps.Insert(b.managedLabels, maps.All(l))
187+
}
188+
return b
189+
}
190+
174191
// TestConfig contains clusterspecific information about a single test configuration.
175192
type TestConfig struct {
176193
spec *TestSpec
@@ -220,6 +237,9 @@ func createTestConfigs(specs ...TestSpec) []*TestConfig {
220237
"kind": machineTemplateKind,
221238
"name": "TestMachineTemplate",
222239
},
240+
"metadata": map[string]interface{}{
241+
"labels": map[string]interface{}{},
242+
},
223243
},
224244
},
225245
},
@@ -229,6 +249,12 @@ func createTestConfigs(specs ...TestSpec) []*TestConfig {
229249

230250
config.machineSet.SetAnnotations(make(map[string]string))
231251

252+
if spec.managedLabels != nil {
253+
if err := unstructured.SetNestedStringMap(config.machineSet.Object, spec.managedLabels, "spec", "template", "spec", "metadata", "labels"); err != nil {
254+
panic(err)
255+
}
256+
}
257+
232258
if !spec.rootIsMachineDeployment {
233259
config.machineSet.SetAnnotations(spec.annotations)
234260
} else {
@@ -258,6 +284,9 @@ func createTestConfigs(specs ...TestSpec) []*TestConfig {
258284
"kind": machineTemplateKind,
259285
"name": "TestMachineTemplate",
260286
},
287+
"metadata": map[string]interface{}{
288+
"labels": map[string]interface{}{},
289+
},
261290
},
262291
},
263292
},
@@ -278,6 +307,12 @@ func createTestConfigs(specs ...TestSpec) []*TestConfig {
278307
},
279308
}
280309
config.machineSet.SetOwnerReferences(ownerRefs)
310+
311+
if spec.managedLabels != nil {
312+
if err := unstructured.SetNestedStringMap(config.machineDeployment.Object, spec.managedLabels, "spec", "template", "spec", "metadata", "labels"); err != nil {
313+
panic(err)
314+
}
315+
}
281316
}
282317
config.machineSet.SetLabels(machineSetLabels)
283318
if err := unstructured.SetNestedStringMap(config.machineSet.Object, machineSetLabels, "spec", "selector", "matchLabels"); err != nil {
@@ -324,6 +359,7 @@ func createTestConfigs(specs ...TestSpec) []*TestConfig {
324359
type TestSpec struct {
325360
annotations map[string]string
326361
capacity map[string]string
362+
managedLabels map[string]string
327363
machineDeploymentName string
328364
machineSetName string
329365
machinePoolName string
@@ -333,20 +369,21 @@ type TestSpec struct {
333369
rootIsMachineDeployment bool
334370
}
335371

336-
func createTestSpecs(namespace, clusterName, namePrefix string, scalableResourceCount, nodeCount int, isMachineDeployment bool, annotations map[string]string, capacity map[string]string) []TestSpec {
372+
func createTestSpecs(namespace, clusterName, namePrefix string, scalableResourceCount, nodeCount int, isMachineDeployment bool, annotations map[string]string, capacity map[string]string, managedLabels map[string]string) []TestSpec {
337373
var specs []TestSpec
338374

339375
for i := 0; i < scalableResourceCount; i++ {
340-
specs = append(specs, createTestSpec(namespace, clusterName, fmt.Sprintf("%s-%d", namePrefix, i), nodeCount, isMachineDeployment, annotations, capacity))
376+
specs = append(specs, createTestSpec(namespace, clusterName, fmt.Sprintf("%s-%d", namePrefix, i), nodeCount, isMachineDeployment, annotations, capacity, managedLabels))
341377
}
342378

343379
return specs
344380
}
345381

346-
func createTestSpec(namespace, clusterName, name string, nodeCount int, isMachineDeployment bool, annotations map[string]string, capacity map[string]string) TestSpec {
382+
func createTestSpec(namespace, clusterName, name string, nodeCount int, isMachineDeployment bool, annotations map[string]string, capacity map[string]string, managedLabels map[string]string) TestSpec {
347383
return TestSpec{
348384
annotations: annotations,
349385
capacity: capacity,
386+
managedLabels: managedLabels,
350387
machineDeploymentName: name,
351388
machineSetName: name,
352389
clusterName: clusterName,

0 commit comments

Comments
 (0)