Skip to content

Commit 19e0071

Browse files
elmikojoelsmith
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 c7b33de commit 19e0071

File tree

2 files changed

+53
-13
lines changed

2 files changed

+53
-13
lines changed

cluster-autoscaler/cloudprovider/clusterapi/clusterapi_nodegroup_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1492,6 +1492,7 @@ func TestNodeGroupTemplateNodeInfo(t *testing.T) {
14921492

14931493
type testCaseConfig struct {
14941494
nodeLabels map[string]string
1495+
nodegroupLabels map[string]string
14951496
includeNodes bool
14961497
expectedErr error
14971498
expectedCapacity map[corev1.ResourceName]int64
@@ -1548,16 +1549,22 @@ func TestNodeGroupTemplateNodeInfo(t *testing.T) {
15481549
},
15491550
config: testCaseConfig{
15501551
expectedErr: nil,
1552+
nodegroupLabels: map[string]string{
1553+
"nodeGroupLabel": "value",
1554+
"anotherLabel": "anotherValue",
1555+
},
15511556
expectedCapacity: map[corev1.ResourceName]int64{
15521557
corev1.ResourceCPU: 2,
15531558
corev1.ResourceMemory: 2048 * 1024 * 1024,
15541559
corev1.ResourcePods: 110,
15551560
gpuapis.ResourceNvidiaGPU: 1,
15561561
},
15571562
expectedNodeLabels: map[string]string{
1563+
"kubernetes.io/hostname": "random value",
15581564
"kubernetes.io/os": "linux",
15591565
"kubernetes.io/arch": "arm64",
1560-
"kubernetes.io/hostname": "random value",
1566+
"nodeGroupLabel": "value",
1567+
"anotherLabel": "anotherValue",
15611568
"my-custom-label": "custom-value",
15621569
},
15631570
},
@@ -1619,6 +1626,12 @@ func TestNodeGroupTemplateNodeInfo(t *testing.T) {
16191626
}
16201627

16211628
test := func(t *testing.T, testConfig *TestConfig, config testCaseConfig) {
1629+
if testConfig.machineDeployment != nil {
1630+
unstructured.SetNestedStringMap(testConfig.machineDeployment.Object, config.nodegroupLabels, "spec", "template", "spec", "metadata", "labels")
1631+
} else {
1632+
unstructured.SetNestedStringMap(testConfig.machineSet.Object, config.nodegroupLabels, "spec", "template", "spec", "metadata", "labels")
1633+
}
1634+
16221635
if config.includeNodes {
16231636
for i := range testConfig.nodes {
16241637
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
@@ -199,37 +199,64 @@ func (r unstructuredScalableResource) MarkMachineForDeletion(machine *unstructur
199199
}
200200

201201
func (r unstructuredScalableResource) Labels() map[string]string {
202+
labels := make(map[string]string, 0)
203+
204+
newlabels, found, err := unstructured.NestedStringMap(r.unstructured.Object, "spec", "template", "spec", "metadata", "labels")
205+
if err != nil {
206+
return nil
207+
}
208+
if found {
209+
for k, v := range newlabels {
210+
labels[k] = v
211+
}
212+
}
213+
202214
annotations := r.unstructured.GetAnnotations()
203215
// annotation value of the form "key1=value1,key2=value2"
204216
if val, found := annotations[labelsKey]; found {
205-
labels := strings.Split(val, ",")
206-
kv := make(map[string]string, len(labels))
207-
for _, label := range labels {
217+
newlabels := strings.Split(val, ",")
218+
for _, label := range newlabels {
208219
split := strings.SplitN(label, "=", 2)
209220
if len(split) == 2 {
210-
kv[split[0]] = split[1]
221+
labels[split[0]] = split[1]
211222
}
212223
}
213-
return kv
214224
}
215-
return nil
225+
226+
return labels
216227
}
217228

218229
func (r unstructuredScalableResource) Taints() []apiv1.Taint {
230+
taints := make([]apiv1.Taint, 0)
231+
232+
newtaints, found, err := unstructured.NestedSlice(r.unstructured.Object, "spec", "template", "spec", "taints")
233+
if err != nil {
234+
return nil
235+
}
236+
if found {
237+
for _, t := range newtaints {
238+
if v, ok := t.(apiv1.Taint); ok {
239+
taints = append(taints, v)
240+
} else {
241+
klog.Warning("Unable to convert data to taint: %v", t)
242+
continue
243+
}
244+
}
245+
}
246+
219247
annotations := r.unstructured.GetAnnotations()
220248
// annotation value the form of "key1=value1:condition,key2=value2:condition"
221249
if val, found := annotations[taintsKey]; found {
222-
taints := strings.Split(val, ",")
223-
ret := make([]apiv1.Taint, 0, len(taints))
224-
for _, taintStr := range taints {
250+
newtaints := strings.Split(val, ",")
251+
for _, taintStr := range newtaints {
225252
taint, err := parseTaint(taintStr)
226253
if err == nil {
227-
ret = append(ret, taint)
254+
taints = append(taints, taint)
228255
}
229256
}
230-
return ret
231257
}
232-
return nil
258+
259+
return taints
233260
}
234261

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

0 commit comments

Comments
 (0)