Skip to content

Commit 4288b1f

Browse files
authored
Merge pull request #3102 from damsien/main
🐛 Fix custom path for webhooks conflict
2 parents fa1d0ee + 4cf8def commit 4288b1f

File tree

2 files changed

+408
-32
lines changed

2 files changed

+408
-32
lines changed

pkg/builder/webhook.go

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,19 @@ import (
3737

3838
// WebhookBuilder builds a Webhook.
3939
type WebhookBuilder struct {
40-
apiType runtime.Object
41-
customDefaulter admission.CustomDefaulter
42-
customDefaulterOpts []admission.DefaulterOption
43-
customValidator admission.CustomValidator
44-
customPath string
45-
gvk schema.GroupVersionKind
46-
mgr manager.Manager
47-
config *rest.Config
48-
recoverPanic *bool
49-
logConstructor func(base logr.Logger, req *admission.Request) logr.Logger
50-
err error
40+
apiType runtime.Object
41+
customDefaulter admission.CustomDefaulter
42+
customDefaulterOpts []admission.DefaulterOption
43+
customValidator admission.CustomValidator
44+
customPath string
45+
customValidatorCustomPath string
46+
customDefaulterCustomPath string
47+
gvk schema.GroupVersionKind
48+
mgr manager.Manager
49+
config *rest.Config
50+
recoverPanic *bool
51+
logConstructor func(base logr.Logger, req *admission.Request) logr.Logger
52+
err error
5153
}
5254

5355
// WebhookManagedBy returns a new webhook builder.
@@ -96,11 +98,25 @@ func (blder *WebhookBuilder) RecoverPanic(recoverPanic bool) *WebhookBuilder {
9698
}
9799

98100
// WithCustomPath overrides the webhook's default path by the customPath
101+
// Deprecated: WithCustomPath should not be used anymore.
102+
// Please use WithValidatorCustomPath or WithDefaulterCustomPath instead.
99103
func (blder *WebhookBuilder) WithCustomPath(customPath string) *WebhookBuilder {
100104
blder.customPath = customPath
101105
return blder
102106
}
103107

108+
// WithValidatorCustomPath overrides the path of the Validator.
109+
func (blder *WebhookBuilder) WithValidatorCustomPath(customPath string) *WebhookBuilder {
110+
blder.customValidatorCustomPath = customPath
111+
return blder
112+
}
113+
114+
// WithDefaulterCustomPath overrides the path of the Defaulter.
115+
func (blder *WebhookBuilder) WithDefaulterCustomPath(customPath string) *WebhookBuilder {
116+
blder.customDefaulterCustomPath = customPath
117+
return blder
118+
}
119+
104120
// Complete builds the webhook.
105121
func (blder *WebhookBuilder) Complete() error {
106122
// Set the Config
@@ -139,6 +155,10 @@ func (blder *WebhookBuilder) setLogConstructor() {
139155
}
140156
}
141157

158+
func (blder *WebhookBuilder) isThereCustomPathConflict() bool {
159+
return (blder.customPath != "" && blder.customDefaulter != nil && blder.customValidator != nil) || (blder.customPath != "" && blder.customDefaulterCustomPath != "") || (blder.customPath != "" && blder.customValidatorCustomPath != "")
160+
}
161+
142162
func (blder *WebhookBuilder) registerWebhooks() error {
143163
typ, err := blder.getType()
144164
if err != nil {
@@ -150,6 +170,17 @@ func (blder *WebhookBuilder) registerWebhooks() error {
150170
return err
151171
}
152172

173+
if blder.isThereCustomPathConflict() {
174+
return errors.New("only one of CustomDefaulter or CustomValidator should be set when using WithCustomPath. Otherwise, WithDefaulterCustomPath() and WithValidatorCustomPath() should be used")
175+
}
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 both customDefaulterCustomPath and validatingCustomPath.
180+
blder.customDefaulterCustomPath = blder.customPath
181+
blder.customValidatorCustomPath = blder.customPath
182+
}
183+
153184
// Register webhook(s) for type
154185
err = blder.registerDefaultingWebhook()
155186
if err != nil {
@@ -174,8 +205,8 @@ func (blder *WebhookBuilder) registerDefaultingWebhook() error {
174205
if mwh != nil {
175206
mwh.LogConstructor = blder.logConstructor
176207
path := generateMutatePath(blder.gvk)
177-
if blder.customPath != "" {
178-
generatedCustomPath, err := generateCustomPath(blder.customPath)
208+
if blder.customDefaulterCustomPath != "" {
209+
generatedCustomPath, err := generateCustomPath(blder.customDefaulterCustomPath)
179210
if err != nil {
180211
return err
181212
}
@@ -212,8 +243,8 @@ func (blder *WebhookBuilder) registerValidatingWebhook() error {
212243
if vwh != nil {
213244
vwh.LogConstructor = blder.logConstructor
214245
path := generateValidatePath(blder.gvk)
215-
if blder.customPath != "" {
216-
generatedCustomPath, err := generateCustomPath(blder.customPath)
246+
if blder.customValidatorCustomPath != "" {
247+
generatedCustomPath, err := generateCustomPath(blder.customValidatorCustomPath)
217248
if err != nil {
218249
return err
219250
}

0 commit comments

Comments
 (0)