Skip to content

Commit 9220470

Browse files
authored
Merge pull request #8188 from towca/jtuznik/tni-fix
CA: stop mutating the result of .TemplateNodeInfo() in SanitizedTemplateNodeInfoFromNodeGroup
2 parents 606aef2 + 2f6c19a commit 9220470

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

cluster-autoscaler/simulator/node_info_utils.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,12 @@ func SanitizedTemplateNodeInfoFromNodeGroup(nodeGroup nodeGroupTemplateNodeInfoG
4949
if err != nil {
5050
return nil, errors.ToAutoscalerError(errors.CloudProviderError, err).AddPrefix("failed to obtain template NodeInfo from node group %q: ", nodeGroup.Id())
5151
}
52-
labels.UpdateDeprecatedLabels(baseNodeInfo.Node().ObjectMeta.Labels)
53-
54-
return SanitizedTemplateNodeInfoFromNodeInfo(baseNodeInfo, nodeGroup.Id(), daemonsets, true, taintConfig)
52+
sanitizedNodeInfo, aErr := SanitizedTemplateNodeInfoFromNodeInfo(baseNodeInfo, nodeGroup.Id(), daemonsets, true, taintConfig)
53+
if aErr != nil {
54+
return nil, aErr
55+
}
56+
labels.UpdateDeprecatedLabels(sanitizedNodeInfo.Node().Labels)
57+
return sanitizedNodeInfo, nil
5558
}
5659

5760
// SanitizedTemplateNodeInfoFromNodeInfo returns a template NodeInfo object based on a real example NodeInfo from the cluster. The template is sanitized, and only

cluster-autoscaler/simulator/node_info_utils_test.go

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ func TestSanitizedTemplateNodeInfoFromNodeGroup(t *testing.T) {
9494
exampleNode.Spec.Taints = []apiv1.Taint{
9595
{Key: taints.ToBeDeletedTaint, Value: "2312532423", Effect: apiv1.TaintEffectNoSchedule},
9696
}
97+
exampleNode.Labels = map[string]string{
98+
"custom": "label",
99+
apiv1.LabelInstanceTypeStable: "some-instance",
100+
apiv1.LabelTopologyRegion: "some-region",
101+
}
97102

98103
for _, tc := range []struct {
99104
testName string
@@ -156,7 +161,7 @@ func TestSanitizedTemplateNodeInfoFromNodeGroup(t *testing.T) {
156161
// Pass empty string as nameSuffix so that it's auto-determined from the sanitized templateNodeInfo, because
157162
// TemplateNodeInfoFromNodeGroupTemplate randomizes the suffix.
158163
// Pass non-empty expectedPods to verify that the set of pods is changed as expected (e.g. DS pods added, non-DS/deleted pods removed).
159-
if err := verifyNodeInfoSanitization(tc.nodeGroup.templateNodeInfoResult, templateNodeInfo, tc.wantPods, "template-node-for-"+tc.nodeGroup.id, "", nil); err != nil {
164+
if err := verifyNodeInfoSanitization(tc.nodeGroup.templateNodeInfoResult, templateNodeInfo, tc.wantPods, "template-node-for-"+tc.nodeGroup.id, "", true, nil); err != nil {
160165
t.Fatalf("TemplateNodeInfoFromExampleNodeInfo(): NodeInfo wasn't properly sanitized: %v", err)
161166
}
162167
})
@@ -168,6 +173,11 @@ func TestSanitizedTemplateNodeInfoFromNodeInfo(t *testing.T) {
168173
exampleNode.Spec.Taints = []apiv1.Taint{
169174
{Key: taints.ToBeDeletedTaint, Value: "2312532423", Effect: apiv1.TaintEffectNoSchedule},
170175
}
176+
exampleNode.Labels = map[string]string{
177+
"custom": "label",
178+
apiv1.LabelInstanceTypeStable: "some-instance",
179+
apiv1.LabelTopologyRegion: "some-region",
180+
}
171181

172182
testCases := []struct {
173183
name string
@@ -318,7 +328,7 @@ func TestSanitizedTemplateNodeInfoFromNodeInfo(t *testing.T) {
318328
// Pass empty string as nameSuffix so that it's auto-determined from the sanitized templateNodeInfo, because
319329
// TemplateNodeInfoFromExampleNodeInfo randomizes the suffix.
320330
// Pass non-empty expectedPods to verify that the set of pods is changed as expected (e.g. DS pods added, non-DS/deleted pods removed).
321-
if err := verifyNodeInfoSanitization(exampleNodeInfo, templateNodeInfo, tc.wantPods, "template-node-for-"+nodeGroupId, "", nil); err != nil {
331+
if err := verifyNodeInfoSanitization(exampleNodeInfo, templateNodeInfo, tc.wantPods, "template-node-for-"+nodeGroupId, "", false, nil); err != nil {
322332
t.Fatalf("TemplateNodeInfoFromExampleNodeInfo(): NodeInfo wasn't properly sanitized: %v", err)
323333
}
324334
})
@@ -333,6 +343,12 @@ func TestSanitizedNodeInfo(t *testing.T) {
333343
{Key: taints.ToBeDeletedTaint, Value: "2312532423", Effect: apiv1.TaintEffectNoSchedule},
334344
{Key: "a", Value: "b", Effect: apiv1.TaintEffectNoSchedule},
335345
}
346+
templateNode.Labels = map[string]string{
347+
"custom": "label",
348+
apiv1.LabelInstanceTypeStable: "some-instance",
349+
apiv1.LabelTopologyRegion: "some-region",
350+
}
351+
336352
pods := []*framework.PodInfo{
337353
{Pod: BuildTestPod("p1", 80, 0, WithNodeName(nodeName))},
338354
{Pod: BuildTestPod("p2", 80, 0, WithNodeName(nodeName))},
@@ -347,7 +363,7 @@ func TestSanitizedNodeInfo(t *testing.T) {
347363
// Verify that the taints are not sanitized (they should be sanitized in the template already).
348364
// Verify that the NodeInfo is sanitized using the template Node name as base.
349365
initialTaints := templateNodeInfo.Node().Spec.Taints
350-
if err := verifyNodeInfoSanitization(templateNodeInfo, freshNodeInfo, nil, templateNodeInfo.Node().Name, suffix, initialTaints); err != nil {
366+
if err := verifyNodeInfoSanitization(templateNodeInfo, freshNodeInfo, nil, templateNodeInfo.Node().Name, suffix, false, initialTaints); err != nil {
351367
t.Fatalf("FreshNodeInfoFromTemplateNodeInfo(): NodeInfo wasn't properly sanitized: %v", err)
352368
}
353369
}
@@ -358,9 +374,11 @@ func TestCreateSanitizedNodeInfo(t *testing.T) {
358374

359375
labelsNode := basicNode.DeepCopy()
360376
labelsNode.Labels = map[string]string{
361-
apiv1.LabelHostname: oldNodeName,
362-
"a": "b",
363-
"x": "y",
377+
apiv1.LabelHostname: oldNodeName,
378+
"a": "b",
379+
"x": "y",
380+
apiv1.LabelInstanceTypeStable: "some-instance",
381+
apiv1.LabelTopologyRegion: "some-region",
364382
}
365383

366384
taintsNode := basicNode.DeepCopy()
@@ -492,7 +510,7 @@ func TestCreateSanitizedNodeInfo(t *testing.T) {
492510
if err != nil {
493511
t.Fatalf("sanitizeNodeInfo(): want nil error, got %v", err)
494512
}
495-
if err := verifyNodeInfoSanitization(tc.nodeInfo, nodeInfo, nil, newNameBase, suffix, tc.wantTaints); err != nil {
513+
if err := verifyNodeInfoSanitization(tc.nodeInfo, nodeInfo, nil, newNameBase, suffix, false, tc.wantTaints); err != nil {
496514
t.Fatalf("sanitizeNodeInfo(): NodeInfo wasn't properly sanitized: %v", err)
497515
}
498516
})
@@ -507,7 +525,7 @@ func TestCreateSanitizedNodeInfo(t *testing.T) {
507525
//
508526
// If expectedPods is nil, the set of pods is expected not to change between initialNodeInfo and sanitizedNodeInfo. If the sanitization is
509527
// expected to change the set of pods, the expected set should be passed to expectedPods.
510-
func verifyNodeInfoSanitization(initialNodeInfo, sanitizedNodeInfo *framework.NodeInfo, expectedPods []*apiv1.Pod, nameBase, nameSuffix string, wantTaints []apiv1.Taint) error {
528+
func verifyNodeInfoSanitization(initialNodeInfo, sanitizedNodeInfo *framework.NodeInfo, expectedPods []*apiv1.Pod, nameBase, nameSuffix string, wantDeprecatedLabels bool, wantTaints []apiv1.Taint) error {
511529
if nameSuffix == "" {
512530
// Determine the suffix from the provided sanitized NodeInfo - it should be the last part of a dash-separated name.
513531
nameParts := strings.Split(sanitizedNodeInfo.Node().Name, "-")
@@ -527,7 +545,7 @@ func verifyNodeInfoSanitization(initialNodeInfo, sanitizedNodeInfo *framework.No
527545

528546
// Verification below assumes the same set of pods between initialNodeInfo and sanitizedNodeInfo.
529547
wantNodeName := fmt.Sprintf("%s-%s", nameBase, nameSuffix)
530-
if err := verifySanitizedNode(initialNodeInfo.Node(), sanitizedNodeInfo.Node(), wantNodeName, wantTaints); err != nil {
548+
if err := verifySanitizedNode(initialNodeInfo.Node(), sanitizedNodeInfo.Node(), wantNodeName, wantDeprecatedLabels, wantTaints); err != nil {
531549
return err
532550
}
533551
if err := verifySanitizedNodeResourceSlices(initialNodeInfo.LocalResourceSlices, sanitizedNodeInfo.LocalResourceSlices, nameSuffix); err != nil {
@@ -540,7 +558,7 @@ func verifyNodeInfoSanitization(initialNodeInfo, sanitizedNodeInfo *framework.No
540558
return nil
541559
}
542560

543-
func verifySanitizedNode(initialNode, sanitizedNode *apiv1.Node, wantNodeName string, wantTaints []apiv1.Taint) error {
561+
func verifySanitizedNode(initialNode, sanitizedNode *apiv1.Node, wantNodeName string, wantDeprecatedLabels bool, wantTaints []apiv1.Taint) error {
544562
if gotName := sanitizedNode.Name; gotName != wantNodeName {
545563
return fmt.Errorf("want sanitized Node name %q, got %q", wantNodeName, gotName)
546564
}
@@ -553,6 +571,9 @@ func verifySanitizedNode(initialNode, sanitizedNode *apiv1.Node, wantNodeName st
553571
wantLabels[k] = v
554572
}
555573
wantLabels[apiv1.LabelHostname] = wantNodeName
574+
if wantDeprecatedLabels {
575+
labels.UpdateDeprecatedLabels(wantLabels)
576+
}
556577
if diff := cmp.Diff(wantLabels, sanitizedNode.Labels); diff != "" {
557578
return fmt.Errorf("sanitized Node labels unexpected, diff (-want +got): %s", diff)
558579
}

0 commit comments

Comments
 (0)