@@ -37,17 +37,19 @@ import (
37
37
38
38
// WebhookBuilder builds a Webhook.
39
39
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
51
53
}
52
54
53
55
// WebhookManagedBy returns a new webhook builder.
@@ -96,11 +98,25 @@ func (blder *WebhookBuilder) RecoverPanic(recoverPanic bool) *WebhookBuilder {
96
98
}
97
99
98
100
// 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.
99
103
func (blder * WebhookBuilder ) WithCustomPath (customPath string ) * WebhookBuilder {
100
104
blder .customPath = customPath
101
105
return blder
102
106
}
103
107
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
+
104
120
// Complete builds the webhook.
105
121
func (blder * WebhookBuilder ) Complete () error {
106
122
// Set the Config
@@ -139,6 +155,10 @@ func (blder *WebhookBuilder) setLogConstructor() {
139
155
}
140
156
}
141
157
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
+
142
162
func (blder * WebhookBuilder ) registerWebhooks () error {
143
163
typ , err := blder .getType ()
144
164
if err != nil {
@@ -150,6 +170,17 @@ func (blder *WebhookBuilder) registerWebhooks() error {
150
170
return err
151
171
}
152
172
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
+
153
184
// Register webhook(s) for type
154
185
err = blder .registerDefaultingWebhook ()
155
186
if err != nil {
@@ -174,8 +205,8 @@ func (blder *WebhookBuilder) registerDefaultingWebhook() error {
174
205
if mwh != nil {
175
206
mwh .LogConstructor = blder .logConstructor
176
207
path := generateMutatePath (blder .gvk )
177
- if blder .customPath != "" {
178
- generatedCustomPath , err := generateCustomPath (blder .customPath )
208
+ if blder .customDefaulterCustomPath != "" {
209
+ generatedCustomPath , err := generateCustomPath (blder .customDefaulterCustomPath )
179
210
if err != nil {
180
211
return err
181
212
}
@@ -212,8 +243,8 @@ func (blder *WebhookBuilder) registerValidatingWebhook() error {
212
243
if vwh != nil {
213
244
vwh .LogConstructor = blder .logConstructor
214
245
path := generateValidatePath (blder .gvk )
215
- if blder .customPath != "" {
216
- generatedCustomPath , err := generateCustomPath (blder .customPath )
246
+ if blder .customValidatorCustomPath != "" {
247
+ generatedCustomPath , err := generateCustomPath (blder .customValidatorCustomPath )
217
248
if err != nil {
218
249
return err
219
250
}
0 commit comments