@@ -31,33 +31,40 @@ import (
31
31
32
32
// WebhookBuilder builds a webhook based on the provided options.
33
33
type WebhookBuilder struct {
34
- // name specifies the Name of the webhook. It must be unique in the http
35
- // server that serves all the webhooks.
34
+ // name specifies the name of the webhook. It must be unique among all webhooks.
36
35
name string
37
36
38
- // path is the URL Path to register this webhook. e.g. "/feature-foo-mutating -pods".
37
+ // path is the URL Path to register this webhook. e.g. "/mutate -pods".
39
38
path string
40
39
41
- // handlers are handlers for handling admission request.
40
+ // handlers handle admission requests.
41
+ // A WebhookBuilder may have multiple handlers.
42
+ // For example, handlers[0] mutates a pod for feature foo.
43
+ // handlers[1] mutates a pod for a different feature bar.
42
44
handlers []admission.Handler
43
45
44
- // t specifies the type of the webhook
46
+ // t specifies the type of the webhook.
47
+ // Currently, Mutating and Validating are supported.
45
48
t * types.WebhookType
49
+
50
+ // operations define the operations this webhook cares.
46
51
// only one of operations and Rules can be set.
47
52
operations []admissionregistrationv1beta1.OperationType
48
-
49
- // resources that this webhook cares.
53
+ // apiType represents the resource that this webhook cares.
50
54
// Only one of apiType and Rules can be set.
51
55
apiType runtime.Object
52
- rules []admissionregistrationv1beta1.RuleWithOperations
56
+ // rules contain a list of admissionregistrationv1beta1.RuleWithOperations
57
+ // It overrides operations and apiType.
58
+ rules []admissionregistrationv1beta1.RuleWithOperations
53
59
54
- // This field maps to the FailurePolicy in the admissionregistrationv1beta1.Webhook
60
+ // failurePolicy maps to the FailurePolicy in the admissionregistrationv1beta1.Webhook
55
61
failurePolicy * admissionregistrationv1beta1.FailurePolicyType
56
62
57
- // This field maps to the NamespaceSelector in the admissionregistrationv1beta1.Webhook
63
+ // namespaceSelector maps to the NamespaceSelector in the admissionregistrationv1beta1.Webhook
58
64
namespaceSelector * metav1.LabelSelector
59
65
60
66
// manager is the manager for the webhook.
67
+ // It is used for provisioning various dependencies for the webhook. e.g. RESTMapper.
61
68
manager manager.Manager
62
69
}
63
70
@@ -66,7 +73,7 @@ func NewWebhookBuilder() *WebhookBuilder {
66
73
return & WebhookBuilder {}
67
74
}
68
75
69
- // Name sets the Name of the webhook.
76
+ // Name sets the name of the webhook.
70
77
// This is optional
71
78
func (b * WebhookBuilder ) Name (name string ) * WebhookBuilder {
72
79
b .name = name
@@ -89,8 +96,11 @@ func (b *WebhookBuilder) Validating() *WebhookBuilder {
89
96
return b
90
97
}
91
98
92
- // Path sets the Path for the webhook.
93
- // This is optional
99
+ // Path sets the path for the webhook.
100
+ // Path needs to be unique among different webhooks.
101
+ // This is optional. If not set, it will be built from the type and resource name.
102
+ // For example, a webhook that mutates pods has a default path of "/mutate-pods"
103
+ // If the defaulting logic can't find a unique path for it, user need to set it manually.
94
104
func (b * WebhookBuilder ) Path (path string ) * WebhookBuilder {
95
105
b .path = path
96
106
return b
@@ -105,15 +115,15 @@ func (b *WebhookBuilder) Operations(ops ...admissionregistrationv1beta1.Operatio
105
115
}
106
116
107
117
// ForType sets the type of resources that the webhook will operate.
108
- // This cannot be use with Rules.
118
+ // It will be overridden by Rules if Rules are not empty .
109
119
func (b * WebhookBuilder ) ForType (obj runtime.Object ) * WebhookBuilder {
110
120
b .apiType = obj
111
121
return b
112
122
}
113
123
114
124
// Rules sets the RuleWithOperations for the webhook.
115
125
// It overrides ForType and Operations.
116
- // This is optional and for advanced user
126
+ // This is optional and for advanced user.
117
127
func (b * WebhookBuilder ) Rules (rules ... admissionregistrationv1beta1.RuleWithOperations ) * WebhookBuilder {
118
128
b .rules = rules
119
129
return b
@@ -134,7 +144,7 @@ func (b *WebhookBuilder) NamespaceSelector(namespaceSelector *metav1.LabelSelect
134
144
return b
135
145
}
136
146
137
- // WithManager set the manager for the webhook for provisioning client etc.
147
+ // WithManager set the manager for the webhook for provisioning various dependencies. e.g. client etc.
138
148
func (b * WebhookBuilder ) WithManager (mgr manager.Manager ) * WebhookBuilder {
139
149
b .manager = mgr
140
150
return b
@@ -208,14 +218,5 @@ func (b *WebhookBuilder) Build() (*admission.Webhook, error) {
208
218
}
209
219
}
210
220
211
- if len (b .path ) == 0 {
212
- if * b .t == types .WebhookTypeMutating {
213
- b .path = "/mutate-" + w .Rules [0 ].Resources [0 ]
214
- } else if * b .t == types .WebhookTypeValidating {
215
- b .path = "/validate-" + w .Rules [0 ].Resources [0 ]
216
- }
217
- }
218
- w .Path = b .path
219
-
220
221
return w , nil
221
222
}
0 commit comments