Skip to content

Commit

Permalink
UPSTREAM: <carry>: add machine api label and taint functionality
Browse files Browse the repository at this point in the history
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
  • Loading branch information
elmiko authored and cloud-team-rebase-bot committed Oct 24, 2024
1 parent 37a9667 commit 3439c82
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,7 @@ func TestNodeGroupTemplateNodeInfo(t *testing.T) {

type testCaseConfig struct {
nodeLabels map[string]string
nodegroupLabels map[string]string
includeNodes bool
expectedErr error
expectedCapacity map[corev1.ResourceName]int64
Expand Down Expand Up @@ -1364,16 +1365,22 @@ func TestNodeGroupTemplateNodeInfo(t *testing.T) {
},
config: testCaseConfig{
expectedErr: nil,
nodegroupLabels: map[string]string{
"nodeGroupLabel": "value",
"anotherLabel": "anotherValue",
},
expectedCapacity: map[corev1.ResourceName]int64{
corev1.ResourceCPU: 2,
corev1.ResourceMemory: 2048 * 1024 * 1024,
corev1.ResourcePods: 110,
gpuapis.ResourceNvidiaGPU: 1,
},
expectedNodeLabels: map[string]string{
"kubernetes.io/hostname": "random value",
"kubernetes.io/os": "linux",
"kubernetes.io/arch": "arm64",
"kubernetes.io/hostname": "random value",
"nodeGroupLabel": "value",
"anotherLabel": "anotherValue",
"my-custom-label": "custom-value",
},
},
Expand Down Expand Up @@ -1408,6 +1415,12 @@ func TestNodeGroupTemplateNodeInfo(t *testing.T) {
}

test := func(t *testing.T, testConfig *testConfig, config testCaseConfig) {
if testConfig.machineDeployment != nil {
unstructured.SetNestedStringMap(testConfig.machineDeployment.Object, config.nodegroupLabels, "spec", "template", "spec", "metadata", "labels")
} else {
unstructured.SetNestedStringMap(testConfig.machineSet.Object, config.nodegroupLabels, "spec", "template", "spec", "metadata", "labels")
}

if config.includeNodes {
for i := range testConfig.nodes {
testConfig.nodes[i].SetLabels(config.nodeLabels)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,37 +175,64 @@ func (r unstructuredScalableResource) MarkMachineForDeletion(machine *unstructur
}

func (r unstructuredScalableResource) Labels() map[string]string {
labels := make(map[string]string, 0)

newlabels, found, err := unstructured.NestedStringMap(r.unstructured.Object, "spec", "template", "spec", "metadata", "labels")
if err != nil {
return nil
}
if found {
for k, v := range newlabels {
labels[k] = v
}
}

annotations := r.unstructured.GetAnnotations()
// annotation value of the form "key1=value1,key2=value2"
if val, found := annotations[labelsKey]; found {
labels := strings.Split(val, ",")
kv := make(map[string]string, len(labels))
for _, label := range labels {
newlabels := strings.Split(val, ",")
for _, label := range newlabels {
split := strings.SplitN(label, "=", 2)
if len(split) == 2 {
kv[split[0]] = split[1]
labels[split[0]] = split[1]
}
}
return kv
}
return nil

return labels
}

func (r unstructuredScalableResource) Taints() []apiv1.Taint {
taints := make([]apiv1.Taint, 0)

newtaints, found, err := unstructured.NestedSlice(r.unstructured.Object, "spec", "template", "spec", "taints")
if err != nil {
return nil
}
if found {
for _, t := range newtaints {
if v, ok := t.(apiv1.Taint); ok {
taints = append(taints, v)
} else {
klog.Warning("Unable to convert data to taint: %v", t)
continue
}
}
}

annotations := r.unstructured.GetAnnotations()
// annotation value the form of "key1=value1:condition,key2=value2:condition"
if val, found := annotations[taintsKey]; found {
taints := strings.Split(val, ",")
ret := make([]apiv1.Taint, 0, len(taints))
for _, taintStr := range taints {
newtaints := strings.Split(val, ",")
for _, taintStr := range newtaints {
taint, err := parseTaint(taintStr)
if err == nil {
ret = append(ret, taint)
taints = append(taints, taint)
}
}
return ret
}
return nil

return taints
}

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

0 comments on commit 3439c82

Please sign in to comment.