Skip to content

Commit

Permalink
fix: resolve duplicate webhook name issue in multi-version API
Browse files Browse the repository at this point in the history
- Adjust webhook names for mutating and validating webhooks to avoid conflicts when creating multiple versions of the same API.
More info: #4144
  • Loading branch information
camilamacedo86 committed Sep 8, 2024
1 parent fa9f70c commit 2b49c7c
Show file tree
Hide file tree
Showing 24 changed files with 96 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ A webhook will automatically be served that calls this defaulting.
The `Default` method is expected to mutate the receiver, setting the defaults.
*/

// +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

// +kubebuilder:object:generate=false
// CronJobCustomDefaulter struct is responsible for setting default values on the custom resource of the
// Kind CronJob when those are created or updated.
Expand Down Expand Up @@ -153,6 +155,7 @@ validate anything on deletion.

// NOTE: The 'path' attribute must follow a specific pattern and should not be modified directly here.
// Modifying the path for an invalid path can cause API server errors; failing to locate the webhook.
// +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

// +kubebuilder:object:generate=false
// CronJobCustomValidator struct is responsible for validating the CronJob resource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,26 @@ kind: MutatingWebhookConfiguration
metadata:
name: mutating-webhook-configuration
webhooks:
- admissionReviewVersions:
- v1
clientConfig:
service:
name: webhook-service
namespace: system
path: /mutate-batch-tutorial-kubebuilder-io-v1-cronjob
failurePolicy: Fail
name: mcronjob-v1.kb.io
rules:
- apiGroups:
- batch.tutorial.kubebuilder.io
apiVersions:
- v1
operations:
- CREATE
- UPDATE
resources:
- cronjobs
sideEffects: None
- admissionReviewVersions:
- v1
clientConfig:
Expand All @@ -30,6 +50,26 @@ kind: ValidatingWebhookConfiguration
metadata:
name: validating-webhook-configuration
webhooks:
- admissionReviewVersions:
- v1
clientConfig:
service:
name: webhook-service
namespace: system
path: /validate-batch-tutorial-kubebuilder-io-v1-cronjob
failurePolicy: Fail
name: vcronjob-v1.kb.io
rules:
- apiGroups:
- batch.tutorial.kubebuilder.io
apiVersions:
- v1
operations:
- CREATE
- UPDATE
resources:
- cronjobs
sideEffects: None
- admissionReviewVersions:
- v1
clientConfig:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ A webhook will automatically be served that calls this defaulting.
The `Default` method is expected to mutate the receiver, setting the defaults.
*/

// +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

// +kubebuilder:object:generate=false
// CronJobCustomDefaulter struct is responsible for setting default values on the custom resource of the
// Kind CronJob when those are created or updated.
Expand Down Expand Up @@ -157,6 +159,7 @@ validate anything on deletion.

// NOTE: The 'path' attribute must follow a specific pattern and should not be modified directly here.
// Modifying the path for an invalid path can cause API server errors; failing to locate the webhook.
// +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

// +kubebuilder:object:generate=false
// CronJobCustomValidator struct is responsible for validating the CronJob resource
Expand Down
10 changes: 0 additions & 10 deletions hack/docs/internal/cronjob-tutorial/generate_cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,16 +455,6 @@ Then, we set up the webhook with the manager.
`// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.`, "")
hackutils.CheckError("fixing cronjob_webhook.go by replace TODO to change verbs", err)

err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_webhook.go"),
`// +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`, "")
hackutils.CheckError("fixing cronjob_webhook.go by replacing marker", err)

err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_webhook.go"),
`// +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`, "")
hackutils.CheckError("fixing cronjob_webhook.go validate batch marker", err)

err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_webhook.go"),
`// TODO(user): Add more fields as needed for defaulting`, fragmentForDefaultFields)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (r *{{ .Resource.Kind }}) SetupWebhookWithManager(mgr ctrl.Manager) error {

//nolint:lll
defaultingWebhookTemplate = `
// +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 }}
// +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 }}
// +kubebuilder:object:generate=false
// {{ .Resource.Kind }}CustomDefaulter struct is responsible for setting default values on the custom resource of the
Expand Down Expand Up @@ -154,7 +154,7 @@ func (d *{{ .Resource.Kind }}CustomDefaulter) Default(ctx context.Context, obj r
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
// NOTE: The 'path' attribute must follow a specific pattern and should not be modified directly here.
// Modifying the path for an invalid path can cause API server errors; failing to locate the webhook.
// +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 }}
// +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 }}
// +kubebuilder:object:generate=false
// {{ .Resource.Kind }}CustomValidator struct is responsible for validating the {{ .Resource.Kind }} resource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (r *Captain) SetupWebhookWithManager(mgr ctrl.Manager) error {

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

// +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
// +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

// +kubebuilder:object:generate=false
// CaptainCustomDefaulter struct is responsible for setting default values on the custom resource of the
Expand Down Expand Up @@ -72,7 +72,7 @@ func (d *CaptainCustomDefaulter) Default(ctx context.Context, obj runtime.Object
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
// NOTE: The 'path' attribute must follow a specific pattern and should not be modified directly here.
// Modifying the path for an invalid path can cause API server errors; failing to locate the webhook.
// +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
// +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

// +kubebuilder:object:generate=false
// CaptainCustomValidator struct is responsible for validating the Captain resource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (r *Destroyer) SetupWebhookWithManager(mgr ctrl.Manager) error {

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

// +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
// +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

// +kubebuilder:object:generate=false
// DestroyerCustomDefaulter struct is responsible for setting default values on the custom resource of the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (r *Cruiser) SetupWebhookWithManager(mgr ctrl.Manager) error {
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
// NOTE: The 'path' attribute must follow a specific pattern and should not be modified directly here.
// Modifying the path for an invalid path can cause API server errors; failing to locate the webhook.
// +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
// +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

// +kubebuilder:object:generate=false
// CruiserCustomValidator struct is responsible for validating the Cruiser resource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (r *Lakers) SetupWebhookWithManager(mgr ctrl.Manager) error {

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

// +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
// +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

// +kubebuilder:object:generate=false
// LakersCustomDefaulter struct is responsible for setting default values on the custom resource of the
Expand Down Expand Up @@ -72,7 +72,7 @@ func (d *LakersCustomDefaulter) Default(ctx context.Context, obj runtime.Object)
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
// NOTE: The 'path' attribute must follow a specific pattern and should not be modified directly here.
// Modifying the path for an invalid path can cause API server errors; failing to locate the webhook.
// +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
// +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

// +kubebuilder:object:generate=false
// LakersCustomValidator struct is responsible for validating the Lakers resource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ webhooks:
namespace: system
path: /mutate-crew-testproject-org-v1-captain
failurePolicy: Fail
name: mcaptain.kb.io
name: mcaptain-v1.kb.io
rules:
- apiGroups:
- crew.testproject.org
Expand All @@ -32,7 +32,7 @@ webhooks:
namespace: system
path: /mutate-ship-testproject-org-v1-destroyer
failurePolicy: Fail
name: mdestroyer.kb.io
name: mdestroyer-v1.kb.io
rules:
- apiGroups:
- ship.testproject.org
Expand All @@ -52,7 +52,7 @@ webhooks:
namespace: system
path: /mutate-testproject-org-v1-lakers
failurePolicy: Fail
name: mlakers.kb.io
name: mlakers-v1.kb.io
rules:
- apiGroups:
- testproject.org
Expand All @@ -78,7 +78,7 @@ webhooks:
namespace: system
path: /validate-crew-testproject-org-v1-captain
failurePolicy: Fail
name: vcaptain.kb.io
name: vcaptain-v1.kb.io
rules:
- apiGroups:
- crew.testproject.org
Expand All @@ -98,7 +98,7 @@ webhooks:
namespace: system
path: /validate-ship-testproject-org-v2alpha1-cruiser
failurePolicy: Fail
name: vcruiser.kb.io
name: vcruiser-v2alpha1.kb.io
rules:
- apiGroups:
- ship.testproject.org
Expand All @@ -118,7 +118,7 @@ webhooks:
namespace: system
path: /validate-testproject-org-v1-lakers
failurePolicy: Fail
name: vlakers.kb.io
name: vlakers-v1.kb.io
rules:
- apiGroups:
- testproject.org
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1540,7 +1540,7 @@ webhooks:
namespace: project-v4-multigroup-with-deploy-image-system
path: /mutate-crew-testproject-org-v1-captain
failurePolicy: Fail
name: mcaptain.kb.io
name: mcaptain-v1.kb.io
rules:
- apiGroups:
- crew.testproject.org
Expand All @@ -1560,7 +1560,7 @@ webhooks:
namespace: project-v4-multigroup-with-deploy-image-system
path: /mutate-ship-testproject-org-v1-destroyer
failurePolicy: Fail
name: mdestroyer.kb.io
name: mdestroyer-v1.kb.io
rules:
- apiGroups:
- ship.testproject.org
Expand All @@ -1580,7 +1580,7 @@ webhooks:
namespace: project-v4-multigroup-with-deploy-image-system
path: /mutate-testproject-org-v1-lakers
failurePolicy: Fail
name: mlakers.kb.io
name: mlakers-v1.kb.io
rules:
- apiGroups:
- testproject.org
Expand All @@ -1606,7 +1606,7 @@ webhooks:
namespace: project-v4-multigroup-with-deploy-image-system
path: /validate-crew-testproject-org-v1-captain
failurePolicy: Fail
name: vcaptain.kb.io
name: vcaptain-v1.kb.io
rules:
- apiGroups:
- crew.testproject.org
Expand All @@ -1626,7 +1626,7 @@ webhooks:
namespace: project-v4-multigroup-with-deploy-image-system
path: /validate-ship-testproject-org-v2alpha1-cruiser
failurePolicy: Fail
name: vcruiser.kb.io
name: vcruiser-v2alpha1.kb.io
rules:
- apiGroups:
- ship.testproject.org
Expand All @@ -1646,7 +1646,7 @@ webhooks:
namespace: project-v4-multigroup-with-deploy-image-system
path: /validate-testproject-org-v1-lakers
failurePolicy: Fail
name: vlakers.kb.io
name: vlakers-v1.kb.io
rules:
- apiGroups:
- testproject.org
Expand Down
4 changes: 2 additions & 2 deletions testdata/project-v4-multigroup/api/crew/v1/captain_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (r *Captain) SetupWebhookWithManager(mgr ctrl.Manager) error {

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

// +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
// +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

// +kubebuilder:object:generate=false
// CaptainCustomDefaulter struct is responsible for setting default values on the custom resource of the
Expand Down Expand Up @@ -72,7 +72,7 @@ func (d *CaptainCustomDefaulter) Default(ctx context.Context, obj runtime.Object
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
// NOTE: The 'path' attribute must follow a specific pattern and should not be modified directly here.
// Modifying the path for an invalid path can cause API server errors; failing to locate the webhook.
// +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
// +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

// +kubebuilder:object:generate=false
// CaptainCustomValidator struct is responsible for validating the Captain resource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (r *Destroyer) SetupWebhookWithManager(mgr ctrl.Manager) error {

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

// +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
// +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

// +kubebuilder:object:generate=false
// DestroyerCustomDefaulter struct is responsible for setting default values on the custom resource of the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (r *Cruiser) SetupWebhookWithManager(mgr ctrl.Manager) error {
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
// NOTE: The 'path' attribute must follow a specific pattern and should not be modified directly here.
// Modifying the path for an invalid path can cause API server errors; failing to locate the webhook.
// +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
// +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

// +kubebuilder:object:generate=false
// CruiserCustomValidator struct is responsible for validating the Cruiser resource
Expand Down
Loading

0 comments on commit 2b49c7c

Please sign in to comment.