-
Notifications
You must be signed in to change notification settings - Fork 40.9k
Refactor: Use field.Required for missing field errors #132536
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
base: master
Are you sure you want to change the base?
Refactor: Use field.Required for missing field errors #132536
Conversation
Hi @xiaoweim. Thanks for your PR. I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
/priority important-longterm |
/cc @aaron-prindle |
This PR may require API review. If so, when the changes are ready, complete the pre-review checklist and request an API review. Status of requested reviews is tracked in the API Review project. |
/ok-to-test |
005f779
to
424bd82
Compare
/retest |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't review all of it super closely -- these ones need to look at what's ACTUALLY being validated.
Only API fields can be "required" and that means either the field value or pointer, but never what is pointed-to (that would be Invalid) or items in a list.
Might make sense to break this into several smaller PRs so we can merge and make progress inrementally.
@@ -34,10 +34,10 @@ func ValidateSubjectAccessReviewSpec(spec authorizationapi.SubjectAccessReviewSp | |||
allErrs = append(allErrs, field.Invalid(fldPath.Child("nonResourceAttributes"), spec.NonResourceAttributes, `cannot be specified in combination with resourceAttributes`)) | |||
} | |||
if spec.ResourceAttributes == nil && spec.NonResourceAttributes == nil { | |||
allErrs = append(allErrs, field.Invalid(fldPath.Child("resourceAttributes"), spec.NonResourceAttributes, `exactly one of nonResourceAttributes or resourceAttributes must be specified`)) | |||
allErrs = append(allErrs, field.Required(fldPath.Child("resourceAttributes"), `exactly one of nonResourceAttributes or resourceAttributes must be specified`)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should stay as Invalid, but the fieldPath should indicate the parent (fldPath
)
} | ||
if len(spec.User) == 0 && len(spec.Groups) == 0 { | ||
allErrs = append(allErrs, field.Invalid(fldPath.Child("user"), spec.User, `at least one of user or group must be specified`)) | ||
allErrs = append(allErrs, field.Required(fldPath.Child("user"), `at least one of user or group must be specified`)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
@@ -52,7 +52,7 @@ func ValidateSelfSubjectAccessReviewSpec(spec authorizationapi.SelfSubjectAccess | |||
allErrs = append(allErrs, field.Invalid(fldPath.Child("nonResourceAttributes"), spec.NonResourceAttributes, `cannot be specified in combination with resourceAttributes`)) | |||
} | |||
if spec.ResourceAttributes == nil && spec.NonResourceAttributes == nil { | |||
allErrs = append(allErrs, field.Invalid(fldPath.Child("resourceAttributes"), spec.NonResourceAttributes, `exactly one of nonResourceAttributes or resourceAttributes must be specified`)) | |||
allErrs = append(allErrs, field.Required(fldPath.Child("resourceAttributes"), `exactly one of nonResourceAttributes or resourceAttributes must be specified`)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
@@ -101,7 +101,7 @@ func TestValidateSARSpec(t *testing.T) { | |||
obj: authorizationapi.SubjectAccessReviewSpec{ | |||
ResourceAttributes: &authorizationapi.ResourceAttributes{}, | |||
}, | |||
msg: `spec.user: Invalid value: "": at least one of user or group must be specified`, | |||
msg: `spec.user: Required value: at least one of user or group must be specified`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
@@ -348,7 +348,7 @@ func validatePodFailurePolicyRule(spec *batch.JobSpec, rule *batch.PodFailurePol | |||
allErrs = append(allErrs, field.Invalid(rulePath, field.OmitValueType{}, "specifying both OnExitCodes and OnPodConditions is not supported")) | |||
} | |||
if rule.OnExitCodes == nil && len(rule.OnPodConditions) == 0 { | |||
allErrs = append(allErrs, field.Invalid(rulePath, field.OmitValueType{}, "specifying one of OnExitCodes and OnPodConditions is required")) | |||
allErrs = append(allErrs, field.Required(rulePath, "specifying one of OnExitCodes and OnPodConditions is required")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
one of ... must be specified
@@ -8498,7 +8498,7 @@ func validateUpgradeDowngradeIPFamilies(oldService, service *core.Service) field | |||
// user *must* set IPFamilyPolicy == SingleStack | |||
if len(service.Spec.IPFamilies) == 1 { | |||
if service.Spec.IPFamilyPolicy == nil || *(service.Spec.IPFamilyPolicy) != core.IPFamilyPolicySingleStack { | |||
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "ipFamilyPolicy"), service.Spec.IPFamilyPolicy, "must be set to 'SingleStack' when releasing the secondary ipFamily")) | |||
allErrs = append(allErrs, field.Required(field.NewPath("spec", "ipFamilyPolicy"), "must be set to 'SingleStack' when releasing the secondary ipFamily")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Invalid is better here
@@ -542,7 +542,7 @@ func ValidatePriorityLevelConfigurationCondition(condition *flowcontrol.Priority | |||
// 4. Wildcard "*" should only do suffix glob matching. Note that wildcard also matches slashes. | |||
func ValidateNonResourceURLPath(path string, fldPath *field.Path) *field.Error { | |||
if len(path) == 0 { | |||
return field.Invalid(fldPath, path, "must not be empty") | |||
return field.Required(fldPath, "") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a trap! Look at how this is called - it is validating a string in a list -- Invalid is correct. Please look out for this in other places. I may have missed some
@@ -534,7 +534,7 @@ func validateIngressBackend(backend *networking.IngressBackend, fldPath *field.P | |||
allErrs = append(allErrs, field.Required(fldPath, "port name or number is required")) | |||
} | |||
default: | |||
allErrs = append(allErrs, field.Invalid(fldPath, "", "resource or service backend is required")) | |||
allErrs = append(allErrs, field.Required(fldPath, "resource or service backend is required")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For "one of" errors, we attribute to the common parent and use Invalid
@@ -763,7 +763,7 @@ func validateDevice(device resource.Device, fldPath *field.Path, sharedCounterTo | |||
setFields = append(setFields, "`nodeName`") | |||
allErrs = append(allErrs, validateNodeName(*device.NodeName, fldPath.Child("nodeName"))...) | |||
} else { | |||
allErrs = append(allErrs, field.Invalid(fldPath.Child("nodeName"), *device.NodeName, "must not be empty")) | |||
allErrs = append(allErrs, field.Required(fldPath.Child("nodeName"), "")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is another of the same trap -- the field (pointer) was specified but the value was "" - Invalid
@@ -136,7 +136,7 @@ func newNodeLogQuery(query url.Values) (*nodeLogQuery, field.ErrorList) { | |||
// Prevent specifying an empty or blank space query. | |||
// Example: kubectl get --raw /api/v1/nodes/$node/proxy/logs?query=" " | |||
if ok && (len(nlq.Files) == 0 && len(nlq.Services) == 0) { | |||
allErrs = append(allErrs, field.Invalid(field.NewPath("query"), queries, "query cannot be empty")) | |||
allErrs = append(allErrs, field.Required(field.NewPath("query"), "")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is another one-of
What type of PR is this?
/kind cleanup
What this PR does / why we need it:
This commit refactors validation logic to use
field.Required
in caseswhere
field.Invalid
was previously used to indicate that a field wasmissing. According to the issue description,
field.Invalid
was sometimesused with a detail message like "must be set", which is semantically
a "required" error.
This is the third and final part of the cleanup work for this issue #111698
Which issue(s) this PR is related to:
Fixes 111698
Does this PR introduce a user-facing change?