Skip to content

Commit ad30658

Browse files
author
Mengqi Yu
committed
🏃 add tests for simplified webhook interface
1 parent 8810470 commit ad30658

File tree

6 files changed

+487
-21
lines changed

6 files changed

+487
-21
lines changed

pkg/builder/build.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"strings"
2222

2323
"k8s.io/apimachinery/pkg/runtime"
24+
"k8s.io/apimachinery/pkg/runtime/schema"
2425
"k8s.io/client-go/rest"
2526
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
2627
"sigs.k8s.io/controller-runtime/pkg/client/config"
@@ -249,9 +250,6 @@ func (blder *Builder) doWebhook() error {
249250
return err
250251
}
251252

252-
partialPath := strings.Replace(gvk.Group, ".", "-", -1) + "-" +
253-
gvk.Version + "-" + strings.ToLower(gvk.Kind)
254-
255253
// TODO: When the conversion webhook lands, we need to handle all registered versions of a given group-kind.
256254
// A potential workflow for defaulting webhook
257255
// 1) a bespoke (non-hub) version comes in
@@ -267,7 +265,7 @@ func (blder *Builder) doWebhook() error {
267265
if defaulter, isDefaulter := blder.apiType.(admission.Defaulter); isDefaulter {
268266
mwh := admission.DefaultingWebhookFor(defaulter)
269267
if mwh != nil {
270-
path := "/mutate-" + partialPath
268+
path := generateMutatePath(gvk)
271269
log.Info("Registering a mutating webhook",
272270
"GVK", gvk,
273271
"path", path)
@@ -279,7 +277,7 @@ func (blder *Builder) doWebhook() error {
279277
if validator, isValidator := blder.apiType.(admission.Validator); isValidator {
280278
vwh := admission.ValidatingWebhookFor(validator)
281279
if vwh != nil {
282-
path := "/validate-" + partialPath
280+
path := generateValidatePath(gvk)
283281
log.Info("Registering a validating webhook",
284282
"GVK", gvk,
285283
"path", path)
@@ -289,3 +287,13 @@ func (blder *Builder) doWebhook() error {
289287

290288
return err
291289
}
290+
291+
func generateMutatePath(gvk schema.GroupVersionKind) string {
292+
return "/mutate-" + strings.Replace(gvk.Group, ".", "-", -1) + "-" +
293+
gvk.Version + "-" + strings.ToLower(gvk.Kind)
294+
}
295+
296+
func generateValidatePath(gvk schema.GroupVersionKind) string {
297+
return "/validate-" + strings.Replace(gvk.Group, ".", "-", -1) + "-" +
298+
gvk.Version + "-" + strings.ToLower(gvk.Kind)
299+
}

0 commit comments

Comments
 (0)