From d999de666f4699ce83b6ce1382391ad9696dd5b5 Mon Sep 17 00:00:00 2001 From: Chandan Kumar Date: Tue, 26 Nov 2024 18:28:58 +0530 Subject: [PATCH] Generate webhook for WatcherDecisionEngine Below is the command used to generate the same: ``` operator-sdk create webhook --group watcher --version v1beta1 --kind WatcherDecisionEngine --programmatic-validation --defaulting ``` Signed-off-by: Chandan Kumar --- PROJECT | 4 ++ api/v1beta1/watcherdecisionengine_webhook.go | 75 ++++++++++++++++++++ api/v1beta1/webhook_suite_test.go | 3 + 3 files changed, 82 insertions(+) create mode 100644 api/v1beta1/watcherdecisionengine_webhook.go diff --git a/PROJECT b/PROJECT index a9443a0..ee423e3 100644 --- a/PROJECT +++ b/PROJECT @@ -46,6 +46,10 @@ resources: kind: WatcherDecisionEngine path: github.com/openstack-k8s-operators/watcher-operator/api/v1beta1 version: v1beta1 + webhooks: + defaulting: true + validation: true + webhookVersion: v1 - api: crdVersion: v1 namespaced: true diff --git a/api/v1beta1/watcherdecisionengine_webhook.go b/api/v1beta1/watcherdecisionengine_webhook.go new file mode 100644 index 0000000..f92fb0f --- /dev/null +++ b/api/v1beta1/watcherdecisionengine_webhook.go @@ -0,0 +1,75 @@ +/* +Copyright 2024. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package v1beta1 + +import ( + "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" +) + +// log is for logging in this package. +var watcherdecisionenginelog = logf.Log.WithName("watcherdecisionengine-resource") + +func (r *WatcherDecisionEngine) SetupWebhookWithManager(mgr ctrl.Manager) error { + return ctrl.NewWebhookManagedBy(mgr). + For(r). + Complete() +} + +// TODO(user): EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! + +//+kubebuilder:webhook:path=/mutate-watcher-openstack-org-v1beta1-watcherdecisionengine,mutating=true,failurePolicy=fail,sideEffects=None,groups=watcher.openstack.org,resources=watcherdecisionengines,verbs=create;update,versions=v1beta1,name=mwatcherdecisionengine.kb.io,admissionReviewVersions=v1 + +var _ webhook.Defaulter = &WatcherDecisionEngine{} + +// Default implements webhook.Defaulter so a webhook will be registered for the type +func (r *WatcherDecisionEngine) Default() { + watcherdecisionenginelog.Info("default", "name", r.Name) + + // TODO(user): fill in your defaulting logic. +} + +// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation. +//+kubebuilder:webhook:path=/validate-watcher-openstack-org-v1beta1-watcherdecisionengine,mutating=false,failurePolicy=fail,sideEffects=None,groups=watcher.openstack.org,resources=watcherdecisionengines,verbs=create;update,versions=v1beta1,name=vwatcherdecisionengine.kb.io,admissionReviewVersions=v1 + +var _ webhook.Validator = &WatcherDecisionEngine{} + +// ValidateCreate implements webhook.Validator so a webhook will be registered for the type +func (r *WatcherDecisionEngine) ValidateCreate() error { + watcherdecisionenginelog.Info("validate create", "name", r.Name) + + // TODO(user): fill in your validation logic upon object creation. + return nil +} + +// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type +func (r *WatcherDecisionEngine) ValidateUpdate(old runtime.Object) error { + watcherdecisionenginelog.Info("validate update", "name", r.Name) + + // TODO(user): fill in your validation logic upon object update. + return nil +} + +// ValidateDelete implements webhook.Validator so a webhook will be registered for the type +func (r *WatcherDecisionEngine) ValidateDelete() error { + watcherdecisionenginelog.Info("validate delete", "name", r.Name) + + // TODO(user): fill in your validation logic upon object deletion. + return nil +} diff --git a/api/v1beta1/webhook_suite_test.go b/api/v1beta1/webhook_suite_test.go index ed39ca3..8516d59 100644 --- a/api/v1beta1/webhook_suite_test.go +++ b/api/v1beta1/webhook_suite_test.go @@ -105,6 +105,9 @@ var _ = BeforeSuite(func() { err = (&WatcherAPI{}).SetupWebhookWithManager(mgr) Expect(err).NotTo(HaveOccurred()) + err = (&WatcherDecisionEngine{}).SetupWebhookWithManager(mgr) + Expect(err).NotTo(HaveOccurred()) + //+kubebuilder:scaffold:webhook go func() {