-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package customresourcevalidation | ||
|
|
||
| import ( | ||
| configv1 "github.com/openshift/api/config/v1" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| utilruntime "k8s.io/apimachinery/pkg/util/runtime" | ||
| "k8s.io/apiserver/pkg/admission" | ||
| ) | ||
|
|
||
| // unstructuredUnpackingAttributes tries to convert to a real object in the config scheme | ||
| type unstructuredUnpackingAttributes struct { | ||
| admission.Attributes | ||
| } | ||
|
|
||
| func (a *unstructuredUnpackingAttributes) GetObject() runtime.Object { | ||
| return toBestObjectPossible(a.Attributes.GetObject()) | ||
| } | ||
|
|
||
| func (a *unstructuredUnpackingAttributes) GetOldObject() runtime.Object { | ||
| return toBestObjectPossible(a.Attributes.GetOldObject()) | ||
| } | ||
|
|
||
| // toBestObjectPossible tries to convert to a real object in the config scheme | ||
| func toBestObjectPossible(orig runtime.Object) runtime.Object { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 instead of having to do the very ugly cast.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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. |
||
| unstructuredOrig, ok := orig.(runtime.Unstructured) | ||
| if !ok { | ||
| return orig | ||
| } | ||
|
|
||
| targetObj, err := configScheme.New(unstructuredOrig.GetObjectKind().GroupVersionKind()) | ||
| if err != nil { | ||
| utilruntime.HandleError(err) | ||
| return unstructuredOrig | ||
| } | ||
| if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstructuredOrig.UnstructuredContent(), targetObj); err != nil { | ||
| utilruntime.HandleError(err) | ||
| return unstructuredOrig | ||
| } | ||
| return targetObj | ||
| } | ||
|
|
||
| var configScheme = runtime.NewScheme() | ||
|
|
||
| func init() { | ||
| utilruntime.Must(configv1.Install(configScheme)) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package customresourcevalidationregistration | ||
|
|
||
| import ( | ||
| "github.com/openshift/origin/pkg/admission/customresourcevalidation/image" | ||
| "k8s.io/apiserver/pkg/admission" | ||
| ) | ||
|
|
||
| // AllCustomResourceValidators are the names of all custom resource validators that should be registered | ||
| var AllCustomResourceValidators = []string{ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. godoc. what are these strings? |
||
| "config.openshift.io/ValidateImage", | ||
| } | ||
|
|
||
| func RegisterCustomResourceValidation(plugins *admission.Plugins) { | ||
| image.Register(plugins) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| package customresourcevalidation | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| "k8s.io/apimachinery/pkg/runtime/schema" | ||
| "k8s.io/apimachinery/pkg/util/validation/field" | ||
| "k8s.io/apiserver/pkg/admission" | ||
| ) | ||
|
|
||
| type ObjectValidator interface { | ||
| ValidateCreate(obj runtime.Object) field.ErrorList | ||
| ValidateUpdate(obj runtime.Object, oldObj runtime.Object) field.ErrorList | ||
| ValidateStatusUpdate(obj runtime.Object, oldObj runtime.Object) field.ErrorList | ||
| } | ||
|
|
||
| // ValidateCustomResource is an implementation of admission.Interface. | ||
| // It looks at all new pods and overrides each container's image pull policy to Always. | ||
| type validateCustomResource struct { | ||
| *admission.Handler | ||
|
|
||
| resources map[schema.GroupResource]bool | ||
| validators map[schema.GroupVersionKind]ObjectValidator | ||
| } | ||
|
|
||
| func NewValidator(resources map[schema.GroupResource]bool, validators map[schema.GroupVersionKind]ObjectValidator) (admission.Interface, error) { | ||
| return &validateCustomResource{ | ||
| Handler: admission.NewHandler(admission.Create, admission.Update), | ||
| resources: resources, | ||
| validators: validators, | ||
| }, nil | ||
| } | ||
|
|
||
| var _ admission.ValidationInterface = &validateCustomResource{} | ||
|
|
||
| // Validate is an admission function that will validate a CRD in config.openshift.io. uncastAttributes are attributes | ||
| // that are of type unstructured. | ||
| func (a *validateCustomResource) Validate(uncastAttributes admission.Attributes) error { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Godoc. What ae uncastAttributes? Attributes with unstructured old+new object inside? |
||
| attributes := &unstructuredUnpackingAttributes{Attributes: uncastAttributes} | ||
| if a.shouldIgnore(attributes) { | ||
| return nil | ||
| } | ||
| validator, ok := a.validators[attributes.GetKind()] | ||
| if !ok { | ||
| return admission.NewForbidden(attributes, fmt.Errorf("unhandled kind: %v", attributes.GetKind())) | ||
| } | ||
|
|
||
| switch attributes.GetOperation() { | ||
| case admission.Create: | ||
| // creating subresources isn't something we understand, but we can be pretty sure we don't need to validate it | ||
| if len(attributes.GetSubresource()) > 0 { | ||
| return nil | ||
| } | ||
| errors := validator.ValidateCreate(attributes.GetObject()) | ||
| if len(errors) == 0 { | ||
| return nil | ||
| } | ||
| return apierrors.NewInvalid(attributes.GetKind().GroupKind(), attributes.GetName(), errors) | ||
|
|
||
| case admission.Update: | ||
| switch attributes.GetSubresource() { | ||
| case "": | ||
| errors := validator.ValidateUpdate(attributes.GetObject(), attributes.GetOldObject()) | ||
| if len(errors) == 0 { | ||
| return nil | ||
| } | ||
| return apierrors.NewInvalid(attributes.GetKind().GroupKind(), attributes.GetName(), errors) | ||
|
|
||
| case "status": | ||
| errors := validator.ValidateStatusUpdate(attributes.GetObject(), attributes.GetOldObject()) | ||
| if len(errors) == 0 { | ||
| return nil | ||
| } | ||
| return apierrors.NewInvalid(attributes.GetKind().GroupKind(), attributes.GetName(), errors) | ||
|
|
||
| default: | ||
| return admission.NewForbidden(attributes, fmt.Errorf("unhandled subresource: %v", attributes.GetSubresource())) | ||
| } | ||
|
|
||
| default: | ||
| return admission.NewForbidden(attributes, fmt.Errorf("unhandled operation: %v", attributes.GetOperation())) | ||
| } | ||
| } | ||
|
|
||
| func (a *validateCustomResource) shouldIgnore(attributes admission.Attributes) bool { | ||
| if !a.resources[attributes.GetResource().GroupResource()] { | ||
| return true | ||
| } | ||
| // if a subresource is specified and it isn't status, skip it | ||
| if len(attributes.GetSubresource()) > 0 && attributes.GetSubresource() != "status" { | ||
| return true | ||
| } | ||
|
|
||
| return false | ||
| } | ||
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.
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.