Skip to content

Conversation

@deads2k
Copy link
Contributor

@deads2k deads2k commented Dec 17, 2018

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 :)

@openshift-ci-robot openshift-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. sig/master size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Dec 17, 2018
@openshift-ci-robot
Copy link

[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

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci-robot openshift-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Dec 17, 2018
@deads2k
Copy link
Contributor Author

deads2k commented Dec 17, 2018

/retest

@enj
Copy link
Contributor

enj commented Dec 20, 2018

@openshift/sig-auth

I think I would take anything over no validation.

@deads2k
Copy link
Contributor Author

deads2k commented Jan 3, 2019

@sanchezl I could use some help with unit tests here and this is a construct you're likely to need to be familiar with

@deads2k
Copy link
Contributor Author

deads2k commented Jan 3, 2019

@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)
Copy link
Contributor

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.

Copy link
Contributor Author

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{}
Copy link
Contributor

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.

Copy link
Contributor Author

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 {
Copy link
Contributor

@smarterclayton smarterclayton Jan 4, 2019

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.

Copy link
Contributor Author

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)
Copy link
Contributor Author

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()))
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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()?

@openshift-ci-robot openshift-ci-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jan 8, 2019
@openshift-ci-robot openshift-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jan 8, 2019
@deads2k deads2k force-pushed the cr-validation branch 2 times, most recently from bc9a508 to fe22771 Compare January 8, 2019 16:22
@deads2k deads2k changed the title [WIP] add easy pattern for customresource validation add easy pattern for customresource validation Jan 8, 2019
@openshift-ci-robot openshift-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jan 8, 2019
@deads2k
Copy link
Contributor Author

deads2k commented Jan 8, 2019

/assign @sttts

ptal

"k8s.io/apiserver/pkg/admission"
)

var AllCustomResourceValidators = []string{
Copy link
Contributor

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 {
Copy link
Contributor

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 {
Copy link
Contributor

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.

Copy link
Contributor Author

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 {
Copy link
Contributor

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"
Copy link
Contributor

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) {
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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)
Copy link
Contributor

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{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

errs

@deads2k deads2k added the lgtm Indicates that a PR is ready to be merged. label Jan 11, 2019
@deads2k
Copy link
Contributor Author

deads2k commented Jan 11, 2019

comments addressed. Some reflective refinement is possible, but I don't see it as blocking.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-ci-robot
Copy link

openshift-ci-robot commented Jan 13, 2019

@deads2k: The following tests failed, say /retest to rerun them all:

Test name Commit Details Rerun command
ci/openshift-jenkins/end_to_end ef05639 link /test end_to_end
ci/prow/e2e-gcp-serial ef05639 link /test e2e-gcp-serial
ci/prow/e2e-gcp ef05639 link /test e2e-gcp
ci/prow/launch-aws ef05639 link /test launch-aws

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.

Details

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/test-infra repository. I understand the commands that are listed here.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-merge-robot openshift-merge-robot merged commit 2f67e56 into openshift:master Jan 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged. sig/master size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants