Skip to content

Commit 5fd81ad

Browse files
Allow to wire a mutation handler
1 parent 3854680 commit 5fd81ad

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

pkg/builder/webhook.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
// WebhookBuilder builds a Webhook.
3838
type WebhookBuilder struct {
3939
apiType runtime.Object
40+
mutatorFactory admission.HandlerFactory
4041
customDefaulter admission.CustomDefaulter
4142
customValidator admission.CustomValidator
4243
gvk schema.GroupVersionKind
@@ -65,6 +66,12 @@ func (blder *WebhookBuilder) For(apiType runtime.Object) *WebhookBuilder {
6566
return blder
6667
}
6768

69+
// WithMutationHandler takes an admission.ObjectHandler interface, a MutatingWebhook will be wired for this type.
70+
func (blder *WebhookBuilder) WithMutatorFactory(factory admission.HandlerFactory) *WebhookBuilder {
71+
blder.mutatorFactory = factory
72+
return blder
73+
}
74+
6875
// WithDefaulter takes an admission.CustomDefaulter interface, a MutatingWebhook will be wired for this type.
6976
func (blder *WebhookBuilder) WithDefaulter(defaulter admission.CustomDefaulter) *WebhookBuilder {
7077
blder.customDefaulter = defaulter
@@ -169,14 +176,19 @@ func (blder *WebhookBuilder) registerDefaultingWebhook() {
169176
}
170177

171178
func (blder *WebhookBuilder) getDefaultingWebhook() *admission.Webhook {
172-
if defaulter := blder.customDefaulter; defaulter != nil {
173-
w := admission.WithCustomDefaulter(blder.mgr.GetScheme(), blder.apiType, defaulter)
174-
if blder.recoverPanic != nil {
175-
w = w.WithRecoverPanic(*blder.recoverPanic)
176-
}
177-
return w
179+
var w *admission.Webhook
180+
if factory := blder.mutatorFactory; factory != nil {
181+
w = admission.WithHandlerFactory(blder.mgr.GetScheme(), blder.apiType, factory)
182+
} else if defaulter := blder.customDefaulter; defaulter != nil {
183+
w = admission.WithCustomDefaulter(blder.mgr.GetScheme(), blder.apiType, defaulter)
178184
}
179-
return nil
185+
if w == nil {
186+
return nil
187+
}
188+
if blder.recoverPanic != nil {
189+
w = w.WithRecoverPanic(*blder.recoverPanic)
190+
}
191+
return w
180192
}
181193

182194
// registerValidatingWebhook registers a validating webhook if necessary.

pkg/webhook/admission/webhook.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"gomodules.xyz/jsonpatch/v2"
2828
admissionv1 "k8s.io/api/admission/v1"
2929
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
30+
"k8s.io/apimachinery/pkg/runtime"
3031
"k8s.io/apimachinery/pkg/util/json"
3132
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3233
"k8s.io/klog/v2"
@@ -95,6 +96,8 @@ func (r *Response) Complete(req Request) error {
9596
return nil
9697
}
9798

99+
type HandlerFactory func(obj runtime.Object, decoder Decoder) Handler
100+
98101
// Handler can handle an AdmissionRequest.
99102
type Handler interface {
100103
// Handle yields a response to an AdmissionRequest.
@@ -114,6 +117,13 @@ func (f HandlerFunc) Handle(ctx context.Context, req Request) Response {
114117
return f(ctx, req)
115118
}
116119

120+
// WithMutator creates a new Webhook for a handler factory.
121+
func WithHandlerFactory(scheme *runtime.Scheme, obj runtime.Object, factory HandlerFactory) *Webhook {
122+
return &Webhook{
123+
Handler: factory(obj, NewDecoder(scheme)),
124+
}
125+
}
126+
117127
// Webhook represents each individual webhook.
118128
//
119129
// It must be registered with a webhook.Server or

0 commit comments

Comments
 (0)