@@ -21,9 +21,6 @@ import (
21
21
"fmt"
22
22
"strings"
23
23
24
- "sigs.k8s.io/controller-runtime/pkg/handler"
25
- "sigs.k8s.io/controller-runtime/pkg/source"
26
-
27
24
. "github.com/onsi/ginkgo"
28
25
. "github.com/onsi/gomega"
29
26
appsv1 "k8s.io/api/apps/v1"
@@ -33,10 +30,13 @@ import (
33
30
"k8s.io/apimachinery/pkg/runtime/schema"
34
31
"k8s.io/apimachinery/pkg/types"
35
32
"k8s.io/client-go/rest"
36
- "sigs.k8s.io/controller-runtime/pkg/client/apiutil"
37
33
"sigs.k8s.io/controller-runtime/pkg/controller"
34
+ "sigs.k8s.io/controller-runtime/pkg/handler"
38
35
"sigs.k8s.io/controller-runtime/pkg/manager"
39
36
"sigs.k8s.io/controller-runtime/pkg/reconcile"
37
+ "sigs.k8s.io/controller-runtime/pkg/scheme"
38
+ "sigs.k8s.io/controller-runtime/pkg/source"
39
+ "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
40
40
)
41
41
42
42
var _ = Describe ("application" , func () {
@@ -47,7 +47,6 @@ var _ = Describe("application", func() {
47
47
getConfig = func () (* rest.Config , error ) { return cfg , nil }
48
48
newController = controller .New
49
49
newManager = manager .New
50
- getGvk = apiutil .GVKForObject
51
50
})
52
51
53
52
AfterEach (func () {
@@ -121,6 +120,93 @@ var _ = Describe("application", func() {
121
120
Expect (err .Error ()).To (ContainSubstring ("expected error" ))
122
121
Expect (instance ).To (BeNil ())
123
122
})
123
+
124
+ It ("should scaffold a defaulting webhook if the type implements the Defaulter interface" , func () {
125
+ By ("creating a controller manager" )
126
+ m , err := manager .New (cfg , manager.Options {})
127
+ Expect (err ).NotTo (HaveOccurred ())
128
+
129
+ By ("registering the type in the Scheme" )
130
+ builder := scheme.Builder {GroupVersion : schema.GroupVersion {Group : "foo.test.org" , Version : "v1" }}
131
+ builder .Register (& TestDefaulter {}, & TestDefaulterList {})
132
+ err = builder .AddToScheme (m .GetScheme ())
133
+ Expect (err ).NotTo (HaveOccurred ())
134
+
135
+ instance , err := ControllerManagedBy (m ).
136
+ For (& TestDefaulter {}).
137
+ Owns (& appsv1.ReplicaSet {}).
138
+ Build (noop )
139
+ Expect (err ).NotTo (HaveOccurred ())
140
+ Expect (instance ).NotTo (BeNil ())
141
+ svr := m .GetWebhookServer ()
142
+ Expect (svr ).NotTo (BeNil ())
143
+
144
+ By ("trying to register an existing mutating webhook path" )
145
+ path := generateMutatePath (schema.GroupVersionKind {Group : "foo.test.org" , Version : "v1" , Kind : "TestDefaulter" })
146
+ Ω (func () { svr .Register (path , nil ) }).Should (Panic ())
147
+
148
+ By ("registering a validating webhook path" )
149
+ path = generateValidatePath (schema.GroupVersionKind {Group : "foo.test.org" , Version : "v1" , Kind : "TestDefaulter" })
150
+ Ω (func () { svr .Register (path , nil ) }).ShouldNot (Panic ())
151
+ })
152
+
153
+ It ("should scaffold a validating webhook if the type implements the Validator interface" , func () {
154
+ By ("creating a controller manager" )
155
+ m , err := manager .New (cfg , manager.Options {})
156
+ Expect (err ).NotTo (HaveOccurred ())
157
+
158
+ By ("registering the type in the Scheme" )
159
+ builder := scheme.Builder {GroupVersion : schema.GroupVersion {Group : "foo.test.org" , Version : "v1" }}
160
+ builder .Register (& TestValidator {}, & TestValidatorList {})
161
+ err = builder .AddToScheme (m .GetScheme ())
162
+ Expect (err ).NotTo (HaveOccurred ())
163
+
164
+ instance , err := ControllerManagedBy (m ).
165
+ For (& TestValidator {}).
166
+ Owns (& appsv1.ReplicaSet {}).
167
+ Build (noop )
168
+ Expect (err ).NotTo (HaveOccurred ())
169
+ Expect (instance ).NotTo (BeNil ())
170
+ svr := m .GetWebhookServer ()
171
+ Expect (svr ).NotTo (BeNil ())
172
+
173
+ By ("registering a mutating webhook path" )
174
+ path := generateMutatePath (schema.GroupVersionKind {Group : "foo.test.org" , Version : "v1" , Kind : "TestValidator" })
175
+ Ω (func () { svr .Register (path , nil ) }).ShouldNot (Panic ())
176
+
177
+ By ("trying to register an existing validating webhook path" )
178
+ path = generateValidatePath (schema.GroupVersionKind {Group : "foo.test.org" , Version : "v1" , Kind : "TestValidator" })
179
+ Ω (func () { svr .Register (path , nil ) }).Should (Panic ())
180
+ })
181
+
182
+ It ("should scaffold defaulting and validating webhooks if the type implements both Defaulter and Validator interfaces" , func () {
183
+ By ("creating a controller manager" )
184
+ m , err := manager .New (cfg , manager.Options {})
185
+ Expect (err ).NotTo (HaveOccurred ())
186
+
187
+ By ("registering the type in the Scheme" )
188
+ builder := scheme.Builder {GroupVersion : schema.GroupVersion {Group : "foo.test.org" , Version : "v1" }}
189
+ builder .Register (& TestDefaultValidator {}, & TestDefaultValidatorList {})
190
+ err = builder .AddToScheme (m .GetScheme ())
191
+ Expect (err ).NotTo (HaveOccurred ())
192
+
193
+ instance , err := ControllerManagedBy (m ).
194
+ For (& TestDefaultValidator {}).
195
+ Owns (& appsv1.ReplicaSet {}).
196
+ Build (noop )
197
+ Expect (err ).NotTo (HaveOccurred ())
198
+ Expect (instance ).NotTo (BeNil ())
199
+ svr := m .GetWebhookServer ()
200
+ Expect (svr ).NotTo (BeNil ())
201
+
202
+ By ("trying to register an existing mutating webhook path" )
203
+ path := generateMutatePath (schema.GroupVersionKind {Group : "foo.test.org" , Version : "v1" , Kind : "TestDefaultValidator" })
204
+ Ω (func () { svr .Register (path , nil ) }).Should (Panic ())
205
+
206
+ By ("trying to register an existing validating webhook path" )
207
+ path = generateValidatePath (schema.GroupVersionKind {Group : "foo.test.org" , Version : "v1" , Kind : "TestDefaultValidator" })
208
+ Ω (func () { svr .Register (path , nil ) }).Should (Panic ())
209
+ })
124
210
})
125
211
126
212
Describe ("Start with SimpleController" , func () {
@@ -281,3 +367,64 @@ type fakeType struct{}
281
367
282
368
func (* fakeType ) GetObjectKind () schema.ObjectKind { return nil }
283
369
func (* fakeType ) DeepCopyObject () runtime.Object { return nil }
370
+
371
+ // TestDefaulter
372
+ var _ runtime.Object = & TestDefaulter {}
373
+
374
+ type TestDefaulter struct {}
375
+
376
+ func (* TestDefaulter ) GetObjectKind () schema.ObjectKind { return nil }
377
+ func (* TestDefaulter ) DeepCopyObject () runtime.Object { return nil }
378
+
379
+ var _ runtime.Object = & TestDefaulterList {}
380
+
381
+ type TestDefaulterList struct {}
382
+
383
+ func (* TestDefaulterList ) GetObjectKind () schema.ObjectKind { return nil }
384
+ func (* TestDefaulterList ) DeepCopyObject () runtime.Object { return nil }
385
+
386
+ func (* TestDefaulter ) Default () {}
387
+
388
+ // TestValidator
389
+ var _ runtime.Object = & TestValidator {}
390
+
391
+ type TestValidator struct {}
392
+
393
+ func (* TestValidator ) GetObjectKind () schema.ObjectKind { return nil }
394
+ func (* TestValidator ) DeepCopyObject () runtime.Object { return nil }
395
+
396
+ var _ runtime.Object = & TestValidatorList {}
397
+
398
+ type TestValidatorList struct {}
399
+
400
+ func (* TestValidatorList ) GetObjectKind () schema.ObjectKind { return nil }
401
+ func (* TestValidatorList ) DeepCopyObject () runtime.Object { return nil }
402
+
403
+ var _ admission.Validator = & TestValidator {}
404
+
405
+ func (* TestValidator ) ValidateCreate () error { return nil }
406
+
407
+ func (* TestValidator ) ValidateUpdate (old runtime.Object ) error { return nil }
408
+
409
+ // TestDefaultValidator
410
+ var _ runtime.Object = & TestDefaultValidator {}
411
+
412
+ type TestDefaultValidator struct {}
413
+
414
+ func (* TestDefaultValidator ) GetObjectKind () schema.ObjectKind { return nil }
415
+ func (* TestDefaultValidator ) DeepCopyObject () runtime.Object { return nil }
416
+
417
+ var _ runtime.Object = & TestDefaultValidatorList {}
418
+
419
+ type TestDefaultValidatorList struct {}
420
+
421
+ func (* TestDefaultValidatorList ) GetObjectKind () schema.ObjectKind { return nil }
422
+ func (* TestDefaultValidatorList ) DeepCopyObject () runtime.Object { return nil }
423
+
424
+ func (* TestDefaultValidator ) Default () {}
425
+
426
+ var _ admission.Validator = & TestDefaultValidator {}
427
+
428
+ func (* TestDefaultValidator ) ValidateCreate () error { return nil }
429
+
430
+ func (* TestDefaultValidator ) ValidateUpdate (old runtime.Object ) error { return nil }
0 commit comments