diff --git a/pkg/webhook/webhook.go b/pkg/webhook/webhook.go index 964ab19..764042f 100644 --- a/pkg/webhook/webhook.go +++ b/pkg/webhook/webhook.go @@ -58,7 +58,7 @@ func (blder *Builder) WithMutatePrefix(prefix string) *Builder { return blder } -// WithValidatePrefix sets a custom prefix for the mutate path of the webhook, default is '/validate-' +// WithValidatePrefix sets a custom prefix for the validate path of the webhook, default is '/validate-' func (blder *Builder) WithValidatePrefix(prefix string) *Builder { blder.prefixMutate = prefix return blder @@ -80,6 +80,7 @@ func (blder *Builder) Complete(i interface{}) error { return fmt.Errorf("validating prefix %q must start with '/'", blder.prefixValidate) } + isWebhook := false if validator, ok := i.(Validator); ok { w, err := blder.createAdmissionWebhook(withValidationHandler(validator, blder.apiType)) if err != nil { @@ -89,6 +90,7 @@ func (blder *Builder) Complete(i interface{}) error { if err := blder.registerValidatingWebhook(w); err != nil { return err } + isWebhook = true } if mutator, ok := i.(Mutator); ok { @@ -100,6 +102,11 @@ func (blder *Builder) Complete(i interface{}) error { if err := blder.registerMutatingWebhook(w); err != nil { return err } + isWebhook = true + } + + if !isWebhook { + return fmt.Errorf("webhook instance %v does implement neither Mutator nor Validator interface", i) } return nil @@ -107,7 +114,7 @@ func (blder *Builder) Complete(i interface{}) error { func (blder *Builder) createAdmissionWebhook(handler Handler) (*admission.Webhook, error) { w := &admission.Webhook{ - Handler: handler, + Handler: handler, } // inject scheme for decoder diff --git a/pkg/webhook/webhook_test.go b/pkg/webhook/webhook_test.go index 1db16ed..a66999d 100644 --- a/pkg/webhook/webhook_test.go +++ b/pkg/webhook/webhook_test.go @@ -92,11 +92,11 @@ var _ = Describe("Webhook", func() { Complete(&webhook.ValidatingWebhook{}) Ω(err).ShouldNot(HaveOccurred()) }) - It("should not fail if interface doesn't match", func() { + It("should fail if interface doesn't match", func() { err := webhook.NewGenericWebhookManagedBy(mgr). For(&corev1.Pod{}). Complete(struct{}{}) - Ω(err).ShouldNot(HaveOccurred()) + Ω(err).Should(HaveOccurred()) }) It("should fail if api type isn't specified", func() { err := webhook.NewGenericWebhookManagedBy(mgr).