Skip to content

Commit

Permalink
Merge pull request #21 from bakito/main
Browse files Browse the repository at this point in the history
check if at least one interface is implemented
  • Loading branch information
snorwin authored Nov 11, 2021
2 parents c680e64 + eab3fc7 commit acad887
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
11 changes: 9 additions & 2 deletions pkg/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -100,14 +102,19 @@ 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
}

func (blder *Builder) createAdmissionWebhook(handler Handler) (*admission.Webhook, error) {
w := &admission.Webhook{
Handler: handler,
Handler: handler,
}

// inject scheme for decoder
Expand Down
4 changes: 2 additions & 2 deletions pkg/webhook/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down

0 comments on commit acad887

Please sign in to comment.