Skip to content

Commit

Permalink
Merge pull request #81 from snorwin/bugfix-injection
Browse files Browse the repository at this point in the history
fix: nil pointer panic due to failed injection
  • Loading branch information
snorwin authored Dec 19, 2023
2 parents 4c425ec + 88256cd commit 8471b08
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
20 changes: 18 additions & 2 deletions pkg/webhook/injection.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,39 @@ import (
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

// ClientInjector is used to inject a client.Client into webhook handlers.
type ClientInjector interface {
InjectClient(client.Client) error
}

// ensure InjectedClient implements ClientInjector
var _ ClientInjector = &InjectedClient{}

// InjectedClient holds an injected client.Client
type InjectedClient struct {
Client client.Client
}

// InjectClient implements the inject.Client interface.
// InjectClient implements the ClientInjector interface.
func (i *InjectedClient) InjectClient(client client.Client) error {
i.Client = client
return nil
}

// DecoderInjector is used to inject an admission.Decoder into webhook handlers.
type DecoderInjector interface {
InjectDecoder(*admission.Decoder) error
}

// ensure InjectedDecoder implements DecoderInjector
var _ DecoderInjector = &InjectedDecoder{}

// InjectedDecoder holds an injected admission.Decoder
type InjectedDecoder struct {
Decoder *admission.Decoder
}

// InjectDecoder implements the admission.DecoderInjector interface.
// InjectDecoder implements the DecoderInjector interface.
func (i *InjectedDecoder) InjectDecoder(decoder *admission.Decoder) error {
i.Decoder = decoder
return nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ func (blder *Builder) Complete(i interface{}) error {
return fmt.Errorf("webhook instance %v does implement neither Mutator nor Validator interface", i)
}

if injector, ok := i.(InjectedClient); ok {
if injector, ok := i.(ClientInjector); ok {
if err := injector.InjectClient(blder.mgr.GetClient()); err != nil {
return err
}
}

if injector, ok := i.(InjectedDecoder); ok {
if injector, ok := i.(DecoderInjector); ok {
if err := injector.InjectDecoder(decoder); err != nil {
return err
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/webhook/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ var _ = Describe("Webhook", func() {
Complete(&webhook.ValidatingWebhook{})
Ω(err).ShouldNot(HaveOccurred())
})
It("should inject client and decoder", func() {
wh := &webhook.ValidatingWebhook{}
err := webhook.NewGenericWebhookManagedBy(mgr).
For(&corev1.Pod{}).
Complete(wh)
Ω(err).ShouldNot(HaveOccurred())

Ω(wh.Client).ShouldNot(BeNil())
Ω(wh.Decoder).ShouldNot(BeNil())
})
It("should not fail if mutating webhook is already registered", func() {
err := webhook.NewGenericWebhookManagedBy(mgr).
For(&corev1.Pod{}).
Expand Down

0 comments on commit 8471b08

Please sign in to comment.