Skip to content

Commit

Permalink
Add webhook validation for empty database and rabbitmq
Browse files Browse the repository at this point in the history
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) <[email protected]>
  • Loading branch information
raukadah committed Jan 8, 2025
1 parent 20ecc51 commit d7009d7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
18 changes: 18 additions & 0 deletions api/v1beta1/watcher_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -64,13 +66,29 @@ 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
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
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
}

Expand Down
29 changes: 29 additions & 0 deletions tests/functional/watcher_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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()))
Expand Down

0 comments on commit d7009d7

Please sign in to comment.