@@ -19,6 +19,7 @@ package v1beta1
1919import (
2020 "fmt"
2121
22+ rabbitmqv1 "github.com/openstack-k8s-operators/infra-operator/apis/rabbitmq/v1beta1"
2223 topologyv1 "github.com/openstack-k8s-operators/infra-operator/apis/topology/v1beta1"
2324 apierrors "k8s.io/apimachinery/pkg/api/errors"
2425 "k8s.io/apimachinery/pkg/runtime"
@@ -59,12 +60,31 @@ func (r *Watcher) Default() {
5960
6061// Default - set defaults for this WatcherCore spec.
6162func (spec * WatcherSpec ) Default () {
63+ spec .WatcherSpecCore .Default ()
6264 spec .WatcherImages .Default (watcherDefaults )
6365}
6466
6567// Default - set defaults for this WatcherSpecCore spec.
6668func (spec * WatcherSpecCore ) Default () {
67- // no validations . Placeholder for defaulting webhook integrated in the OpenStackControlPlane
69+ // Default MessagingBus from deprecated RabbitMqClusterName
70+ if spec .RabbitMqClusterName != nil {
71+ rabbitmqv1 .DefaultRabbitMqConfig (& spec .MessagingBus , * spec .RabbitMqClusterName )
72+ }
73+
74+ // Default NotificationsBus if NotificationsBusInstance is specified
75+ if spec .NotificationsBusInstance != nil && * spec .NotificationsBusInstance != "" {
76+ if spec .NotificationsBus == nil {
77+ // Initialize NotificationsBus with MessagingBus values to inherit user/vhost
78+ spec .NotificationsBus = & rabbitmqv1.RabbitMqConfig {
79+ User : spec .MessagingBus .User ,
80+ Vhost : spec .MessagingBus .Vhost ,
81+ }
82+ }
83+ // Always default the Cluster field from NotificationsBusInstance if it's empty
84+ if spec .NotificationsBus .Cluster == "" {
85+ rabbitmqv1 .DefaultRabbitMqConfig (spec .NotificationsBus , * spec .NotificationsBusInstance )
86+ }
87+ }
6888}
6989
7090var _ webhook.Validator = & Watcher {}
@@ -95,15 +115,15 @@ func (spec *WatcherSpec) ValidateCreate(basePath *field.Path, namespace string)
95115func (spec * WatcherSpecCore ) ValidateCreate (basePath * field.Path , namespace string ) field.ErrorList {
96116 var allErrs field.ErrorList
97117
98- if * spec .DatabaseInstance == "" || spec .DatabaseInstance == nil {
118+ if spec .DatabaseInstance == nil || * spec .DatabaseInstance == "" {
99119 allErrs = append (
100120 allErrs ,
101121 field .Invalid (
102122 basePath .Child ("databaseInstance" ), "" , "databaseInstance field should not be empty" ),
103123 )
104124 }
105125
106- if * spec .RabbitMqClusterName == "" || spec .RabbitMqClusterName == nil {
126+ if spec .RabbitMqClusterName == nil || * spec .RabbitMqClusterName == "" {
107127 allErrs = append (
108128 allErrs ,
109129 field .Invalid (
@@ -148,22 +168,38 @@ func (spec *WatcherSpec) ValidateUpdate(old WatcherSpec, basePath *field.Path, n
148168func (spec * WatcherSpecCore ) ValidateUpdate (old WatcherSpecCore , basePath * field.Path , namespace string ) field.ErrorList {
149169 var allErrs field.ErrorList
150170
151- if * spec .DatabaseInstance == "" || spec .DatabaseInstance == nil {
171+ if spec .DatabaseInstance == nil || * spec .DatabaseInstance == "" {
152172 allErrs = append (
153173 allErrs ,
154174 field .Invalid (
155175 basePath .Child ("databaseInstance" ), "" , "databaseInstance field should not be empty" ),
156176 )
157177 }
158178
159- if * spec .RabbitMqClusterName == "" || spec .RabbitMqClusterName == nil {
179+ if spec .RabbitMqClusterName == nil || * spec .RabbitMqClusterName == "" {
160180 allErrs = append (
161181 allErrs ,
162182 field .Invalid (
163183 basePath .Child ("rabbitMqClusterName" ), "" , "rabbitMqClusterName field should not be empty" ),
164184 )
165185 }
166186
187+ // Reject changes to deprecated RabbitMqClusterName field
188+ if spec .RabbitMqClusterName != nil && old .RabbitMqClusterName != nil &&
189+ * spec .RabbitMqClusterName != * old .RabbitMqClusterName {
190+ allErrs = append (allErrs , field .Forbidden (
191+ basePath .Child ("rabbitMqClusterName" ),
192+ "rabbitMqClusterName is deprecated and cannot be changed. Please use messagingBus.cluster instead" ))
193+ }
194+
195+ // Reject changes to deprecated NotificationsBusInstance field
196+ if spec .NotificationsBusInstance != nil && old .NotificationsBusInstance != nil &&
197+ * spec .NotificationsBusInstance != * old .NotificationsBusInstance {
198+ allErrs = append (allErrs , field .Forbidden (
199+ basePath .Child ("notificationsBusInstance" ),
200+ "notificationsBusInstance is deprecated and cannot be changed. Please use notificationsBus.cluster instead" ))
201+ }
202+
167203 allErrs = append (allErrs , spec .ValidateWatcherTopology (basePath , namespace )... )
168204
169205 return allErrs
0 commit comments