Skip to content

Commit 5963ad2

Browse files
committed
Fix admission webhook injection, no mgr, no client
The webhook server logic was not handling injection correctly -- it implemented injection for a couple of specific fields, rather than receiving and dealing with an injectfunc. This fixes that. As a side effect, it also removes the handle to manager and client from the webhook server, since those aren't used by the server, and should thus be injected from the manager.
1 parent eee0406 commit 5963ad2

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

example/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,15 @@ func main() {
9292
Build()
9393

9494
entryLog.Info("setting up webhook server")
95-
as, err := webhook.NewServer(mgr, webhook.ServerOptions{
95+
as, err := webhook.NewServer(webhook.ServerOptions{
9696
Port: 9876,
9797
CertDir: "/tmp/cert",
9898
})
9999
if err != nil {
100100
entryLog.Error(err, "unable to create a new webhook server")
101101
os.Exit(1)
102102
}
103+
mgr.Add(as)
103104

104105
entryLog.Info("registering webhooks to the webhook server")
105106
err = as.Register(mutatingWebhook, validatingWebhook)

pkg/webhook/admission/webhook.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,12 @@ func (w *Webhook) Validate() error {
209209
return nil
210210
}
211211

212-
var _ inject.Injector = &Webhook{}
213-
214-
// InjectFunc injects dependencies into the handlers.
212+
// InjectFunc injects the field setter into the webhook.
215213
func (w *Webhook) InjectFunc(f inject.Func) error {
214+
// inject directly into the handlers. It would be more correct
215+
// to do this in a sync.Once in Handle (since we don't have some
216+
// other start/finalize-type method), but it's more efficient to
217+
// do it here, presumably.
216218
for _, handler := range w.Handlers {
217219
if err := f(handler); err != nil {
218220
return err

pkg/webhook/server.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"strconv"
2626
"sync"
2727

28-
"sigs.k8s.io/controller-runtime/pkg/manager"
2928
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
3029
)
3130

@@ -62,11 +61,8 @@ type Server struct {
6261
// registry maps a path to a http.Handler.
6362
registry map[string]http.Handler
6463

65-
// setFields is used to inject dependencies into webhooks
66-
setFields func(i interface{}) error
67-
68-
// manager is the manager that this webhook server will be registered.
69-
manager manager.Manager
64+
// setFields allows injecting dependencies from an external source
65+
setFields inject.Func
7066

7167
once sync.Once
7268
}
@@ -85,12 +81,11 @@ type Webhook interface {
8581
}
8682

8783
// NewServer creates a new admission webhook server.
88-
func NewServer(mgr manager.Manager, options ServerOptions) (*Server, error) {
84+
func NewServer(options ServerOptions) (*Server, error) {
8985
as := &Server{
9086
sMux: http.NewServeMux(),
9187
registry: map[string]http.Handler{},
9288
ServerOptions: options,
93-
manager: mgr,
9489
}
9590

9691
return as, nil
@@ -130,23 +125,27 @@ func (s *Server) Register(webhooks ...Webhook) error {
130125
}
131126
}
132127

133-
// Lazily add Server to manager.
134-
// Because the all webhook handlers to be in place, so we can inject the things they need.
135-
return s.manager.Add(s)
128+
return nil
136129
}
137130

138131
// Handle registers a http.Handler for the given pattern.
139132
func (s *Server) Handle(pattern string, handler http.Handler) {
140133
s.sMux.Handle(pattern, handler)
141134
}
142135

143-
var _ manager.Runnable = &Server{}
144-
145136
// Start runs the server.
146137
// It will install the webhook related resources depend on the server configuration.
147138
func (s *Server) Start(stop <-chan struct{}) error {
148139
s.once.Do(s.setDefault)
149140

141+
// inject fields here as opposed to in Register so that we're certain to have our setFields
142+
// function available.
143+
for _, webhook := range s.registry {
144+
if err := s.setFields(webhook); err != nil {
145+
return err
146+
}
147+
}
148+
150149
// TODO: watch the cert dir. Reload the cert if it changes
151150
cert, err := tls.LoadX509KeyPair(path.Join(s.CertDir, certName), path.Join(s.CertDir, keyName))
152151
if err != nil {
@@ -187,7 +186,7 @@ func (s *Server) Start(stop <-chan struct{}) error {
187186
return nil
188187
}
189188

190-
// InjectFunc injects dependencies into the handlers.
189+
// InjectFunc injects the field setter into the server.
191190
func (s *Server) InjectFunc(f inject.Func) error {
192191
s.setFields = f
193192
return nil

0 commit comments

Comments
 (0)