@@ -19,18 +19,14 @@ package webhook
1919import (
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
3632const (
@@ -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.
6553type 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.
8371type 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.
10796func (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.
168143func (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