Skip to content

Commit 896f6de

Browse files
authored
⚠ Remove deprecated Defaulter and Validator interfaces (#2877)
* remove deprecated Defaulter and Validator interfaces Signed-off-by: Troy Connor <[email protected]> * remove log, add test for customValidator/customDefaulter Signed-off-by: Troy Connor <[email protected]> * remove deprecated funcs, add test for customDefaulter Signed-off-by: Troy Connor <[email protected]> * minimize the diff Signed-off-by: Troy Connor <[email protected]> --------- Signed-off-by: Troy Connor <[email protected]>
1 parent e6c3d13 commit 896f6de

File tree

8 files changed

+114
-455
lines changed

8 files changed

+114
-455
lines changed

pkg/builder/webhook.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,6 @@ func (blder *WebhookBuilder) getDefaultingWebhook() *admission.Webhook {
176176
}
177177
return w
178178
}
179-
if defaulter, ok := blder.apiType.(admission.Defaulter); ok {
180-
w := admission.DefaultingWebhookFor(blder.mgr.GetScheme(), defaulter)
181-
if blder.recoverPanic != nil {
182-
w = w.WithRecoverPanic(*blder.recoverPanic)
183-
}
184-
return w
185-
}
186-
log.Info(
187-
"skip registering a mutating webhook, object does not implement admission.Defaulter or WithDefaulter wasn't called",
188-
"GVK", blder.gvk)
189179
return nil
190180
}
191181

@@ -215,16 +205,6 @@ func (blder *WebhookBuilder) getValidatingWebhook() *admission.Webhook {
215205
}
216206
return w
217207
}
218-
if validator, ok := blder.apiType.(admission.Validator); ok {
219-
w := admission.ValidatingWebhookFor(blder.mgr.GetScheme(), validator)
220-
if blder.recoverPanic != nil {
221-
w = w.WithRecoverPanic(*blder.recoverPanic)
222-
}
223-
return w
224-
}
225-
log.Info(
226-
"skip registering a validating webhook, object does not implement admission.Validator or WithValidator wasn't called",
227-
"GVK", blder.gvk)
228208
return nil
229209
}
230210

pkg/builder/webhook_test.go

Lines changed: 20 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func runTests(admissionReviewVersion string) {
7777
close(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() {
8181
By("creating a controller manager")
8282
m, err := manager.New(cfg, manager.Options{})
8383
ExpectWithOffset(1, err).NotTo(HaveOccurred())
@@ -90,6 +90,7 @@ func runTests(admissionReviewVersion string) {
9090

9191
err = WebhookManagedBy(m).
9292
For(&TestDefaulter{}).
93+
WithDefaulter(&TestCustomDefaulter{}).
9394
Complete()
9495
ExpectWithOffset(1, err).NotTo(HaveOccurred())
9596
svr := m.GetWebhookServer()
@@ -147,7 +148,7 @@ func runTests(admissionReviewVersion string) {
147148
ExpectWithOffset(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() {
151152
By("creating a controller manager")
152153
m, err := manager.New(cfg, manager.Options{})
153154
ExpectWithOffset(1, err).NotTo(HaveOccurred())
@@ -159,7 +160,9 @@ func runTests(admissionReviewVersion string) {
159160
ExpectWithOffset(1, err).NotTo(HaveOccurred())
160161

161162
err = WebhookManagedBy(m).
162-
For(&TestDefaulter{Panic: true}).
163+
For(&TestDefaulter{}).
164+
WithDefaulter(&TestCustomDefaulter{}).
165+
RecoverPanic(true).
163166
// RecoverPanic defaults to true.
164167
Complete()
165168
ExpectWithOffset(1, err).NotTo(HaveOccurred())
@@ -285,7 +288,7 @@ func runTests(admissionReviewVersion string) {
285288
ExpectWithOffset(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() {
289292
By("creating a controller manager")
290293
m, err := manager.New(cfg, manager.Options{})
291294
ExpectWithOffset(1, err).NotTo(HaveOccurred())
@@ -298,6 +301,7 @@ func runTests(admissionReviewVersion string) {
298301

299302
err = WebhookManagedBy(m).
300303
For(&TestValidator{}).
304+
WithValidator(&TestCustomValidator{}).
301305
Complete()
302306
ExpectWithOffset(1, err).NotTo(HaveOccurred())
303307
svr := m.GetWebhookServer()
@@ -356,7 +360,7 @@ func runTests(admissionReviewVersion string) {
356360
ExpectWithOffset(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() {
360364
By("creating a controller manager")
361365
m, err := manager.New(cfg, manager.Options{})
362366
ExpectWithOffset(1, err).NotTo(HaveOccurred())
@@ -368,7 +372,8 @@ func runTests(admissionReviewVersion string) {
368372
ExpectWithOffset(1, err).NotTo(HaveOccurred())
369373

370374
err = WebhookManagedBy(m).
371-
For(&TestValidator{Panic: true}).
375+
For(&TestValidator{}).
376+
WithValidator(&TestCustomValidator{}).
372377
RecoverPanic(true).
373378
Complete()
374379
ExpectWithOffset(1, err).NotTo(HaveOccurred())
@@ -496,80 +501,7 @@ func runTests(admissionReviewVersion string) {
496501
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"`))
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() {
573505
By("creating a controller manager")
574506
ctx, cancel := context.WithCancel(context.Background())
575507

@@ -584,6 +516,7 @@ func runTests(admissionReviewVersion string) {
584516

585517
err = WebhookManagedBy(m).
586518
For(&TestValidator{}).
519+
WithValidator(&TestCustomValidator{}).
587520
Complete()
588521
ExpectWithOffset(1, err).NotTo(HaveOccurred())
589522
svr := m.GetWebhookServer()
@@ -712,15 +645,6 @@ type TestDefaulterList struct{}
712645
func (*TestDefaulterList) GetObjectKind() schema.ObjectKind { return nil }
713646
func (*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.
725649
var _ runtime.Object = &TestValidator{}
726650

@@ -753,43 +677,6 @@ type TestValidatorList struct{}
753677
func (*TestValidatorList) GetObjectKind() schema.ObjectKind { return nil }
754678
func (*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.
794681
var _ runtime.Object = &TestDefaultValidator{}
795682

@@ -822,37 +709,7 @@ type TestDefaultValidatorList struct{}
822709
func (*TestDefaultValidatorList) GetObjectKind() schema.ObjectKind { return nil }
823710
func (*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-
856713
type TestCustomDefaulter struct{}
857714

858715
func (*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

868725
d := obj.(*TestDefaulter) //nolint:ifshort
726+
if d.Panic {
727+
panic("fake panic test")
728+
}
729+
869730
if d.Replica < 2 {
870731
d.Replica = 2
871732
}
@@ -889,6 +750,9 @@ func (*TestCustomValidator) ValidateCreate(ctx context.Context, obj runtime.Obje
889750
}
890751

891752
v := obj.(*TestValidator) //nolint:ifshort
753+
if v.Panic {
754+
panic("fake panic test")
755+
}
892756
if v.Replica < 0 {
893757
return nil, errors.New("number of replica should be greater than or equal to 0")
894758
}

0 commit comments

Comments
 (0)