diff --git a/pkg/apis/core/validation/validation.go b/pkg/apis/core/validation/validation.go index 006bae7bdfa..3b7d9826ed8 100644 --- a/pkg/apis/core/validation/validation.go +++ b/pkg/apis/core/validation/validation.go @@ -3005,9 +3005,6 @@ func ValidateNodeSelectorRequirement(rq core.NodeSelectorRequirement, fldPath *f func ValidateNodeSelectorTerm(term core.NodeSelectorTerm, fldPath *field.Path) field.ErrorList { allErrs := field.ErrorList{} - if len(term.MatchExpressions) == 0 { - return append(allErrs, field.Required(fldPath.Child("matchExpressions"), "must have at least one node selector requirement")) - } for j, req := range term.MatchExpressions { allErrs = append(allErrs, ValidateNodeSelectorRequirement(req, fldPath.Child("matchExpressions").Index(j))...) } diff --git a/pkg/apis/core/validation/validation_test.go b/pkg/apis/core/validation/validation_test.go index fabd63a72f9..c63bde09bf8 100644 --- a/pkg/apis/core/validation/validation_test.go +++ b/pkg/apis/core/validation/validation_test.go @@ -6069,6 +6069,47 @@ func TestValidatePod(t *testing.T) { }, ), }, + { // Serialized node affinity requirements. + ObjectMeta: metav1.ObjectMeta{ + Name: "123", + Namespace: "ns", + }, + Spec: validPodSpec( + // TODO: Uncomment and move this block and move inside NodeAffinity once + // RequiredDuringSchedulingRequiredDuringExecution is implemented + // RequiredDuringSchedulingRequiredDuringExecution: &core.NodeSelector{ + // NodeSelectorTerms: []core.NodeSelectorTerm{ + // { + // MatchExpressions: []core.NodeSelectorRequirement{ + // { + // Key: "key1", + // Operator: core.NodeSelectorOpExists + // }, + // }, + // }, + // }, + // }, + &core.Affinity{ + NodeAffinity: &core.NodeAffinity{ + RequiredDuringSchedulingIgnoredDuringExecution: &core.NodeSelector{ + NodeSelectorTerms: []core.NodeSelectorTerm{ + { + MatchExpressions: []core.NodeSelectorRequirement{}, + }, + }, + }, + PreferredDuringSchedulingIgnoredDuringExecution: []core.PreferredSchedulingTerm{ + { + Weight: 10, + Preference: core.NodeSelectorTerm{ + MatchExpressions: []core.NodeSelectorRequirement{}, + }, + }, + }, + }, + }, + ), + }, { // Serialized pod affinity in affinity requirements in annotations. ObjectMeta: metav1.ObjectMeta{ Name: "123", @@ -6517,26 +6558,6 @@ func TestValidatePod(t *testing.T) { }), }, }, - "invalid requiredDuringSchedulingIgnoredDuringExecution node selector term, matchExpressions must have at least one node selector requirement": { - expectedError: "spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution.nodeSelectorTerms[0].matchExpressions", - spec: core.Pod{ - ObjectMeta: metav1.ObjectMeta{ - Name: "123", - Namespace: "ns", - }, - Spec: validPodSpec(&core.Affinity{ - NodeAffinity: &core.NodeAffinity{ - RequiredDuringSchedulingIgnoredDuringExecution: &core.NodeSelector{ - NodeSelectorTerms: []core.NodeSelectorTerm{ - { - MatchExpressions: []core.NodeSelectorRequirement{}, - }, - }, - }, - }, - }), - }, - }, "invalid weight in preferredDuringSchedulingIgnoredDuringExecution in pod affinity annotations, weight should be in range 1-100": { expectedError: "must be in the range 1-100", spec: core.Pod{