Fix validation to use field.Forbidden instead of Required

Co-authored-by: David Eads <deads2k@users.noreply.github.com>
Signed-off-by: Mike Spreitzer <mspreitz@us.ibm.com>
This commit is contained in:
Mike Spreitzer 2023-07-13 01:21:56 -04:00
parent 7305c96710
commit ce90eb2cc2
2 changed files with 3 additions and 21 deletions

View File

@ -396,7 +396,7 @@ func ValidatePriorityLevelConfigurationSpec(spec *flowcontrol.PriorityLevelConfi
} }
case flowcontrol.PriorityLevelEnablementLimited: case flowcontrol.PriorityLevelEnablementLimited:
if spec.Exempt != nil { if spec.Exempt != nil {
allErrs = append(allErrs, field.Required(fldPath.Child("exempt"), "must be nil if the type is Limited")) allErrs = append(allErrs, field.Forbidden(fldPath.Child("exempt"), "must be nil if the type is Limited"))
} }
if spec.Limited == nil { if spec.Limited == nil {

View File

@ -828,24 +828,6 @@ func TestPriorityLevelConfigurationValidation(t *testing.T) {
field.Invalid(field.NewPath("spec").Child("type"), flowcontrol.PriorityLevelEnablementLimited, "type must be 'Exempt' if and only if name is 'exempt'"), field.Invalid(field.NewPath("spec").Child("type"), flowcontrol.PriorityLevelEnablementLimited, "type must be 'Exempt' if and only if name is 'exempt'"),
field.Invalid(field.NewPath("spec"), badSpec, "spec of 'exempt' except the 'spec.exempt' field must equal the fixed value"), field.Invalid(field.NewPath("spec"), badSpec, "spec of 'exempt' except the 'spec.exempt' field must equal the fixed value"),
}, },
}, {
name: "limited must not set exempt priority level configuration for borrowing",
priorityLevelConfiguration: &flowcontrol.PriorityLevelConfiguration{
ObjectMeta: metav1.ObjectMeta{
Name: "broken-limited",
},
Spec: flowcontrol.PriorityLevelConfigurationSpec{
Type: flowcontrol.PriorityLevelEnablementLimited,
Exempt: &flowcontrol.ExemptPriorityLevelConfiguration{
NominalConcurrencyShares: pointer.Int32(10),
LendablePercent: pointer.Int32(20),
},
},
},
expectedErrors: field.ErrorList{
field.Required(field.NewPath("spec").Child("exempt"), "must be nil if the type is Limited"),
field.Required(field.NewPath("spec").Child("limited"), "must not be empty when type is Limited"),
},
}, { }, {
name: "exempt priority level should have appropriate values for Exempt field", name: "exempt priority level should have appropriate values for Exempt field",
priorityLevelConfiguration: &flowcontrol.PriorityLevelConfiguration{ priorityLevelConfiguration: &flowcontrol.PriorityLevelConfiguration{
@ -875,7 +857,7 @@ func TestPriorityLevelConfigurationValidation(t *testing.T) {
priorityLevelConfiguration: exemptTypeRepurposed, priorityLevelConfiguration: exemptTypeRepurposed,
expectedErrors: field.ErrorList{ expectedErrors: field.ErrorList{
field.Invalid(field.NewPath("spec").Child("type"), flowcontrol.PriorityLevelEnablementLimited, "type must be 'Exempt' if and only if name is 'exempt'"), field.Invalid(field.NewPath("spec").Child("type"), flowcontrol.PriorityLevelEnablementLimited, "type must be 'Exempt' if and only if name is 'exempt'"),
field.Required(field.NewPath("spec").Child("exempt"), "must be nil if the type is Limited"), field.Forbidden(field.NewPath("spec").Child("exempt"), "must be nil if the type is Limited"),
field.Invalid(field.NewPath("spec"), exemptTypeRepurposed.Spec, "spec of 'exempt' except the 'spec.exempt' field must equal the fixed value"), field.Invalid(field.NewPath("spec"), exemptTypeRepurposed.Spec, "spec of 'exempt' except the 'spec.exempt' field must equal the fixed value"),
}, },
}, { }, {
@ -911,7 +893,7 @@ func TestPriorityLevelConfigurationValidation(t *testing.T) {
}, },
}, },
expectedErrors: field.ErrorList{ expectedErrors: field.ErrorList{
field.Required(field.NewPath("spec").Child("exempt"), "must be nil if the type is Limited"), field.Forbidden(field.NewPath("spec").Child("exempt"), "must be nil if the type is Limited"),
field.Required(field.NewPath("spec").Child("limited"), "must not be empty when type is Limited"), field.Required(field.NewPath("spec").Child("limited"), "must not be empty when type is Limited"),
}, },
}, { }, {