@@ -19,7 +19,6 @@ package webhook
19
19
import (
20
20
"context"
21
21
"crypto/tls"
22
- "fmt"
23
22
"net"
24
23
"net/http"
25
24
"path"
@@ -30,7 +29,6 @@ import (
30
29
"sigs.k8s.io/controller-runtime/pkg/client/config"
31
30
"sigs.k8s.io/controller-runtime/pkg/manager"
32
31
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
33
- atypes "sigs.k8s.io/controller-runtime/pkg/webhook/admission/types"
34
32
)
35
33
36
34
const (
@@ -55,9 +53,6 @@ type ServerOptions struct {
55
53
// It knows how to talk to a kubernetes cluster.
56
54
// Client will be injected by the manager if not set.
57
55
Client client.Client
58
-
59
- // err will be non-nil if there is an error occur during initialization.
60
- err error // nolint: structcheck
61
56
}
62
57
63
58
// Server is an admission webhook server that can serve traffic and
@@ -71,16 +66,21 @@ type Server struct {
71
66
72
67
sMux * http.ServeMux
73
68
// registry maps a path to a http.Handler.
74
- registry map [string ]Webhook
69
+ registry map [string ]http. Handler
75
70
76
71
// manager is the manager that this webhook server will be registered.
77
72
manager manager.Manager
78
73
74
+ // err will be non-nil if there is an error occur during initialization.
75
+ err error
76
+
79
77
once sync.Once
80
78
}
81
79
82
80
// Webhook defines the basics that a webhook should support.
83
81
type Webhook interface {
82
+ http.Handler
83
+
84
84
// GetPath returns the path that the webhook registered.
85
85
GetPath () string
86
86
// Handler returns a http.Handler for the webhook.
@@ -95,7 +95,7 @@ func NewServer(name string, mgr manager.Manager, options ServerOptions) (*Server
95
95
as := & Server {
96
96
Name : name ,
97
97
sMux : http .NewServeMux (),
98
- registry : map [string ]Webhook {},
98
+ registry : map [string ]http. Handler {},
99
99
ServerOptions : options ,
100
100
manager : mgr ,
101
101
}
@@ -109,7 +109,7 @@ func (s *Server) setDefault() {
109
109
s .Name = "default-k8s-webhook-server"
110
110
}
111
111
if s .registry == nil {
112
- s .registry = map [string ]Webhook {}
112
+ s .registry = map [string ]http. Handler {}
113
113
}
114
114
if s .sMux == nil {
115
115
s .sMux = http .DefaultServeMux
@@ -136,31 +136,34 @@ func (s *Server) setDefault() {
136
136
}
137
137
138
138
// Register validates and registers webhook(s) in the server
139
- func (s * Server ) Register (webhooks ... Webhook ) error {
139
+ func (s * Server ) Register (webhooks ... Webhook ) {
140
140
for i , webhook := range webhooks {
141
141
// validate the webhook before registering it.
142
142
err := webhook .Validate ()
143
143
if err != nil {
144
- return err
145
- }
146
- _ , found := s .registry [webhook .GetPath ()]
147
- if found {
148
- return fmt .Errorf ("can't register duplicate path: %v" , webhook .GetPath ())
144
+ s .err = err
145
+ return
149
146
}
150
- s . registry [ webhook . GetPath ()] = webhooks [ i ]
147
+ // Handle actually ensures that no duplicate paths are registered.
151
148
s .sMux .Handle (webhook .GetPath (), webhook .Handler ())
149
+ s .registry [webhook .GetPath ()] = webhooks [i ]
152
150
}
153
-
154
- // Lazily add Server to manager.
155
- // Because the all webhook handlers to be in place, so we can inject the things they need.
156
- return s .manager .Add (s )
157
151
}
158
152
159
153
// Handle registers a http.Handler for the given pattern.
160
154
func (s * Server ) Handle (pattern string , handler http.Handler ) {
161
155
s .sMux .Handle (pattern , handler )
162
156
}
163
157
158
+ // Complete must be called to complete the server setup
159
+ func (s * Server ) Complete () error {
160
+ if s .err != nil {
161
+ return s .err
162
+ }
163
+ // TODO(mengqiy): inject dependencies into each http.Handler
164
+ return s .manager .Add (s )
165
+ }
166
+
164
167
var _ manager.Runnable = & Server {}
165
168
166
169
// Start runs the server.
@@ -216,21 +219,12 @@ var _ inject.Client = &Server{}
216
219
// InjectClient injects the client into the server
217
220
func (s * Server ) InjectClient (c client.Client ) error {
218
221
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 {}
228
-
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
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
+ }
234
228
}
235
229
}
236
230
return nil
0 commit comments