Skip to content

Commit 04c6578

Browse files
committed
Review Remarks
1 parent ab4e8c9 commit 04c6578

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

pkg/builder/webhook.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,16 @@ import (
3636

3737
// WebhookBuilder builds a Webhook.
3838
type WebhookBuilder struct {
39-
apiType runtime.Object
40-
customDefaulter admission.CustomDefaulter
41-
customValidator admission.CustomValidator
42-
gvk schema.GroupVersionKind
43-
mgr manager.Manager
44-
config *rest.Config
45-
recoverPanic *bool
46-
logConstructor func(base logr.Logger, req *admission.Request) logr.Logger
47-
err error
39+
apiType runtime.Object
40+
customDefaulter admission.CustomDefaulter
41+
customDefaulterOpts []admission.DefaulterOption
42+
customValidator admission.CustomValidator
43+
gvk schema.GroupVersionKind
44+
mgr manager.Manager
45+
config *rest.Config
46+
recoverPanic *bool
47+
logConstructor func(base logr.Logger, req *admission.Request) logr.Logger
48+
err error
4849
}
4950

5051
// WebhookManagedBy returns a new webhook builder.
@@ -65,9 +66,11 @@ func (blder *WebhookBuilder) For(apiType runtime.Object) *WebhookBuilder {
6566
return blder
6667
}
6768

68-
// WithDefaulter takes an admission.CustomDefaulter interface, a MutatingWebhook will be wired for this type.
69-
func (blder *WebhookBuilder) WithDefaulter(defaulter admission.CustomDefaulter) *WebhookBuilder {
69+
// WithDefaulter takes an admission.CustomDefaulter interface, a MutatingWebhook with the provided opts (admission.DefaulterOption)
70+
// will be wired for this type.
71+
func (blder *WebhookBuilder) WithDefaulter(defaulter admission.CustomDefaulter, opts ...admission.DefaulterOption) *WebhookBuilder {
7072
blder.customDefaulter = defaulter
73+
blder.customDefaulterOpts = opts
7174
return blder
7275
}
7376

@@ -170,7 +173,7 @@ func (blder *WebhookBuilder) registerDefaultingWebhook() {
170173

171174
func (blder *WebhookBuilder) getDefaultingWebhook() *admission.Webhook {
172175
if defaulter := blder.customDefaulter; defaulter != nil {
173-
w := admission.WithCustomDefaulter(blder.mgr.GetScheme(), blder.apiType, defaulter)
176+
w := admission.WithCustomDefaulter(blder.mgr.GetScheme(), blder.apiType, defaulter, blder.customDefaulterOpts...)
174177
if blder.recoverPanic != nil {
175178
w = w.WithRecoverPanic(*blder.recoverPanic)
176179
}

pkg/webhook/admission/defaulter_custom.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,18 @@ type defaulterOptions struct {
4040
removeUnknownFields bool
4141
}
4242

43-
type defaulterOption func(*defaulterOptions)
43+
// DefaulterOption defines the type of a CustomDefaulter's option
44+
type DefaulterOption func(*defaulterOptions)
4445

45-
// DefaulterRemoveUnknownFields makes the defaulter prune the fields that are not recognized in the local scheme.
46+
// DefaulterRemoveUnknownFields makes the defaulter prune fields that are in the json object retrieved by the
47+
// webhook but not in the local go type. This happens for example when the CRD in the apiserver has fields that
48+
// our go type doesn't know about, because it's outdated.
4649
func DefaulterRemoveUnknownFields(o *defaulterOptions) {
4750
o.removeUnknownFields = true
4851
}
4952

5053
// WithCustomDefaulter creates a new Webhook for a CustomDefaulter interface.
51-
func WithCustomDefaulter(scheme *runtime.Scheme, obj runtime.Object, defaulter CustomDefaulter, opts ...defaulterOption) *Webhook {
54+
func WithCustomDefaulter(scheme *runtime.Scheme, obj runtime.Object, defaulter CustomDefaulter, opts ...DefaulterOption) *Webhook {
5255
options := &defaulterOptions{}
5356
for _, o := range opts {
5457
o(options)

0 commit comments

Comments
 (0)