Skip to content

Commit aa84331

Browse files
author
Mengqi Yu
committed
🐛 fix webhook injector
1 parent abf0c07 commit aa84331

File tree

2 files changed

+9
-30
lines changed

2 files changed

+9
-30
lines changed

pkg/webhook/admission/webhook.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
admissionv1beta1 "k8s.io/api/admission/v1beta1"
3232
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
3333
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
34-
"sigs.k8s.io/controller-runtime/pkg/client"
3534
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
3635
atypes "sigs.k8s.io/controller-runtime/pkg/webhook/admission/types"
3736
"sigs.k8s.io/controller-runtime/pkg/webhook/types"
@@ -234,24 +233,12 @@ func (w *Webhook) Validate() error {
234233
return nil
235234
}
236235

237-
var _ inject.Client = &Webhook{}
236+
var _ inject.Injector = &Webhook{}
238237

239-
// InjectClient injects the client into the handlers
240-
func (w *Webhook) InjectClient(c client.Client) error {
238+
// InjectFunc injects dependencies into the handlers.
239+
func (w *Webhook) InjectFunc(f inject.Func) error {
241240
for _, handler := range w.Handlers {
242-
if _, err := inject.ClientInto(c, handler); err != nil {
243-
return err
244-
}
245-
}
246-
return nil
247-
}
248-
249-
var _ inject.Decoder = &Webhook{}
250-
251-
// InjectDecoder injects the decoder into the handlers
252-
func (w *Webhook) InjectDecoder(d atypes.Decoder) error {
253-
for _, handler := range w.Handlers {
254-
if _, err := inject.DecoderInto(d, handler); err != nil {
241+
if err := f(handler); err != nil {
255242
return err
256243
}
257244
}

pkg/webhook/server.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"sigs.k8s.io/controller-runtime/pkg/client"
3232
"sigs.k8s.io/controller-runtime/pkg/manager"
3333
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
34-
atypes "sigs.k8s.io/controller-runtime/pkg/webhook/admission/types"
3534
"sigs.k8s.io/controller-runtime/pkg/webhook/internal/cert"
3635
"sigs.k8s.io/controller-runtime/pkg/webhook/internal/cert/writer"
3736
"sigs.k8s.io/controller-runtime/pkg/webhook/types"
@@ -181,6 +180,11 @@ func (s *Server) Register(webhooks ...Webhook) error {
181180
}
182181
s.registry[webhook.GetPath()] = webhooks[i]
183182
s.sMux.Handle(webhook.GetPath(), webhook.Handler())
183+
184+
// Inject dependencies into each webhook
185+
if err := s.manager.SetFields(webhooks[i]); err != nil {
186+
return err
187+
}
184188
}
185189

186190
// Lazily add Server to manager.
@@ -294,15 +298,3 @@ func (s *Server) InjectClient(c client.Client) error {
294298
}
295299
return nil
296300
}
297-
298-
var _ inject.Decoder = &Server{}
299-
300-
// InjectDecoder injects the client into the server
301-
func (s *Server) InjectDecoder(d atypes.Decoder) error {
302-
for _, wh := range s.registry {
303-
if _, err := inject.DecoderInto(d, wh.Handler()); err != nil {
304-
return err
305-
}
306-
}
307-
return nil
308-
}

0 commit comments

Comments
 (0)