Skip to content

Commit 8dc0c38

Browse files
committed
Fix custom path for webhooks conflict
Signed-off-by: Damien Dassieu <[email protected]>
1 parent f06769c commit 8dc0c38

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

pkg/builder/webhook.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type WebhookBuilder struct {
4141
customDefaulter admission.CustomDefaulter
4242
customDefaulterOpts []admission.DefaulterOption
4343
customValidator admission.CustomValidator
44+
customPath string
4445
validatingCustomPath string
4546
defaultingCustomPath string
4647
gvk schema.GroupVersionKind
@@ -96,6 +97,14 @@ func (blder *WebhookBuilder) RecoverPanic(recoverPanic bool) *WebhookBuilder {
9697
return blder
9798
}
9899

100+
// WithCustomPath overrides the webhook's default path by the customPath
101+
// Deprecated: WithCustomPath should not be used anymore.
102+
// Please use WithValidatingCustomPath or WithDefaultingCustomPath instead.
103+
func (blder *WebhookBuilder) WithCustomPath(customPath string) *WebhookBuilder {
104+
blder.customPath = customPath
105+
return blder
106+
}
107+
99108
// WithValidatingCustomPath overrides the webhook's default validating path by the customPath
100109
func (blder *WebhookBuilder) WithValidatingCustomPath(customPath string) *WebhookBuilder {
101110
blder.validatingCustomPath = customPath
@@ -146,6 +155,10 @@ func (blder *WebhookBuilder) setLogConstructor() {
146155
}
147156
}
148157

158+
func (blder *WebhookBuilder) isThereCustomPathConflict() bool {
159+
return (blder.customPath != "" && blder.customDefaulter != nil && blder.customValidator != nil) || (blder.customPath != "" && blder.defaultingCustomPath != "") || (blder.customPath != "" && blder.validatingCustomPath != "")
160+
}
161+
149162
func (blder *WebhookBuilder) registerWebhooks() error {
150163
typ, err := blder.getType()
151164
if err != nil {
@@ -157,6 +170,18 @@ func (blder *WebhookBuilder) registerWebhooks() error {
157170
return err
158171
}
159172

173+
if blder.isThereCustomPathConflict() {
174+
return errors.New("only one of the custom defaulter or custom validtor should be set when using WithCustomPath. Otherwise, WithDefaultingCustomPath() and WithValidatingCustomPath() should be used.")
175+
} else {
176+
if blder.customPath != "" {
177+
// isThereCustomPathConflict() already checks for potential conflicts.
178+
// Since we are sure that only one of customDefaulter or customValidator will be used,
179+
// we can set set both defaultingCustomPath and validatingCustomPath.
180+
blder.defaultingCustomPath = blder.customPath
181+
blder.validatingCustomPath = blder.customPath
182+
}
183+
}
184+
160185
// Register webhook(s) for type
161186
err = blder.registerDefaultingWebhook()
162187
if err != nil {

pkg/builder/webhook_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,78 @@ func runTests(admissionReviewVersion string) {
817817
svr.WebhookMux().ServeHTTP(w, req)
818818
ExpectWithOffset(1, w.Code).To(Equal(http.StatusNotFound))
819819
})
820+
821+
It("should not scaffold a custom defaulting and a custom validating webhook with the same custom path", func() {
822+
By("creating a controller manager")
823+
m, err := manager.New(cfg, manager.Options{})
824+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
825+
826+
By("registering the type in the Scheme")
827+
builder := scheme.Builder{GroupVersion: testValidatorGVK.GroupVersion()}
828+
builder.Register(&TestDefaultValidator{}, &TestDefaultValidatorList{})
829+
err = builder.AddToScheme(m.GetScheme())
830+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
831+
832+
customPath := "/custom-path"
833+
err = WebhookManagedBy(m).
834+
For(&TestDefaultValidator{}).
835+
WithDefaulter(&TestCustomDefaultValidator{}).
836+
WithValidator(&TestCustomDefaultValidator{}).
837+
WithLogConstructor(func(base logr.Logger, req *admission.Request) logr.Logger {
838+
return admission.DefaultLogConstructor(testingLogger, req)
839+
}).
840+
WithCustomPath(customPath).
841+
Complete()
842+
ExpectWithOffset(1, err).To(HaveOccurred())
843+
})
844+
845+
It("should not scaffold a custom defaulting when setting a custom path and a defaulting custom path", func() {
846+
By("creating a controller manager")
847+
m, err := manager.New(cfg, manager.Options{})
848+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
849+
850+
By("registering the type in the Scheme")
851+
builder := scheme.Builder{GroupVersion: testValidatorGVK.GroupVersion()}
852+
builder.Register(&TestDefaultValidator{}, &TestDefaultValidatorList{})
853+
err = builder.AddToScheme(m.GetScheme())
854+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
855+
856+
customPath := "/custom-path"
857+
err = WebhookManagedBy(m).
858+
For(&TestDefaulter{}).
859+
WithDefaulter(&TestCustomDefaulter{}).
860+
WithLogConstructor(func(base logr.Logger, req *admission.Request) logr.Logger {
861+
return admission.DefaultLogConstructor(testingLogger, req)
862+
}).
863+
WithDefaultingCustomPath(customPath).
864+
WithCustomPath(customPath).
865+
Complete()
866+
ExpectWithOffset(1, err).To(HaveOccurred())
867+
})
868+
869+
It("should not scaffold a custom defaulting when setting a custom path and a validating custom path", func() {
870+
By("creating a controller manager")
871+
m, err := manager.New(cfg, manager.Options{})
872+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
873+
874+
By("registering the type in the Scheme")
875+
builder := scheme.Builder{GroupVersion: testValidatorGVK.GroupVersion()}
876+
builder.Register(&TestDefaultValidator{}, &TestDefaultValidatorList{})
877+
err = builder.AddToScheme(m.GetScheme())
878+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
879+
880+
customPath := "/custom-path"
881+
err = WebhookManagedBy(m).
882+
For(&TestValidator{}).
883+
WithValidator(&TestCustomValidator{}).
884+
WithLogConstructor(func(base logr.Logger, req *admission.Request) logr.Logger {
885+
return admission.DefaultLogConstructor(testingLogger, req)
886+
}).
887+
WithDefaultingCustomPath(customPath).
888+
WithCustomPath(customPath).
889+
Complete()
890+
ExpectWithOffset(1, err).To(HaveOccurred())
891+
})
820892
}
821893

822894
// TestDefaulter.

0 commit comments

Comments
 (0)