Skip to content

Commit 5356505

Browse files
committed
Rename validator & defaulter functions
Signed-off-by: Damien Dassieu <[email protected]>
1 parent 8dc0c38 commit 5356505

File tree

2 files changed

+41
-43
lines changed

2 files changed

+41
-43
lines changed

pkg/builder/webhook.go

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +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-
validatingCustomPath string
46-
defaultingCustomPath 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
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
5353
}
5454

5555
// WebhookManagedBy returns a new webhook builder.
@@ -99,21 +99,21 @@ func (blder *WebhookBuilder) RecoverPanic(recoverPanic bool) *WebhookBuilder {
9999

100100
// WithCustomPath overrides the webhook's default path by the customPath
101101
// Deprecated: WithCustomPath should not be used anymore.
102-
// Please use WithValidatingCustomPath or WithDefaultingCustomPath instead.
102+
// Please use WithValidatorCustomPath or WithDefaulterCustomPath instead.
103103
func (blder *WebhookBuilder) WithCustomPath(customPath string) *WebhookBuilder {
104104
blder.customPath = customPath
105105
return blder
106106
}
107107

108-
// WithValidatingCustomPath overrides the webhook's default validating path by the customPath
109-
func (blder *WebhookBuilder) WithValidatingCustomPath(customPath string) *WebhookBuilder {
110-
blder.validatingCustomPath = customPath
108+
// WithValidatorCustomPath overrides the path of the Validator.
109+
func (blder *WebhookBuilder) WithValidatorCustomPath(customPath string) *WebhookBuilder {
110+
blder.customValidatorCustomPath = customPath
111111
return blder
112112
}
113113

114-
// WithDefaultingCustomPath overrides the webhook's default defaulting path by the customPath
115-
func (blder *WebhookBuilder) WithDefaultingCustomPath(customPath string) *WebhookBuilder {
116-
blder.defaultingCustomPath = customPath
114+
// WithDefaulterCustomPath overrides the path of the Defaulter.
115+
func (blder *WebhookBuilder) WithDefaulterCustomPath(customPath string) *WebhookBuilder {
116+
blder.customDefaulterCustomPath = customPath
117117
return blder
118118
}
119119

@@ -156,7 +156,7 @@ func (blder *WebhookBuilder) setLogConstructor() {
156156
}
157157

158158
func (blder *WebhookBuilder) isThereCustomPathConflict() bool {
159-
return (blder.customPath != "" && blder.customDefaulter != nil && blder.customValidator != nil) || (blder.customPath != "" && blder.defaultingCustomPath != "") || (blder.customPath != "" && blder.validatingCustomPath != "")
159+
return (blder.customPath != "" && blder.customDefaulter != nil && blder.customValidator != nil) || (blder.customPath != "" && blder.customDefaulterCustomPath != "") || (blder.customPath != "" && blder.customValidatorCustomPath != "")
160160
}
161161

162162
func (blder *WebhookBuilder) registerWebhooks() error {
@@ -171,15 +171,14 @@ func (blder *WebhookBuilder) registerWebhooks() error {
171171
}
172172

173173
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-
}
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
183182
}
184183

185184
// Register webhook(s) for type
@@ -206,8 +205,8 @@ func (blder *WebhookBuilder) registerDefaultingWebhook() error {
206205
if mwh != nil {
207206
mwh.LogConstructor = blder.logConstructor
208207
path := generateMutatePath(blder.gvk)
209-
if blder.defaultingCustomPath != "" {
210-
generatedCustomPath, err := generateCustomPath(blder.defaultingCustomPath)
208+
if blder.customDefaulterCustomPath != "" {
209+
generatedCustomPath, err := generateCustomPath(blder.customDefaulterCustomPath)
211210
if err != nil {
212211
return err
213212
}
@@ -244,8 +243,8 @@ func (blder *WebhookBuilder) registerValidatingWebhook() error {
244243
if vwh != nil {
245244
vwh.LogConstructor = blder.logConstructor
246245
path := generateValidatePath(blder.gvk)
247-
if blder.validatingCustomPath != "" {
248-
generatedCustomPath, err := generateCustomPath(blder.validatingCustomPath)
246+
if blder.customValidatorCustomPath != "" {
247+
generatedCustomPath, err := generateCustomPath(blder.customValidatorCustomPath)
249248
if err != nil {
250249
return err
251250
}

pkg/builder/webhook_test.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ const (
4747
"apiVersion":"admission.k8s.io/`
4848

4949
svcBaseAddr = "http://svc-name.svc-ns.svc"
50+
51+
customPath = "/custom-path"
5052
)
5153

5254
var _ = Describe("webhook", func() {
@@ -171,7 +173,7 @@ func runTests(admissionReviewVersion string) {
171173
WithLogConstructor(func(base logr.Logger, req *admission.Request) logr.Logger {
172174
return admission.DefaultLogConstructor(testingLogger, req)
173175
}).
174-
WithDefaultingCustomPath(customPath).
176+
WithDefaulterCustomPath(customPath).
175177
Complete()
176178
ExpectWithOffset(1, err).NotTo(HaveOccurred())
177179
svr := m.GetWebhookServer()
@@ -391,7 +393,7 @@ func runTests(admissionReviewVersion string) {
391393
WithLogConstructor(func(base logr.Logger, req *admission.Request) logr.Logger {
392394
return admission.DefaultLogConstructor(testingLogger, req)
393395
}).
394-
WithValidatingCustomPath(customPath).
396+
WithValidatorCustomPath(customPath).
395397
Complete()
396398
ExpectWithOffset(1, err).NotTo(HaveOccurred())
397399
svr := m.GetWebhookServer()
@@ -731,8 +733,8 @@ func runTests(admissionReviewVersion string) {
731733
WithLogConstructor(func(base logr.Logger, req *admission.Request) logr.Logger {
732734
return admission.DefaultLogConstructor(testingLogger, req)
733735
}).
734-
WithValidatingCustomPath(validatingCustomPath).
735-
WithDefaultingCustomPath(defaultingCustomPath).
736+
WithValidatorCustomPath(validatingCustomPath).
737+
WithDefaulterCustomPath(defaultingCustomPath).
736738
Complete()
737739
ExpectWithOffset(1, err).NotTo(HaveOccurred())
738740
svr := m.GetWebhookServer()
@@ -829,7 +831,6 @@ func runTests(admissionReviewVersion string) {
829831
err = builder.AddToScheme(m.GetScheme())
830832
ExpectWithOffset(1, err).NotTo(HaveOccurred())
831833

832-
customPath := "/custom-path"
833834
err = WebhookManagedBy(m).
834835
For(&TestDefaultValidator{}).
835836
WithDefaulter(&TestCustomDefaultValidator{}).
@@ -853,14 +854,13 @@ func runTests(admissionReviewVersion string) {
853854
err = builder.AddToScheme(m.GetScheme())
854855
ExpectWithOffset(1, err).NotTo(HaveOccurred())
855856

856-
customPath := "/custom-path"
857857
err = WebhookManagedBy(m).
858858
For(&TestDefaulter{}).
859859
WithDefaulter(&TestCustomDefaulter{}).
860860
WithLogConstructor(func(base logr.Logger, req *admission.Request) logr.Logger {
861861
return admission.DefaultLogConstructor(testingLogger, req)
862862
}).
863-
WithDefaultingCustomPath(customPath).
863+
WithDefaulterCustomPath(customPath).
864864
WithCustomPath(customPath).
865865
Complete()
866866
ExpectWithOffset(1, err).To(HaveOccurred())
@@ -877,14 +877,13 @@ func runTests(admissionReviewVersion string) {
877877
err = builder.AddToScheme(m.GetScheme())
878878
ExpectWithOffset(1, err).NotTo(HaveOccurred())
879879

880-
customPath := "/custom-path"
881880
err = WebhookManagedBy(m).
882881
For(&TestValidator{}).
883882
WithValidator(&TestCustomValidator{}).
884883
WithLogConstructor(func(base logr.Logger, req *admission.Request) logr.Logger {
885884
return admission.DefaultLogConstructor(testingLogger, req)
886885
}).
887-
WithDefaultingCustomPath(customPath).
886+
WithDefaulterCustomPath(customPath).
888887
WithCustomPath(customPath).
889888
Complete()
890889
ExpectWithOffset(1, err).To(HaveOccurred())

0 commit comments

Comments
 (0)