-
Notifications
You must be signed in to change notification settings - Fork 1.2k
⚠️ Generic Validator and Defaulter #3360
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
Open
alvaroaleman
wants to merge
11
commits into
kubernetes-sigs:main
Choose a base branch
from
alvaroaleman:typed
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+883
−679
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
955d723
Type defaulter and validator
alvaroaleman 8196239
Builder
alvaroaleman fc15d4a
Introduce WebhookFor
alvaroaleman 48a1c08
Update existing WebhookManagedBy
alvaroaleman 3cc1910
Linting
alvaroaleman 7a149e8
Preserve alias for NewWebhookManagedBy
alvaroaleman 2b33f30
Use WithDefaulter/WithValidator going forward
alvaroaleman 6fc8cb1
Test defaulter
alvaroaleman cad0736
TestValidatorBuilder
alvaroaleman 4344a72
Mixed
alvaroaleman 3c11c98
Custom and Typed validator/defaulter are mutually exclusive
alvaroaleman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -37,11 +37,13 @@ import ( | |||||
| ) | ||||||
|
|
||||||
| // WebhookBuilder builds a Webhook. | ||||||
| type WebhookBuilder struct { | ||||||
| type WebhookBuilder[T runtime.Object] struct { | ||||||
| apiType runtime.Object | ||||||
| customDefaulter admission.CustomDefaulter | ||||||
| defaulter admission.Defaulter[T] | ||||||
| customDefaulterOpts []admission.DefaulterOption | ||||||
| customValidator admission.CustomValidator | ||||||
| validator admission.Validator[T] | ||||||
| customPath string | ||||||
| customValidatorCustomPath string | ||||||
| customDefaulterCustomPath string | ||||||
|
|
@@ -56,59 +58,61 @@ type WebhookBuilder struct { | |||||
| } | ||||||
|
|
||||||
| // WebhookManagedBy returns a new webhook builder. | ||||||
| func WebhookManagedBy(m manager.Manager) *WebhookBuilder { | ||||||
| return &WebhookBuilder{mgr: m} | ||||||
| func WebhookManagedBy[T runtime.Object](m manager.Manager, object T) *WebhookBuilder[T] { | ||||||
| return &WebhookBuilder[T]{mgr: m, apiType: object} | ||||||
| } | ||||||
|
|
||||||
| // TODO(droot): update the GoDoc for conversion. | ||||||
|
|
||||||
| // For takes a runtime.Object which should be a CR. | ||||||
| // If the given object implements the admission.Defaulter interface, a MutatingWebhook will be wired for this type. | ||||||
| // If the given object implements the admission.Validator interface, a ValidatingWebhook will be wired for this type. | ||||||
| func (blder *WebhookBuilder) For(apiType runtime.Object) *WebhookBuilder { | ||||||
| if blder.apiType != nil { | ||||||
| blder.err = errors.New("For(...) should only be called once, could not assign multiple objects for webhook registration") | ||||||
| } | ||||||
| blder.apiType = apiType | ||||||
| // WithCustomDefaulter takes an admission.CustomDefaulter interface, a MutatingWebhook with the provided opts (admission.DefaulterOption) | ||||||
| // will be wired for this type. | ||||||
| // Deprecated: Use WithAdmissionDefaulter instead. | ||||||
|
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.
Suggested change
similar in l.82 |
||||||
| func (blder *WebhookBuilder[T]) WithCustomDefaulter(defaulter admission.CustomDefaulter, opts ...admission.DefaulterOption) *WebhookBuilder[T] { | ||||||
| blder.customDefaulter = defaulter | ||||||
| blder.customDefaulterOpts = opts | ||||||
| return blder | ||||||
| } | ||||||
|
|
||||||
| // WithDefaulter takes an admission.CustomDefaulter interface, a MutatingWebhook with the provided opts (admission.DefaulterOption) | ||||||
| // will be wired for this type. | ||||||
| func (blder *WebhookBuilder) WithDefaulter(defaulter admission.CustomDefaulter, opts ...admission.DefaulterOption) *WebhookBuilder { | ||||||
| blder.customDefaulter = defaulter | ||||||
| // WithDefaulter sets up the provided admission.Defaulter in a defaulting webhook. | ||||||
| func (blder *WebhookBuilder[T]) WithDefaulter(defaulter admission.Defaulter[T], opts ...admission.DefaulterOption) *WebhookBuilder[T] { | ||||||
| blder.defaulter = defaulter | ||||||
| blder.customDefaulterOpts = opts | ||||||
| return blder | ||||||
| } | ||||||
|
|
||||||
| // WithValidator takes a admission.CustomValidator interface, a ValidatingWebhook will be wired for this type. | ||||||
| func (blder *WebhookBuilder) WithValidator(validator admission.CustomValidator) *WebhookBuilder { | ||||||
| // WithCustomValidator takes a admission.CustomValidator interface, a ValidatingWebhook will be wired for this type. | ||||||
| // Deprecated: Use WithAdmissionValidator instead. | ||||||
| func (blder *WebhookBuilder[T]) WithCustomValidator(validator admission.CustomValidator) *WebhookBuilder[T] { | ||||||
| blder.customValidator = validator | ||||||
| return blder | ||||||
| } | ||||||
|
|
||||||
| // WithValidator sets up the provided admission.Validator in a validating webhook. | ||||||
| func (blder *WebhookBuilder[T]) WithValidator(validator admission.Validator[T]) *WebhookBuilder[T] { | ||||||
| blder.validator = validator | ||||||
| return blder | ||||||
| } | ||||||
|
|
||||||
| // WithConverter takes a func that constructs a converter.Converter. | ||||||
| // The Converter will then be used by the conversion endpoint for the type passed into For(). | ||||||
| func (blder *WebhookBuilder) WithConverter(converterConstructor func(*runtime.Scheme) (conversion.Converter, error)) *WebhookBuilder { | ||||||
| // The Converter will then be used by the conversion endpoint for the type passed into NewWebhookManagedBy() | ||||||
| func (blder *WebhookBuilder[T]) WithConverter(converterConstructor func(*runtime.Scheme) (conversion.Converter, error)) *WebhookBuilder[T] { | ||||||
| blder.converterConstructor = converterConstructor | ||||||
| return blder | ||||||
| } | ||||||
|
|
||||||
| // WithLogConstructor overrides the webhook's LogConstructor. | ||||||
| func (blder *WebhookBuilder) WithLogConstructor(logConstructor func(base logr.Logger, req *admission.Request) logr.Logger) *WebhookBuilder { | ||||||
| func (blder *WebhookBuilder[T]) WithLogConstructor(logConstructor func(base logr.Logger, req *admission.Request) logr.Logger) *WebhookBuilder[T] { | ||||||
| blder.logConstructor = logConstructor | ||||||
| return blder | ||||||
| } | ||||||
|
|
||||||
| // WithContextFunc overrides the webhook's WithContextFunc. | ||||||
| func (blder *WebhookBuilder) WithContextFunc(contextFunc func(context.Context, *http.Request) context.Context) *WebhookBuilder { | ||||||
| func (blder *WebhookBuilder[T]) WithContextFunc(contextFunc func(context.Context, *http.Request) context.Context) *WebhookBuilder[T] { | ||||||
| blder.contextFunc = contextFunc | ||||||
| return blder | ||||||
| } | ||||||
|
|
||||||
| // RecoverPanic indicates whether panics caused by the webhook should be recovered. | ||||||
| // Defaults to true. | ||||||
| func (blder *WebhookBuilder) RecoverPanic(recoverPanic bool) *WebhookBuilder { | ||||||
| func (blder *WebhookBuilder[T]) RecoverPanic(recoverPanic bool) *WebhookBuilder[T] { | ||||||
| blder.recoverPanic = &recoverPanic | ||||||
| return blder | ||||||
| } | ||||||
|
|
@@ -117,25 +121,25 @@ func (blder *WebhookBuilder) RecoverPanic(recoverPanic bool) *WebhookBuilder { | |||||
| // | ||||||
| // Deprecated: WithCustomPath should not be used anymore. | ||||||
| // Please use WithValidatorCustomPath or WithDefaulterCustomPath instead. | ||||||
| func (blder *WebhookBuilder) WithCustomPath(customPath string) *WebhookBuilder { | ||||||
| func (blder *WebhookBuilder[T]) WithCustomPath(customPath string) *WebhookBuilder[T] { | ||||||
| blder.customPath = customPath | ||||||
| return blder | ||||||
| } | ||||||
|
|
||||||
| // WithValidatorCustomPath overrides the path of the Validator. | ||||||
| func (blder *WebhookBuilder) WithValidatorCustomPath(customPath string) *WebhookBuilder { | ||||||
| func (blder *WebhookBuilder[T]) WithValidatorCustomPath(customPath string) *WebhookBuilder[T] { | ||||||
| blder.customValidatorCustomPath = customPath | ||||||
| return blder | ||||||
| } | ||||||
|
|
||||||
| // WithDefaulterCustomPath overrides the path of the Defaulter. | ||||||
| func (blder *WebhookBuilder) WithDefaulterCustomPath(customPath string) *WebhookBuilder { | ||||||
| func (blder *WebhookBuilder[T]) WithDefaulterCustomPath(customPath string) *WebhookBuilder[T] { | ||||||
| blder.customDefaulterCustomPath = customPath | ||||||
| return blder | ||||||
| } | ||||||
|
|
||||||
| // Complete builds the webhook. | ||||||
| func (blder *WebhookBuilder) Complete() error { | ||||||
| func (blder *WebhookBuilder[T]) Complete() error { | ||||||
| // Set the Config | ||||||
| blder.loadRestConfig() | ||||||
|
|
||||||
|
|
@@ -146,13 +150,13 @@ func (blder *WebhookBuilder) Complete() error { | |||||
| return blder.registerWebhooks() | ||||||
| } | ||||||
|
|
||||||
| func (blder *WebhookBuilder) loadRestConfig() { | ||||||
| func (blder *WebhookBuilder[T]) loadRestConfig() { | ||||||
| if blder.config == nil { | ||||||
| blder.config = blder.mgr.GetConfig() | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| func (blder *WebhookBuilder) setLogConstructor() { | ||||||
| func (blder *WebhookBuilder[T]) setLogConstructor() { | ||||||
| if blder.logConstructor == nil { | ||||||
| blder.logConstructor = func(base logr.Logger, req *admission.Request) logr.Logger { | ||||||
| log := base.WithValues( | ||||||
|
|
@@ -172,11 +176,11 @@ func (blder *WebhookBuilder) setLogConstructor() { | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| func (blder *WebhookBuilder) isThereCustomPathConflict() bool { | ||||||
| func (blder *WebhookBuilder[T]) isThereCustomPathConflict() bool { | ||||||
| return (blder.customPath != "" && blder.customDefaulter != nil && blder.customValidator != nil) || (blder.customPath != "" && blder.customDefaulterCustomPath != "") || (blder.customPath != "" && blder.customValidatorCustomPath != "") | ||||||
| } | ||||||
|
|
||||||
| func (blder *WebhookBuilder) registerWebhooks() error { | ||||||
| func (blder *WebhookBuilder[T]) registerWebhooks() error { | ||||||
| typ, err := blder.getType() | ||||||
| if err != nil { | ||||||
| return err | ||||||
|
|
@@ -217,8 +221,11 @@ func (blder *WebhookBuilder) registerWebhooks() error { | |||||
| } | ||||||
|
|
||||||
| // registerDefaultingWebhook registers a defaulting webhook if necessary. | ||||||
| func (blder *WebhookBuilder) registerDefaultingWebhook() error { | ||||||
| mwh := blder.getDefaultingWebhook() | ||||||
| func (blder *WebhookBuilder[T]) registerDefaultingWebhook() error { | ||||||
| mwh, err := blder.getDefaultingWebhook() | ||||||
| if err != nil { | ||||||
| return err | ||||||
| } | ||||||
| if mwh != nil { | ||||||
| mwh.LogConstructor = blder.logConstructor | ||||||
| mwh.WithContextFunc = blder.contextFunc | ||||||
|
|
@@ -244,20 +251,28 @@ func (blder *WebhookBuilder) registerDefaultingWebhook() error { | |||||
| return nil | ||||||
| } | ||||||
|
|
||||||
| func (blder *WebhookBuilder) getDefaultingWebhook() *admission.Webhook { | ||||||
| if defaulter := blder.customDefaulter; defaulter != nil { | ||||||
| w := admission.WithCustomDefaulter(blder.mgr.GetScheme(), blder.apiType, defaulter, blder.customDefaulterOpts...) | ||||||
| if blder.recoverPanic != nil { | ||||||
| w = w.WithRecoverPanic(*blder.recoverPanic) | ||||||
| func (blder *WebhookBuilder[T]) getDefaultingWebhook() (*admission.Webhook, error) { | ||||||
| var w *admission.Webhook | ||||||
| if defaulter := blder.defaulter; defaulter != nil { | ||||||
|
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.
Suggested change
nit. Similar for validator. I would also cleanup the var in the else if. Just unnecessary noise :) |
||||||
| if blder.customDefaulter != nil { | ||||||
| return nil, errors.New("only one of Defaulter or CustomDefaulter can be set") | ||||||
| } | ||||||
| return w | ||||||
| w = admission.WithDefaulter(blder.mgr.GetScheme(), blder.defaulter, blder.customDefaulterOpts...) | ||||||
| } else if customDefaulter := blder.customDefaulter; customDefaulter != nil { | ||||||
| w = admission.WithCustomDefaulter(blder.mgr.GetScheme(), blder.apiType, customDefaulter, blder.customDefaulterOpts...) | ||||||
| } | ||||||
| return nil | ||||||
| if w != nil && blder.recoverPanic != nil { | ||||||
| w = w.WithRecoverPanic(*blder.recoverPanic) | ||||||
| } | ||||||
| return w, nil | ||||||
| } | ||||||
|
|
||||||
| // registerValidatingWebhook registers a validating webhook if necessary. | ||||||
| func (blder *WebhookBuilder) registerValidatingWebhook() error { | ||||||
| vwh := blder.getValidatingWebhook() | ||||||
| func (blder *WebhookBuilder[T]) registerValidatingWebhook() error { | ||||||
| vwh, err := blder.getValidatingWebhook() | ||||||
| if err != nil { | ||||||
| return err | ||||||
| } | ||||||
| if vwh != nil { | ||||||
| vwh.LogConstructor = blder.logConstructor | ||||||
| vwh.WithContextFunc = blder.contextFunc | ||||||
|
|
@@ -283,18 +298,23 @@ func (blder *WebhookBuilder) registerValidatingWebhook() error { | |||||
| return nil | ||||||
| } | ||||||
|
|
||||||
| func (blder *WebhookBuilder) getValidatingWebhook() *admission.Webhook { | ||||||
| if validator := blder.customValidator; validator != nil { | ||||||
| w := admission.WithCustomValidator(blder.mgr.GetScheme(), blder.apiType, validator) | ||||||
| if blder.recoverPanic != nil { | ||||||
| w = w.WithRecoverPanic(*blder.recoverPanic) | ||||||
| func (blder *WebhookBuilder[T]) getValidatingWebhook() (*admission.Webhook, error) { | ||||||
| var w *admission.Webhook | ||||||
| if validator := blder.validator; validator != nil { | ||||||
| if blder.customValidator != nil { | ||||||
| return nil, errors.New("only one of Validator or CustomValidator can be set") | ||||||
| } | ||||||
| return w | ||||||
| w = admission.WithValidator(blder.mgr.GetScheme(), validator) | ||||||
| } else if customValidator := blder.customValidator; customValidator != nil { | ||||||
| w = admission.WithCustomValidator(blder.mgr.GetScheme(), blder.apiType, customValidator) | ||||||
| } | ||||||
| return nil | ||||||
| if w != nil && blder.recoverPanic != nil { | ||||||
| w = w.WithRecoverPanic(*blder.recoverPanic) | ||||||
| } | ||||||
| return w, nil | ||||||
| } | ||||||
|
|
||||||
| func (blder *WebhookBuilder) registerConversionWebhook() error { | ||||||
| func (blder *WebhookBuilder[T]) registerConversionWebhook() error { | ||||||
| if blder.converterConstructor != nil { | ||||||
| converter, err := blder.converterConstructor(blder.mgr.GetScheme()) | ||||||
| if err != nil { | ||||||
|
|
@@ -323,14 +343,14 @@ func (blder *WebhookBuilder) registerConversionWebhook() error { | |||||
| return nil | ||||||
| } | ||||||
|
|
||||||
| func (blder *WebhookBuilder) getType() (runtime.Object, error) { | ||||||
| func (blder *WebhookBuilder[T]) getType() (runtime.Object, error) { | ||||||
| if blder.apiType != nil { | ||||||
| return blder.apiType, nil | ||||||
| } | ||||||
| return nil, errors.New("For() must be called with a valid object") | ||||||
| return nil, errors.New("NewWebhookManagedBy() must be called with a valid object") | ||||||
| } | ||||||
|
|
||||||
| func (blder *WebhookBuilder) isAlreadyHandled(path string) bool { | ||||||
| func (blder *WebhookBuilder[T]) isAlreadyHandled(path string) bool { | ||||||
| if blder.mgr.GetWebhookServer().WebhookMux() == nil { | ||||||
| return false | ||||||
| } | ||||||
|
|
||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
So I've spent a fair bit of time on pondering about how to best deal with this in the webhook builder:
object Tfunc arg and used the existingWithValidatorandWithDefaultermethods. That works but requires everyone to explicitly typeWebhookManagedByas the typing must be know during initial construction and can not be inferred during subsequent method calls. This has two drawbacks IMHO:CustomValidator/Defaulterwould have to type this toruntime.Objectwhich would likely cause further confusionWebhookForthat has the same signature as the currentWebhookMangagedByand made theWebhookManagedBynon-generic and return a*WebhookBuilder[runtime.Object]. This is great for existing code as it will all keep working, but once we remove this, it will be confusing to have different names for the controller and webhook builder IMHOWebhookManagedBygeneric, add an explicit type argument so type inference works and add successors toWithValidator/Defaulterin the form ofWithAdmissionValidator/Defaulter. This means a breaking change for everyone that should be pretty easy to understand and fix and avoid requiring to type this toruntime.Objectfor existing validators/defaulters. The main drawback of that is that the new names aren't as nice (happy to hear suggestions for that).All in all, the last option seemed the by far least bad one. What do you think?
Uh oh!
There was an error while loading. Please reload this page.
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.
Agree with all your points.
I see the following options
No absolutely strong opinions from my side, but if possible I would like to get to WithValidator/Defaulter long-term. I have a slight tendency for option 3. We already have to do a breaking change in this PR, maybe it's better to just get it over with and do slightly more breaking changes now then dragging this out over a few years. It will also give a clear hint to folks that they should just migrate to the typed versions which is super straightforward then (just start using types in Validator/Defaulter, it's not even necessary to use different methods on the builder for Validator/Defaulter). So slightly less effort to do the right migration (use types), slightly more effort to delay the migration and keep using CustomValidator/Defaulter.
Somewhat related. Do you know why
Defaulteris spelled witherandValidatorwithor? (probably don't want to change that though because it's not worth the additional confusion)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.
Okay, if we say that we want to end up with
WithValidator/Defaulterthen option 3 makes most sense, otherwise its just a back and forth. I guess we can add a codesnippet before/after to the changelog to make that at least easyThat is just the English language, one has an
ersuffix and the otherorThere 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.
Agree, sounds good