Skip to content

Commit 8471b08

Browse files
authored
Merge pull request #81 from snorwin/bugfix-injection
fix: nil pointer panic due to failed injection
2 parents 4c425ec + 88256cd commit 8471b08

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

pkg/webhook/injection.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,39 @@ import (
55
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
66
)
77

8+
// ClientInjector is used to inject a client.Client into webhook handlers.
9+
type ClientInjector interface {
10+
InjectClient(client.Client) error
11+
}
12+
13+
// ensure InjectedClient implements ClientInjector
14+
var _ ClientInjector = &InjectedClient{}
15+
816
// InjectedClient holds an injected client.Client
917
type InjectedClient struct {
1018
Client client.Client
1119
}
1220

13-
// InjectClient implements the inject.Client interface.
21+
// InjectClient implements the ClientInjector interface.
1422
func (i *InjectedClient) InjectClient(client client.Client) error {
1523
i.Client = client
1624
return nil
1725
}
1826

27+
// DecoderInjector is used to inject an admission.Decoder into webhook handlers.
28+
type DecoderInjector interface {
29+
InjectDecoder(*admission.Decoder) error
30+
}
31+
32+
// ensure InjectedDecoder implements DecoderInjector
33+
var _ DecoderInjector = &InjectedDecoder{}
34+
1935
// InjectedDecoder holds an injected admission.Decoder
2036
type InjectedDecoder struct {
2137
Decoder *admission.Decoder
2238
}
2339

24-
// InjectDecoder implements the admission.DecoderInjector interface.
40+
// InjectDecoder implements the DecoderInjector interface.
2541
func (i *InjectedDecoder) InjectDecoder(decoder *admission.Decoder) error {
2642
i.Decoder = decoder
2743
return nil

pkg/webhook/webhook.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ func (blder *Builder) Complete(i interface{}) error {
108108
return fmt.Errorf("webhook instance %v does implement neither Mutator nor Validator interface", i)
109109
}
110110

111-
if injector, ok := i.(InjectedClient); ok {
111+
if injector, ok := i.(ClientInjector); ok {
112112
if err := injector.InjectClient(blder.mgr.GetClient()); err != nil {
113113
return err
114114
}
115115
}
116116

117-
if injector, ok := i.(InjectedDecoder); ok {
117+
if injector, ok := i.(DecoderInjector); ok {
118118
if err := injector.InjectDecoder(decoder); err != nil {
119119
return err
120120
}

pkg/webhook/webhook_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ var _ = Describe("Webhook", func() {
7070
Complete(&webhook.ValidatingWebhook{})
7171
Ω(err).ShouldNot(HaveOccurred())
7272
})
73+
It("should inject client and decoder", func() {
74+
wh := &webhook.ValidatingWebhook{}
75+
err := webhook.NewGenericWebhookManagedBy(mgr).
76+
For(&corev1.Pod{}).
77+
Complete(wh)
78+
Ω(err).ShouldNot(HaveOccurred())
79+
80+
Ω(wh.Client).ShouldNot(BeNil())
81+
Ω(wh.Decoder).ShouldNot(BeNil())
82+
})
7383
It("should not fail if mutating webhook is already registered", func() {
7484
err := webhook.NewGenericWebhookManagedBy(mgr).
7585
For(&corev1.Pod{}).

0 commit comments

Comments
 (0)