@@ -77,7 +77,7 @@ func runTests(admissionReviewVersion string) {
77
77
close (stop )
78
78
})
79
79
80
- It ("should scaffold a defaulting webhook if the type implements the Defaulter interface" , func () {
80
+ It ("should scaffold a custom defaulting webhook if the type implements the CustomDefaulter interface" , func () {
81
81
By ("creating a controller manager" )
82
82
m , err := manager .New (cfg , manager.Options {})
83
83
ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
@@ -90,6 +90,7 @@ func runTests(admissionReviewVersion string) {
90
90
91
91
err = WebhookManagedBy (m ).
92
92
For (& TestDefaulter {}).
93
+ WithDefaulter (& TestCustomDefaulter {}).
93
94
Complete ()
94
95
ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
95
96
svr := m .GetWebhookServer ()
@@ -147,7 +148,7 @@ func runTests(admissionReviewVersion string) {
147
148
ExpectWithOffset (1 , w .Code ).To (Equal (http .StatusNotFound ))
148
149
})
149
150
150
- It ("should scaffold a defaulting webhook which recovers from panics" , func () {
151
+ It ("should scaffold a custom defaulting webhook which recovers from panics" , func () {
151
152
By ("creating a controller manager" )
152
153
m , err := manager .New (cfg , manager.Options {})
153
154
ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
@@ -159,7 +160,9 @@ func runTests(admissionReviewVersion string) {
159
160
ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
160
161
161
162
err = WebhookManagedBy (m ).
162
- For (& TestDefaulter {Panic : true }).
163
+ For (& TestDefaulter {}).
164
+ WithDefaulter (& TestCustomDefaulter {}).
165
+ RecoverPanic (true ).
163
166
// RecoverPanic defaults to true.
164
167
Complete ()
165
168
ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
@@ -285,7 +288,7 @@ func runTests(admissionReviewVersion string) {
285
288
ExpectWithOffset (1 , w .Code ).To (Equal (http .StatusNotFound ))
286
289
})
287
290
288
- It ("should scaffold a validating webhook if the type implements the Validator interface" , func () {
291
+ It ("should scaffold a custom validating webhook if the type implements the CustomValidator interface" , func () {
289
292
By ("creating a controller manager" )
290
293
m , err := manager .New (cfg , manager.Options {})
291
294
ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
@@ -298,6 +301,7 @@ func runTests(admissionReviewVersion string) {
298
301
299
302
err = WebhookManagedBy (m ).
300
303
For (& TestValidator {}).
304
+ WithValidator (& TestCustomValidator {}).
301
305
Complete ()
302
306
ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
303
307
svr := m .GetWebhookServer ()
@@ -356,7 +360,7 @@ func runTests(admissionReviewVersion string) {
356
360
ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"code":403` ))
357
361
})
358
362
359
- It ("should scaffold a validating webhook which recovers from panics" , func () {
363
+ It ("should scaffold a custom validating webhook which recovers from panics" , func () {
360
364
By ("creating a controller manager" )
361
365
m , err := manager .New (cfg , manager.Options {})
362
366
ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
@@ -368,7 +372,8 @@ func runTests(admissionReviewVersion string) {
368
372
ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
369
373
370
374
err = WebhookManagedBy (m ).
371
- For (& TestValidator {Panic : true }).
375
+ For (& TestValidator {}).
376
+ WithValidator (& TestCustomValidator {}).
372
377
RecoverPanic (true ).
373
378
Complete ()
374
379
ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
@@ -496,80 +501,7 @@ func runTests(admissionReviewVersion string) {
496
501
EventuallyWithOffset (1 , logBuffer ).Should (gbytes .Say (`"msg":"Validating object","object":{"name":"foo","namespace":"default"},"namespace":"default","name":"foo","resource":{"group":"foo.test.org","version":"v1","resource":"testvalidator"},"user":"","requestID":"07e52e8d-4513-11e9-a716-42010a800270"` ))
497
502
})
498
503
499
- It ("should scaffold defaulting and validating webhooks if the type implements both Defaulter and Validator interfaces" , func () {
500
- By ("creating a controller manager" )
501
- m , err := manager .New (cfg , manager.Options {})
502
- ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
503
-
504
- By ("registering the type in the Scheme" )
505
- builder := scheme.Builder {GroupVersion : testDefaultValidatorGVK .GroupVersion ()}
506
- builder .Register (& TestDefaultValidator {}, & TestDefaultValidatorList {})
507
- err = builder .AddToScheme (m .GetScheme ())
508
- ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
509
-
510
- err = WebhookManagedBy (m ).
511
- For (& TestDefaultValidator {}).
512
- Complete ()
513
- ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
514
- svr := m .GetWebhookServer ()
515
- ExpectWithOffset (1 , svr ).NotTo (BeNil ())
516
-
517
- reader := strings .NewReader (admissionReviewGV + admissionReviewVersion + `",
518
- "request":{
519
- "uid":"07e52e8d-4513-11e9-a716-42010a800270",
520
- "kind":{
521
- "group":"",
522
- "version":"v1",
523
- "kind":"TestDefaultValidator"
524
- },
525
- "resource":{
526
- "group":"",
527
- "version":"v1",
528
- "resource":"testdefaultvalidator"
529
- },
530
- "namespace":"default",
531
- "operation":"CREATE",
532
- "object":{
533
- "replica":1
534
- },
535
- "oldObject":null
536
- }
537
- }` )
538
-
539
- ctx , cancel := context .WithCancel (context .Background ())
540
- cancel ()
541
- err = svr .Start (ctx )
542
- if err != nil && ! os .IsNotExist (err ) {
543
- ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
544
- }
545
-
546
- By ("sending a request to a mutating webhook path" )
547
- path := generateMutatePath (testDefaultValidatorGVK )
548
- req := httptest .NewRequest ("POST" , svcBaseAddr + path , reader )
549
- req .Header .Add ("Content-Type" , "application/json" )
550
- w := httptest .NewRecorder ()
551
- svr .WebhookMux ().ServeHTTP (w , req )
552
- ExpectWithOffset (1 , w .Code ).To (Equal (http .StatusOK ))
553
- By ("sanity checking the response contains reasonable field" )
554
- ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"allowed":true` ))
555
- ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"patch":` ))
556
- ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"code":200` ))
557
-
558
- By ("sending a request to a validating webhook path" )
559
- path = generateValidatePath (testDefaultValidatorGVK )
560
- _ , err = reader .Seek (0 , 0 )
561
- ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
562
- req = httptest .NewRequest ("POST" , svcBaseAddr + path , reader )
563
- req .Header .Add ("Content-Type" , "application/json" )
564
- w = httptest .NewRecorder ()
565
- svr .WebhookMux ().ServeHTTP (w , req )
566
- ExpectWithOffset (1 , w .Code ).To (Equal (http .StatusOK ))
567
- By ("sanity checking the response contains reasonable field" )
568
- ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"allowed":true` ))
569
- ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"code":200` ))
570
- })
571
-
572
- It ("should scaffold a validating webhook if the type implements the Validator interface to validate deletes" , func () {
504
+ It ("should scaffold a custom validating webhook if the type implements the CustomValidator interface to validate deletes" , func () {
573
505
By ("creating a controller manager" )
574
506
ctx , cancel := context .WithCancel (context .Background ())
575
507
@@ -584,6 +516,7 @@ func runTests(admissionReviewVersion string) {
584
516
585
517
err = WebhookManagedBy (m ).
586
518
For (& TestValidator {}).
519
+ WithValidator (& TestCustomValidator {}).
587
520
Complete ()
588
521
ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
589
522
svr := m .GetWebhookServer ()
@@ -712,15 +645,6 @@ type TestDefaulterList struct{}
712
645
func (* TestDefaulterList ) GetObjectKind () schema.ObjectKind { return nil }
713
646
func (* TestDefaulterList ) DeepCopyObject () runtime.Object { return nil }
714
647
715
- func (d * TestDefaulter ) Default () {
716
- if d .Panic {
717
- panic ("fake panic test" )
718
- }
719
- if d .Replica < 2 {
720
- d .Replica = 2
721
- }
722
- }
723
-
724
648
// TestValidator.
725
649
var _ runtime.Object = & TestValidator {}
726
650
@@ -753,43 +677,6 @@ type TestValidatorList struct{}
753
677
func (* TestValidatorList ) GetObjectKind () schema.ObjectKind { return nil }
754
678
func (* TestValidatorList ) DeepCopyObject () runtime.Object { return nil }
755
679
756
- var _ admission.Validator = & TestValidator {}
757
-
758
- func (v * TestValidator ) ValidateCreate () (admission.Warnings , error ) {
759
- if v .Panic {
760
- panic ("fake panic test" )
761
- }
762
- if v .Replica < 0 {
763
- return nil , errors .New ("number of replica should be greater than or equal to 0" )
764
- }
765
- return nil , nil
766
- }
767
-
768
- func (v * TestValidator ) ValidateUpdate (old runtime.Object ) (admission.Warnings , error ) {
769
- if v .Panic {
770
- panic ("fake panic test" )
771
- }
772
- if v .Replica < 0 {
773
- return nil , errors .New ("number of replica should be greater than or equal to 0" )
774
- }
775
- if oldObj , ok := old .(* TestValidator ); ! ok {
776
- return nil , fmt .Errorf ("the old object is expected to be %T" , oldObj )
777
- } else if v .Replica < oldObj .Replica {
778
- return nil , fmt .Errorf ("new replica %v should not be fewer than old replica %v" , v .Replica , oldObj .Replica )
779
- }
780
- return nil , nil
781
- }
782
-
783
- func (v * TestValidator ) ValidateDelete () (admission.Warnings , error ) {
784
- if v .Panic {
785
- panic ("fake panic test" )
786
- }
787
- if v .Replica > 0 {
788
- return nil , errors .New ("number of replica should be less than or equal to 0 to delete" )
789
- }
790
- return nil , nil
791
- }
792
-
793
680
// TestDefaultValidator.
794
681
var _ runtime.Object = & TestDefaultValidator {}
795
682
@@ -822,37 +709,7 @@ type TestDefaultValidatorList struct{}
822
709
func (* TestDefaultValidatorList ) GetObjectKind () schema.ObjectKind { return nil }
823
710
func (* TestDefaultValidatorList ) DeepCopyObject () runtime.Object { return nil }
824
711
825
- func (dv * TestDefaultValidator ) Default () {
826
- if dv .Replica < 2 {
827
- dv .Replica = 2
828
- }
829
- }
830
-
831
- var _ admission.Validator = & TestDefaultValidator {}
832
-
833
- func (dv * TestDefaultValidator ) ValidateCreate () (admission.Warnings , error ) {
834
- if dv .Replica < 0 {
835
- return nil , errors .New ("number of replica should be greater than or equal to 0" )
836
- }
837
- return nil , nil
838
- }
839
-
840
- func (dv * TestDefaultValidator ) ValidateUpdate (old runtime.Object ) (admission.Warnings , error ) {
841
- if dv .Replica < 0 {
842
- return nil , errors .New ("number of replica should be greater than or equal to 0" )
843
- }
844
- return nil , nil
845
- }
846
-
847
- func (dv * TestDefaultValidator ) ValidateDelete () (admission.Warnings , error ) {
848
- if dv .Replica > 0 {
849
- return nil , errors .New ("number of replica should be less than or equal to 0 to delete" )
850
- }
851
- return nil , nil
852
- }
853
-
854
712
// TestCustomDefaulter.
855
-
856
713
type TestCustomDefaulter struct {}
857
714
858
715
func (* TestCustomDefaulter ) Default (ctx context.Context , obj runtime.Object ) error {
@@ -866,6 +723,10 @@ func (*TestCustomDefaulter) Default(ctx context.Context, obj runtime.Object) err
866
723
}
867
724
868
725
d := obj .(* TestDefaulter ) //nolint:ifshort
726
+ if d .Panic {
727
+ panic ("fake panic test" )
728
+ }
729
+
869
730
if d .Replica < 2 {
870
731
d .Replica = 2
871
732
}
@@ -889,6 +750,9 @@ func (*TestCustomValidator) ValidateCreate(ctx context.Context, obj runtime.Obje
889
750
}
890
751
891
752
v := obj .(* TestValidator ) //nolint:ifshort
753
+ if v .Panic {
754
+ panic ("fake panic test" )
755
+ }
892
756
if v .Replica < 0 {
893
757
return nil , errors .New ("number of replica should be greater than or equal to 0" )
894
758
}
0 commit comments