Skip to content

Commit 47a7b28

Browse files
author
Mengqi Yu
committed
🐛 use mgr.SetFields() for webhook server and implement injector interface for webhooks
1 parent d169520 commit 47a7b28

File tree

2 files changed

+18
-30
lines changed

2 files changed

+18
-30
lines changed

pkg/webhook/admission/webhook.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030

3131
admissionv1beta1 "k8s.io/api/admission/v1beta1"
3232
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
33-
"sigs.k8s.io/controller-runtime/pkg/client"
3433
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
3534
atypes "sigs.k8s.io/controller-runtime/pkg/webhook/admission/types"
3635
"sigs.k8s.io/controller-runtime/pkg/webhook/types"
@@ -210,24 +209,12 @@ func (w *Webhook) Validate() error {
210209
return nil
211210
}
212211

213-
var _ inject.Client = &Webhook{}
212+
var _ inject.Injector = &Webhook{}
214213

215-
// InjectClient injects the client into the handlers
216-
func (w *Webhook) InjectClient(c client.Client) error {
214+
// InjectFunc injects dependencies into the handlers.
215+
func (w *Webhook) InjectFunc(f inject.Func) error {
217216
for _, handler := range w.Handlers {
218-
if _, err := inject.ClientInto(c, handler); err != nil {
219-
return err
220-
}
221-
}
222-
return nil
223-
}
224-
225-
var _ inject.Decoder = &Webhook{}
226-
227-
// InjectDecoder injects the decoder into the handlers
228-
func (w *Webhook) InjectDecoder(d atypes.Decoder) error {
229-
for _, handler := range w.Handlers {
230-
if _, err := inject.DecoderInto(d, handler); err != nil {
217+
if err := f(handler); err != nil {
231218
return err
232219
}
233220
}

pkg/webhook/server.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ type Server struct {
7171
// manager is the manager that this webhook server will be registered.
7272
manager manager.Manager
7373

74+
// setFields is used to inject dependencies into webhooks
75+
setFields func(i interface{}) error
76+
7477
// err will be non-nil if there is an error occur during initialization.
7578
err error
7679

@@ -160,7 +163,13 @@ func (s *Server) Complete() error {
160163
if s.err != nil {
161164
return s.err
162165
}
163-
// TODO(mengqiy): inject dependencies into each http.Handler
166+
167+
for path := range s.registry {
168+
// Inject dependencies into each http.Handler
169+
if err := s.setFields(s.registry[path]); err != nil {
170+
return err
171+
}
172+
}
164173
return s.manager.Add(s)
165174
}
166175

@@ -214,18 +223,10 @@ func (s *Server) Start(stop <-chan struct{}) error {
214223
return nil
215224
}
216225

217-
var _ inject.Client = &Server{}
226+
var _ inject.Injector = &Server{}
218227

219-
// InjectClient injects the client into the server
220-
func (s *Server) InjectClient(c client.Client) error {
221-
s.Client = c
222-
for path := range s.registry {
223-
// TODO(mengqiy): remove this in PR #316
224-
if wh, ok := s.registry[path].(Webhook); ok {
225-
if _, err := inject.ClientInto(c, wh.Handler()); err != nil {
226-
return err
227-
}
228-
}
229-
}
228+
// InjectFunc injects dependencies into the handlers.
229+
func (s *Server) InjectFunc(f inject.Func) error {
230+
s.setFields = f
230231
return nil
231232
}

0 commit comments

Comments
 (0)