Skip to content

Commit 6c50eee

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 Signed-off-by: David Xia <[email protected]>
1 parent 0fa4366 commit 6c50eee

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

pkg/webhook/parser.go

+33-4
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ 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"
4345
)
4446

4547
var (
@@ -118,6 +120,12 @@ type Config struct {
118120
// Name indicates the name of this webhook configuration. Should be a domain with at least three segments separated by dots
119121
Name string
120122

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

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

319330
path := c.Path
320331
if path != "" {
332+
var name, namespace string
333+
var port *int32
334+
335+
if c.ServiceName != "" {
336+
name = c.ServiceName
337+
} else {
338+
name = defaultServiceName
339+
}
340+
if c.ServiceNamespace != "" {
341+
namespace = c.ServiceNamespace
342+
} else {
343+
namespace = defaultServiceNamespace
344+
}
345+
if c.ServicePort != nil {
346+
port = c.ServicePort
347+
}
348+
321349
return admissionregv1.WebhookClientConfig{
322350
Service: &admissionregv1.ServiceReference{
323-
Name: "webhook-service",
324-
Namespace: "system",
351+
Name: name,
352+
Namespace: namespace,
325353
Path: &path,
354+
Port: port,
326355
},
327356
}, nil
328357
}

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)