From a577c5c3fca1932b214dae63cb4546bdc64a9634 Mon Sep 17 00:00:00 2001 From: David Xia Date: Wed, 10 Jul 2024 11:38:48 -0400 Subject: [PATCH] feat: allow customizing generated webhook's name closes #865 --- pkg/webhook/parser.go | 16 ++++++++++++++-- pkg/webhook/zz_generated.markerhelp.go | 4 ++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/pkg/webhook/parser.go b/pkg/webhook/parser.go index 5652b4c45..fdcdb720d 100644 --- a/pkg/webhook/parser.go +++ b/pkg/webhook/parser.go @@ -102,6 +102,9 @@ type Config struct { // Name indicates the name of this webhook configuration. Should be a domain with at least three segments separated by dots Name string + // K8sName indicates the K8s name of this webhook configuration. + K8sName string `marker:"k8sName,optional"` + // Path specifies that path that the API server should connect to this webhook on. Must be // prefixed with a '/validate-' or '/mutate-' depending on the type, and followed by // $GROUP-$VERSION-$KIND where all values are lower-cased and the periods in the group @@ -370,6 +373,9 @@ func (g Generator) Generate(ctx *genall.GenerationContext) error { supportedWebhookVersions := supportedWebhookVersions() mutatingCfgs := make(map[string][]admissionregv1.MutatingWebhook, len(supportedWebhookVersions)) validatingCfgs := make(map[string][]admissionregv1.ValidatingWebhook, len(supportedWebhookVersions)) + mutatingWebhookConfigurationK8sName := "mutating-webhook-configuration" + validatingWebhookConfigurationK8sName := "validating-webhook-configuration" + for _, root := range ctx.Roots { markerSet, err := markers.PackageMarkers(ctx.Collector, root) if err != nil { @@ -395,6 +401,9 @@ func (g Generator) Generate(ctx *genall.GenerationContext) error { for _, webhookVersion := range webhookVersions { mutatingCfgs[webhookVersion] = append(mutatingCfgs[webhookVersion], w) } + if cfg.K8sName != "" { + mutatingWebhookConfigurationK8sName = cfg.K8sName + } } else { w, err := cfg.ToValidatingWebhook() if err != nil { @@ -403,6 +412,9 @@ func (g Generator) Generate(ctx *genall.GenerationContext) error { for _, webhookVersion := range webhookVersions { validatingCfgs[webhookVersion] = append(validatingCfgs[webhookVersion], w) } + if cfg.K8sName != "" { + validatingWebhookConfigurationK8sName = cfg.K8sName + } } } } @@ -418,7 +430,7 @@ func (g Generator) Generate(ctx *genall.GenerationContext) error { Version: version, Kind: "MutatingWebhookConfiguration", }) - objRaw.SetName("mutating-webhook-configuration") + objRaw.SetName(mutatingWebhookConfigurationK8sName) objRaw.Webhooks = cfgs for i := range objRaw.Webhooks { // SideEffects is required in admissionregistration/v1, if this is not set or set to `Some` or `Known`, @@ -449,7 +461,7 @@ func (g Generator) Generate(ctx *genall.GenerationContext) error { Version: version, Kind: "ValidatingWebhookConfiguration", }) - objRaw.SetName("validating-webhook-configuration") + objRaw.SetName(validatingWebhookConfigurationK8sName) objRaw.Webhooks = cfgs for i := range objRaw.Webhooks { // SideEffects is required in admissionregistration/v1, if this is not set or set to `Some` or `Known`, diff --git a/pkg/webhook/zz_generated.markerhelp.go b/pkg/webhook/zz_generated.markerhelp.go index d40bcc81f..3f3ec11e9 100644 --- a/pkg/webhook/zz_generated.markerhelp.go +++ b/pkg/webhook/zz_generated.markerhelp.go @@ -72,6 +72,10 @@ func (Config) Help() *markers.DefinitionHelp { Summary: "indicates the name of this webhook configuration. Should be a domain with at least three segments separated by dots", Details: "", }, + "K8sName": { + Summary: "indicates the K8s name of this webhook configuration.", + Details: "", + }, "Path": { Summary: "specifies that path that the API server should connect to this webhook on. Must be", Details: "prefixed with a '/validate-' or '/mutate-' depending on the type, and followed by\n$GROUP-$VERSION-$KIND where all values are lower-cased and the periods in the group\nare substituted for hyphens. For example, a validating webhook path for type\nbatch.tutorial.kubebuilder.io/v1,Kind=CronJob would be\n/validate-batch-tutorial-kubebuilder-io-v1-cronjob",