From 42bd2b085a2b65cf67bbbf1bcc4f6ace3dd7a308 Mon Sep 17 00:00:00 2001 From: Jonas Hungershausen Date: Wed, 10 Jan 2024 22:31:44 +0100 Subject: [PATCH] Use CustomValidator and CustomDefaulter instead of Validator and Defaulter --- .../project/api/v1/cronjob_webhook.go | 27 ++++++++------ .../project/internal/controller/suite_test.go | 1 - .../webhook-implementation.md | 4 +-- .../cronjob-tutorial/generate_cronjob.go | 9 +++-- .../webhook_implementation.go | 5 ++- pkg/plugin/util/util.go | 8 ++--- .../internal/templates/api/webhook.go | 36 +++++++++++-------- .../api/crew/v1/captain_webhook.go | 25 +++++++------ .../api/ship/v1/destroyer_webhook.go | 11 ++++-- .../api/ship/v1/zz_generated.deepcopy.go | 2 +- .../api/ship/v2alpha1/cruiser_webhook.go | 17 +++++---- .../api/v1/lakers_webhook.go | 25 +++++++------ .../api/crew/v1/captain_webhook.go | 25 +++++++------ .../api/ship/v1/destroyer_webhook.go | 11 ++++-- .../api/ship/v1/zz_generated.deepcopy.go | 2 +- .../api/ship/v2alpha1/cruiser_webhook.go | 17 +++++---- .../api/v1/lakers_webhook.go | 25 +++++++------ .../api/v1alpha1/memcached_webhook.go | 17 +++++---- testdata/project-v4/api/v1/admiral_webhook.go | 11 ++++-- testdata/project-v4/api/v1/captain_webhook.go | 25 +++++++------ 20 files changed, 185 insertions(+), 118 deletions(-) diff --git a/docs/book/src/cronjob-tutorial/testdata/project/api/v1/cronjob_webhook.go b/docs/book/src/cronjob-tutorial/testdata/project/api/v1/cronjob_webhook.go index c7a8ca05a18..4fa59aba39d 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/api/v1/cronjob_webhook.go +++ b/docs/book/src/cronjob-tutorial/testdata/project/api/v1/cronjob_webhook.go @@ -18,6 +18,7 @@ limitations under the License. package v1 import ( + "context" "github.com/robfig/cron" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" @@ -46,6 +47,8 @@ Then, we set up the webhook with the manager. func (r *CronJob) SetupWebhookWithManager(mgr ctrl.Manager) error { return ctrl.NewWebhookManagedBy(mgr). For(r). + WithValidator(r). + WithDefaulter(r). Complete() } @@ -59,16 +62,16 @@ The meaning of each marker can be found [here](/reference/markers/webhook.md). //+kubebuilder:webhook:path=/mutate-batch-tutorial-kubebuilder-io-v1-cronjob,mutating=true,failurePolicy=fail,groups=batch.tutorial.kubebuilder.io,resources=cronjobs,verbs=create;update,versions=v1,name=mcronjob.kb.io,sideEffects=None,admissionReviewVersions=v1 /* -We use the `webhook.Defaulter` interface to set defaults to our CRD. +We use the `webhook.CustomDefaulter` interface to set defaults to our CRD. A webhook will automatically be served that calls this defaulting. The `Default` method is expected to mutate the receiver, setting the defaults. */ -var _ webhook.Defaulter = &CronJob{} +var _ webhook.CustomDefaulter = &CronJob{} -// Default implements webhook.Defaulter so a webhook will be registered for the type -func (r *CronJob) Default() { +// Default implements webhook.CustomDefaulter so a webhook will be registered for the type +func (r *CronJob) Default(ctx context.Context, obj runtime.Object) error { cronjoblog.Info("default", "name", r.Name) if r.Spec.ConcurrencyPolicy == "" { @@ -85,6 +88,8 @@ func (r *CronJob) Default() { r.Spec.FailedJobsHistoryLimit = new(int32) *r.Spec.FailedJobsHistoryLimit = 1 } + + return nil } /* @@ -115,24 +120,24 @@ Here, however, we just use the same shared validation for `ValidateCreate` and validate anything on deletion. */ -var _ webhook.Validator = &CronJob{} +var _ webhook.CustomValidator = &CronJob{} -// ValidateCreate implements webhook.Validator so a webhook will be registered for the type -func (r *CronJob) ValidateCreate() (admission.Warnings, error) { +// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type +func (r *CronJob) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { cronjoblog.Info("validate create", "name", r.Name) return nil, r.validateCronJob() } -// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type -func (r *CronJob) ValidateUpdate(old runtime.Object) (admission.Warnings, error) { +// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type +func (r *CronJob) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) { cronjoblog.Info("validate update", "name", r.Name) return nil, r.validateCronJob() } -// ValidateDelete implements webhook.Validator so a webhook will be registered for the type -func (r *CronJob) ValidateDelete() (admission.Warnings, error) { +// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type +func (r *CronJob) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { cronjoblog.Info("validate delete", "name", r.Name) // TODO(user): fill in your validation logic upon object deletion. diff --git a/docs/book/src/cronjob-tutorial/testdata/project/internal/controller/suite_test.go b/docs/book/src/cronjob-tutorial/testdata/project/internal/controller/suite_test.go index 1b5f665a1af..0f664f221e0 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/internal/controller/suite_test.go +++ b/docs/book/src/cronjob-tutorial/testdata/project/internal/controller/suite_test.go @@ -22,7 +22,6 @@ Kubebuilder scaffolded a `internal/controller/suite_test.go` file that does the First, it will contain the necessary imports. */ - package controller import ( diff --git a/docs/book/src/cronjob-tutorial/webhook-implementation.md b/docs/book/src/cronjob-tutorial/webhook-implementation.md index 756bbb1474b..38fc8f56f04 100644 --- a/docs/book/src/cronjob-tutorial/webhook-implementation.md +++ b/docs/book/src/cronjob-tutorial/webhook-implementation.md @@ -1,8 +1,8 @@ # Implementing defaulting/validating webhooks If you want to implement [admission webhooks](../reference/admission-webhook.md) -for your CRD, the only thing you need to do is to implement the `Defaulter` -and (or) the `Validator` interface. +for your CRD, the only thing you need to do is to implement the `CustomDefaulter` +and (or) the `CustomValidator` interface. Kubebuilder takes care of the rest for you, such as diff --git a/hack/docs/internal/cronjob-tutorial/generate_cronjob.go b/hack/docs/internal/cronjob-tutorial/generate_cronjob.go index 0039c0454c4..2206622060c 100644 --- a/hack/docs/internal/cronjob-tutorial/generate_cronjob.go +++ b/hack/docs/internal/cronjob-tutorial/generate_cronjob.go @@ -382,6 +382,8 @@ func updateWebhook(sp *Sample) { err = pluginutil.ReplaceInFile( filepath.Join(sp.ctx.Dir, "api/v1/cronjob_webhook.go"), `import ( + "context" + "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -427,6 +429,7 @@ Then, we set up the webhook with the manager. `cronjoblog.Info("default", "name", r.Name) // TODO(user): fill in your defaulting logic. + return nil `, WebhookValidate) CheckError("fixing cronjob_webhook.go by adding logic", err) @@ -448,7 +451,7 @@ Then, we set up the webhook with the manager. err = pluginutil.InsertCode( filepath.Join(sp.ctx.Dir, "api/v1/cronjob_webhook.go"), - `func (r *CronJob) ValidateDelete() (admission.Warnings, error) { + `func (r *CronJob) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { cronjoblog.Info("validate delete", "name", r.Name) // TODO(user): fill in your validation logic upon object deletion. @@ -596,8 +599,8 @@ func updateExample(sp *Sample) { } func addControllerTest(sp *Sample) { - var fs = afero.NewOsFs() - err := afero.WriteFile(fs, filepath.Join(sp.ctx.Dir, "internal/controller/cronjob_controller_test.go"), []byte(ControllerTest), 0600) + fs := afero.NewOsFs() + err := afero.WriteFile(fs, filepath.Join(sp.ctx.Dir, "internal/controller/cronjob_controller_test.go"), []byte(ControllerTest), 0o600) CheckError("adding cronjob_controller_test", err) } diff --git a/hack/docs/internal/cronjob-tutorial/webhook_implementation.go b/hack/docs/internal/cronjob-tutorial/webhook_implementation.go index c65171fefa4..a8cc34fff13 100644 --- a/hack/docs/internal/cronjob-tutorial/webhook_implementation.go +++ b/hack/docs/internal/cronjob-tutorial/webhook_implementation.go @@ -17,6 +17,7 @@ limitations under the License. package cronjob const WebhookIntro = `import ( + "context" "github.com/robfig/cron" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" @@ -47,7 +48,7 @@ The meaning of each marker can be found [here](/reference/markers/webhook.md). //+kubebuilder:webhook:path=/mutate-batch-tutorial-kubebuilder-io-v1-cronjob,mutating=true,failurePolicy=fail,groups=batch.tutorial.kubebuilder.io,resources=cronjobs,verbs=create;update,versions=v1,name=mcronjob.kb.io,sideEffects=None,admissionReviewVersions=v1 /* -We use the` + " `" + `webhook.Defaulter` + "`" + ` interface to set defaults to our CRD. +We use the` + " `" + `webhook.CustomDefaulter` + "`" + ` interface to set defaults to our CRD. A webhook will automatically be served that calls this defaulting. The` + " `" + `Default` + "`" + ` method is expected to mutate the receiver, setting the defaults. @@ -70,6 +71,8 @@ const WebhookValidate = ` cronjoblog.Info("default", "name", r.Name) r.Spec.FailedJobsHistoryLimit = new(int32) *r.Spec.FailedJobsHistoryLimit = 1 } + + return nil } /* diff --git a/pkg/plugin/util/util.go b/pkg/plugin/util/util.go index d4b34d5d616..bc22e73f149 100644 --- a/pkg/plugin/util/util.go +++ b/pkg/plugin/util/util.go @@ -152,8 +152,8 @@ func ImplementWebhooks(filename string) error { str, err = EnsureExistAndReplace( str, "// TODO(user): fill in your defaulting logic.", - `if r.Spec.Count == 0 { - r.Spec.Count = 5 + `if res.Spec.Count == 0 { + res.Spec.Count = 5 }`) if err != nil { return err @@ -163,7 +163,7 @@ func ImplementWebhooks(filename string) error { str, err = EnsureExistAndReplace( str, "// TODO(user): fill in your validation logic upon object creation.", - `if r.Spec.Count < 0 { + `if res.Spec.Count < 0 { return nil, errors.New(".spec.count must >= 0") }`) if err != nil { @@ -172,7 +172,7 @@ func ImplementWebhooks(filename string) error { str, err = EnsureExistAndReplace( str, "// TODO(user): fill in your validation logic upon object update.", - `if r.Spec.Count < 0 { + `if newRes.Spec.Count < 0 { return nil, errors.New(".spec.count must >= 0") }`) if err != nil { diff --git a/pkg/plugins/golang/v4/scaffolds/internal/templates/api/webhook.go b/pkg/plugins/golang/v4/scaffolds/internal/templates/api/webhook.go index 60ff9051a27..8b07c8e8b14 100644 --- a/pkg/plugins/golang/v4/scaffolds/internal/templates/api/webhook.go +++ b/pkg/plugins/golang/v4/scaffolds/internal/templates/api/webhook.go @@ -85,13 +85,14 @@ package {{ .Resource.Version }} import ( ctrl "sigs.k8s.io/controller-runtime" logf "sigs.k8s.io/controller-runtime/pkg/log" - {{- if .Resource.HasValidationWebhook }} - "k8s.io/apimachinery/pkg/runtime" - "sigs.k8s.io/controller-runtime/pkg/webhook/admission" - {{- end }} {{- if or .Resource.HasValidationWebhook .Resource.HasDefaultingWebhook }} + "context" + "k8s.io/apimachinery/pkg/runtime" "sigs.k8s.io/controller-runtime/pkg/webhook" {{- end }} + {{- if .Resource.HasValidationWebhook }} + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" + {{- end }} ) @@ -102,6 +103,12 @@ var {{ lower .Resource.Kind }}log = logf.Log.WithName("{{ lower .Resource.Kind } func (r *{{ .Resource.Kind }}) SetupWebhookWithManager(mgr ctrl.Manager) error { return ctrl.NewWebhookManagedBy(mgr). For(r). + {{- if .Resource.HasValidationWebhook }} + WithValidator(r). + {{- end }} + {{- if .Resource.HasDefaultingWebhook }} + WithDefaulter(r). + {{- end }} Complete() } @@ -112,13 +119,14 @@ func (r *{{ .Resource.Kind }}) SetupWebhookWithManager(mgr ctrl.Manager) error { 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 }} -var _ webhook.Defaulter = &{{ .Resource.Kind }}{} +var _ webhook.CustomDefaulter = &{{ .Resource.Kind }}{} -// Default implements webhook.Defaulter so a webhook will be registered for the type -func (r *{{ .Resource.Kind }}) Default() { +// Default implements webhook.CustomDefaulter so a webhook will be registered for the type +func (r *{{ .Resource.Kind }}) Default(ctx context.Context, obj runtime.Object) error { {{ lower .Resource.Kind }}log.Info("default", "name", r.Name) // TODO(user): fill in your defaulting logic. + return nil } ` @@ -127,26 +135,26 @@ func (r *{{ .Resource.Kind }}) Default() { // TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation. //+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 }} -var _ webhook.Validator = &{{ .Resource.Kind }}{} +var _ webhook.CustomValidator = &{{ .Resource.Kind }}{} -// ValidateCreate implements webhook.Validator so a webhook will be registered for the type -func (r *{{ .Resource.Kind }}) ValidateCreate() (admission.Warnings, error) { +// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type +func (r *{{ .Resource.Kind }}) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { {{ lower .Resource.Kind }}log.Info("validate create", "name", r.Name) // TODO(user): fill in your validation logic upon object creation. return nil, nil } -// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type -func (r *{{ .Resource.Kind }}) ValidateUpdate(old runtime.Object) (admission.Warnings, error) { +// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type +func (r *{{ .Resource.Kind }}) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) { {{ lower .Resource.Kind }}log.Info("validate update", "name", r.Name) // TODO(user): fill in your validation logic upon object update. return nil, nil } -// ValidateDelete implements webhook.Validator so a webhook will be registered for the type -func (r *{{ .Resource.Kind }}) ValidateDelete() (admission.Warnings, error) { +// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type +func (r *{{ .Resource.Kind }}) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { {{ lower .Resource.Kind }}log.Info("validate delete", "name", r.Name) // TODO(user): fill in your validation logic upon object deletion. diff --git a/testdata/project-v4-multigroup-with-deploy-image/api/crew/v1/captain_webhook.go b/testdata/project-v4-multigroup-with-deploy-image/api/crew/v1/captain_webhook.go index 27aaa567a40..dfe9cbc2689 100644 --- a/testdata/project-v4-multigroup-with-deploy-image/api/crew/v1/captain_webhook.go +++ b/testdata/project-v4-multigroup-with-deploy-image/api/crew/v1/captain_webhook.go @@ -17,6 +17,8 @@ limitations under the License. package v1 import ( + "context" + "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -31,6 +33,8 @@ var captainlog = logf.Log.WithName("captain-resource") func (r *Captain) SetupWebhookWithManager(mgr ctrl.Manager) error { return ctrl.NewWebhookManagedBy(mgr). For(r). + WithValidator(r). + WithDefaulter(r). Complete() } @@ -38,38 +42,39 @@ func (r *Captain) SetupWebhookWithManager(mgr ctrl.Manager) error { //+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 -var _ webhook.Defaulter = &Captain{} +var _ webhook.CustomDefaulter = &Captain{} -// Default implements webhook.Defaulter so a webhook will be registered for the type -func (r *Captain) Default() { +// Default implements webhook.CustomDefaulter so a webhook will be registered for the type +func (r *Captain) Default(ctx context.Context, obj runtime.Object) error { captainlog.Info("default", "name", r.Name) // TODO(user): fill in your defaulting logic. + return nil } // TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation. //+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 -var _ webhook.Validator = &Captain{} +var _ webhook.CustomValidator = &Captain{} -// ValidateCreate implements webhook.Validator so a webhook will be registered for the type -func (r *Captain) ValidateCreate() (admission.Warnings, error) { +// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type +func (r *Captain) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { captainlog.Info("validate create", "name", r.Name) // TODO(user): fill in your validation logic upon object creation. return nil, nil } -// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type -func (r *Captain) ValidateUpdate(old runtime.Object) (admission.Warnings, error) { +// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type +func (r *Captain) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) { captainlog.Info("validate update", "name", r.Name) // TODO(user): fill in your validation logic upon object update. return nil, nil } -// ValidateDelete implements webhook.Validator so a webhook will be registered for the type -func (r *Captain) ValidateDelete() (admission.Warnings, error) { +// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type +func (r *Captain) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { captainlog.Info("validate delete", "name", r.Name) // TODO(user): fill in your validation logic upon object deletion. diff --git a/testdata/project-v4-multigroup-with-deploy-image/api/ship/v1/destroyer_webhook.go b/testdata/project-v4-multigroup-with-deploy-image/api/ship/v1/destroyer_webhook.go index 89c6aa292c2..0b356923334 100644 --- a/testdata/project-v4-multigroup-with-deploy-image/api/ship/v1/destroyer_webhook.go +++ b/testdata/project-v4-multigroup-with-deploy-image/api/ship/v1/destroyer_webhook.go @@ -17,6 +17,9 @@ limitations under the License. package v1 import ( + "context" + + "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/webhook" @@ -29,6 +32,7 @@ var destroyerlog = logf.Log.WithName("destroyer-resource") func (r *Destroyer) SetupWebhookWithManager(mgr ctrl.Manager) error { return ctrl.NewWebhookManagedBy(mgr). For(r). + WithDefaulter(r). Complete() } @@ -36,11 +40,12 @@ func (r *Destroyer) SetupWebhookWithManager(mgr ctrl.Manager) error { //+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 -var _ webhook.Defaulter = &Destroyer{} +var _ webhook.CustomDefaulter = &Destroyer{} -// Default implements webhook.Defaulter so a webhook will be registered for the type -func (r *Destroyer) Default() { +// Default implements webhook.CustomDefaulter so a webhook will be registered for the type +func (r *Destroyer) Default(ctx context.Context, obj runtime.Object) error { destroyerlog.Info("default", "name", r.Name) // TODO(user): fill in your defaulting logic. + return nil } diff --git a/testdata/project-v4-multigroup-with-deploy-image/api/ship/v1/zz_generated.deepcopy.go b/testdata/project-v4-multigroup-with-deploy-image/api/ship/v1/zz_generated.deepcopy.go index 8931c51a317..ca3974a1d81 100644 --- a/testdata/project-v4-multigroup-with-deploy-image/api/ship/v1/zz_generated.deepcopy.go +++ b/testdata/project-v4-multigroup-with-deploy-image/api/ship/v1/zz_generated.deepcopy.go @@ -21,7 +21,7 @@ limitations under the License. package v1 import ( - runtime "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime" ) // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. diff --git a/testdata/project-v4-multigroup-with-deploy-image/api/ship/v2alpha1/cruiser_webhook.go b/testdata/project-v4-multigroup-with-deploy-image/api/ship/v2alpha1/cruiser_webhook.go index 30b36680210..9d4901da58b 100644 --- a/testdata/project-v4-multigroup-with-deploy-image/api/ship/v2alpha1/cruiser_webhook.go +++ b/testdata/project-v4-multigroup-with-deploy-image/api/ship/v2alpha1/cruiser_webhook.go @@ -17,6 +17,8 @@ limitations under the License. package v2alpha1 import ( + "context" + "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -31,6 +33,7 @@ var cruiserlog = logf.Log.WithName("cruiser-resource") func (r *Cruiser) SetupWebhookWithManager(mgr ctrl.Manager) error { return ctrl.NewWebhookManagedBy(mgr). For(r). + WithValidator(r). Complete() } @@ -39,26 +42,26 @@ func (r *Cruiser) SetupWebhookWithManager(mgr ctrl.Manager) error { // TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation. //+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 -var _ webhook.Validator = &Cruiser{} +var _ webhook.CustomValidator = &Cruiser{} -// ValidateCreate implements webhook.Validator so a webhook will be registered for the type -func (r *Cruiser) ValidateCreate() (admission.Warnings, error) { +// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type +func (r *Cruiser) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { cruiserlog.Info("validate create", "name", r.Name) // TODO(user): fill in your validation logic upon object creation. return nil, nil } -// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type -func (r *Cruiser) ValidateUpdate(old runtime.Object) (admission.Warnings, error) { +// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type +func (r *Cruiser) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) { cruiserlog.Info("validate update", "name", r.Name) // TODO(user): fill in your validation logic upon object update. return nil, nil } -// ValidateDelete implements webhook.Validator so a webhook will be registered for the type -func (r *Cruiser) ValidateDelete() (admission.Warnings, error) { +// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type +func (r *Cruiser) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { cruiserlog.Info("validate delete", "name", r.Name) // TODO(user): fill in your validation logic upon object deletion. diff --git a/testdata/project-v4-multigroup-with-deploy-image/api/v1/lakers_webhook.go b/testdata/project-v4-multigroup-with-deploy-image/api/v1/lakers_webhook.go index 6ac8250486e..f5244bc49d9 100644 --- a/testdata/project-v4-multigroup-with-deploy-image/api/v1/lakers_webhook.go +++ b/testdata/project-v4-multigroup-with-deploy-image/api/v1/lakers_webhook.go @@ -17,6 +17,8 @@ limitations under the License. package v1 import ( + "context" + "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -31,6 +33,8 @@ var lakerslog = logf.Log.WithName("lakers-resource") func (r *Lakers) SetupWebhookWithManager(mgr ctrl.Manager) error { return ctrl.NewWebhookManagedBy(mgr). For(r). + WithValidator(r). + WithDefaulter(r). Complete() } @@ -38,38 +42,39 @@ func (r *Lakers) SetupWebhookWithManager(mgr ctrl.Manager) error { //+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 -var _ webhook.Defaulter = &Lakers{} +var _ webhook.CustomDefaulter = &Lakers{} -// Default implements webhook.Defaulter so a webhook will be registered for the type -func (r *Lakers) Default() { +// Default implements webhook.CustomDefaulter so a webhook will be registered for the type +func (r *Lakers) Default(ctx context.Context, obj runtime.Object) error { lakerslog.Info("default", "name", r.Name) // TODO(user): fill in your defaulting logic. + return nil } // TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation. //+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 -var _ webhook.Validator = &Lakers{} +var _ webhook.CustomValidator = &Lakers{} -// ValidateCreate implements webhook.Validator so a webhook will be registered for the type -func (r *Lakers) ValidateCreate() (admission.Warnings, error) { +// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type +func (r *Lakers) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { lakerslog.Info("validate create", "name", r.Name) // TODO(user): fill in your validation logic upon object creation. return nil, nil } -// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type -func (r *Lakers) ValidateUpdate(old runtime.Object) (admission.Warnings, error) { +// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type +func (r *Lakers) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) { lakerslog.Info("validate update", "name", r.Name) // TODO(user): fill in your validation logic upon object update. return nil, nil } -// ValidateDelete implements webhook.Validator so a webhook will be registered for the type -func (r *Lakers) ValidateDelete() (admission.Warnings, error) { +// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type +func (r *Lakers) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { lakerslog.Info("validate delete", "name", r.Name) // TODO(user): fill in your validation logic upon object deletion. diff --git a/testdata/project-v4-multigroup/api/crew/v1/captain_webhook.go b/testdata/project-v4-multigroup/api/crew/v1/captain_webhook.go index 27aaa567a40..dfe9cbc2689 100644 --- a/testdata/project-v4-multigroup/api/crew/v1/captain_webhook.go +++ b/testdata/project-v4-multigroup/api/crew/v1/captain_webhook.go @@ -17,6 +17,8 @@ limitations under the License. package v1 import ( + "context" + "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -31,6 +33,8 @@ var captainlog = logf.Log.WithName("captain-resource") func (r *Captain) SetupWebhookWithManager(mgr ctrl.Manager) error { return ctrl.NewWebhookManagedBy(mgr). For(r). + WithValidator(r). + WithDefaulter(r). Complete() } @@ -38,38 +42,39 @@ func (r *Captain) SetupWebhookWithManager(mgr ctrl.Manager) error { //+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 -var _ webhook.Defaulter = &Captain{} +var _ webhook.CustomDefaulter = &Captain{} -// Default implements webhook.Defaulter so a webhook will be registered for the type -func (r *Captain) Default() { +// Default implements webhook.CustomDefaulter so a webhook will be registered for the type +func (r *Captain) Default(ctx context.Context, obj runtime.Object) error { captainlog.Info("default", "name", r.Name) // TODO(user): fill in your defaulting logic. + return nil } // TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation. //+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 -var _ webhook.Validator = &Captain{} +var _ webhook.CustomValidator = &Captain{} -// ValidateCreate implements webhook.Validator so a webhook will be registered for the type -func (r *Captain) ValidateCreate() (admission.Warnings, error) { +// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type +func (r *Captain) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { captainlog.Info("validate create", "name", r.Name) // TODO(user): fill in your validation logic upon object creation. return nil, nil } -// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type -func (r *Captain) ValidateUpdate(old runtime.Object) (admission.Warnings, error) { +// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type +func (r *Captain) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) { captainlog.Info("validate update", "name", r.Name) // TODO(user): fill in your validation logic upon object update. return nil, nil } -// ValidateDelete implements webhook.Validator so a webhook will be registered for the type -func (r *Captain) ValidateDelete() (admission.Warnings, error) { +// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type +func (r *Captain) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { captainlog.Info("validate delete", "name", r.Name) // TODO(user): fill in your validation logic upon object deletion. diff --git a/testdata/project-v4-multigroup/api/ship/v1/destroyer_webhook.go b/testdata/project-v4-multigroup/api/ship/v1/destroyer_webhook.go index 89c6aa292c2..0b356923334 100644 --- a/testdata/project-v4-multigroup/api/ship/v1/destroyer_webhook.go +++ b/testdata/project-v4-multigroup/api/ship/v1/destroyer_webhook.go @@ -17,6 +17,9 @@ limitations under the License. package v1 import ( + "context" + + "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/webhook" @@ -29,6 +32,7 @@ var destroyerlog = logf.Log.WithName("destroyer-resource") func (r *Destroyer) SetupWebhookWithManager(mgr ctrl.Manager) error { return ctrl.NewWebhookManagedBy(mgr). For(r). + WithDefaulter(r). Complete() } @@ -36,11 +40,12 @@ func (r *Destroyer) SetupWebhookWithManager(mgr ctrl.Manager) error { //+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 -var _ webhook.Defaulter = &Destroyer{} +var _ webhook.CustomDefaulter = &Destroyer{} -// Default implements webhook.Defaulter so a webhook will be registered for the type -func (r *Destroyer) Default() { +// Default implements webhook.CustomDefaulter so a webhook will be registered for the type +func (r *Destroyer) Default(ctx context.Context, obj runtime.Object) error { destroyerlog.Info("default", "name", r.Name) // TODO(user): fill in your defaulting logic. + return nil } diff --git a/testdata/project-v4-multigroup/api/ship/v1/zz_generated.deepcopy.go b/testdata/project-v4-multigroup/api/ship/v1/zz_generated.deepcopy.go index 8931c51a317..ca3974a1d81 100644 --- a/testdata/project-v4-multigroup/api/ship/v1/zz_generated.deepcopy.go +++ b/testdata/project-v4-multigroup/api/ship/v1/zz_generated.deepcopy.go @@ -21,7 +21,7 @@ limitations under the License. package v1 import ( - runtime "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime" ) // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. diff --git a/testdata/project-v4-multigroup/api/ship/v2alpha1/cruiser_webhook.go b/testdata/project-v4-multigroup/api/ship/v2alpha1/cruiser_webhook.go index 30b36680210..9d4901da58b 100644 --- a/testdata/project-v4-multigroup/api/ship/v2alpha1/cruiser_webhook.go +++ b/testdata/project-v4-multigroup/api/ship/v2alpha1/cruiser_webhook.go @@ -17,6 +17,8 @@ limitations under the License. package v2alpha1 import ( + "context" + "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -31,6 +33,7 @@ var cruiserlog = logf.Log.WithName("cruiser-resource") func (r *Cruiser) SetupWebhookWithManager(mgr ctrl.Manager) error { return ctrl.NewWebhookManagedBy(mgr). For(r). + WithValidator(r). Complete() } @@ -39,26 +42,26 @@ func (r *Cruiser) SetupWebhookWithManager(mgr ctrl.Manager) error { // TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation. //+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 -var _ webhook.Validator = &Cruiser{} +var _ webhook.CustomValidator = &Cruiser{} -// ValidateCreate implements webhook.Validator so a webhook will be registered for the type -func (r *Cruiser) ValidateCreate() (admission.Warnings, error) { +// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type +func (r *Cruiser) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { cruiserlog.Info("validate create", "name", r.Name) // TODO(user): fill in your validation logic upon object creation. return nil, nil } -// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type -func (r *Cruiser) ValidateUpdate(old runtime.Object) (admission.Warnings, error) { +// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type +func (r *Cruiser) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) { cruiserlog.Info("validate update", "name", r.Name) // TODO(user): fill in your validation logic upon object update. return nil, nil } -// ValidateDelete implements webhook.Validator so a webhook will be registered for the type -func (r *Cruiser) ValidateDelete() (admission.Warnings, error) { +// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type +func (r *Cruiser) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { cruiserlog.Info("validate delete", "name", r.Name) // TODO(user): fill in your validation logic upon object deletion. diff --git a/testdata/project-v4-multigroup/api/v1/lakers_webhook.go b/testdata/project-v4-multigroup/api/v1/lakers_webhook.go index 6ac8250486e..f5244bc49d9 100644 --- a/testdata/project-v4-multigroup/api/v1/lakers_webhook.go +++ b/testdata/project-v4-multigroup/api/v1/lakers_webhook.go @@ -17,6 +17,8 @@ limitations under the License. package v1 import ( + "context" + "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -31,6 +33,8 @@ var lakerslog = logf.Log.WithName("lakers-resource") func (r *Lakers) SetupWebhookWithManager(mgr ctrl.Manager) error { return ctrl.NewWebhookManagedBy(mgr). For(r). + WithValidator(r). + WithDefaulter(r). Complete() } @@ -38,38 +42,39 @@ func (r *Lakers) SetupWebhookWithManager(mgr ctrl.Manager) error { //+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 -var _ webhook.Defaulter = &Lakers{} +var _ webhook.CustomDefaulter = &Lakers{} -// Default implements webhook.Defaulter so a webhook will be registered for the type -func (r *Lakers) Default() { +// Default implements webhook.CustomDefaulter so a webhook will be registered for the type +func (r *Lakers) Default(ctx context.Context, obj runtime.Object) error { lakerslog.Info("default", "name", r.Name) // TODO(user): fill in your defaulting logic. + return nil } // TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation. //+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 -var _ webhook.Validator = &Lakers{} +var _ webhook.CustomValidator = &Lakers{} -// ValidateCreate implements webhook.Validator so a webhook will be registered for the type -func (r *Lakers) ValidateCreate() (admission.Warnings, error) { +// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type +func (r *Lakers) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { lakerslog.Info("validate create", "name", r.Name) // TODO(user): fill in your validation logic upon object creation. return nil, nil } -// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type -func (r *Lakers) ValidateUpdate(old runtime.Object) (admission.Warnings, error) { +// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type +func (r *Lakers) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) { lakerslog.Info("validate update", "name", r.Name) // TODO(user): fill in your validation logic upon object update. return nil, nil } -// ValidateDelete implements webhook.Validator so a webhook will be registered for the type -func (r *Lakers) ValidateDelete() (admission.Warnings, error) { +// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type +func (r *Lakers) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { lakerslog.Info("validate delete", "name", r.Name) // TODO(user): fill in your validation logic upon object deletion. diff --git a/testdata/project-v4-with-deploy-image/api/v1alpha1/memcached_webhook.go b/testdata/project-v4-with-deploy-image/api/v1alpha1/memcached_webhook.go index 75ba7f7a540..dc3907db212 100644 --- a/testdata/project-v4-with-deploy-image/api/v1alpha1/memcached_webhook.go +++ b/testdata/project-v4-with-deploy-image/api/v1alpha1/memcached_webhook.go @@ -17,6 +17,8 @@ limitations under the License. package v1alpha1 import ( + "context" + "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -31,6 +33,7 @@ var memcachedlog = logf.Log.WithName("memcached-resource") func (r *Memcached) SetupWebhookWithManager(mgr ctrl.Manager) error { return ctrl.NewWebhookManagedBy(mgr). For(r). + WithValidator(r). Complete() } @@ -39,26 +42,26 @@ func (r *Memcached) SetupWebhookWithManager(mgr ctrl.Manager) error { // TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation. //+kubebuilder:webhook:path=/validate-example-com-testproject-org-v1alpha1-memcached,mutating=false,failurePolicy=fail,sideEffects=None,groups=example.com.testproject.org,resources=memcacheds,verbs=create;update,versions=v1alpha1,name=vmemcached.kb.io,admissionReviewVersions=v1 -var _ webhook.Validator = &Memcached{} +var _ webhook.CustomValidator = &Memcached{} -// ValidateCreate implements webhook.Validator so a webhook will be registered for the type -func (r *Memcached) ValidateCreate() (admission.Warnings, error) { +// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type +func (r *Memcached) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { memcachedlog.Info("validate create", "name", r.Name) // TODO(user): fill in your validation logic upon object creation. return nil, nil } -// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type -func (r *Memcached) ValidateUpdate(old runtime.Object) (admission.Warnings, error) { +// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type +func (r *Memcached) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) { memcachedlog.Info("validate update", "name", r.Name) // TODO(user): fill in your validation logic upon object update. return nil, nil } -// ValidateDelete implements webhook.Validator so a webhook will be registered for the type -func (r *Memcached) ValidateDelete() (admission.Warnings, error) { +// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type +func (r *Memcached) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { memcachedlog.Info("validate delete", "name", r.Name) // TODO(user): fill in your validation logic upon object deletion. diff --git a/testdata/project-v4/api/v1/admiral_webhook.go b/testdata/project-v4/api/v1/admiral_webhook.go index 3705be8e4fb..11a1f91d895 100644 --- a/testdata/project-v4/api/v1/admiral_webhook.go +++ b/testdata/project-v4/api/v1/admiral_webhook.go @@ -17,6 +17,9 @@ limitations under the License. package v1 import ( + "context" + + "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/webhook" @@ -29,6 +32,7 @@ var admirallog = logf.Log.WithName("admiral-resource") func (r *Admiral) SetupWebhookWithManager(mgr ctrl.Manager) error { return ctrl.NewWebhookManagedBy(mgr). For(r). + WithDefaulter(r). Complete() } @@ -36,11 +40,12 @@ func (r *Admiral) SetupWebhookWithManager(mgr ctrl.Manager) error { //+kubebuilder:webhook:path=/mutate-crew-testproject-org-v1-admiral,mutating=true,failurePolicy=fail,sideEffects=None,groups=crew.testproject.org,resources=admirales,verbs=create;update,versions=v1,name=madmiral.kb.io,admissionReviewVersions=v1 -var _ webhook.Defaulter = &Admiral{} +var _ webhook.CustomDefaulter = &Admiral{} -// Default implements webhook.Defaulter so a webhook will be registered for the type -func (r *Admiral) Default() { +// Default implements webhook.CustomDefaulter so a webhook will be registered for the type +func (r *Admiral) Default(ctx context.Context, obj runtime.Object) error { admirallog.Info("default", "name", r.Name) // TODO(user): fill in your defaulting logic. + return nil } diff --git a/testdata/project-v4/api/v1/captain_webhook.go b/testdata/project-v4/api/v1/captain_webhook.go index 27aaa567a40..dfe9cbc2689 100644 --- a/testdata/project-v4/api/v1/captain_webhook.go +++ b/testdata/project-v4/api/v1/captain_webhook.go @@ -17,6 +17,8 @@ limitations under the License. package v1 import ( + "context" + "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -31,6 +33,8 @@ var captainlog = logf.Log.WithName("captain-resource") func (r *Captain) SetupWebhookWithManager(mgr ctrl.Manager) error { return ctrl.NewWebhookManagedBy(mgr). For(r). + WithValidator(r). + WithDefaulter(r). Complete() } @@ -38,38 +42,39 @@ func (r *Captain) SetupWebhookWithManager(mgr ctrl.Manager) error { //+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 -var _ webhook.Defaulter = &Captain{} +var _ webhook.CustomDefaulter = &Captain{} -// Default implements webhook.Defaulter so a webhook will be registered for the type -func (r *Captain) Default() { +// Default implements webhook.CustomDefaulter so a webhook will be registered for the type +func (r *Captain) Default(ctx context.Context, obj runtime.Object) error { captainlog.Info("default", "name", r.Name) // TODO(user): fill in your defaulting logic. + return nil } // TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation. //+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 -var _ webhook.Validator = &Captain{} +var _ webhook.CustomValidator = &Captain{} -// ValidateCreate implements webhook.Validator so a webhook will be registered for the type -func (r *Captain) ValidateCreate() (admission.Warnings, error) { +// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type +func (r *Captain) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { captainlog.Info("validate create", "name", r.Name) // TODO(user): fill in your validation logic upon object creation. return nil, nil } -// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type -func (r *Captain) ValidateUpdate(old runtime.Object) (admission.Warnings, error) { +// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type +func (r *Captain) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) { captainlog.Info("validate update", "name", r.Name) // TODO(user): fill in your validation logic upon object update. return nil, nil } -// ValidateDelete implements webhook.Validator so a webhook will be registered for the type -func (r *Captain) ValidateDelete() (admission.Warnings, error) { +// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type +func (r *Captain) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { captainlog.Info("validate delete", "name", r.Name) // TODO(user): fill in your validation logic upon object deletion.