@@ -77,7 +77,7 @@ func runTests(admissionReviewVersion string) {
7777close (stop )
7878})
7979
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 () {
8181By ("creating a controller manager" )
8282m , err := manager .New (cfg , manager.Options {})
8383ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
@@ -90,6 +90,7 @@ func runTests(admissionReviewVersion string) {
9090
9191err = WebhookManagedBy (m ).
9292For (& TestDefaulter {}).
93+ WithDefaulter (& TestCustomDefaulter {}).
9394Complete ()
9495ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
9596svr := m .GetWebhookServer ()
@@ -147,7 +148,7 @@ func runTests(admissionReviewVersion string) {
147148ExpectWithOffset (1 , w .Code ).To (Equal (http .StatusNotFound ))
148149})
149150
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 () {
151152By ("creating a controller manager" )
152153m , err := manager .New (cfg , manager.Options {})
153154ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
@@ -159,7 +160,9 @@ func runTests(admissionReviewVersion string) {
159160ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
160161
161162err = WebhookManagedBy (m ).
162- For (& TestDefaulter {Panic : true }).
163+ For (& TestDefaulter {}).
164+ WithDefaulter (& TestCustomDefaulter {}).
165+ RecoverPanic (true ).
163166// RecoverPanic defaults to true.
164167Complete ()
165168ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
@@ -285,7 +288,7 @@ func runTests(admissionReviewVersion string) {
285288ExpectWithOffset (1 , w .Code ).To (Equal (http .StatusNotFound ))
286289})
287290
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 () {
289292By ("creating a controller manager" )
290293m , err := manager .New (cfg , manager.Options {})
291294ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
@@ -298,6 +301,7 @@ func runTests(admissionReviewVersion string) {
298301
299302err = WebhookManagedBy (m ).
300303For (& TestValidator {}).
304+ WithValidator (& TestCustomValidator {}).
301305Complete ()
302306ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
303307svr := m .GetWebhookServer ()
@@ -356,7 +360,7 @@ func runTests(admissionReviewVersion string) {
356360ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"code":403` ))
357361})
358362
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 () {
360364By ("creating a controller manager" )
361365m , err := manager .New (cfg , manager.Options {})
362366ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
@@ -368,7 +372,8 @@ func runTests(admissionReviewVersion string) {
368372ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
369373
370374err = WebhookManagedBy (m ).
371- For (& TestValidator {Panic : true }).
375+ For (& TestValidator {}).
376+ WithValidator (& TestCustomValidator {}).
372377RecoverPanic (true ).
373378Complete ()
374379ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
@@ -496,80 +501,7 @@ func runTests(admissionReviewVersion string) {
496501EventuallyWithOffset (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"` ))
497502})
498503
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 () {
573505By ("creating a controller manager" )
574506ctx , cancel := context .WithCancel (context .Background ())
575507
@@ -584,6 +516,7 @@ func runTests(admissionReviewVersion string) {
584516
585517err = WebhookManagedBy (m ).
586518For (& TestValidator {}).
519+ WithValidator (& TestCustomValidator {}).
587520Complete ()
588521ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
589522svr := m .GetWebhookServer ()
@@ -712,15 +645,6 @@ type TestDefaulterList struct{}
712645func (* TestDefaulterList ) GetObjectKind () schema.ObjectKind { return nil }
713646func (* TestDefaulterList ) DeepCopyObject () runtime.Object { return nil }
714647
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-
724648// TestValidator.
725649var _ runtime.Object = & TestValidator {}
726650
@@ -753,43 +677,6 @@ type TestValidatorList struct{}
753677func (* TestValidatorList ) GetObjectKind () schema.ObjectKind { return nil }
754678func (* TestValidatorList ) DeepCopyObject () runtime.Object { return nil }
755679
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-
793680// TestDefaultValidator.
794681var _ runtime.Object = & TestDefaultValidator {}
795682
@@ -822,37 +709,7 @@ type TestDefaultValidatorList struct{}
822709func (* TestDefaultValidatorList ) GetObjectKind () schema.ObjectKind { return nil }
823710func (* TestDefaultValidatorList ) DeepCopyObject () runtime.Object { return nil }
824711
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-
854712// TestCustomDefaulter.
855-
856713type TestCustomDefaulter struct {}
857714
858715func (* TestCustomDefaulter ) Default (ctx context.Context , obj runtime.Object ) error {
@@ -866,6 +723,10 @@ func (*TestCustomDefaulter) Default(ctx context.Context, obj runtime.Object) err
866723}
867724
868725d := obj .(* TestDefaulter ) //nolint:ifshort
726+ if d .Panic {
727+ panic ("fake panic test" )
728+ }
729+
869730if d .Replica < 2 {
870731d .Replica = 2
871732}
@@ -889,6 +750,9 @@ func (*TestCustomValidator) ValidateCreate(ctx context.Context, obj runtime.Obje
889750}
890751
891752v := obj .(* TestValidator ) //nolint:ifshort
753+ if v .Panic {
754+ panic ("fake panic test" )
755+ }
892756if v .Replica < 0 {
893757return nil , errors .New ("number of replica should be greater than or equal to 0" )
894758}
0 commit comments