Skip to content

Commit 29adcfc

Browse files
author
Mengqi Yu
committed
⚠️fix webhook injector, drop name and client
1 parent 81b48be commit 29adcfc

File tree

3 files changed

+27
-85
lines changed

3 files changed

+27
-85
lines changed

example/main.go

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

9494
entryLog.Info("setting up webhook server")
95-
as, err := webhook.NewServer("foo-admission-server", mgr, webhook.ServerOptions{
95+
as, err := webhook.NewServer(mgr, webhook.ServerOptions{
9696
Port: 9876,
9797
CertDir: "/tmp/cert",
9898
})
@@ -104,7 +104,7 @@ func main() {
104104
entryLog.Info("registering webhooks to the webhook server")
105105
err = as.Register(mutatingWebhook, validatingWebhook)
106106
if err != nil {
107-
entryLog.Error(err, "unable to register webhooks in the admission server")
107+
entryLog.Error(err, "unable to setup the admission server")
108108
os.Exit(1)
109109
}
110110

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: 21 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,14 @@ package webhook
1919
import (
2020
"context"
2121
"crypto/tls"
22-
"fmt"
2322
"net"
2423
"net/http"
2524
"path"
2625
"strconv"
2726
"sync"
2827

29-
"sigs.k8s.io/controller-runtime/pkg/client"
30-
"sigs.k8s.io/controller-runtime/pkg/client/config"
3128
"sigs.k8s.io/controller-runtime/pkg/manager"
3229
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
33-
atypes "sigs.k8s.io/controller-runtime/pkg/webhook/admission/types"
3430
)
3531

3632
const (
@@ -50,28 +46,20 @@ type ServerOptions struct {
5046
// If using SecretCertWriter in Provisioner, the server will provision the certificate in a secret,
5147
// the user is responsible to mount the secret to the this location for the server to consume.
5248
CertDir string
53-
54-
// Client is a client defined in controller-runtime instead of a client-go client.
55-
// It knows how to talk to a kubernetes cluster.
56-
// Client will be injected by the manager if not set.
57-
Client client.Client
58-
59-
// err will be non-nil if there is an error occur during initialization.
60-
err error // nolint: structcheck
6149
}
6250

6351
// Server is an admission webhook server that can serve traffic and
6452
// generates related k8s resources for deploying.
6553
type Server struct {
66-
// Name is the name of server
67-
Name string
68-
6954
// ServerOptions contains options for configuring the admission server.
7055
ServerOptions
7156

7257
sMux *http.ServeMux
7358
// registry maps a path to a http.Handler.
74-
registry map[string]Webhook
59+
registry map[string]http.Handler
60+
61+
// setFields is used to inject dependencies into webhooks
62+
setFields func(i interface{}) error
7563

7664
// manager is the manager that this webhook server will be registered.
7765
manager manager.Manager
@@ -81,6 +69,8 @@ type Server struct {
8169

8270
// Webhook defines the basics that a webhook should support.
8371
type Webhook interface {
72+
http.Handler
73+
8474
// GetPath returns the path that the webhook registered.
8575
GetPath() string
8676
// Handler returns a http.Handler for the webhook.
@@ -91,11 +81,10 @@ type Webhook interface {
9181
}
9282

9383
// NewServer creates a new admission webhook server.
94-
func NewServer(name string, mgr manager.Manager, options ServerOptions) (*Server, error) {
84+
func NewServer(mgr manager.Manager, options ServerOptions) (*Server, error) {
9585
as := &Server{
96-
Name: name,
9786
sMux: http.NewServeMux(),
98-
registry: map[string]Webhook{},
87+
registry: map[string]http.Handler{},
9988
ServerOptions: options,
10089
manager: mgr,
10190
}
@@ -105,34 +94,18 @@ func NewServer(name string, mgr manager.Manager, options ServerOptions) (*Server
10594

10695
// setDefault does defaulting for the Server.
10796
func (s *Server) setDefault() {
108-
if len(s.Name) == 0 {
109-
s.Name = "default-k8s-webhook-server"
110-
}
11197
if s.registry == nil {
112-
s.registry = map[string]Webhook{}
98+
s.registry = map[string]http.Handler{}
11399
}
114100
if s.sMux == nil {
115-
s.sMux = http.DefaultServeMux
101+
s.sMux = http.NewServeMux()
116102
}
117103
if s.Port <= 0 {
118104
s.Port = 443
119105
}
120106
if len(s.CertDir) == 0 {
121107
s.CertDir = path.Join("k8s-webhook-server", "cert")
122108
}
123-
124-
if s.Client == nil {
125-
cfg, err := config.GetConfig()
126-
if err != nil {
127-
s.err = err
128-
return
129-
}
130-
s.Client, err = client.New(cfg, client.Options{})
131-
if err != nil {
132-
s.err = err
133-
return
134-
}
135-
}
136109
}
137110

138111
// Register validates and registers webhook(s) in the server
@@ -143,12 +116,14 @@ func (s *Server) Register(webhooks ...Webhook) error {
143116
if err != nil {
144117
return err
145118
}
146-
_, found := s.registry[webhook.GetPath()]
147-
if found {
148-
return fmt.Errorf("can't register duplicate path: %v", webhook.GetPath())
149-
}
150-
s.registry[webhook.GetPath()] = webhooks[i]
119+
// Handle actually ensures that no duplicate paths are registered.
151120
s.sMux.Handle(webhook.GetPath(), webhook.Handler())
121+
s.registry[webhook.GetPath()] = webhooks[i]
122+
123+
// Inject dependencies to each webhook.
124+
if err := s.setFields(webhooks[i]); err != nil {
125+
return err
126+
}
152127
}
153128

154129
// Lazily add Server to manager.
@@ -167,9 +142,6 @@ var _ manager.Runnable = &Server{}
167142
// It will install the webhook related resources depend on the server configuration.
168143
func (s *Server) Start(stop <-chan struct{}) error {
169144
s.once.Do(s.setDefault)
170-
if s.err != nil {
171-
return s.err
172-
}
173145

174146
// TODO: watch the cert dir. Reload the cert if it changes
175147
cert, err := tls.LoadX509KeyPair(path.Join(s.CertDir, certName), path.Join(s.CertDir, keyName))
@@ -211,27 +183,10 @@ func (s *Server) Start(stop <-chan struct{}) error {
211183
return nil
212184
}
213185

214-
var _ inject.Client = &Server{}
215-
216-
// InjectClient injects the client into the server
217-
func (s *Server) InjectClient(c client.Client) error {
218-
s.Client = c
219-
for _, wh := range s.registry {
220-
if _, err := inject.ClientInto(c, wh.Handler()); err != nil {
221-
return err
222-
}
223-
}
224-
return nil
225-
}
226-
227-
var _ inject.Decoder = &Server{}
186+
var _ inject.Injector = &Server{}
228187

229-
// InjectDecoder injects the decoder into the server
230-
func (s *Server) InjectDecoder(d atypes.Decoder) error {
231-
for _, wh := range s.registry {
232-
if _, err := inject.DecoderInto(d, wh.Handler()); err != nil {
233-
return err
234-
}
235-
}
188+
// InjectFunc injects dependencies into the handlers.
189+
func (s *Server) InjectFunc(f inject.Func) error {
190+
s.setFields = f
236191
return nil
237192
}

0 commit comments

Comments
 (0)