Skip to content

Commit 174bca0

Browse files
authored
Merge pull request #4147 from k8s-infra-cherrypick-robot/cherry-pick-4122-to-release-1.11
[release-1.11] fix NodeTaints and NodeLabels return type for AzureManagedMachinePools
2 parents 214f458 + 832f208 commit 174bca0

File tree

2 files changed

+99
-19
lines changed

2 files changed

+99
-19
lines changed

azure/services/agentpools/spec.go

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -388,40 +388,53 @@ func (s *AgentPoolSpec) Parameters(ctx context.Context, existing interface{}) (p
388388

389389
// mergeSystemNodeLabels appends any kubernetes.azure.com-prefixed labels from the AKS label set
390390
// into the local capz label set.
391-
func mergeSystemNodeLabels(capz, aks map[string]*string) map[string]*string {
392-
ret := capz
393-
if ret == nil {
391+
func mergeSystemNodeLabels(capzLabels, aksLabels map[string]*string) map[string]*string {
392+
// if both are nil, return nil for no change
393+
if capzLabels == nil && aksLabels == nil {
394+
return nil
395+
}
396+
397+
// Either one of the capzLabels or aksLabels is nil.
398+
// Prioritize capzLabels if it is not nil.
399+
var ret map[string]*string
400+
if capzLabels != nil {
401+
ret = capzLabels
402+
} else {
394403
ret = make(map[string]*string)
395404
}
405+
396406
// Look for labels returned from the AKS node pool API that begin with kubernetes.azure.com
397-
for aksNodeLabelKey := range aks {
407+
for aksNodeLabelKey := range aksLabels {
398408
if azureutil.IsAzureSystemNodeLabelKey(aksNodeLabelKey) {
399-
ret[aksNodeLabelKey] = aks[aksNodeLabelKey]
409+
ret[aksNodeLabelKey] = aksLabels[aksNodeLabelKey]
400410
}
401411
}
402-
// Preserve nil-ness of capz
403-
if capz == nil && len(ret) == 0 {
404-
ret = nil
405-
}
412+
413+
// if no labels are found, ret will be an empty map to clear out the NodeLabels at AKS API
406414
return ret
407415
}
408416

409417
// mergeSystemNodeTaints appends any kubernetes.azure.com-prefixed taints from the AKS taint set
410418
// into the local capz taint set.
411-
func mergeSystemNodeTaints(capz, aks []*string) []*string {
412-
ret := capz
413-
if ret == nil {
419+
func mergeSystemNodeTaints(capzNodeTaints, aksNodeTaints []*string) []*string {
420+
if capzNodeTaints == nil && aksNodeTaints == nil {
421+
return nil
422+
}
423+
424+
var ret []*string
425+
if capzNodeTaints != nil {
426+
ret = capzNodeTaints
427+
} else {
414428
ret = make([]*string, 0)
415429
}
430+
416431
// Look for taints returned from the AKS node pool API that begin with kubernetes.azure.com
417-
for _, aksNodeTaint := range aks {
432+
for _, aksNodeTaint := range aksNodeTaints {
418433
if azureutil.IsAzureSystemNodeLabelKey(*aksNodeTaint) {
419434
ret = append(ret, aksNodeTaint)
420435
}
421436
}
422-
// Preserve nil-ness of capz
423-
if capz == nil && len(ret) == 0 {
424-
ret = nil
425-
}
437+
438+
// if no taints are found, ret will be an empty array to clear out the nodeTaints at AKS API
426439
return ret
427440
}

azure/services/agentpools/spec_test.go

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ func sdkWithNodeTaints(nodeTaints []*string) func(*armcontainerservice.AgentPool
156156
}
157157
}
158158

159+
func sdkWithNodeLabels(nodeLabels map[string]*string) func(*armcontainerservice.AgentPool) {
160+
return func(pool *armcontainerservice.AgentPool) {
161+
pool.Properties.NodeLabels = nodeLabels
162+
}
163+
}
164+
159165
func TestParameters(t *testing.T) {
160166
testcases := []struct {
161167
name string
@@ -364,6 +370,27 @@ func TestParameters(t *testing.T) {
364370
expected: nil,
365371
expectedError: nil,
366372
},
373+
{
374+
name: "difference in system node labels with empty label at CAPZ and non-nil label at AKS should preserve AKS K8s label",
375+
spec: fakeAgentPool(
376+
func(pool *AgentPoolSpec) {
377+
pool.NodeLabels = nil
378+
},
379+
),
380+
existing: sdkFakeAgentPool(
381+
func(pool *armcontainerservice.AgentPool) {
382+
pool.Properties.NodeLabels = map[string]*string{
383+
"fake-label": ptr.To("fake-value"),
384+
"kubernetes.azure.com/scalesetpriority": ptr.To("spot"),
385+
}
386+
},
387+
sdkWithProvisioningState("Succeeded"),
388+
),
389+
expected: sdkFakeAgentPool(sdkWithNodeLabels(map[string]*string{
390+
"kubernetes.azure.com/scalesetpriority": ptr.To("spot"),
391+
})),
392+
expectedError: nil,
393+
},
367394
{
368395
name: "difference in non-system node taints with empty taints should trigger update",
369396
spec: fakeAgentPool(
@@ -377,7 +404,7 @@ func TestParameters(t *testing.T) {
377404
},
378405
sdkWithProvisioningState("Succeeded"),
379406
),
380-
expected: sdkFakeAgentPool(sdkWithNodeTaints(nil)),
407+
expected: sdkFakeAgentPool(sdkWithNodeTaints(make([]*string, 0))),
381408
expectedError: nil,
382409
},
383410
{
@@ -396,6 +423,25 @@ func TestParameters(t *testing.T) {
396423
expected: nil,
397424
expectedError: nil,
398425
},
426+
{
427+
name: "difference in system node taints with empty taints at CAPZ and non-nil taints at AKS should preserve K8s taints",
428+
spec: fakeAgentPool(
429+
func(pool *AgentPoolSpec) {
430+
pool.NodeTaints = nil
431+
},
432+
),
433+
existing: sdkFakeAgentPool(
434+
func(pool *armcontainerservice.AgentPool) {
435+
pool.Properties.NodeTaints = []*string{
436+
ptr.To(azureutil.AzureSystemNodeLabelPrefix + "-fake-taint"),
437+
ptr.To("fake-old-taint"),
438+
}
439+
},
440+
sdkWithProvisioningState("Succeeded"),
441+
),
442+
expected: sdkFakeAgentPool(sdkWithNodeTaints([]*string{ptr.To(azureutil.AzureSystemNodeLabelPrefix + "-fake-taint")})),
443+
expectedError: nil,
444+
},
399445
{
400446
name: "parameters with an existing agent pool and update needed on node taints",
401447
spec: fakeAgentPool(),
@@ -437,6 +483,18 @@ func TestParameters(t *testing.T) {
437483
expected: nil,
438484
expectedError: nil,
439485
},
486+
{
487+
name: "empty node labels should not trigger an update",
488+
spec: fakeAgentPool(
489+
func(pool *AgentPoolSpec) { pool.NodeLabels = nil },
490+
),
491+
existing: sdkFakeAgentPool(
492+
func(pool *armcontainerservice.AgentPool) { pool.Properties.NodeLabels = nil },
493+
sdkWithProvisioningState("Succeeded"),
494+
),
495+
expected: nil,
496+
expectedError: nil,
497+
},
440498
}
441499
for _, tc := range testcases {
442500
tc := tc
@@ -493,7 +551,16 @@ func TestMergeSystemNodeLabels(t *testing.T) {
493551
"foo": ptr.To("bar"),
494552
"hello": ptr.To("world"),
495553
},
496-
expected: nil,
554+
expected: map[string]*string{},
555+
},
556+
{
557+
name: "labels are nil when both the capz and aks labels are nil",
558+
capzLabels: nil,
559+
aksLabels: map[string]*string{
560+
"foo": ptr.To("bar"),
561+
"hello": ptr.To("world"),
562+
},
563+
expected: map[string]*string{},
497564
},
498565
{
499566
name: "delete one label",

0 commit comments

Comments
 (0)