Skip to content

Commit 71e76c2

Browse files
committed
Review fixes
1 parent 9fa37ab commit 71e76c2

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

internal/controllers/machine/machine_controller_noderef.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ func propagateMachineTaintsToNode(node *corev1.Node, machineTaints []clusterv1.M
398398
}
399399

400400
// Ensure the taint is set on the node and has the correct value.
401-
if changedTaints := taints.EnsureNodeTaint(node, convertMachineTaintToCoreV1Taint(taint)); changedTaints {
401+
if changedTaints := ensureNodeTaintWithValue(node, convertMachineTaintToCoreV1Taint(taint)); changedTaints {
402402
changed = true
403403
}
404404
}
@@ -408,6 +408,10 @@ func propagateMachineTaintsToNode(node *corev1.Node, machineTaints []clusterv1.M
408408

409409
// Remove all identified taints from the node.
410410
for taintToDelete := range taintsToDelete {
411+
if taintToDelete == "" {
412+
continue
413+
}
414+
411415
splitted := strings.Split(taintToDelete, ":")
412416
if len(splitted) != 2 {
413417
return changed, fmt.Errorf("invalid taint format: %q", taintToDelete)
@@ -430,6 +434,29 @@ func propagateMachineTaintsToNode(node *corev1.Node, machineTaints []clusterv1.M
430434
return changed, nil
431435
}
432436

437+
// ensureNodeTaintWithValue makes sure the node has the Taint with the expected value.
438+
// It returns true if the taints are modified, false otherwise.
439+
func ensureNodeTaintWithValue(node *corev1.Node, taint corev1.Taint) bool {
440+
for i, currentTaint := range node.Spec.Taints {
441+
if !taint.MatchTaint(&currentTaint) {
442+
continue
443+
}
444+
445+
// Modify the taint if the value is different.
446+
if currentTaint.Value != taint.Value {
447+
node.Spec.Taints[i] = taint
448+
return true
449+
}
450+
451+
// The taint is already set and has the correct value.
452+
return false
453+
}
454+
455+
// Add the taint if not present.
456+
node.Spec.Taints = append(node.Spec.Taints, taint)
457+
return true
458+
}
459+
433460
// marshalMachineTaintsAnnotation marshals the tracking annotation value.
434461
func marshalMachineTaintsAnnotation(ownedTaints sets.Set[string]) string {
435462
taints := ownedTaints.UnsortedList()

internal/controllers/machine/machine_controller_noderef_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,9 +1589,18 @@ func Test_propagateMachineTaintsToNode(t *testing.T) {
15891589
Effect: corev1.TaintEffectNoSchedule,
15901590
Propagation: clusterv1.MachineTaintPropagationAlways,
15911591
}
1592+
15921593
transitionOnInitialization := transitionAlways
15931594
transitionOnInitialization.Propagation = clusterv1.MachineTaintPropagationOnInitialization
15941595

1596+
transitionExistingAlwaysNewValue := existingAlwaysTaint
1597+
transitionExistingAlwaysNewValue.Value = "transition-value-new"
1598+
1599+
transitionExistingAlwaysNewEffect := existingAlwaysTaint
1600+
transitionExistingAlwaysNewEffect.Effect = corev1.TaintEffectNoSchedule
1601+
1602+
externalNodeTaint := corev1.Taint{Key: "external-taint", Value: "external-value", Effect: corev1.TaintEffectNoExecute}
1603+
15951604
tests := []struct {
15961605
name string
15971606
node *corev1.Node
@@ -1642,6 +1651,15 @@ func Test_propagateMachineTaintsToNode(t *testing.T) {
16421651
expectedAnnotation: "added-always:NoSchedule",
16431652
expectChanged: true,
16441653
},
1654+
{
1655+
name: "Add missing but tracked Always taint, tracking annotation, no other taints",
1656+
node: builder.Node("").
1657+
WithAnnotations(map[string]string{clusterv1.TaintsFromMachineAnnotation: "existing-always:NoExecute"}).Build(),
1658+
machineTaints: []clusterv1.MachineTaint{existingAlwaysTaint},
1659+
expectedTaints: []corev1.Taint{convertMachineTaintToCoreV1Taint(existingAlwaysTaint)},
1660+
expectedAnnotation: "existing-always:NoExecute",
1661+
expectChanged: true,
1662+
},
16451663
{
16461664
name: "Delete Always taint, tracking annotation, no other taints",
16471665
node: builder.Node("").
@@ -1701,6 +1719,54 @@ func Test_propagateMachineTaintsToNode(t *testing.T) {
17011719
expectedAnnotation: "transition-taint:NoSchedule",
17021720
expectChanged: true,
17031721
},
1722+
{
1723+
name: "Transition Always taint to have a new value should change the value also on the node",
1724+
node: builder.Node("").
1725+
WithAnnotations(map[string]string{clusterv1.TaintsFromMachineAnnotation: "existing-always:NoExecute"}).
1726+
WithTaints(convertMachineTaintToCoreV1Taint(existingAlwaysTaint)).Build(),
1727+
machineTaints: []clusterv1.MachineTaint{transitionExistingAlwaysNewValue},
1728+
expectedTaints: []corev1.Taint{convertMachineTaintToCoreV1Taint(transitionExistingAlwaysNewValue)},
1729+
expectedAnnotation: "existing-always:NoExecute",
1730+
expectChanged: true,
1731+
},
1732+
{
1733+
name: "Transition Always taint to have a new effect should change the effect also on the node",
1734+
node: builder.Node("").
1735+
WithAnnotations(map[string]string{clusterv1.TaintsFromMachineAnnotation: "existing-always:NoExecute"}).
1736+
WithTaints(convertMachineTaintToCoreV1Taint(existingAlwaysTaint)).Build(),
1737+
machineTaints: []clusterv1.MachineTaint{transitionExistingAlwaysNewEffect},
1738+
expectedTaints: []corev1.Taint{convertMachineTaintToCoreV1Taint(transitionExistingAlwaysNewEffect)},
1739+
expectedAnnotation: "existing-always:NoSchedule",
1740+
expectChanged: true,
1741+
},
1742+
{
1743+
name: "Add missing taints, no tracking annotation, preserve other taints",
1744+
node: builder.Node("").
1745+
WithTaints(externalNodeTaint).Build(),
1746+
machineTaints: []clusterv1.MachineTaint{alwaysTaint, onInitializationTaint},
1747+
expectedTaints: []corev1.Taint{externalNodeTaint, convertMachineTaintToCoreV1Taint(alwaysTaint), convertMachineTaintToCoreV1Taint(onInitializationTaint)},
1748+
expectedAnnotation: "added-always:NoSchedule",
1749+
expectChanged: true,
1750+
},
1751+
{
1752+
name: "Adopt existing taint, no tracking annotation",
1753+
node: builder.Node("").
1754+
WithTaints(convertMachineTaintToCoreV1Taint(existingAlwaysTaint)).Build(),
1755+
machineTaints: []clusterv1.MachineTaint{existingAlwaysTaint},
1756+
expectedTaints: []corev1.Taint{convertMachineTaintToCoreV1Taint(existingAlwaysTaint)},
1757+
expectedAnnotation: "existing-always:NoExecute",
1758+
expectChanged: true,
1759+
},
1760+
{
1761+
name: "Recover from broken tracking annotation",
1762+
node: builder.Node("").
1763+
WithAnnotations(map[string]string{clusterv1.TaintsFromMachineAnnotation: "existing-always:NoExecute,"}).
1764+
WithTaints(convertMachineTaintToCoreV1Taint(existingAlwaysTaint)).Build(),
1765+
machineTaints: []clusterv1.MachineTaint{existingAlwaysTaint},
1766+
expectedTaints: []corev1.Taint{convertMachineTaintToCoreV1Taint(existingAlwaysTaint)},
1767+
expectedAnnotation: "existing-always:NoExecute",
1768+
expectChanged: true,
1769+
},
17041770
}
17051771
for _, tt := range tests {
17061772
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)