Skip to content

Commit b3122fd

Browse files
elmikocloud-team-rebase-bot
authored andcommitted
UPSTREAM: <carry>: add machine api label and taint functionality
This change re-adds the machine api support for labels and taints on node groups. The code was removed upstream as it is openshift specific, see this pull request[0]. It also adds in the functionality of the upstream override annotation for labels and taints[1] to support https://issues.redhat.com/browse/MIXEDARCH-259 [0]: kubernetes#5249 [1]: kubernetes#5382
1 parent 06b0046 commit b3122fd

File tree

2 files changed

+58
-19
lines changed

2 files changed

+58
-19
lines changed

cluster-autoscaler/cloudprovider/clusterapi/clusterapi_nodegroup_test.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,12 +1451,12 @@ func TestNodeGroupTemplateNodeInfo(t *testing.T) {
14511451
}
14521452

14531453
type testCaseConfig struct {
1454-
nodeLabels map[string]string
1455-
includeNodes bool
1456-
expectedErr error
1457-
expectedCapacity map[corev1.ResourceName]int64
1458-
expectedNodeLabels map[string]string
1459-
expectedResourceSlice testResourceSlice
1454+
nodeLabels map[string]string
1455+
nodegroupLabels map[string]string
1456+
includeNodes bool
1457+
expectedErr error
1458+
expectedCapacity map[corev1.ResourceName]int64
1459+
expectedNodeLabels map[string]string
14601460
}
14611461

14621462
testCases := []struct {
@@ -1508,16 +1508,22 @@ func TestNodeGroupTemplateNodeInfo(t *testing.T) {
15081508
},
15091509
config: testCaseConfig{
15101510
expectedErr: nil,
1511+
nodegroupLabels: map[string]string{
1512+
"nodeGroupLabel": "value",
1513+
"anotherLabel": "anotherValue",
1514+
},
15111515
expectedCapacity: map[corev1.ResourceName]int64{
15121516
corev1.ResourceCPU: 2,
15131517
corev1.ResourceMemory: 2048 * 1024 * 1024,
15141518
corev1.ResourcePods: 110,
15151519
gpuapis.ResourceNvidiaGPU: 1,
15161520
},
15171521
expectedNodeLabels: map[string]string{
1522+
"kubernetes.io/hostname": "random value",
15181523
"kubernetes.io/os": "linux",
15191524
"kubernetes.io/arch": "arm64",
1520-
"kubernetes.io/hostname": "random value",
1525+
"nodeGroupLabel": "value",
1526+
"anotherLabel": "anotherValue",
15211527
"my-custom-label": "custom-value",
15221528
},
15231529
},
@@ -1579,6 +1585,12 @@ func TestNodeGroupTemplateNodeInfo(t *testing.T) {
15791585
}
15801586

15811587
test := func(t *testing.T, testConfig *testConfig, config testCaseConfig) {
1588+
if testConfig.machineDeployment != nil {
1589+
unstructured.SetNestedStringMap(testConfig.machineDeployment.Object, config.nodegroupLabels, "spec", "template", "spec", "metadata", "labels")
1590+
} else {
1591+
unstructured.SetNestedStringMap(testConfig.machineSet.Object, config.nodegroupLabels, "spec", "template", "spec", "metadata", "labels")
1592+
}
1593+
15821594
if config.includeNodes {
15831595
for i := range testConfig.nodes {
15841596
testConfig.nodes[i].SetLabels(config.nodeLabels)

cluster-autoscaler/cloudprovider/clusterapi/clusterapi_unstructured.go

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -242,37 +242,64 @@ func (r unstructuredScalableResource) MarkMachineForDeletion(machine *unstructur
242242
}
243243

244244
func (r unstructuredScalableResource) Labels() map[string]string {
245+
labels := make(map[string]string, 0)
246+
247+
newlabels, found, err := unstructured.NestedStringMap(r.unstructured.Object, "spec", "template", "spec", "metadata", "labels")
248+
if err != nil {
249+
return nil
250+
}
251+
if found {
252+
for k, v := range newlabels {
253+
labels[k] = v
254+
}
255+
}
256+
245257
annotations := r.unstructured.GetAnnotations()
246258
// annotation value of the form "key1=value1,key2=value2"
247259
if val, found := annotations[labelsKey]; found {
248-
labels := strings.Split(val, ",")
249-
kv := make(map[string]string, len(labels))
250-
for _, label := range labels {
260+
newlabels := strings.Split(val, ",")
261+
for _, label := range newlabels {
251262
split := strings.SplitN(label, "=", 2)
252263
if len(split) == 2 {
253-
kv[split[0]] = split[1]
264+
labels[split[0]] = split[1]
254265
}
255266
}
256-
return kv
257267
}
258-
return nil
268+
269+
return labels
259270
}
260271

261272
func (r unstructuredScalableResource) Taints() []apiv1.Taint {
273+
taints := make([]apiv1.Taint, 0)
274+
275+
newtaints, found, err := unstructured.NestedSlice(r.unstructured.Object, "spec", "template", "spec", "taints")
276+
if err != nil {
277+
return nil
278+
}
279+
if found {
280+
for _, t := range newtaints {
281+
if v, ok := t.(apiv1.Taint); ok {
282+
taints = append(taints, v)
283+
} else {
284+
klog.Warning("Unable to convert data to taint: %v", t)
285+
continue
286+
}
287+
}
288+
}
289+
262290
annotations := r.unstructured.GetAnnotations()
263291
// annotation value the form of "key1=value1:condition,key2=value2:condition"
264292
if val, found := annotations[taintsKey]; found {
265-
taints := strings.Split(val, ",")
266-
ret := make([]apiv1.Taint, 0, len(taints))
267-
for _, taintStr := range taints {
293+
newtaints := strings.Split(val, ",")
294+
for _, taintStr := range newtaints {
268295
taint, err := parseTaint(taintStr)
269296
if err == nil {
270-
ret = append(ret, taint)
297+
taints = append(taints, taint)
271298
}
272299
}
273-
return ret
274300
}
275-
return nil
301+
302+
return taints
276303
}
277304

278305
// A node group can scale from zero if it can inform about the CPU and memory

0 commit comments

Comments
 (0)