Skip to content

Commit

Permalink
Allow to wire a mutation handler
Browse files Browse the repository at this point in the history
  • Loading branch information
alculquicondor committed Sep 3, 2024
1 parent 3854680 commit 5fd81ad
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
26 changes: 19 additions & 7 deletions pkg/builder/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
// WebhookBuilder builds a Webhook.
type WebhookBuilder struct {
apiType runtime.Object
mutatorFactory admission.HandlerFactory
customDefaulter admission.CustomDefaulter
customValidator admission.CustomValidator
gvk schema.GroupVersionKind
Expand Down Expand Up @@ -65,6 +66,12 @@ func (blder *WebhookBuilder) For(apiType runtime.Object) *WebhookBuilder {
return blder
}

// WithMutationHandler takes an admission.ObjectHandler interface, a MutatingWebhook will be wired for this type.

Check failure on line 69 in pkg/builder/webhook.go

View workflow job for this annotation

GitHub Actions / lint

exported: comment on exported method WebhookBuilder.WithMutatorFactory should be of the form "WithMutatorFactory ..." (revive)
func (blder *WebhookBuilder) WithMutatorFactory(factory admission.HandlerFactory) *WebhookBuilder {
blder.mutatorFactory = factory
return blder
}

// WithDefaulter takes an admission.CustomDefaulter interface, a MutatingWebhook will be wired for this type.
func (blder *WebhookBuilder) WithDefaulter(defaulter admission.CustomDefaulter) *WebhookBuilder {
blder.customDefaulter = defaulter
Expand Down Expand Up @@ -169,14 +176,19 @@ func (blder *WebhookBuilder) registerDefaultingWebhook() {
}

func (blder *WebhookBuilder) getDefaultingWebhook() *admission.Webhook {
if defaulter := blder.customDefaulter; defaulter != nil {
w := admission.WithCustomDefaulter(blder.mgr.GetScheme(), blder.apiType, defaulter)
if blder.recoverPanic != nil {
w = w.WithRecoverPanic(*blder.recoverPanic)
}
return w
var w *admission.Webhook
if factory := blder.mutatorFactory; factory != nil {
w = admission.WithHandlerFactory(blder.mgr.GetScheme(), blder.apiType, factory)
} else if defaulter := blder.customDefaulter; defaulter != nil {
w = admission.WithCustomDefaulter(blder.mgr.GetScheme(), blder.apiType, defaulter)
}
return nil
if w == nil {
return nil
}
if blder.recoverPanic != nil {
w = w.WithRecoverPanic(*blder.recoverPanic)
}
return w
}

// registerValidatingWebhook registers a validating webhook if necessary.
Expand Down
10 changes: 10 additions & 0 deletions pkg/webhook/admission/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"gomodules.xyz/jsonpatch/v2"
admissionv1 "k8s.io/api/admission/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/json"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -95,6 +96,8 @@ func (r *Response) Complete(req Request) error {
return nil
}

type HandlerFactory func(obj runtime.Object, decoder Decoder) Handler

Check failure on line 99 in pkg/webhook/admission/webhook.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported type HandlerFactory should have comment or be unexported (revive)

// Handler can handle an AdmissionRequest.
type Handler interface {
// Handle yields a response to an AdmissionRequest.
Expand All @@ -114,6 +117,13 @@ func (f HandlerFunc) Handle(ctx context.Context, req Request) Response {
return f(ctx, req)
}

// WithMutator creates a new Webhook for a handler factory.

Check failure on line 120 in pkg/webhook/admission/webhook.go

View workflow job for this annotation

GitHub Actions / lint

exported: comment on exported function WithHandlerFactory should be of the form "WithHandlerFactory ..." (revive)
func WithHandlerFactory(scheme *runtime.Scheme, obj runtime.Object, factory HandlerFactory) *Webhook {
return &Webhook{
Handler: factory(obj, NewDecoder(scheme)),
}
}

// Webhook represents each individual webhook.
//
// It must be registered with a webhook.Server or
Expand Down

0 comments on commit 5fd81ad

Please sign in to comment.