Skip to content

Commit

Permalink
feat: allow customizing generated webhook's name
Browse files Browse the repository at this point in the history
closes #865
  • Loading branch information
davidxia committed Jul 10, 2024
1 parent b07ad66 commit a577c5c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
16 changes: 14 additions & 2 deletions pkg/webhook/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
}
}
}
}
Expand All @@ -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`,
Expand Down Expand Up @@ -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`,
Expand Down
4 changes: 4 additions & 0 deletions pkg/webhook/zz_generated.markerhelp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a577c5c

Please sign in to comment.