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 9, 2025
1 parent 20ecc51 commit aa71a31
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 17 deletions.
4 changes: 2 additions & 2 deletions api/v1beta1/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type WatcherCommon struct {
// +kubebuilder:validation:Required
// MariaDB instance name
// Required to use the mariadb-operator instance to create the DB and user
DatabaseInstance string `json:"databaseInstance"`
DatabaseInstance *string `json:"databaseInstance"`

// +kubebuilder:validation:Optional
// +kubebuilder:default=watcher
Expand Down Expand Up @@ -69,7 +69,7 @@ type WatcherTemplate struct {
// +kubebuilder:default=rabbitmq
// RabbitMQ instance name
// Needed to request a transportURL that is created and used in Barbican
RabbitMqClusterName string `json:"rabbitMqClusterName"`
RabbitMqClusterName *string `json:"rabbitMqClusterName"`

// +kubebuilder:validation:Optional
// +kubebuilder:default=osp-secret
Expand Down
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
20 changes: 15 additions & 5 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions controllers/watcher_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,11 +436,11 @@ func (r *WatcherReconciler) ensureDB(
// create watcher DB instance
//
db := mariadbv1.NewDatabaseForAccount(
instance.Spec.DatabaseInstance, // mariadb/galera service to target
watcher.DatabaseName, // name used in CREATE DATABASE in mariadb
watcher.DatabaseCRName, // CR name for MariaDBDatabase
instance.Spec.DatabaseAccount, // CR name for MariaDBAccount
instance.Namespace, // namespace
*instance.Spec.DatabaseInstance, // mariadb/galera service to target
watcher.DatabaseName, // name used in CREATE DATABASE in mariadb
watcher.DatabaseCRName, // CR name for MariaDBDatabase
instance.Spec.DatabaseAccount, // CR name for MariaDBAccount
instance.Namespace, // namespace
)

// create or patch the DB
Expand Down Expand Up @@ -507,7 +507,7 @@ func (r *WatcherReconciler) ensureMQ(
}

op, err := controllerutil.CreateOrUpdate(ctx, r.Client, transportURL, func() error {
transportURL.Spec.RabbitmqClusterName = instance.Spec.RabbitMqClusterName
transportURL.Spec.RabbitmqClusterName = *instance.Spec.RabbitMqClusterName

err := controllerutil.SetControllerReference(instance, transportURL, r.Scheme)
return err
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
9 changes: 5 additions & 4 deletions tests/functional/watcher_test_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ import (
"github.com/openstack-k8s-operators/watcher-operator/pkg/watcher"

"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"
)

type APIType string

// WatcherTestData is the data structure used to provide input data to envTest
type WatcherTestData struct {
//DatabaseHostname string
DatabaseInstance string
RabbitMqClusterName string
DatabaseInstance *string
RabbitMqClusterName *string
Instance types.NamespacedName
Watcher types.NamespacedName
WatcherDatabaseName types.NamespacedName
Expand Down Expand Up @@ -67,7 +68,7 @@ func GetWatcherTestData(watcherName types.NamespacedName) WatcherTestData {
Namespace: watcherName.Namespace,
Name: "watcher",
},
DatabaseInstance: "openstack",
DatabaseInstance: ptr.To("openstack"),
//DatabaseHostname: "database-hostname",
WatcherDatabaseAccountSecret: types.NamespacedName{
Namespace: watcherName.Namespace,
Expand All @@ -77,7 +78,7 @@ func GetWatcherTestData(watcherName types.NamespacedName) WatcherTestData {
Namespace: watcherName.Namespace,
Name: "test-osp-secret",
},
RabbitMqClusterName: "rabbitmq",
RabbitMqClusterName: ptr.To("rabbitmq"),
WatcherTransportURL: types.NamespacedName{
Namespace: watcherName.Namespace,
Name: fmt.Sprintf("%s-watcher-transport", watcherName.Name),
Expand Down

0 comments on commit aa71a31

Please sign in to comment.