11package webhook
22
33import (
4+ "fmt"
45 "net/http"
56 "net/url"
67 "strings"
@@ -16,13 +17,21 @@ import (
1617
1718// Builder builds a Webhook.
1819type Builder struct {
19- mgr manager.Manager
20- apiType runtime.Object
20+ mgr manager.Manager
21+ apiType runtime.Object
22+ pathValidate string
23+ pathMutate string
24+ prefixValidate string
25+ prefixMutate string
2126}
2227
2328// NewGenericWebhookManagedBy returns a new webhook Builder that will be invoked by the provided manager.Manager.
2429func NewGenericWebhookManagedBy (mgr manager.Manager ) * Builder {
25- return & Builder {mgr : mgr }
30+ return & Builder {
31+ mgr : mgr ,
32+ prefixMutate : "/mutate-" ,
33+ prefixValidate : "/validate-" ,
34+ }
2635}
2736
2837// For takes a runtime.Object which should be a CR.
@@ -31,10 +40,42 @@ func (blder *Builder) For(apiType runtime.Object) *Builder {
3140 return blder
3241}
3342
43+ func (blder * Builder ) WithMutatePath (path string ) * Builder {
44+ blder .pathMutate = path
45+ return blder
46+ }
47+
48+ func (blder * Builder ) WithValidatePath (path string ) * Builder {
49+ blder .pathValidate = path
50+ return blder
51+ }
52+
53+ func (blder * Builder ) WithMutatePrefix (prefix string ) * Builder {
54+ blder .prefixMutate = prefix
55+ return blder
56+ }
57+
58+ func (blder * Builder ) WithValidatePrefix (prefix string ) * Builder {
59+ blder .prefixMutate = prefix
60+ return blder
61+ }
62+
3463// Complete builds the webhook.
3564// If the given object implements the Mutator interface, a MutatingWebhook will be created.
3665// If the given object implements the Validator interface, a ValidatingWebhook will be created.
3766func (blder * Builder ) Complete (i interface {}) error {
67+
68+ if blder .pathMutate != "" && ! strings .HasPrefix (blder .pathMutate , "/" ) {
69+ return fmt .Errorf ("mutating path %q must start with '/'" , blder .pathMutate )
70+ } else if ! strings .HasPrefix (blder .prefixMutate , "/" ) {
71+ return fmt .Errorf ("mutating prefix %q must start with '/'" , blder .prefixMutate )
72+ }
73+ if blder .pathValidate != "" && ! strings .HasPrefix (blder .pathValidate , "/" ) {
74+ return fmt .Errorf ("validating path %q must start with '/'" , blder .pathValidate )
75+ } else if ! strings .HasPrefix (blder .prefixValidate , "/" ) {
76+ return fmt .Errorf ("validating prefix %q must start with '/'" , blder .prefixValidate )
77+ }
78+
3879 if validator , ok := i .(Validator ); ok {
3980 w , err := blder .createAdmissionWebhook (& handler {Handler : validator , Object : blder .apiType })
4081 if err != nil {
@@ -91,7 +132,7 @@ func (blder *Builder) registerValidatingWebhook(w *admission.Webhook) error {
91132 return err
92133 }
93134
94- path := generatePath ("/validate-" , gvk )
135+ path := generatePath (blder . pathValidate , blder . prefixValidate , gvk )
95136 if ! isAlreadyHandled (blder .mgr , path ) {
96137 blder .mgr .GetWebhookServer ().Register (path , w )
97138 }
@@ -105,7 +146,7 @@ func (blder *Builder) registerMutatingWebhook(w *admission.Webhook) error {
105146 return err
106147 }
107148
108- path := generatePath ("/mutate-" , gvk )
149+ path := generatePath (blder . pathMutate , blder . prefixMutate , gvk )
109150 if ! isAlreadyHandled (blder .mgr , path ) {
110151 blder .mgr .GetWebhookServer ().Register (path , w )
111152 }
@@ -126,7 +167,10 @@ func isAlreadyHandled(mgr ctrl.Manager, path string) bool {
126167 return false
127168}
128169
129- func generatePath (prefix string , gvk schema.GroupVersionKind ) string {
170+ func generatePath (override string , prefix string , gvk schema.GroupVersionKind ) string {
171+ if override != "" {
172+ return override
173+ }
130174 return prefix + strings .Replace (gvk .Group , "." , "-" , - 1 ) + "-" +
131175 gvk .Version + "-" + strings .ToLower (gvk .Kind )
132176}
0 commit comments