Skip to content

Commit f0e5eed

Browse files
committed
✨ Allow customizing generated webhook K8s Service
Allow customizing the webhook K8s Service's name, namespace, and port. ## example usage Look for the new `serviceName`, `serviceNamespace`, and `servicePort` fields. ``` ❯ GOBIN=(pwd)/bin go install ./cmd/* ❯ ./bin/controller-gen webhook -w Webhook +kubebuilder:webhook:admissionReviewVersions=<[]string>,failurePolicy=<string>,groups=<[]string>[,matchPolicy=<string>],mutating=<bool>,name=<string>[,path=<string>][,reinvocationPolicy=<string>],resources=<[]string>[,serviceName=<string>][,serviceNamespace=<string>][,servicePort=<int>][,sideEffects=<string>][,timeoutSeconds=<int>][,url=<string>],verbs=<[]string>,versions=<[]string>[,webhookVersions=<[]string>] package specifies how a webhook should be served. ... ``` contributes to #865
1 parent 94bcbe7 commit f0e5eed

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

pkg/webhook/parser.go

+34-4
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@ import (
3838

3939
// The default {Mutating,Validating}WebhookConfiguration version to generate.
4040
const (
41-
v1 = "v1"
42-
defaultWebhookVersion = v1
41+
v1 = "v1"
42+
defaultWebhookVersion = v1
43+
defaultServiceName = "webhook-service"
44+
defaultServiceNamespace = "system"
45+
defaultServicePort = 443
4346
)
4447

4548
var (
@@ -118,6 +121,12 @@ type Config struct {
118121
// Name indicates the name of this webhook configuration. Should be a domain with at least three segments separated by dots
119122
Name string
120123

124+
// ServiceName indicates the name of the K8s Service the webhook uses.
125+
ServiceName string `marker:"serviceName,optional"`
126+
127+
// ServiceNamespace indicates the namespace of the K8s Service the webhook uses.
128+
ServiceNamespace string `marker:"serviceNamespace,optional"`
129+
121130
// Path specifies that path that the API server should connect to this webhook on. Must be
122131
// prefixed with a '/validate-' or '/mutate-' depending on the type, and followed by
123132
// $GROUP-$VERSION-$KIND where all values are lower-cased and the periods in the group
@@ -126,6 +135,9 @@ type Config struct {
126135
// /validate-batch-tutorial-kubebuilder-io-v1-cronjob
127136
Path string `marker:"path,optional"`
128137

138+
// ServicePort indicates the port of the K8s Service the webhook uses
139+
ServicePort int32 `marker:"servicePort,optional"`
140+
129141
// WebhookVersions specifies the target API versions of the {Mutating,Validating}WebhookConfiguration objects
130142
// itself to generate. The only supported value is v1. Defaults to v1.
131143
WebhookVersions []string `marker:"webhookVersions,optional"`
@@ -318,11 +330,29 @@ func (c Config) clientConfig() (admissionregv1.WebhookClientConfig, error) {
318330

319331
path := c.Path
320332
if path != "" {
333+
var name, namespace string
334+
var port *int32
335+
336+
if c.ServiceName != "" {
337+
name = c.ServiceName
338+
} else {
339+
name = defaultServiceName
340+
}
341+
if c.ServiceNamespace != "" {
342+
namespace = c.ServiceNamespace
343+
} else {
344+
namespace = defaultServiceNamespace
345+
}
346+
if c.ServicePort != 0 {
347+
port = &c.ServicePort
348+
}
349+
321350
return admissionregv1.WebhookClientConfig{
322351
Service: &admissionregv1.ServiceReference{
323-
Name: "webhook-service",
324-
Namespace: "system",
352+
Name: name,
353+
Namespace: namespace,
325354
Path: &path,
355+
Port: port,
326356
},
327357
}, nil
328358
}

pkg/webhook/zz_generated.markerhelp.go

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)