Skip to content

Commit 2b49c7c

Browse files
fix: resolve duplicate webhook name issue in multi-version API
- Adjust webhook names for mutating and validating webhooks to avoid conflicts when creating multiple versions of the same API. More info: #4144
1 parent fa9f70c commit 2b49c7c

File tree

24 files changed

+96
-60
lines changed

24 files changed

+96
-60
lines changed

docs/book/src/cronjob-tutorial/testdata/project/api/v1/cronjob_webhook.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ A webhook will automatically be served that calls this defaulting.
7575
The `Default` method is expected to mutate the receiver, setting the defaults.
7676
*/
7777

78+
// +kubebuilder:webhook:path=/mutate-batch-tutorial-kubebuilder-io-v1-cronjob,mutating=true,failurePolicy=fail,sideEffects=None,groups=batch.tutorial.kubebuilder.io,resources=cronjobs,verbs=create;update,versions=v1,name=mcronjob-v1.kb.io,admissionReviewVersions=v1
79+
7880
// +kubebuilder:object:generate=false
7981
// CronJobCustomDefaulter struct is responsible for setting default values on the custom resource of the
8082
// Kind CronJob when those are created or updated.
@@ -153,6 +155,7 @@ validate anything on deletion.
153155

154156
// NOTE: The 'path' attribute must follow a specific pattern and should not be modified directly here.
155157
// Modifying the path for an invalid path can cause API server errors; failing to locate the webhook.
158+
// +kubebuilder:webhook:path=/validate-batch-tutorial-kubebuilder-io-v1-cronjob,mutating=false,failurePolicy=fail,sideEffects=None,groups=batch.tutorial.kubebuilder.io,resources=cronjobs,verbs=create;update,versions=v1,name=vcronjob-v1.kb.io,admissionReviewVersions=v1
156159

157160
// +kubebuilder:object:generate=false
158161
// CronJobCustomValidator struct is responsible for validating the CronJob resource

docs/book/src/cronjob-tutorial/testdata/project/config/webhook/manifests.yaml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,26 @@ kind: MutatingWebhookConfiguration
44
metadata:
55
name: mutating-webhook-configuration
66
webhooks:
7+
- admissionReviewVersions:
8+
- v1
9+
clientConfig:
10+
service:
11+
name: webhook-service
12+
namespace: system
13+
path: /mutate-batch-tutorial-kubebuilder-io-v1-cronjob
14+
failurePolicy: Fail
15+
name: mcronjob-v1.kb.io
16+
rules:
17+
- apiGroups:
18+
- batch.tutorial.kubebuilder.io
19+
apiVersions:
20+
- v1
21+
operations:
22+
- CREATE
23+
- UPDATE
24+
resources:
25+
- cronjobs
26+
sideEffects: None
727
- admissionReviewVersions:
828
- v1
929
clientConfig:
@@ -30,6 +50,26 @@ kind: ValidatingWebhookConfiguration
3050
metadata:
3151
name: validating-webhook-configuration
3252
webhooks:
53+
- admissionReviewVersions:
54+
- v1
55+
clientConfig:
56+
service:
57+
name: webhook-service
58+
namespace: system
59+
path: /validate-batch-tutorial-kubebuilder-io-v1-cronjob
60+
failurePolicy: Fail
61+
name: vcronjob-v1.kb.io
62+
rules:
63+
- apiGroups:
64+
- batch.tutorial.kubebuilder.io
65+
apiVersions:
66+
- v1
67+
operations:
68+
- CREATE
69+
- UPDATE
70+
resources:
71+
- cronjobs
72+
sideEffects: None
3373
- admissionReviewVersions:
3474
- v1
3575
clientConfig:

docs/book/src/multiversion-tutorial/testdata/project/api/v1/cronjob_webhook.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ A webhook will automatically be served that calls this defaulting.
7979
The `Default` method is expected to mutate the receiver, setting the defaults.
8080
*/
8181

82+
// +kubebuilder:webhook:path=/mutate-batch-tutorial-kubebuilder-io-v1-cronjob,mutating=true,failurePolicy=fail,sideEffects=None,groups=batch.tutorial.kubebuilder.io,resources=cronjobs,verbs=create;update,versions=v1,name=mcronjob-v1.kb.io,admissionReviewVersions=v1
83+
8284
// +kubebuilder:object:generate=false
8385
// CronJobCustomDefaulter struct is responsible for setting default values on the custom resource of the
8486
// Kind CronJob when those are created or updated.
@@ -157,6 +159,7 @@ validate anything on deletion.
157159

158160
// NOTE: The 'path' attribute must follow a specific pattern and should not be modified directly here.
159161
// Modifying the path for an invalid path can cause API server errors; failing to locate the webhook.
162+
// +kubebuilder:webhook:path=/validate-batch-tutorial-kubebuilder-io-v1-cronjob,mutating=false,failurePolicy=fail,sideEffects=None,groups=batch.tutorial.kubebuilder.io,resources=cronjobs,verbs=create;update,versions=v1,name=vcronjob-v1.kb.io,admissionReviewVersions=v1
160163

161164
// +kubebuilder:object:generate=false
162165
// CronJobCustomValidator struct is responsible for validating the CronJob resource

hack/docs/internal/cronjob-tutorial/generate_cronjob.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -455,16 +455,6 @@ Then, we set up the webhook with the manager.
455455
`// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.`, "")
456456
hackutils.CheckError("fixing cronjob_webhook.go by replace TODO to change verbs", err)
457457

458-
err = pluginutil.ReplaceInFile(
459-
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_webhook.go"),
460-
`// +kubebuilder:webhook:path=/mutate-batch-tutorial-kubebuilder-io-v1-cronjob,mutating=true,failurePolicy=fail,sideEffects=None,groups=batch.tutorial.kubebuilder.io,resources=cronjobs,verbs=create;update,versions=v1,name=mcronjob.kb.io,admissionReviewVersions=v1`, "")
461-
hackutils.CheckError("fixing cronjob_webhook.go by replacing marker", err)
462-
463-
err = pluginutil.ReplaceInFile(
464-
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_webhook.go"),
465-
`// +kubebuilder:webhook:path=/validate-batch-tutorial-kubebuilder-io-v1-cronjob,mutating=false,failurePolicy=fail,sideEffects=None,groups=batch.tutorial.kubebuilder.io,resources=cronjobs,verbs=create;update,versions=v1,name=vcronjob.kb.io,admissionReviewVersions=v1`, "")
466-
hackutils.CheckError("fixing cronjob_webhook.go validate batch marker", err)
467-
468458
err = pluginutil.ReplaceInFile(
469459
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_webhook.go"),
470460
`// TODO(user): Add more fields as needed for defaulting`, fragmentForDefaultFields)

pkg/plugins/golang/v4/scaffolds/internal/templates/api/webhook.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func (r *{{ .Resource.Kind }}) SetupWebhookWithManager(mgr ctrl.Manager) error {
121121

122122
//nolint:lll
123123
defaultingWebhookTemplate = `
124-
// +kubebuilder:webhook:{{ if ne .Resource.Webhooks.WebhookVersion "v1" }}webhookVersions={{"{"}}{{ .Resource.Webhooks.WebhookVersion }}{{"}"}},{{ end }}path=/mutate-{{ .QualifiedGroupWithDash }}-{{ .Resource.Version }}-{{ lower .Resource.Kind }},mutating=true,failurePolicy=fail,sideEffects=None,groups={{ .Resource.QualifiedGroup }},resources={{ .Resource.Plural }},verbs=create;update,versions={{ .Resource.Version }},name=m{{ lower .Resource.Kind }}.kb.io,admissionReviewVersions={{ .AdmissionReviewVersions }}
124+
// +kubebuilder:webhook:{{ if ne .Resource.Webhooks.WebhookVersion "v1" }}webhookVersions={{"{"}}{{ .Resource.Webhooks.WebhookVersion }}{{"}"}},{{ end }}path=/mutate-{{ .QualifiedGroupWithDash }}-{{ .Resource.Version }}-{{ lower .Resource.Kind }},mutating=true,failurePolicy=fail,sideEffects=None,groups={{ .Resource.QualifiedGroup }},resources={{ .Resource.Plural }},verbs=create;update,versions={{ .Resource.Version }},name=m{{ lower .Resource.Kind }}-{{ .Resource.Version }}.kb.io,admissionReviewVersions={{ .AdmissionReviewVersions }}
125125
126126
// +kubebuilder:object:generate=false
127127
// {{ .Resource.Kind }}CustomDefaulter struct is responsible for setting default values on the custom resource of the
@@ -154,7 +154,7 @@ func (d *{{ .Resource.Kind }}CustomDefaulter) Default(ctx context.Context, obj r
154154
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
155155
// NOTE: The 'path' attribute must follow a specific pattern and should not be modified directly here.
156156
// Modifying the path for an invalid path can cause API server errors; failing to locate the webhook.
157-
// +kubebuilder:webhook:{{ if ne .Resource.Webhooks.WebhookVersion "v1" }}webhookVersions={{"{"}}{{ .Resource.Webhooks.WebhookVersion }}{{"}"}},{{ end }}path=/validate-{{ .QualifiedGroupWithDash }}-{{ .Resource.Version }}-{{ lower .Resource.Kind }},mutating=false,failurePolicy=fail,sideEffects=None,groups={{ .Resource.QualifiedGroup }},resources={{ .Resource.Plural }},verbs=create;update,versions={{ .Resource.Version }},name=v{{ lower .Resource.Kind }}.kb.io,admissionReviewVersions={{ .AdmissionReviewVersions }}
157+
// +kubebuilder:webhook:{{ if ne .Resource.Webhooks.WebhookVersion "v1" }}webhookVersions={{"{"}}{{ .Resource.Webhooks.WebhookVersion }}{{"}"}},{{ end }}path=/validate-{{ .QualifiedGroupWithDash }}-{{ .Resource.Version }}-{{ lower .Resource.Kind }},mutating=false,failurePolicy=fail,sideEffects=None,groups={{ .Resource.QualifiedGroup }},resources={{ .Resource.Plural }},verbs=create;update,versions={{ .Resource.Version }},name=v{{ lower .Resource.Kind }}-{{ .Resource.Version }}.kb.io,admissionReviewVersions={{ .AdmissionReviewVersions }}
158158
159159
// +kubebuilder:object:generate=false
160160
// {{ .Resource.Kind }}CustomValidator struct is responsible for validating the {{ .Resource.Kind }} resource

testdata/project-v4-multigroup-with-deploy-image/api/crew/v1/captain_webhook.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (r *Captain) SetupWebhookWithManager(mgr ctrl.Manager) error {
4242

4343
// TODO(user): EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
4444

45-
// +kubebuilder:webhook:path=/mutate-crew-testproject-org-v1-captain,mutating=true,failurePolicy=fail,sideEffects=None,groups=crew.testproject.org,resources=captains,verbs=create;update,versions=v1,name=mcaptain.kb.io,admissionReviewVersions=v1
45+
// +kubebuilder:webhook:path=/mutate-crew-testproject-org-v1-captain,mutating=true,failurePolicy=fail,sideEffects=None,groups=crew.testproject.org,resources=captains,verbs=create;update,versions=v1,name=mcaptain-v1.kb.io,admissionReviewVersions=v1
4646

4747
// +kubebuilder:object:generate=false
4848
// CaptainCustomDefaulter struct is responsible for setting default values on the custom resource of the
@@ -72,7 +72,7 @@ func (d *CaptainCustomDefaulter) Default(ctx context.Context, obj runtime.Object
7272
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
7373
// NOTE: The 'path' attribute must follow a specific pattern and should not be modified directly here.
7474
// Modifying the path for an invalid path can cause API server errors; failing to locate the webhook.
75-
// +kubebuilder:webhook:path=/validate-crew-testproject-org-v1-captain,mutating=false,failurePolicy=fail,sideEffects=None,groups=crew.testproject.org,resources=captains,verbs=create;update,versions=v1,name=vcaptain.kb.io,admissionReviewVersions=v1
75+
// +kubebuilder:webhook:path=/validate-crew-testproject-org-v1-captain,mutating=false,failurePolicy=fail,sideEffects=None,groups=crew.testproject.org,resources=captains,verbs=create;update,versions=v1,name=vcaptain-v1.kb.io,admissionReviewVersions=v1
7676

7777
// +kubebuilder:object:generate=false
7878
// CaptainCustomValidator struct is responsible for validating the Captain resource

testdata/project-v4-multigroup-with-deploy-image/api/ship/v1/destroyer_webhook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (r *Destroyer) SetupWebhookWithManager(mgr ctrl.Manager) error {
4040

4141
// TODO(user): EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
4242

43-
// +kubebuilder:webhook:path=/mutate-ship-testproject-org-v1-destroyer,mutating=true,failurePolicy=fail,sideEffects=None,groups=ship.testproject.org,resources=destroyers,verbs=create;update,versions=v1,name=mdestroyer.kb.io,admissionReviewVersions=v1
43+
// +kubebuilder:webhook:path=/mutate-ship-testproject-org-v1-destroyer,mutating=true,failurePolicy=fail,sideEffects=None,groups=ship.testproject.org,resources=destroyers,verbs=create;update,versions=v1,name=mdestroyer-v1.kb.io,admissionReviewVersions=v1
4444

4545
// +kubebuilder:object:generate=false
4646
// DestroyerCustomDefaulter struct is responsible for setting default values on the custom resource of the

testdata/project-v4-multigroup-with-deploy-image/api/ship/v2alpha1/cruiser_webhook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (r *Cruiser) SetupWebhookWithManager(mgr ctrl.Manager) error {
4444
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
4545
// NOTE: The 'path' attribute must follow a specific pattern and should not be modified directly here.
4646
// Modifying the path for an invalid path can cause API server errors; failing to locate the webhook.
47-
// +kubebuilder:webhook:path=/validate-ship-testproject-org-v2alpha1-cruiser,mutating=false,failurePolicy=fail,sideEffects=None,groups=ship.testproject.org,resources=cruisers,verbs=create;update,versions=v2alpha1,name=vcruiser.kb.io,admissionReviewVersions=v1
47+
// +kubebuilder:webhook:path=/validate-ship-testproject-org-v2alpha1-cruiser,mutating=false,failurePolicy=fail,sideEffects=None,groups=ship.testproject.org,resources=cruisers,verbs=create;update,versions=v2alpha1,name=vcruiser-v2alpha1.kb.io,admissionReviewVersions=v1
4848

4949
// +kubebuilder:object:generate=false
5050
// CruiserCustomValidator struct is responsible for validating the Cruiser resource

testdata/project-v4-multigroup-with-deploy-image/api/v1/lakers_webhook.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (r *Lakers) SetupWebhookWithManager(mgr ctrl.Manager) error {
4242

4343
// TODO(user): EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
4444

45-
// +kubebuilder:webhook:path=/mutate-testproject-org-v1-lakers,mutating=true,failurePolicy=fail,sideEffects=None,groups=testproject.org,resources=lakers,verbs=create;update,versions=v1,name=mlakers.kb.io,admissionReviewVersions=v1
45+
// +kubebuilder:webhook:path=/mutate-testproject-org-v1-lakers,mutating=true,failurePolicy=fail,sideEffects=None,groups=testproject.org,resources=lakers,verbs=create;update,versions=v1,name=mlakers-v1.kb.io,admissionReviewVersions=v1
4646

4747
// +kubebuilder:object:generate=false
4848
// LakersCustomDefaulter struct is responsible for setting default values on the custom resource of the
@@ -72,7 +72,7 @@ func (d *LakersCustomDefaulter) Default(ctx context.Context, obj runtime.Object)
7272
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
7373
// NOTE: The 'path' attribute must follow a specific pattern and should not be modified directly here.
7474
// Modifying the path for an invalid path can cause API server errors; failing to locate the webhook.
75-
// +kubebuilder:webhook:path=/validate-testproject-org-v1-lakers,mutating=false,failurePolicy=fail,sideEffects=None,groups=testproject.org,resources=lakers,verbs=create;update,versions=v1,name=vlakers.kb.io,admissionReviewVersions=v1
75+
// +kubebuilder:webhook:path=/validate-testproject-org-v1-lakers,mutating=false,failurePolicy=fail,sideEffects=None,groups=testproject.org,resources=lakers,verbs=create;update,versions=v1,name=vlakers-v1.kb.io,admissionReviewVersions=v1
7676

7777
// +kubebuilder:object:generate=false
7878
// LakersCustomValidator struct is responsible for validating the Lakers resource

testdata/project-v4-multigroup-with-deploy-image/config/webhook/manifests.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ webhooks:
1212
namespace: system
1313
path: /mutate-crew-testproject-org-v1-captain
1414
failurePolicy: Fail
15-
name: mcaptain.kb.io
15+
name: mcaptain-v1.kb.io
1616
rules:
1717
- apiGroups:
1818
- crew.testproject.org
@@ -32,7 +32,7 @@ webhooks:
3232
namespace: system
3333
path: /mutate-ship-testproject-org-v1-destroyer
3434
failurePolicy: Fail
35-
name: mdestroyer.kb.io
35+
name: mdestroyer-v1.kb.io
3636
rules:
3737
- apiGroups:
3838
- ship.testproject.org
@@ -52,7 +52,7 @@ webhooks:
5252
namespace: system
5353
path: /mutate-testproject-org-v1-lakers
5454
failurePolicy: Fail
55-
name: mlakers.kb.io
55+
name: mlakers-v1.kb.io
5656
rules:
5757
- apiGroups:
5858
- testproject.org
@@ -78,7 +78,7 @@ webhooks:
7878
namespace: system
7979
path: /validate-crew-testproject-org-v1-captain
8080
failurePolicy: Fail
81-
name: vcaptain.kb.io
81+
name: vcaptain-v1.kb.io
8282
rules:
8383
- apiGroups:
8484
- crew.testproject.org
@@ -98,7 +98,7 @@ webhooks:
9898
namespace: system
9999
path: /validate-ship-testproject-org-v2alpha1-cruiser
100100
failurePolicy: Fail
101-
name: vcruiser.kb.io
101+
name: vcruiser-v2alpha1.kb.io
102102
rules:
103103
- apiGroups:
104104
- ship.testproject.org
@@ -118,7 +118,7 @@ webhooks:
118118
namespace: system
119119
path: /validate-testproject-org-v1-lakers
120120
failurePolicy: Fail
121-
name: vlakers.kb.io
121+
name: vlakers-v1.kb.io
122122
rules:
123123
- apiGroups:
124124
- testproject.org

0 commit comments

Comments
 (0)