Skip to content

Commit 936e083

Browse files
TomerNewmank8s-ci-robot
authored andcommitted
Adding validation to module toleration effect
Users can add a toleration to Module with invalid effects (for example "blabla"). This commit adds a webhook validation to the toleration effect value, ensuring only: NoExecute, NoSchedule, PreferNoSchedule can be applied.
1 parent eeb1b9d commit 936e083

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

internal/webhook/module.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"errors"
2222
"fmt"
23+
corev1 "k8s.io/api/core/v1"
2324
"regexp"
2425
"strings"
2526

@@ -109,6 +110,10 @@ func validateModule(mod *kmmv1beta1.Module) (admission.Warnings, error) {
109110
return nil, fmt.Errorf("failed to validate kernel mappings: %v", err)
110111
}
111112

113+
if err := validateModuleTolerarations(mod); err != nil {
114+
return nil, fmt.Errorf("failed to validate Module's tolerations: %v", err)
115+
}
116+
112117
return nil, validateModprobe(mod.Spec.ModuleLoader.Container.Modprobe)
113118
}
114119

@@ -207,3 +212,14 @@ func validateModprobe(modprobe kmmv1beta1.ModprobeSpec) error {
207212

208213
return nil
209214
}
215+
func validateModuleTolerarations(mod *kmmv1beta1.Module) error {
216+
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)
222+
}
223+
}
224+
return nil
225+
}

internal/webhook/module_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package webhook
1818

1919
import (
2020
"context"
21+
v1 "k8s.io/api/core/v1"
2122
"strings"
2223

2324
kmmv1beta1 "github.com/kubernetes-sigs/kernel-module-management/api/v1beta1"
@@ -551,3 +552,32 @@ var _ = Describe("ValidateDelete", func() {
551552
Expect(err).To(MatchError(NotImplemented))
552553
})
553554
})
555+
556+
var _ = Describe("validateModuleTolerarations", func() {
557+
It("should fail when Module has an invalid toleration effect", func() {
558+
mod := validModule
559+
mod.Spec.Tolerations = []v1.Toleration{
560+
{
561+
Key: "Test-Key1", Operator: "Test-Equal1", Value: "Test-Value1", Effect: v1.TaintEffectPreferNoSchedule,
562+
},
563+
{
564+
Key: "Test-Key2", Operator: "Test-Equal2", Value: "Test-Value2", Effect: "Test-Effect",
565+
},
566+
}
567+
568+
err := validateModuleTolerarations(&mod)
569+
Expect(err).To(HaveOccurred())
570+
})
571+
It("should work when all tolerations have valid effects ", func() {
572+
mod := validModule
573+
mod.Spec.Tolerations = []v1.Toleration{
574+
{
575+
Key: "Test-Key", Operator: "Test-Equal", Value: "Test-Value", Effect: v1.TaintEffectPreferNoSchedule,
576+
},
577+
}
578+
579+
err := validateModuleTolerarations(&mod)
580+
Expect(err).ToNot(HaveOccurred())
581+
})
582+
583+
})

0 commit comments

Comments
 (0)