Skip to content

Commit d7009d7

Browse files
committed
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) <raukadah@gmail.com>
1 parent 20ecc51 commit d7009d7

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

api/v1beta1/watcher_webhook.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package v1beta1
1818

1919
import (
20+
"errors"
21+
2022
"k8s.io/apimachinery/pkg/runtime"
2123
logf "sigs.k8s.io/controller-runtime/pkg/log"
2224
"sigs.k8s.io/controller-runtime/pkg/webhook"
@@ -64,13 +66,29 @@ var _ webhook.Validator = &Watcher{}
6466
func (r *Watcher) ValidateCreate() (admission.Warnings, error) {
6567
watcherlog.Info("validate create", "name", r.Name)
6668

69+
if r.Spec.DatabaseInstance == "" {
70+
return nil, errors.New("DatabaseInstance field should not be empty.")
71+
}
72+
73+
if r.Spec.RabbitMqClusterName == "" {
74+
return nil, errors.New("RabbitMqClusterName field should not be empty")
75+
}
76+
6777
return nil, nil
6878
}
6979

7080
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
7181
func (r *Watcher) ValidateUpdate(runtime.Object) (admission.Warnings, error) {
7282
watcherlog.Info("validate update", "name", r.Name)
7383

84+
if r.Spec.DatabaseInstance == "" {
85+
return nil, errors.New("DatabaseInstance field should not be empty.")
86+
}
87+
88+
if r.Spec.RabbitMqClusterName == "" {
89+
return nil, errors.New("RabbitMqClusterName field should not be empty")
90+
}
91+
7492
return nil, nil
7593
}
7694

tests/functional/watcher_controller_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ var (
2424
"databaseInstance": "openstack",
2525
}
2626

27+
MinimalWatcherEmptyDatabaseSpec = map[string]interface{}{
28+
"databaseInstance": "",
29+
}
30+
31+
MinimalWatcherEmptyRabbitMqSpec = map[string]interface{}{
32+
"rabbitMqClusterName": "",
33+
}
34+
2735
MinimalWatcherContainerSpec = map[string]interface{}{
2836
"databaseInstance": "openstack",
2937
"apiContainerImageURL": "watcher-api-custom-image",
@@ -488,6 +496,27 @@ var _ = Describe("Watcher controller", func() {
488496
Expect(Watcher.Spec.ApplierContainerImageURL).To(Equal("watcher-applier-custom-image-env"))
489497
})
490498
})
499+
500+
When("Watcher is created with empty databaseinstance", func() {
501+
BeforeEach(func() {
502+
DeferCleanup(th.DeleteInstance, CreateWatcher(watcherTest.Instance, MinimalWatcherEmptyDatabaseSpec))
503+
})
504+
It("It should raise error for empty databaseInstance", func() {
505+
err := GetWatcher(watcherTest.Instance)
506+
Expect(err).To(HaveOccurred())
507+
})
508+
})
509+
510+
When("Watcher is created with empty RabbitMqClusterName", func() {
511+
BeforeEach(func() {
512+
DeferCleanup(th.DeleteInstance, CreateWatcher(watcherTest.Instance, MinimalWatcherEmptyRabbitMqSpec))
513+
})
514+
It("It should raise error for empty rabbitMqClusterName", func() {
515+
err := GetWatcher(watcherTest.Instance)
516+
Expect(err).To(HaveOccurred())
517+
})
518+
})
519+
491520
When("Watcher with non-default values are created", func() {
492521
BeforeEach(func() {
493522
DeferCleanup(th.DeleteInstance, CreateWatcher(watcherTest.Instance, GetNonDefaultWatcherSpec()))

0 commit comments

Comments
 (0)