Skip to content

Commit c2ca3a6

Browse files
committed
Review Remarks
1 parent 545a8c5 commit c2ca3a6

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

pkg/builder/webhook.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,17 @@ import (
3737

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

5253
// WebhookManagedBy returns a new webhook builder.
@@ -67,9 +68,11 @@ func (blder *WebhookBuilder) For(apiType runtime.Object) *WebhookBuilder {
6768
return blder
6869
}
6970

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

@@ -194,7 +197,7 @@ func (blder *WebhookBuilder) registerDefaultingWebhook() error {
194197

195198
func (blder *WebhookBuilder) getDefaultingWebhook() *admission.Webhook {
196199
if defaulter := blder.customDefaulter; defaulter != nil {
197-
w := admission.WithCustomDefaulter(blder.mgr.GetScheme(), blder.apiType, defaulter)
200+
w := admission.WithCustomDefaulter(blder.mgr.GetScheme(), blder.apiType, defaulter, blder.customDefaulterOpts...)
198201
if blder.recoverPanic != nil {
199202
w = w.WithRecoverPanic(*blder.recoverPanic)
200203
}

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)