@@ -73,10 +73,19 @@ func (b *WebhookBuilder) Name(name string) *WebhookBuilder {
73
73
return b
74
74
}
75
75
76
- // Type sets the type of the admission webhook
77
- // This is required
78
- func (b * WebhookBuilder ) Type (t types.WebhookType ) * WebhookBuilder {
79
- b .t = & t
76
+ // Mutating sets the type to mutating admission webhook
77
+ // Only one of Mutating and Validating can be invoked.
78
+ func (b * WebhookBuilder ) Mutating () * WebhookBuilder {
79
+ m := types .WebhookTypeMutating
80
+ b .t = & m
81
+ return b
82
+ }
83
+
84
+ // Validating sets the type to validating admission webhook
85
+ // Only one of Mutating and Validating can be invoked.
86
+ func (b * WebhookBuilder ) Validating () * WebhookBuilder {
87
+ m := types .WebhookTypeValidating
88
+ b .t = & m
80
89
return b
81
90
}
82
91
@@ -165,48 +174,48 @@ func (b *WebhookBuilder) Build() (*admission.Webhook, error) {
165
174
Handlers : b .handlers ,
166
175
}
167
176
168
- if len (b .path ) == 0 {
169
- if * b .t == types .WebhookTypeMutating {
170
- b .path = "/mutatingwebhook"
171
- } else if * b .t == types .WebhookTypeValidating {
172
- b .path = "/validatingwebhook"
173
- }
174
- }
175
- w .Path = b .path
176
-
177
177
if b .rules != nil {
178
178
w .Rules = b .rules
179
- return w , nil
180
- }
179
+ } else {
180
+ if b .manager == nil {
181
+ return nil , errors .New ("manager should be set using WithManager" )
182
+ }
183
+ gvk , err := apiutil .GVKForObject (b .apiType , b .manager .GetScheme ())
184
+ if err != nil {
185
+ return nil , err
186
+ }
187
+ mapper := b .manager .GetRESTMapper ()
188
+ mapping , err := mapper .RESTMapping (gvk .GroupKind (), gvk .Version )
189
+ if err != nil {
190
+ return nil , err
191
+ }
181
192
182
- if b .manager == nil {
183
- return nil , errors .New ("manager should be set using WithManager" )
184
- }
185
- gvk , err := apiutil .GVKForObject (b .apiType , b .manager .GetScheme ())
186
- if err != nil {
187
- return nil , err
188
- }
189
- mapper := b .manager .GetRESTMapper ()
190
- mapping , err := mapper .RESTMapping (gvk .GroupKind (), gvk .Version )
191
- if err != nil {
192
- return nil , err
193
+ if b .operations == nil {
194
+ b .operations = []admissionregistrationv1beta1.OperationType {
195
+ admissionregistrationv1beta1 .Create ,
196
+ admissionregistrationv1beta1 .Update ,
197
+ }
198
+ }
199
+ w .Rules = []admissionregistrationv1beta1.RuleWithOperations {
200
+ {
201
+ Operations : b .operations ,
202
+ Rule : admissionregistrationv1beta1.Rule {
203
+ APIGroups : []string {gvk .Group },
204
+ APIVersions : []string {gvk .Version },
205
+ Resources : []string {mapping .Resource .Resource },
206
+ },
207
+ },
208
+ }
193
209
}
194
210
195
- if b .operations == nil {
196
- b .operations = []admissionregistrationv1beta1.OperationType {
197
- admissionregistrationv1beta1 .Create ,
198
- admissionregistrationv1beta1 .Update ,
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 ]
199
216
}
200
217
}
201
- w .Rules = []admissionregistrationv1beta1.RuleWithOperations {
202
- {
203
- Operations : b .operations ,
204
- Rule : admissionregistrationv1beta1.Rule {
205
- APIGroups : []string {gvk .Group },
206
- APIVersions : []string {gvk .Version },
207
- Resources : []string {mapping .Resource .Resource },
208
- },
209
- },
210
- }
218
+ w .Path = b .path
219
+
211
220
return w , nil
212
221
}
0 commit comments