From d7009d739d2639d3554518135014693772a79acc Mon Sep 17 00:00:00 2001 From: "Chandan Kumar (raukadah)" Date: Wed, 8 Jan 2025 14:57:04 +0530 Subject: [PATCH] Add webhook validation for empty database and rabbitmq databaseInstance and rabbitMqClusterName are required fields. If an user specify databaseInstance and rabbitMqClusterName field as empty string. The webhook should fail it saying as there cannot be empty. This pr adds the validations for the same. Signed-off-by: Chandan Kumar (raukadah) --- api/v1beta1/watcher_webhook.go | 18 +++++++++++++ tests/functional/watcher_controller_test.go | 29 +++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/api/v1beta1/watcher_webhook.go b/api/v1beta1/watcher_webhook.go index af9a8f0..35b2c13 100644 --- a/api/v1beta1/watcher_webhook.go +++ b/api/v1beta1/watcher_webhook.go @@ -17,6 +17,8 @@ limitations under the License. package v1beta1 import ( + "errors" + "k8s.io/apimachinery/pkg/runtime" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/webhook" @@ -64,6 +66,14 @@ var _ webhook.Validator = &Watcher{} func (r *Watcher) ValidateCreate() (admission.Warnings, error) { watcherlog.Info("validate create", "name", r.Name) + if r.Spec.DatabaseInstance == "" { + return nil, errors.New("DatabaseInstance field should not be empty.") + } + + if r.Spec.RabbitMqClusterName == "" { + return nil, errors.New("RabbitMqClusterName field should not be empty") + } + return nil, nil } @@ -71,6 +81,14 @@ func (r *Watcher) ValidateCreate() (admission.Warnings, error) { func (r *Watcher) ValidateUpdate(runtime.Object) (admission.Warnings, error) { watcherlog.Info("validate update", "name", r.Name) + if r.Spec.DatabaseInstance == "" { + return nil, errors.New("DatabaseInstance field should not be empty.") + } + + if r.Spec.RabbitMqClusterName == "" { + return nil, errors.New("RabbitMqClusterName field should not be empty") + } + return nil, nil } diff --git a/tests/functional/watcher_controller_test.go b/tests/functional/watcher_controller_test.go index 87b737d..48dee3d 100644 --- a/tests/functional/watcher_controller_test.go +++ b/tests/functional/watcher_controller_test.go @@ -24,6 +24,14 @@ var ( "databaseInstance": "openstack", } + MinimalWatcherEmptyDatabaseSpec = map[string]interface{}{ + "databaseInstance": "", + } + + MinimalWatcherEmptyRabbitMqSpec = map[string]interface{}{ + "rabbitMqClusterName": "", + } + MinimalWatcherContainerSpec = map[string]interface{}{ "databaseInstance": "openstack", "apiContainerImageURL": "watcher-api-custom-image", @@ -488,6 +496,27 @@ var _ = Describe("Watcher controller", func() { Expect(Watcher.Spec.ApplierContainerImageURL).To(Equal("watcher-applier-custom-image-env")) }) }) + + When("Watcher is created with empty databaseinstance", func() { + BeforeEach(func() { + DeferCleanup(th.DeleteInstance, CreateWatcher(watcherTest.Instance, MinimalWatcherEmptyDatabaseSpec)) + }) + It("It should raise error for empty databaseInstance", func() { + err := GetWatcher(watcherTest.Instance) + Expect(err).To(HaveOccurred()) + }) + }) + + When("Watcher is created with empty RabbitMqClusterName", func() { + BeforeEach(func() { + DeferCleanup(th.DeleteInstance, CreateWatcher(watcherTest.Instance, MinimalWatcherEmptyRabbitMqSpec)) + }) + It("It should raise error for empty rabbitMqClusterName", func() { + err := GetWatcher(watcherTest.Instance) + Expect(err).To(HaveOccurred()) + }) + }) + When("Watcher with non-default values are created", func() { BeforeEach(func() { DeferCleanup(th.DeleteInstance, CreateWatcher(watcherTest.Instance, GetNonDefaultWatcherSpec()))