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].

[0]: kubernetes#5249
  • Loading branch information
elmiko committed Feb 16, 2023
1 parent 78493a8 commit f166a59
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
Expand Up @@ -1281,6 +1281,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 @@ -1319,11 +1320,9 @@ func TestNodeGroupTemplateNodeInfo(t *testing.T) {
gpuapis.ResourceNvidiaGPU: 1,
},
expectedNodeLabels: map[string]string{
"kubernetes.io/os": "linux",
"beta.kubernetes.io/os": "linux",
"kubernetes.io/arch": "amd64",
"beta.kubernetes.io/arch": "amd64",
"kubernetes.io/hostname": "random value",
"kubernetes.io/os": "linux",
"kubernetes.io/arch": "amd64",
"kubernetes.io/hostname": "random value",
},
},
},
Expand All @@ -1345,12 +1344,11 @@ func TestNodeGroupTemplateNodeInfo(t *testing.T) {
corev1.ResourcePods: 110,
},
expectedNodeLabels: map[string]string{
"kubernetes.io/os": "linux",
"beta.kubernetes.io/os": "linux",
"kubernetes.io/arch": "amd64",
"beta.kubernetes.io/arch": "amd64",
"nodeGroupLabel": "value",
"anotherLabel": "anotherValue",
"kubernetes.io/hostname": "random value",
"kubernetes.io/os": "linux",
"kubernetes.io/arch": "amd64",
"nodeGroupLabel": "value",
"anotherLabel": "anotherValue",
},
},
},
Expand Down Expand Up @@ -1384,6 +1382,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 Expand Up @@ -1442,8 +1446,6 @@ func TestNodeGroupTemplateNodeInfo(t *testing.T) {
} else {
t.Errorf("Expected node label %q to exist in node", key)
}
if value != config.expectedNodeLabels[key] {
}
}
}
}
Expand Down
Expand Up @@ -172,16 +172,28 @@ func (r unstructuredScalableResource) MarkMachineForDeletion(machine *unstructur
}

func (r unstructuredScalableResource) Labels() map[string]string {
// TODO implement this once the community has decided how they will handle labels
// this issue is related, https://github.com/kubernetes-sigs/cluster-api/issues/7006

return nil
labels, found, err := unstructured.NestedStringMap(r.unstructured.Object, "spec", "template", "spec", "metadata", "labels")
if !found || err != nil {
return nil
}
return labels
}

func (r unstructuredScalableResource) Taints() []apiv1.Taint {
// TODO implement this once the community has decided how they will handle taints

return nil
taints, found, err := unstructured.NestedSlice(r.unstructured.Object, "spec", "template", "spec", "taints")
if !found || err != nil {
return nil
}
ret := make([]apiv1.Taint, len(taints))
for i, t := range taints {
if v, ok := t.(apiv1.Taint); ok {
ret[i] = v
} else {
// if we cannot convert the interface to a Taint, return early with zero value
return nil
}
}
return ret
}

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

0 comments on commit f166a59

Please sign in to comment.