Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validating admissionchecks #1220

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions pkg/webhooks/workload_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package webhooks
import (
"context"
"fmt"
"strings"

corev1 "k8s.io/api/core/v1"
apivalidation "k8s.io/apimachinery/pkg/api/validation"
Expand All @@ -35,6 +36,7 @@ import (

kueue "sigs.k8s.io/kueue/apis/kueue/v1beta1"
"sigs.k8s.io/kueue/pkg/features"
"sigs.k8s.io/kueue/pkg/util/slices"
"sigs.k8s.io/kueue/pkg/workload"
)

Expand Down Expand Up @@ -142,6 +144,7 @@ func ValidateWorkload(obj *kueue.Workload) field.ErrorList {

allErrs = append(allErrs, metav1validation.ValidateConditions(obj.Status.Conditions, statusPath.Child("conditions"))...)
allErrs = append(allErrs, validateReclaimablePods(obj, statusPath.Child("reclaimablePods"))...)
allErrs = append(allErrs, validateAdmissionChecks(obj, statusPath.Child("admissionChecks"))...)

return allErrs
}
Expand Down Expand Up @@ -182,6 +185,103 @@ func validateContainer(c *corev1.Container, path *field.Path) field.ErrorList {
return allErrs
}

func validateAdmissionChecks(obj *kueue.Workload, basePath *field.Path) field.ErrorList {
var allErrs field.ErrorList
for i := range obj.Status.AdmissionChecks {
admissionChecksPath := basePath.Index(i)
ac := &obj.Status.AdmissionChecks[i]
if len(ac.PodSetUpdates) > 0 && len(ac.PodSetUpdates) != len(obj.Spec.PodSets) {
allErrs = append(allErrs, field.Invalid(admissionChecksPath.Child("podSetUpdates"), field.OmitValueType{}, "must have the same number of podSetUpdates as the podSets"))
}
allErrs = append(allErrs, validatePodSetUpdates(ac, obj, admissionChecksPath.Child("podSetUpdates"))...)
}
return allErrs
}

func validatePodSetUpdates(acs *kueue.AdmissionCheckState, obj *kueue.Workload, basePath *field.Path) field.ErrorList {
var allErrs field.ErrorList

knowPodSets := sets.New(slices.Map(obj.Spec.PodSets, func(ps *kueue.PodSet) string {
return ps.Name
})...)

for i := range acs.PodSetUpdates {
psu := &acs.PodSetUpdates[i]
psuPath := basePath.Index(i)
if !knowPodSets.Has(psu.Name) {
allErrs = append(allErrs, field.NotSupported(psuPath.Child("name"), psu.Name, sets.List(knowPodSets)))
}
allErrs = append(allErrs, validateTolerations(psu.Tolerations, psuPath.Child("tolerations"))...)
allErrs = append(allErrs, apivalidation.ValidateAnnotations(psu.Annotations, psuPath.Child("annotations"))...)
allErrs = append(allErrs, metav1validation.ValidateLabels(psu.NodeSelector, psuPath.Child("nodeSelector"))...)
allErrs = append(allErrs, metav1validation.ValidateLabels(psu.Labels, psuPath.Child("labels"))...)
}
return allErrs
}

func validateImmutablePodSetUpdates(newObj, oldObj *kueue.Workload, basePath *field.Path) field.ErrorList {
var allErrs field.ErrorList
newAcs := slices.ToRefMap(newObj.Status.AdmissionChecks, func(f *kueue.AdmissionCheckState) string { return f.Name })
for i := range oldObj.Status.AdmissionChecks {
oldAc := &oldObj.Status.AdmissionChecks[i]
newAc, found := newAcs[oldAc.Name]
if !found {
continue
}
if oldAc.State == kueue.CheckStateReady && newAc.State == kueue.CheckStateReady {
allErrs = append(allErrs, apivalidation.ValidateImmutableField(newAc.PodSetUpdates, oldAc.PodSetUpdates, basePath.Index(i).Child("podSetUpdates"))...)
}
}
return allErrs
}

// validateTolerations is extracted from git.k8s.io/kubernetes/pkg/apis/core/validation/validation.go
// we do not import it as dependency, see the comment:
// https://github.com/kubernetes/kubernetes/issues/79384#issuecomment-505627280
func validateTolerations(tolerations []corev1.Toleration, fldPath *field.Path) field.ErrorList {
stuton marked this conversation as resolved.
Show resolved Hide resolved
allErrors := field.ErrorList{}
for i, toleration := range tolerations {
idxPath := fldPath.Index(i)
// validate the toleration key
if len(toleration.Key) > 0 {
allErrors = append(allErrors, metav1validation.ValidateLabelName(toleration.Key, idxPath.Child("key"))...)
}

// empty toleration key with Exists operator and empty value means match all taints
if len(toleration.Key) == 0 && toleration.Operator != corev1.TolerationOpExists {
allErrors = append(allErrors, field.Invalid(idxPath.Child("operator"), toleration.Operator,
"operator must be Exists when `key` is empty, which means \"match all values and all keys\""))
}

if toleration.TolerationSeconds != nil && toleration.Effect != corev1.TaintEffectNoExecute {
allErrors = append(allErrors, field.Invalid(idxPath.Child("effect"), toleration.Effect,
"effect must be 'NoExecute' when `tolerationSeconds` is set"))
}

// validate toleration operator and value
switch toleration.Operator {
// empty operator means Equal
case corev1.TolerationOpEqual, "":
if errs := validation.IsValidLabelValue(toleration.Value); len(errs) != 0 {
allErrors = append(allErrors, field.Invalid(idxPath.Child("operator"), toleration.Value, strings.Join(errs, ";")))
}
case corev1.TolerationOpExists:
if len(toleration.Value) > 0 {
allErrors = append(allErrors, field.Invalid(idxPath.Child("operator"), toleration, "value must be empty when `operator` is 'Exists'"))
}
default:
validValues := []string{string(corev1.TolerationOpEqual), string(corev1.TolerationOpExists)}
allErrors = append(allErrors, field.NotSupported(idxPath.Child("operator"), toleration.Operator, validValues))
}

// validate toleration effect, empty toleration effect means match all taint effects
if len(toleration.Effect) > 0 {
allErrors = append(allErrors, validateTaintEffect(&toleration.Effect, true, idxPath.Child("effect"))...)
}
}
return allErrors
}

func validateAdmission(obj *kueue.Workload, path *field.Path) field.ErrorList {
admission := obj.Status.Admission
var allErrs field.ErrorList
Expand Down Expand Up @@ -242,6 +342,7 @@ func validateReclaimablePods(obj *kueue.Workload, basePath *field.Path) field.Er
func ValidateWorkloadUpdate(newObj, oldObj *kueue.Workload) field.ErrorList {
var allErrs field.ErrorList
specPath := field.NewPath("spec")
statusPath := field.NewPath("status")
allErrs = append(allErrs, ValidateWorkload(newObj)...)
allErrs = append(allErrs, apivalidation.ValidateImmutableField(newObj.Spec.PodSets, oldObj.Spec.PodSets, specPath.Child("podSets"))...)
allErrs = append(allErrs, apivalidation.ValidateImmutableField(newObj.Spec.PriorityClassSource, oldObj.Spec.PriorityClassSource, specPath.Child("priorityClassSource"))...)
Expand All @@ -251,6 +352,7 @@ func ValidateWorkloadUpdate(newObj, oldObj *kueue.Workload) field.ErrorList {
allErrs = append(allErrs, validateReclaimablePodsUpdate(newObj, oldObj, field.NewPath("status", "reclaimablePods"))...)
}
allErrs = append(allErrs, validateAdmissionUpdate(newObj.Status.Admission, oldObj.Status.Admission, field.NewPath("status", "admission"))...)
allErrs = append(allErrs, validateImmutablePodSetUpdates(newObj, oldObj, statusPath.Child("admissionChecks"))...)

return allErrs
}
Expand Down
142 changes: 142 additions & 0 deletions pkg/webhooks/workload_webhook_test.go
stuton marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ package webhooks
import (
"context"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/utils/ptr"

kueue "sigs.k8s.io/kueue/apis/kueue/v1beta1"
"sigs.k8s.io/kueue/pkg/constants"
Expand Down Expand Up @@ -94,6 +97,8 @@ func TestValidateWorkload(t *testing.T) {
specPath := field.NewPath("spec")
podSetsPath := specPath.Child("podSets")
statusPath := field.NewPath("status")
firstAdmissionChecksPath := statusPath.Child("admissionChecks").Index(0)
podSetUpdatePath := firstAdmissionChecksPath.Child("podSetUpdates")
firstPodSetSpecPath := podSetsPath.Index(0).Child("template", "spec")
testCases := map[string]struct {
workload *kueue.Workload
Expand Down Expand Up @@ -232,6 +237,103 @@ func TestValidateWorkload(t *testing.T) {
field.Invalid(firstPodSetSpecPath.Child("containers").Index(0).Child("resources", "requests").Key(string(corev1.ResourcePods)), nil, ""),
},
},
"empty podSetUpdates": {
workload: testingutil.MakeWorkload(testWorkloadName, testWorkloadNamespace).AdmissionChecks(kueue.AdmissionCheckState{}).Obj(),
wantErr: nil,
},
"should podSetUpdates have the same number of podSets": {
stuton marked this conversation as resolved.
Show resolved Hide resolved
workload: testingutil.MakeWorkload(testWorkloadName, testWorkloadNamespace).PodSets(
*testingutil.MakePodSet("first", 1).Obj(),
*testingutil.MakePodSet("second", 1).Obj(),
).AdmissionChecks(
kueue.AdmissionCheckState{PodSetUpdates: []kueue.PodSetUpdate{{Name: "first"}}},
).Obj(),
wantErr: field.ErrorList{
field.Invalid(podSetUpdatePath, nil, "must have the same number of podSetUpdates as the podSets"),
},
},
"mismatched names in podSetUpdates with names in podSets": {
workload: testingutil.MakeWorkload(testWorkloadName, testWorkloadNamespace).PodSets(
*testingutil.MakePodSet("first", 1).Obj(),
*testingutil.MakePodSet("second", 1).Obj(),
).AdmissionChecks(
kueue.AdmissionCheckState{PodSetUpdates: []kueue.PodSetUpdate{{Name: "first"}, {Name: "third"}}},
).Obj(),
wantErr: field.ErrorList{
field.NotSupported(firstAdmissionChecksPath.Child("podSetUpdates").Index(1).Child("name"), nil, nil),
},
},
"matched names in podSetUpdates with names in podSets": {
workload: testingutil.MakeWorkload(testWorkloadName, testWorkloadNamespace).PodSets(
*testingutil.MakePodSet("first", 1).Obj(),
*testingutil.MakePodSet("second", 1).Obj(),
).AdmissionChecks(
kueue.AdmissionCheckState{
PodSetUpdates: []kueue.PodSetUpdate{
{
Name: "first",
Labels: map[string]string{"l1": "first"},
Annotations: map[string]string{"foo": "bar"},
Tolerations: []corev1.Toleration{
{
Key: "t1",
Operator: corev1.TolerationOpEqual,
Value: "t1v",
Effect: corev1.TaintEffectNoExecute,
TolerationSeconds: ptr.To[int64](5),
},
},
NodeSelector: map[string]string{"type": "first"},
},
{
Name: "second",
Labels: map[string]string{"l2": "second"},
Annotations: map[string]string{"foo": "baz"},
Tolerations: []corev1.Toleration{
{
Key: "t2",
Operator: corev1.TolerationOpEqual,
Value: "t2v",
Effect: corev1.TaintEffectNoExecute,
TolerationSeconds: ptr.To[int64](10),
},
},
NodeSelector: map[string]string{"type": "second"},
},
},
},
).Obj(),
},
"invalid label name of podSetUpdate": {
workload: testingutil.MakeWorkload(testWorkloadName, testWorkloadNamespace).
AdmissionChecks(
kueue.AdmissionCheckState{PodSetUpdates: []kueue.PodSetUpdate{{Name: "main", Labels: map[string]string{"@abc": "foo"}}}},
).
Obj(),
wantErr: field.ErrorList{
field.Invalid(podSetUpdatePath.Index(0).Child("labels"), "@abc", ""),
},
},
"invalid node selector name of podSetUpdate": {
workload: testingutil.MakeWorkload(testWorkloadName, testWorkloadNamespace).
AdmissionChecks(
kueue.AdmissionCheckState{PodSetUpdates: []kueue.PodSetUpdate{{Name: "main", NodeSelector: map[string]string{"@abc": "foo"}}}},
).
Obj(),
wantErr: field.ErrorList{
field.Invalid(podSetUpdatePath.Index(0).Child("nodeSelector"), "@abc", ""),
},
},
"invalid label value of podSetUpdate": {
workload: testingutil.MakeWorkload(testWorkloadName, testWorkloadNamespace).
AdmissionChecks(
kueue.AdmissionCheckState{PodSetUpdates: []kueue.PodSetUpdate{{Name: "main", Labels: map[string]string{"foo": "@abc"}}}},
).
Obj(),
wantErr: field.ErrorList{
field.Invalid(podSetUpdatePath.Index(0).Child("labels"), "@abc", ""),
},
},
"invalid reclaimablePods": {
workload: testingutil.MakeWorkload(testWorkloadName, testWorkloadNamespace).
PodSets(
Expand Down Expand Up @@ -466,6 +568,46 @@ func TestValidateWorkloadUpdate(t *testing.T) {
field.Invalid(field.NewPath("spec").Child("priorityClassName"), nil, ""),
},
},
"podSetUpdates should be immutable when state is ready": {
stuton marked this conversation as resolved.
Show resolved Hide resolved
before: testingutil.MakeWorkload(testWorkloadName, testWorkloadNamespace).PodSets(
*testingutil.MakePodSet("first", 1).Obj(),
*testingutil.MakePodSet("second", 1).Obj(),
).AdmissionChecks(kueue.AdmissionCheckState{
PodSetUpdates: []kueue.PodSetUpdate{{Name: "first", Labels: map[string]string{"foo": "bar"}}, {Name: "second"}},
State: kueue.CheckStateReady,
}).Obj(),
after: testingutil.MakeWorkload(testWorkloadName, testWorkloadNamespace).PodSets(
*testingutil.MakePodSet("first", 1).Obj(),
*testingutil.MakePodSet("second", 1).Obj(),
).AdmissionChecks(kueue.AdmissionCheckState{
PodSetUpdates: []kueue.PodSetUpdate{{Name: "first", Labels: map[string]string{"foo": "baz"}}, {Name: "second"}},
State: kueue.CheckStateReady,
}).Obj(),
wantErr: field.ErrorList{
field.Invalid(field.NewPath("status").Child("admissionChecks").Index(0).Child("podSetUpdates"), nil, ""),
},
},
"should change other fields of admissionchecks when podSetUpdates is immutable": {
before: testingutil.MakeWorkload(testWorkloadName, testWorkloadNamespace).PodSets(
*testingutil.MakePodSet("first", 1).Obj(),
*testingutil.MakePodSet("second", 1).Obj(),
).AdmissionChecks(kueue.AdmissionCheckState{
Name: "ac1",
Message: "old",
PodSetUpdates: []kueue.PodSetUpdate{{Name: "first", Labels: map[string]string{"foo": "bar"}}, {Name: "second"}},
State: kueue.CheckStateReady,
}).Obj(),
after: testingutil.MakeWorkload(testWorkloadName, testWorkloadNamespace).PodSets(
*testingutil.MakePodSet("first", 1).Obj(),
*testingutil.MakePodSet("second", 1).Obj(),
).AdmissionChecks(kueue.AdmissionCheckState{
Name: "ac1",
Message: "new",
LastTransitionTime: metav1.NewTime(time.Now()),
PodSetUpdates: []kueue.PodSetUpdate{{Name: "first", Labels: map[string]string{"foo": "bar"}}, {Name: "second"}},
State: kueue.CheckStateReady,
}).Obj(),
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
Expand Down