-
Notifications
You must be signed in to change notification settings - Fork 4.8k
add easy pattern for customresource validation #21679
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
add easy pattern for customresource validation #21679
Conversation
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: deads2k The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
|
/retest |
|
@openshift/sig-auth I think I would take anything over no validation. |
|
@sanchezl I could use some help with unit tests here and this is a construct you're likely to need to be familiar with |
After the test I think we're ready to move on this. |
| // Register registers a plugin | ||
| func Register(plugins *admission.Plugins) { | ||
| plugins.Register("config.openshift.io/ValidateImage", func(config io.Reader) (admission.Interface, error) { | ||
| return customresourcevalidation.NewValidator(configv1.Resource("images"), validateObjCreateFn, validateObjUpdateFn, validateObjStatusUpdateFn) |
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.
Would be better as an interface rather than passing raw functions around, unless you're going to expect people to define these inline.
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.
Would be better as an interface rather than passing raw functions around, unless you're going to expect people to define these inline.
Yeah, and people probably want/need a slice of them to handle versions.
| } | ||
|
|
||
| func validateObjStatusUpdateFn(uncastObj runtime.Object, uncastOldObj runtime.Object) field.ErrorList { | ||
| allErrs := field.ErrorList{} |
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.
If you're only going to call these functions once per type, this is very boilerplate-y and could be made much simpler by just doing the conversion yourself here with a helper that is shared.
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.
by just doing the conversion yourself here with a helper that is shared.
That would make these functions significantly different than "normal" validation functions and doesn't help with the boilerplate since the same defense against errors is needed
| } | ||
|
|
||
| // toBestObjectPossible tries to convert to a real object in the config scheme | ||
| func toBestObjectPossible(orig runtime.Object) runtime.Object { |
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 function really could just be a shared helper that takes the unstructured and the into and then does the type check, verifies the config scheme allows into, converts, and optionally returns invalid errors. That bypasses the cast and make this much easier to read.
var image *configv1.Image
if errs := ConvertFromUnstructured(obj, image); len(errs) > 0 {
return errs
}
...
instead of having to do the very ugly cast.
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 function really could just be a shared helper that takes the unstructured and the into
The into isn't known, that's part of what this function is figuring out. If it gets specified then I have reflection trying to clone it. Also the unstructured has to be cast somewhere.
| // Register registers a plugin | ||
| func Register(plugins *admission.Plugins) { | ||
| plugins.Register("config.openshift.io/ValidateImage", func(config io.Reader) (admission.Interface, error) { | ||
| return customresourcevalidation.NewValidator(configv1.Resource("images"), validateObjCreateFn, validateObjUpdateFn, validateObjStatusUpdateFn) |
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 needs to be on the group resource, not the group version resource
| return apierrors.NewInvalid(attributes.GetKind().GroupKind(), attributes.GetName(), errors) | ||
|
|
||
| default: | ||
| admission.NewForbidden(attributes, fmt.Errorf("unhandled subresource: %v", attributes.GetSubresource())) |
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.
Should this error (and the other Forbidden error below) not be returned?
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.
Should this error (and the other Forbidden error below) not be returned?
bug.
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 this specific line, should we not just ignore (i.e. return nil), to be consistent with shouldIgnore()?
59ea688 to
7907dcd
Compare
bc9a508 to
fe22771
Compare
|
/assign @sttts ptal |
| "k8s.io/apiserver/pkg/admission" | ||
| ) | ||
|
|
||
| var AllCustomResourceValidators = []string{ |
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.
godoc. what are these strings?
|
|
||
| var _ admission.ValidationInterface = &validateCustomResource{} | ||
|
|
||
| func (a *validateCustomResource) Validate(uncastAttributes admission.Attributes) error { |
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.
Godoc. What ae uncastAttributes? Attributes with unstructured old+new object inside?
| ) | ||
|
|
||
| // unstructuredUnpackingAttributes tries to convert to a real object in the config scheme | ||
| type unstructuredUnpackingAttributes struct { |
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.
why this wrapper struct? An explicit conversion in the admission handler would be much simpler. No need for on-the-fly conversion.
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.
why this wrapper struct? An explicit conversion in the admission handler would be much simpler. No need for on-the-fly conversion.
We don't know whether or not we can until point of use. Also, point of use makes sure that that we don't miss a spot.
| "k8s.io/apiserver/pkg/admission" | ||
| ) | ||
|
|
||
| func RequireNameCluster(name string, prefix bool) []string { |
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.
what is this? A helper? Mover to an helper.go file.
| configv1 "github.com/openshift/api/config/v1" | ||
|
|
||
| "github.com/openshift/origin/pkg/admission/customresourcevalidation" | ||
| "k8s.io/apimachinery/pkg/api/validation" |
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.
order
| }) | ||
| } | ||
|
|
||
| func toImageV1(uncastObj runtime.Object) (*configv1.Image, field.ErrorList) { |
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.
do we need this boilerplate in every validation? Can't we check in a generic way in the framework code, if necessary by passing the target kind there.
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.
do we need this boilerplate in every validation? Can't we check in a generic way in the framework code, if necessary by passing the target kind there.
Probably via reflection. I'm open to future refinement.
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.
Probably via reflection. I'm open to future refinement.
you have to use reflection for the actual method calls
| } | ||
|
|
||
| func (imageV1) ValidateUpdate(uncastObj runtime.Object, uncastOldObj runtime.Object) field.ErrorList { | ||
| obj, castErrors := toImageV1(uncastObj) |
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.
s/castErrors/errs/
| return castErrors | ||
| } | ||
|
|
||
| allErrs := field.ErrorList{} |
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.
errs
f307440 to
738fa31
Compare
|
comments addressed. Some reflective refinement is possible, but I don't see it as blocking. |
|
/retest Please review the full test history for this PR and help us cut down flakes. |
|
@deads2k: The following tests failed, say
Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR. DetailsInstructions 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/test-infra repository. I understand the commands that are listed here. |
|
/retest Please review the full test history for this PR and help us cut down flakes. |
This makes an easy pattern to follow and provide CR validation functions. I'm thinking about our config APIs in particular. This isn't totally pure, but it does solve our ordering issue, makes it easy for teams to add validation, provides for immutable fields, and could maybe be added as a webhook later if we decided against this.
@derekwaynecarr @smarterclayton we've discussed the need for validation
@openshift/sig-master think of something better or we'll have to hold our noses :)