@@ -19,18 +19,14 @@ package webhook
19
19
import (
20
20
"context"
21
21
"crypto/tls"
22
- "fmt"
23
22
"net"
24
23
"net/http"
25
24
"path"
26
25
"strconv"
27
26
"sync"
28
27
29
- "sigs.k8s.io/controller-runtime/pkg/client"
30
- "sigs.k8s.io/controller-runtime/pkg/client/config"
31
28
"sigs.k8s.io/controller-runtime/pkg/manager"
32
29
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
33
- atypes "sigs.k8s.io/controller-runtime/pkg/webhook/admission/types"
34
30
)
35
31
36
32
const (
@@ -50,28 +46,20 @@ type ServerOptions struct {
50
46
// If using SecretCertWriter in Provisioner, the server will provision the certificate in a secret,
51
47
// the user is responsible to mount the secret to the this location for the server to consume.
52
48
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
61
49
}
62
50
63
51
// Server is an admission webhook server that can serve traffic and
64
52
// generates related k8s resources for deploying.
65
53
type Server struct {
66
- // Name is the name of server
67
- Name string
68
-
69
54
// ServerOptions contains options for configuring the admission server.
70
55
ServerOptions
71
56
72
57
sMux * http.ServeMux
73
58
// 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
75
63
76
64
// manager is the manager that this webhook server will be registered.
77
65
manager manager.Manager
@@ -81,6 +69,8 @@ type Server struct {
81
69
82
70
// Webhook defines the basics that a webhook should support.
83
71
type Webhook interface {
72
+ http.Handler
73
+
84
74
// GetPath returns the path that the webhook registered.
85
75
GetPath () string
86
76
// Handler returns a http.Handler for the webhook.
@@ -91,11 +81,10 @@ type Webhook interface {
91
81
}
92
82
93
83
// 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 ) {
95
85
as := & Server {
96
- Name : name ,
97
86
sMux : http .NewServeMux (),
98
- registry : map [string ]Webhook {},
87
+ registry : map [string ]http. Handler {},
99
88
ServerOptions : options ,
100
89
manager : mgr ,
101
90
}
@@ -105,34 +94,18 @@ func NewServer(name string, mgr manager.Manager, options ServerOptions) (*Server
105
94
106
95
// setDefault does defaulting for the Server.
107
96
func (s * Server ) setDefault () {
108
- if len (s .Name ) == 0 {
109
- s .Name = "default-k8s-webhook-server"
110
- }
111
97
if s .registry == nil {
112
- s .registry = map [string ]Webhook {}
98
+ s .registry = map [string ]http. Handler {}
113
99
}
114
100
if s .sMux == nil {
115
- s .sMux = http .DefaultServeMux
101
+ s .sMux = http .NewServeMux ()
116
102
}
117
103
if s .Port <= 0 {
118
104
s .Port = 443
119
105
}
120
106
if len (s .CertDir ) == 0 {
121
107
s .CertDir = path .Join ("k8s-webhook-server" , "cert" )
122
108
}
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
- }
136
109
}
137
110
138
111
// Register validates and registers webhook(s) in the server
@@ -143,12 +116,14 @@ func (s *Server) Register(webhooks ...Webhook) error {
143
116
if err != nil {
144
117
return err
145
118
}
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.
151
120
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
+ }
152
127
}
153
128
154
129
// Lazily add Server to manager.
@@ -167,9 +142,6 @@ var _ manager.Runnable = &Server{}
167
142
// It will install the webhook related resources depend on the server configuration.
168
143
func (s * Server ) Start (stop <- chan struct {}) error {
169
144
s .once .Do (s .setDefault )
170
- if s .err != nil {
171
- return s .err
172
- }
173
145
174
146
// TODO: watch the cert dir. Reload the cert if it changes
175
147
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 {
211
183
return nil
212
184
}
213
185
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 {}
228
187
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
236
191
return nil
237
192
}
0 commit comments