Skip to content

Commit 9af2aac

Browse files
committed
fixup! feat: Support removing all taints
1 parent 3dd874d commit 9af2aac

File tree

4 files changed

+84
-20
lines changed

4 files changed

+84
-20
lines changed

docs/content/customization/generic/taints.md

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This customization will be available when the
1010

1111
## Example
1212

13+
### Control plane taints
14+
1315
To configure taints for the control plane nodes, specify the following configuration:
1416

1517
```yaml
@@ -29,19 +31,39 @@ spec:
2931
value: some-value
3032
```
3133
32-
{{% alert title="Default control plane taints" color="warning" %}}
34+
#### Default control-plane taint applied by kubeadm
35+
3336
When using this customization, the default taint added by kubeadm to the control plane nodes will not be added unless
3437
explicitly specified as well.
3538
36-
To add the default taint to the control-plane, add the following taint along with any custom taints you wish to add to
37-
the control-plane taints:
39+
To add the default taint back to the control-plane, add the following taint along with any custom taints you wish to add
40+
to the control-plane taints:
3841
3942
```yaml
4043
- key: node-role.kubernetes.io/control-plane
4144
effect: NoSchedule
4245
```
4346
44-
{{% /alert %}}
47+
#### Removing all taints from control-plane nodes
48+
49+
To remove the default control plane taints set by kubeadm (and therefore allow scheduling to control plane nodes without
50+
adding explicit tolerations to your pod manifests), set `controlPlane.taints` to an empty array:
51+
52+
```yaml
53+
apiVersion: cluster.x-k8s.io/v1beta1
54+
kind: Cluster
55+
metadata:
56+
name: <NAME>
57+
spec:
58+
topology:
59+
variables:
60+
- name: clusterConfig
61+
value:
62+
controlPlane:
63+
taints: []
64+
```
65+
66+
### Worker node taints
4567

4668
Taints for individual nodepools can be configured similarly:
4769

pkg/handlers/generic/mutation/taints/inject_controlplane.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,13 @@ func (h *taintsControlPlanePatchHandler) Mutate(
9191
if obj.Spec.Template.Spec.KubeadmConfigSpec.JoinConfiguration != nil {
9292
obj.Spec.Template.Spec.KubeadmConfigSpec.JoinConfiguration = &bootstrapv1.JoinConfiguration{}
9393
}
94-
coreTaints := toCoreTaints(taintsVar)
95-
96-
obj.Spec.Template.Spec.KubeadmConfigSpec.InitConfiguration.NodeRegistration.Taints = append(
94+
obj.Spec.Template.Spec.KubeadmConfigSpec.InitConfiguration.NodeRegistration.Taints = toCoreTaints(
9795
obj.Spec.Template.Spec.KubeadmConfigSpec.InitConfiguration.NodeRegistration.Taints,
98-
coreTaints...,
96+
taintsVar,
9997
)
100-
obj.Spec.Template.Spec.KubeadmConfigSpec.JoinConfiguration.NodeRegistration.Taints = append(
98+
obj.Spec.Template.Spec.KubeadmConfigSpec.JoinConfiguration.NodeRegistration.Taints = toCoreTaints(
10199
obj.Spec.Template.Spec.KubeadmConfigSpec.JoinConfiguration.NodeRegistration.Taints,
102-
coreTaints...,
100+
taintsVar,
103101
)
104102

105103
return nil

pkg/handlers/generic/mutation/taints/inject_controlplane_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,33 @@ var _ = Describe("Generate taints patches for Control Plane", func() {
5555
),
5656
}},
5757
},
58+
{
59+
Name: "taints for control plane set to empty slice to remove default taints",
60+
Vars: []runtimehooksv1.Variable{
61+
capitest.VariableWithValue(
62+
v1alpha1.ClusterConfigVariableName,
63+
[]v1alpha1.Taint{},
64+
v1alpha1.ControlPlaneConfigVariableName,
65+
VariableName,
66+
),
67+
},
68+
RequestItem: request.NewKubeadmControlPlaneTemplateRequestItem(""),
69+
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{{
70+
Operation: "add",
71+
Path: "/spec/template/spec/kubeadmConfigSpec/initConfiguration/nodeRegistration/taints",
72+
ValueMatcher: gomega.SatisfyAll(
73+
gomega.Not(gomega.BeNil()),
74+
gomega.BeEmpty(),
75+
),
76+
}, {
77+
Operation: "add",
78+
Path: "/spec/template/spec/kubeadmConfigSpec/joinConfiguration/nodeRegistration/taints",
79+
ValueMatcher: gomega.SatisfyAll(
80+
gomega.Not(gomega.BeNil()),
81+
gomega.BeEmpty(),
82+
),
83+
}},
84+
},
5885
}
5986

6087
// create test node for each case

pkg/handlers/generic/mutation/taints/inject_worker.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,37 @@ func (h *taintsWorkerPatchHandler) Mutate(
9090
if obj.Spec.Template.Spec.JoinConfiguration != nil {
9191
obj.Spec.Template.Spec.JoinConfiguration = &bootstrapv1.JoinConfiguration{}
9292
}
93-
obj.Spec.Template.Spec.JoinConfiguration.NodeRegistration.Taints = append(
93+
obj.Spec.Template.Spec.JoinConfiguration.NodeRegistration.Taints = toCoreTaints(
9494
obj.Spec.Template.Spec.JoinConfiguration.NodeRegistration.Taints,
95-
toCoreTaints(taintsVar)...,
95+
taintsVar,
9696
)
9797
return nil
9898
})
9999
}
100100

101-
func toCoreTaints(taints []v1alpha1.Taint) []v1.Taint {
102-
return lo.Map(taints, func(t v1alpha1.Taint, _ int) v1.Taint {
103-
return v1.Taint{
104-
Key: t.Key,
105-
Effect: v1.TaintEffect(t.Effect),
106-
Value: t.Value,
107-
}
108-
})
101+
func toCoreTaints(existingTaints []v1.Taint, newTaints []v1alpha1.Taint) []v1.Taint {
102+
var newCoreTaints []v1.Taint
103+
// Only initialize newCoreTaints if newTaints is not nil otherwise not setting the value at all will
104+
// end up with an empty (but initialized) slice which will remove all taints, which is not the desired behavior.
105+
if newTaints != nil {
106+
newCoreTaints = lo.Map(newTaints, func(t v1alpha1.Taint, _ int) v1.Taint {
107+
return v1.Taint{
108+
Key: t.Key,
109+
Effect: v1.TaintEffect(t.Effect),
110+
Value: t.Value,
111+
}
112+
})
113+
}
114+
115+
switch {
116+
// If no new taints then return existing taints.
117+
case newTaints == nil:
118+
return existingTaints
119+
// If no existing taints then return new taints.
120+
case existingTaints == nil:
121+
return newCoreTaints
122+
// If both existing and new taints are present then append new taints to existing taints.
123+
default:
124+
return append(existingTaints, newCoreTaints...)
125+
}
109126
}

0 commit comments

Comments
 (0)