Skip to content

Commit 2b090cc

Browse files
TomerNewmank8s-ci-robot
authored andcommitted
Adding validation to module toleration operator value
Users can add a toleration to Module with invalid operator values (for example "invalid123"). This commit adds a webhook validation to the toleration operator value, ensuring only: Exists, Equal operator values can be applied.
1 parent a8ff4d4 commit 2b090cc

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

internal/webhook/module.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func validateModule(mod *kmmv1beta1.Module) (admission.Warnings, error) {
110110
return nil, fmt.Errorf("failed to validate kernel mappings: %v", err)
111111
}
112112

113-
if err := validateModuleTolerarations(mod); err != nil {
113+
if err := validateModuleTolerations(mod); err != nil {
114114
return nil, fmt.Errorf("failed to validate Module's tolerations: %v", err)
115115
}
116116

@@ -212,13 +212,17 @@ func validateModprobe(modprobe kmmv1beta1.ModprobeSpec) error {
212212

213213
return nil
214214
}
215-
func validateModuleTolerarations(mod *kmmv1beta1.Module) error {
215+
func validateModuleTolerations(mod *kmmv1beta1.Module) error {
216216
for _, toleration := range mod.Spec.Tolerations {
217-
switch toleration.Effect {
218-
case corev1.TaintEffectNoSchedule, corev1.TaintEffectNoExecute, corev1.TaintEffectPreferNoSchedule:
219-
continue
220-
default:
221-
return fmt.Errorf("invalid toleration effect %s. allowed values are NoSchedule, NoExecute, PreferNoSchedule", toleration.Effect)
217+
218+
if toleration.Operator != corev1.TolerationOpExists && toleration.Operator != corev1.TolerationOpEqual {
219+
return fmt.Errorf("toleration operator can be only {Exists, Equal} but got %s", toleration.Operator)
220+
}
221+
222+
if toleration.Effect != corev1.TaintEffectNoSchedule &&
223+
toleration.Effect != corev1.TaintEffectNoExecute &&
224+
toleration.Effect != corev1.TaintEffectPreferNoSchedule {
225+
return fmt.Errorf("toleration effect can be only {NoSchedule, NoExecute, PreferNoSchedule} but got %s", toleration.Effect)
222226
}
223227
}
224228
return nil

internal/webhook/module_test.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -558,25 +558,45 @@ var _ = Describe("validateModuleTolerarations", func() {
558558
mod := validModule
559559
mod.Spec.Tolerations = []v1.Toleration{
560560
{
561-
Key: "Test-Key1", Operator: "Test-Equal1", Value: "Test-Value1", Effect: v1.TaintEffectPreferNoSchedule,
561+
Key: "Test-Key1", Operator: v1.TolerationOpEqual, Value: "Test-Value1", Effect: v1.TaintEffectPreferNoSchedule,
562562
},
563563
{
564-
Key: "Test-Key2", Operator: "Test-Equal2", Value: "Test-Value2", Effect: "Test-Effect",
564+
Key: "Test-Key2", Operator: v1.TolerationOpExists, Value: "Test-Value2", Effect: "Invalid-Effect",
565565
},
566566
}
567567

568-
err := validateModuleTolerarations(&mod)
568+
err := validateModuleTolerations(&mod)
569+
Expect(err).To(HaveOccurred())
570+
})
571+
It("should fail when Module has an invalid toleration operator", func() {
572+
mod := validModule
573+
mod.Spec.Tolerations = []v1.Toleration{
574+
{
575+
Key: "Test-Key1", Operator: v1.TolerationOpEqual, Value: "Test-Value1", Effect: v1.TaintEffectPreferNoSchedule,
576+
},
577+
{
578+
Key: "Test-Key2", Operator: "Invalid-operator-value", Value: "Test-Value2", Effect: v1.TaintEffectNoExecute,
579+
},
580+
}
581+
582+
err := validateModuleTolerations(&mod)
569583
Expect(err).To(HaveOccurred())
570584
})
571585
It("should work when all tolerations have valid effects ", func() {
572586
mod := validModule
573587
mod.Spec.Tolerations = []v1.Toleration{
574588
{
575-
Key: "Test-Key", Operator: "Test-Equal", Value: "Test-Value", Effect: v1.TaintEffectPreferNoSchedule,
589+
Key: "Test-Key1", Operator: v1.TolerationOpExists, Value: "Test-Value", Effect: v1.TaintEffectPreferNoSchedule,
590+
},
591+
{
592+
Key: "Test-Key2", Operator: v1.TolerationOpEqual, Value: "Test-Value", Effect: v1.TaintEffectNoSchedule,
593+
},
594+
{
595+
Key: "Test-Key3", Operator: v1.TolerationOpEqual, Value: "Test-Value", Effect: v1.TaintEffectNoExecute,
576596
},
577597
}
578598

579-
err := validateModuleTolerarations(&mod)
599+
err := validateModuleTolerations(&mod)
580600
Expect(err).ToNot(HaveOccurred())
581601
})
582602

0 commit comments

Comments
 (0)