@@ -18,12 +18,9 @@ package builder
18
18
19
19
import (
20
20
"fmt"
21
- "net/http"
22
- "net/url"
23
21
"strings"
24
22
25
23
"k8s.io/apimachinery/pkg/runtime"
26
- "k8s.io/apimachinery/pkg/runtime/schema"
27
24
"k8s.io/client-go/rest"
28
25
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
29
26
"sigs.k8s.io/controller-runtime/pkg/client/config"
@@ -33,8 +30,6 @@ import (
33
30
"sigs.k8s.io/controller-runtime/pkg/predicate"
34
31
"sigs.k8s.io/controller-runtime/pkg/reconcile"
35
32
"sigs.k8s.io/controller-runtime/pkg/source"
36
- "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
37
- "sigs.k8s.io/controller-runtime/pkg/webhook/conversion"
38
33
)
39
34
40
35
// Supporting mocking out functions for testing
@@ -71,8 +66,6 @@ func ControllerManagedBy(m manager.Manager) *Builder {
71
66
// update events by *reconciling the object*.
72
67
// This is the equivalent of calling
73
68
// Watches(&source.Kind{Type: apiType}, &handler.EnqueueRequestForObject{})
74
- // If the passed in object has implemented the admission.Defaulter interface, a MutatingWebhook will be wired for this type.
75
- // If the passed in object has implemented the admission.Validator interface, a ValidatingWebhook will be wired for this type.
76
69
//
77
70
// Deprecated: Use For
78
71
func (blder * Builder ) ForType (apiType runtime.Object ) * Builder {
@@ -83,8 +76,6 @@ func (blder *Builder) ForType(apiType runtime.Object) *Builder {
83
76
// update events by *reconciling the object*.
84
77
// This is the equivalent of calling
85
78
// Watches(&source.Kind{Type: apiType}, &handler.EnqueueRequestForObject{})
86
- // If the passed in object has implemented the admission.Defaulter interface, a MutatingWebhook will be wired for this type.
87
- // If the passed in object has implemented the admission.Validator interface, a ValidatingWebhook will be wired for this type.
88
79
func (blder * Builder ) For (apiType runtime.Object ) * Builder {
89
80
blder .apiType = apiType
90
81
return blder
@@ -159,7 +150,7 @@ func (blder *Builder) Build(r reconcile.Reconciler) (manager.Manager, error) {
159
150
}
160
151
161
152
// Set the Config
162
- if err := blder .doConfig (); err != nil {
153
+ if err := blder .loadRestConfig (); err != nil {
163
154
return nil , err
164
155
}
165
156
@@ -173,11 +164,6 @@ func (blder *Builder) Build(r reconcile.Reconciler) (manager.Manager, error) {
173
164
return nil , err
174
165
}
175
166
176
- // Set the Webook if needed
177
- if err := blder .doWebhook (); err != nil {
178
- return nil , err
179
- }
180
-
181
167
// Set the Watch
182
168
if err := blder .doWatch (); err != nil {
183
169
return nil , err
@@ -217,7 +203,7 @@ func (blder *Builder) doWatch() error {
217
203
return nil
218
204
}
219
205
220
- func (blder * Builder ) doConfig () error {
206
+ func (blder * Builder ) loadRestConfig () error {
221
207
if blder .config != nil {
222
208
return nil
223
209
}
@@ -258,79 +244,3 @@ func (blder *Builder) doController(r reconcile.Reconciler) error {
258
244
blder .ctrl , err = newController (name , blder .mgr , controller.Options {Reconciler : r })
259
245
return err
260
246
}
261
-
262
- func (blder * Builder ) doWebhook () error {
263
- // Create a webhook for each type
264
- gvk , err := apiutil .GVKForObject (blder .apiType , blder .mgr .GetScheme ())
265
- if err != nil {
266
- return err
267
- }
268
-
269
- // TODO: When the conversion webhook lands, we need to handle all registered versions of a given group-kind.
270
- // A potential workflow for defaulting webhook
271
- // 1) a bespoke (non-hub) version comes in
272
- // 2) convert it to the hub version
273
- // 3) do defaulting
274
- // 4) convert it back to the same bespoke version
275
- // 5) calculate the JSON patch
276
- //
277
- // A potential workflow for validating webhook
278
- // 1) a bespoke (non-hub) version comes in
279
- // 2) convert it to the hub version
280
- // 3) do validation
281
- if defaulter , isDefaulter := blder .apiType .(admission.Defaulter ); isDefaulter {
282
- mwh := admission .DefaultingWebhookFor (defaulter )
283
- if mwh != nil {
284
- path := generateMutatePath (gvk )
285
-
286
- // Checking if the path is already registered.
287
- // If so, just skip it.
288
- if ! blder .isAlreadyHandled (path ) {
289
- log .Info ("Registering a mutating webhook" ,
290
- "GVK" , gvk ,
291
- "path" , path )
292
- blder .mgr .GetWebhookServer ().Register (path , mwh )
293
- }
294
- }
295
- }
296
-
297
- if validator , isValidator := blder .apiType .(admission.Validator ); isValidator {
298
- vwh := admission .ValidatingWebhookFor (validator )
299
- if vwh != nil {
300
- path := generateValidatePath (gvk )
301
-
302
- // Checking if the path is already registered.
303
- // If so, just skip it.
304
- if ! blder .isAlreadyHandled (path ) {
305
- log .Info ("Registering a validating webhook" ,
306
- "GVK" , gvk ,
307
- "path" , path )
308
- blder .mgr .GetWebhookServer ().Register (path , vwh )
309
- }
310
- }
311
- }
312
-
313
- err = conversion .CheckConvertibility (blder .mgr .GetScheme (), blder .apiType )
314
- if err != nil {
315
- log .Error (err , "conversion check failed" , "GVK" , gvk )
316
- }
317
- return nil
318
- }
319
-
320
- func (blder * Builder ) isAlreadyHandled (path string ) bool {
321
- h , p := blder .mgr .GetWebhookServer ().WebhookMux .Handler (& http.Request {URL : & url.URL {Path : path }})
322
- if p == path && h != nil {
323
- return true
324
- }
325
- return false
326
- }
327
-
328
- func generateMutatePath (gvk schema.GroupVersionKind ) string {
329
- return "/mutate-" + strings .Replace (gvk .Group , "." , "-" , - 1 ) + "-" +
330
- gvk .Version + "-" + strings .ToLower (gvk .Kind )
331
- }
332
-
333
- func generateValidatePath (gvk schema.GroupVersionKind ) string {
334
- return "/validate-" + strings .Replace (gvk .Group , "." , "-" , - 1 ) + "-" +
335
- gvk .Version + "-" + strings .ToLower (gvk .Kind )
336
- }
0 commit comments