Skip to content

Commit 6d271fe

Browse files
committed
Review changes v2
1 parent 1a3cb80 commit 6d271fe

File tree

5 files changed

+216
-135
lines changed

5 files changed

+216
-135
lines changed

api/core/v1beta2/common_types.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,15 +412,22 @@ func (r *ContractVersionedObjectReference) GroupKind() schema.GroupKind {
412412
// MachineTaint defines a taint equivalent to corev1.Taint, but additionally having a propagation field.
413413
type MachineTaint struct {
414414
// key is the taint key to be applied to a node.
415+
// Must be a valid qualified name of maximum size 63 characters
416+
// with an optional subdomain prefix of maximum size 253 characters,
417+
// separated by a `/`.
415418
// +required
416419
// +kubebuilder:validation:MinLength=1
417-
// +kubebuilder:validation:MaxLength=253
420+
// +kubebuilder:validation:MaxLength=317
421+
// +kubebuilder:validation:Pattern=^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*\/)?([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]$
422+
// +kubebuilder:validation:XValidation:rule="self.contains('/') ? ( self.split('/') [0].size() <= 253 && self.split('/') [1].size() <= 63 && self.split('/').size() == 2 ) : self.size() <= 63",message="key must be a valid qualified name of max size 63 characters with an optional subdomain prefix of max size 253 characters"
418423
Key string `json:"key,omitempty"`
419424

420425
// value is the taint value corresponding to the taint key.
426+
// It must be a valid label value of maximum size 63 characters.
421427
// +optional
422428
// +kubebuilder:validation:MinLength=1
423429
// +kubebuilder:validation:MaxLength=63
430+
// +kubebuilder:validation:Pattern=^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?$
424431
Value string `json:"value,omitempty"`
425432

426433
// effect is the effect for the taint. Valid values are NoSchedule, PreferNoSchedule and NoExecute.

internal/controllers/machineset/machineset_controller_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ var _ reconcile.Reconciler = &Reconciler{}
6363

6464
func TestMachineSetReconciler(t *testing.T) {
6565
setup := func(t *testing.T, g *WithT) (*corev1.Namespace, *clusterv1.Cluster) {
66-
utilfeature.SetFeatureGateDuringTest(t, feature.Gates, feature.MachineTaintPropagation, true)
67-
6866
t.Helper()
6967

68+
utilfeature.SetFeatureGateDuringTest(t, feature.Gates, feature.MachineTaintPropagation, true)
69+
7070
t.Log("Creating the namespace")
7171
ns, err := env.CreateNamespace(ctx, "test-machine-set-reconciler")
7272
g.Expect(err).ToNot(HaveOccurred())

internal/webhooks/machine.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ import (
2222
"strings"
2323

2424
apierrors "k8s.io/apimachinery/pkg/api/errors"
25-
metav1validation "k8s.io/apimachinery/pkg/apis/meta/v1/validation"
2625
"k8s.io/apimachinery/pkg/runtime"
27-
"k8s.io/apimachinery/pkg/util/validation"
2826
"k8s.io/apimachinery/pkg/util/validation/field"
2927
"k8s.io/utils/ptr"
3028
ctrl "sigs.k8s.io/controller-runtime"
@@ -160,16 +158,6 @@ func validateMachineTaints(taints []clusterv1.MachineTaint, taintsPath *field.Pa
160158
for i, taint := range taints {
161159
idxPath := taintsPath.Index(i)
162160

163-
// Validate key syntax.
164-
if errs := metav1validation.ValidateLabelName(taint.Key, idxPath.Child("key")); len(errs) > 0 {
165-
allErrs = append(allErrs, errs...)
166-
}
167-
168-
// Validate value syntax.
169-
if errs := validation.IsValidLabelValue(taint.Value); len(errs) > 0 {
170-
allErrs = append(allErrs, field.Invalid(idxPath.Child("value"), taint.Value, strings.Join(errs, ";")))
171-
}
172-
173161
// The following validations uses a switch statement, because if one of them matches, then the others won't.
174162

175163
switch {

internal/webhooks/machine_test.go

Lines changed: 0 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,11 @@ import (
2020
"testing"
2121

2222
. "github.com/onsi/gomega"
23-
corev1 "k8s.io/api/core/v1"
2423
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25-
utilfeature "k8s.io/component-base/featuregate/testing"
2624
"k8s.io/utils/ptr"
2725

2826
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
29-
"sigs.k8s.io/cluster-api/feature"
3027
"sigs.k8s.io/cluster-api/internal/webhooks/util"
31-
"sigs.k8s.io/cluster-api/util/test/builder"
3228
)
3329

3430
func TestMachineDefault(t *testing.T) {
@@ -229,119 +225,3 @@ func TestMachineVersionValidation(t *testing.T) {
229225
})
230226
}
231227
}
232-
233-
func TestMachineTaintValidation(t *testing.T) {
234-
m := builder.Machine("default", "machine1").
235-
WithBootstrapTemplate(builder.BootstrapTemplate("default", "bootstrap-template").Build())
236-
webhook := &Machine{}
237-
238-
tests := []struct {
239-
name string
240-
machine *clusterv1.Machine
241-
featureEnabled bool
242-
expectErr bool
243-
}{
244-
{
245-
name: "should allow empty taints with feature gate disabled",
246-
featureEnabled: false,
247-
machine: m.DeepCopy().Build(),
248-
expectErr: false,
249-
},
250-
{
251-
name: "should allow empty taints with feature gate enabled",
252-
featureEnabled: true,
253-
machine: m.DeepCopy().Build(),
254-
expectErr: false,
255-
},
256-
{
257-
name: "should block taint key node.cluster.x-k8s.io/uninitialized",
258-
featureEnabled: true,
259-
machine: m.DeepCopy().WithTaints(clusterv1.MachineTaint{
260-
Key: "node.cluster.x-k8s.io/uninitialized", Effect: corev1.TaintEffectNoSchedule,
261-
}).Build(),
262-
expectErr: true,
263-
},
264-
{
265-
name: "should block taint key node.cluster.x-k8s.io/outdated-revision",
266-
featureEnabled: true,
267-
machine: m.DeepCopy().WithTaints(clusterv1.MachineTaint{
268-
Key: "node.cluster.x-k8s.io/outdated-revision", Effect: corev1.TaintEffectNoSchedule,
269-
}).Build(),
270-
expectErr: true,
271-
},
272-
{
273-
name: "should block taint with key prefix node.kubernetes.io/, which is not `out-of-service`",
274-
featureEnabled: true,
275-
machine: m.DeepCopy().WithTaints(clusterv1.MachineTaint{
276-
Key: "node.kubernetes.io/some-taint", Effect: corev1.TaintEffectNoSchedule,
277-
}).Build(),
278-
expectErr: true,
279-
},
280-
{
281-
name: "should allow taint node.kubernetes.io/out-of-service",
282-
featureEnabled: true,
283-
machine: m.DeepCopy().WithTaints(clusterv1.MachineTaint{
284-
Key: "node.kubernetes.io/out-of-service", Effect: corev1.TaintEffectNoSchedule,
285-
}).Build(),
286-
expectErr: false,
287-
},
288-
{
289-
name: "should block taint with key prefix node.cloudprovider.kubernetes.io/",
290-
featureEnabled: true,
291-
machine: m.DeepCopy().WithTaints(clusterv1.MachineTaint{
292-
Key: "node.cloudprovider.kubernetes.io/some-taint", Effect: corev1.TaintEffectNoSchedule,
293-
}).Build(),
294-
expectErr: true,
295-
},
296-
{
297-
name: "should block taint key node-role.kubernetes.io/master",
298-
featureEnabled: true,
299-
machine: m.DeepCopy().WithTaints(clusterv1.MachineTaint{
300-
Key: "node-role.kubernetes.io/master", Effect: corev1.TaintEffectNoSchedule,
301-
}).Build(),
302-
expectErr: true,
303-
},
304-
{
305-
name: "should allow taint key node-role.kubernetes.io/control-plane for control-plane nodes",
306-
featureEnabled: true,
307-
machine: m.DeepCopy().
308-
WithLabels(map[string]string{clusterv1.MachineControlPlaneLabel: "true"}).
309-
WithTaints(clusterv1.MachineTaint{
310-
Key: "node-role.kubernetes.io/control-plane", Effect: corev1.TaintEffectNoSchedule,
311-
}).Build(),
312-
expectErr: false,
313-
},
314-
{
315-
name: "should block taint key node-role.kubernetes.io/control-plane for worker nodes",
316-
featureEnabled: true,
317-
machine: m.DeepCopy().WithTaints(clusterv1.MachineTaint{
318-
Key: "node-role.kubernetes.io/control-plane", Effect: corev1.TaintEffectNoSchedule,
319-
}).Build(),
320-
expectErr: true,
321-
},
322-
}
323-
for i := range tests {
324-
tt := tests[i]
325-
t.Run(tt.name, func(t *testing.T) {
326-
g := NewWithT(t)
327-
328-
utilfeature.SetFeatureGateDuringTest(t, feature.Gates, feature.MachineTaintPropagation, tt.featureEnabled)
329-
330-
warnings, err := webhook.ValidateCreate(ctx, tt.machine)
331-
if tt.expectErr {
332-
g.Expect(err).To(HaveOccurred())
333-
} else {
334-
g.Expect(err).ToNot(HaveOccurred())
335-
}
336-
g.Expect(warnings).To(BeEmpty())
337-
338-
warnings, err = webhook.ValidateUpdate(ctx, tt.machine, tt.machine)
339-
if tt.expectErr {
340-
g.Expect(err).To(HaveOccurred())
341-
} else {
342-
g.Expect(err).ToNot(HaveOccurred())
343-
}
344-
g.Expect(warnings).To(BeEmpty())
345-
})
346-
}
347-
}

0 commit comments

Comments
 (0)