From da29d5ac845916c146086558e10c748bc2ea71d4 Mon Sep 17 00:00:00 2001 From: Luca Miccini Date: Tue, 16 Dec 2025 14:54:39 +0100 Subject: [PATCH 1/2] Rabbitmq vhost and user support Add new messagingBus and notificationsBus interfaces to hold cluster, user and vhost names for optional usage. The controller adds these values to the TransportURL create request when present. Additionally, we migrate RabbitMQ cluster name to RabbitMq config struct using DefaultRabbitMqConfig from infra-operator to automatically populate the new Cluster field from legacy RabbitMqClusterName. Example usage: spec: messagingBus: cluster: rpc-rabbitmq user: rpc-user vhost: rpc-vhost notificationsBus: cluster: notifications-rabbitmq user: notifications-user vhost: notifications-vhost Jira: https://issues.redhat.com/browse/OSPRH-22694 --- api/bases/cinder.openstack.org_cinders.yaml | 35 ++ api/go.mod | 2 +- api/go.sum | 16 + api/v1beta1/cinder_types.go | 15 +- api/v1beta1/cinder_webhook.go | 96 ++++ api/v1beta1/zz_generated.deepcopy.go | 7 + .../bases/cinder.openstack.org_cinders.yaml | 35 ++ internal/controller/cinder_controller.go | 85 ++- test/functional/cinder_controller_test.go | 501 ++++++++++++++++-- test/functional/cinder_webhook_test.go | 118 +++++ 10 files changed, 828 insertions(+), 82 deletions(-) create mode 100644 test/functional/cinder_webhook_test.go diff --git a/api/bases/cinder.openstack.org_cinders.yaml b/api/bases/cinder.openstack.org_cinders.yaml index 6f35e4d5c..0052f2b5e 100644 --- a/api/bases/cinder.openstack.org_cinders.yaml +++ b/api/bases/cinder.openstack.org_cinders.yaml @@ -2032,6 +2032,22 @@ spec: default: memcached description: Memcached instance name. type: string + messagingBus: + description: MessagingBus configuration (username, vhost, and cluster) + properties: + cluster: + description: Name of the cluster + minLength: 1 + type: string + user: + description: User - RabbitMQ username + type: string + vhost: + description: Vhost - RabbitMQ vhost name + type: string + required: + - cluster + type: object nodeSelector: additionalProperties: type: string @@ -2040,10 +2056,28 @@ spec: NodeSelector here acts as a default value and can be overridden by service specific NodeSelector Settings. type: object + notificationsBus: + description: NotificationsBus configuration (username, vhost, and + cluster) for notifications + properties: + cluster: + description: Name of the cluster + minLength: 1 + type: string + user: + description: User - RabbitMQ username + type: string + vhost: + description: Vhost - RabbitMQ vhost name + type: string + required: + - cluster + type: object notificationsBusInstance: description: |- RabbitMQ instance name used to request a transportURL that is used for notification purposes + Deprecated: Use NotificationsBus.Cluster instead type: string passwordSelectors: default: @@ -2067,6 +2101,7 @@ spec: description: |- RabbitMQ instance name Needed to request a transportURL that is created and used in Cinder + Deprecated: Use MessagingBus.Cluster instead type: string secret: description: Secret containing OpenStack password information diff --git a/api/go.mod b/api/go.mod index 1b45f1ba3..be1f82156 100644 --- a/api/go.mod +++ b/api/go.mod @@ -17,7 +17,6 @@ require ( github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/emicklei/go-restful/v3 v3.12.2 // indirect - github.com/evanphx/json-patch v5.9.11+incompatible // indirect github.com/evanphx/json-patch/v5 v5.9.11 // indirect github.com/fsnotify/fsnotify v1.9.0 // indirect github.com/fxamacker/cbor/v2 v2.9.0 // indirect @@ -46,6 +45,7 @@ require ( github.com/prometheus/client_model v0.6.2 // indirect github.com/prometheus/common v0.65.0 // indirect github.com/prometheus/procfs v0.16.1 // indirect + github.com/rabbitmq/cluster-operator/v2 v2.16.0 // indirect github.com/spf13/pflag v1.0.7 // indirect github.com/stretchr/testify v1.11.1 // indirect github.com/x448/float16 v0.8.4 // indirect diff --git a/api/go.sum b/api/go.sum index c084cf51b..bb6072ff7 100644 --- a/api/go.sum +++ b/api/go.sum @@ -1,3 +1,4 @@ +github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww= github.com/Masterminds/semver/v3 v3.4.0 h1:Zog+i5UMtVoCU8oKka5P7i9q9HgrJeGzI9SA1Xbatp0= github.com/Masterminds/semver/v3 v3.4.0/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= @@ -74,6 +75,7 @@ github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee h1:W5t00kpgFd github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= +<<<<<<< HEAD github.com/onsi/ginkgo/v2 v2.28.1 h1:S4hj+HbZp40fNKuLUQOYLDgZLwNUVn19N3Atb98NCyI= github.com/onsi/ginkgo/v2 v2.28.1/go.mod h1:CLtbVInNckU3/+gC8LzkGUb9oF+e8W8TdUsxPwvdOgE= github.com/onsi/gomega v1.39.1 h1:1IJLAad4zjPn2PsnhH70V4DKRFlrCzGBNrNaru+Vf28= @@ -84,6 +86,20 @@ github.com/openstack-k8s-operators/lib-common/modules/common v0.6.1-0.2026012814 github.com/openstack-k8s-operators/lib-common/modules/common v0.6.1-0.20260128142552-e2c25eccae5a/go.mod h1:ndqfy1KbVorHH6+zlUFPIrCRhMSxO3ImYJUGaooE0x0= github.com/openstack-k8s-operators/lib-common/modules/storage v0.6.1-0.20260128142552-e2c25eccae5a h1:teKxfVLDxJD9ahjeh29GlKHiXNUFDkVRmkpJdeKAvGE= github.com/openstack-k8s-operators/lib-common/modules/storage v0.6.1-0.20260128142552-e2c25eccae5a/go.mod h1:jofj+VqDszxLCZSBYo794KGkCjMo01xzhQ/gffYzf3I= +======= +github.com/onsi/ginkgo/v2 v2.27.5 h1:ZeVgZMx2PDMdJm/+w5fE/OyG6ILo1Y3e+QX4zSR0zTE= +github.com/onsi/ginkgo/v2 v2.27.5/go.mod h1:ArE1D/XhNXBXCBkKOLkbsb2c81dQHCRcF5zwn/ykDRo= +github.com/onsi/gomega v1.39.0 h1:y2ROC3hKFmQZJNFeGAMeHZKkjBL65mIZcvrLQBF9k6Q= +github.com/onsi/gomega v1.39.0/go.mod h1:ZCU1pkQcXDO5Sl9/VVEGlDyp+zm0m1cmeG5TOzLgdh4= +github.com/openstack-k8s-operators/infra-operator/apis v0.6.1-0.20260124124521-834dd78eceb9 h1:87CTyKbobvZPGOqiigSO+/PPbMnFHoYecjwBdnxxEGw= +github.com/openstack-k8s-operators/infra-operator/apis v0.6.1-0.20260124124521-834dd78eceb9/go.mod h1:ZXwFlspJCdZEUjMbmaf61t5AMB4u2vMyAMMoe/vJroE= +github.com/openstack-k8s-operators/lib-common/modules/common v0.6.1-0.20260126081203-efc2df9207eb h1:S7tnYO/E1f1KQfcp7N5bam8+ax/ExDTOhZ1WqG4Bfu0= +github.com/openstack-k8s-operators/lib-common/modules/common v0.6.1-0.20260126081203-efc2df9207eb/go.mod h1:ndqfy1KbVorHH6+zlUFPIrCRhMSxO3ImYJUGaooE0x0= +github.com/openstack-k8s-operators/lib-common/modules/storage v0.6.1-0.20260126081203-efc2df9207eb h1:0kP9V1pKfRno6ss7qAy3GcfVK29CobWym6WA7AYA7wY= +github.com/openstack-k8s-operators/lib-common/modules/storage v0.6.1-0.20260126081203-efc2df9207eb/go.mod h1:jofj+VqDszxLCZSBYo794KGkCjMo01xzhQ/gffYzf3I= +github.com/openstack-k8s-operators/rabbitmq-cluster-operator/v2 v2.6.1-0.20250929174222-a0d328fa4dec h1:saovr368HPAKHN0aRPh8h8n9s9dn3d8Frmfua0UYRlc= +github.com/openstack-k8s-operators/rabbitmq-cluster-operator/v2 v2.6.1-0.20250929174222-a0d328fa4dec/go.mod h1:Nh2NEePLjovUQof2krTAg4JaAoLacqtPTZQXK6izNfg= +>>>>>>> 7e5a34a (Rabbitmq vhost and user support) github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= diff --git a/api/v1beta1/cinder_types.go b/api/v1beta1/cinder_types.go index f3d4f3955..723df6206 100644 --- a/api/v1beta1/cinder_types.go +++ b/api/v1beta1/cinder_types.go @@ -17,6 +17,7 @@ limitations under the License. package v1beta1 import ( + rabbitmqv1 "github.com/openstack-k8s-operators/infra-operator/apis/rabbitmq/v1beta1" topologyv1 "github.com/openstack-k8s-operators/infra-operator/apis/topology/v1beta1" "github.com/openstack-k8s-operators/lib-common/modules/common/condition" "github.com/openstack-k8s-operators/lib-common/modules/storage" @@ -68,7 +69,16 @@ type CinderSpecBase struct { // +kubebuilder:default=rabbitmq // RabbitMQ instance name // Needed to request a transportURL that is created and used in Cinder - RabbitMqClusterName string `json:"rabbitMqClusterName"` + // Deprecated: Use MessagingBus.Cluster instead + RabbitMqClusterName string `json:"rabbitMqClusterName" deprecated:"messagingBus.cluster"` + + // +kubebuilder:validation:Optional + // MessagingBus configuration (username, vhost, and cluster) + MessagingBus rabbitmqv1.RabbitMqConfig `json:"messagingBus,omitempty"` + + // +kubebuilder:validation:Optional + // NotificationsBus configuration (username, vhost, and cluster) for notifications + NotificationsBus *rabbitmqv1.RabbitMqConfig `json:"notificationsBus,omitempty"` // +kubebuilder:validation:Required // +kubebuilder:default=memcached @@ -114,7 +124,8 @@ type CinderSpecBase struct { // +kubebuilder:validation:Optional // RabbitMQ instance name used to request a transportURL that is used for // notification purposes - NotificationsBusInstance *string `json:"notificationsBusInstance,omitempty"` + // Deprecated: Use NotificationsBus.Cluster instead + NotificationsBusInstance *string `json:"notificationsBusInstance,omitempty" deprecated:"notificationsBus.cluster"` // +kubebuilder:validation:Optional // +operator-sdk:csv:customresourcedefinitions:type=spec diff --git a/api/v1beta1/cinder_webhook.go b/api/v1beta1/cinder_webhook.go index 32c81ee8f..e66882dd5 100644 --- a/api/v1beta1/cinder_webhook.go +++ b/api/v1beta1/cinder_webhook.go @@ -26,6 +26,7 @@ import ( "golang.org/x/exp/maps" + rabbitmqv1 "github.com/openstack-k8s-operators/infra-operator/apis/rabbitmq/v1beta1" topologyv1 "github.com/openstack-k8s-operators/infra-operator/apis/topology/v1beta1" "github.com/openstack-k8s-operators/lib-common/modules/common/service" "github.com/openstack-k8s-operators/lib-common/modules/common/util" @@ -111,6 +112,23 @@ func (r *Cinder) Default() { // Default - set defaults for this Cinder spec func (spec *CinderSpecBase) Default() { + // Only migrate from deprecated field if the new field is not already set + if spec.MessagingBus.Cluster == "" { + rabbitmqv1.DefaultRabbitMqConfig(&spec.MessagingBus, spec.RabbitMqClusterName) + } + + // Default NotificationsBus if NotificationsBusInstance is specified + if spec.NotificationsBusInstance != nil && *spec.NotificationsBusInstance != "" { + if spec.NotificationsBus == nil { + // Initialize empty NotificationsBus - credentials will be created dynamically + // to ensure separation from MessagingBus (RPC and notifications should never share credentials) + spec.NotificationsBus = &rabbitmqv1.RabbitMqConfig{} + } + // Always default the Cluster field from NotificationsBusInstance if it's empty + if spec.NotificationsBus.Cluster == "" { + rabbitmqv1.DefaultRabbitMqConfig(spec.NotificationsBus, *spec.NotificationsBusInstance) + } + } if spec.DBPurge.Age == 0 { spec.DBPurge.Age = cinderDefaults.DBPurgeAge @@ -128,6 +146,64 @@ func (spec *CinderSpecCore) Default() { spec.CinderSpecBase.Default() } +// getDeprecatedFields returns the centralized list of deprecated fields for CinderSpecBase +func (spec *CinderSpecBase) getDeprecatedFields(old *CinderSpecBase) []common_webhook.DeprecatedFieldUpdate { + // Get new field value (handle nil NotificationsBus) + var newNotifBusCluster *string + if spec.NotificationsBus != nil { + newNotifBusCluster = &spec.NotificationsBus.Cluster + } + + deprecatedFields := []common_webhook.DeprecatedFieldUpdate{ + { + DeprecatedFieldName: "rabbitMqClusterName", + NewFieldPath: []string{"messagingBus", "cluster"}, + NewDeprecatedValue: &spec.RabbitMqClusterName, + NewValue: &spec.MessagingBus.Cluster, + }, + { + DeprecatedFieldName: "notificationsBusInstance", + NewFieldPath: []string{"notificationsBus", "cluster"}, + NewDeprecatedValue: spec.NotificationsBusInstance, + NewValue: newNotifBusCluster, + }, + } + + // If old spec is provided (UPDATE operation), add old values + if old != nil { + deprecatedFields[0].OldDeprecatedValue = &old.RabbitMqClusterName + deprecatedFields[1].OldDeprecatedValue = old.NotificationsBusInstance + } + + return deprecatedFields +} + +// validateDeprecatedFieldsCreate validates deprecated fields during CREATE operations +func (spec *CinderSpecBase) validateDeprecatedFieldsCreate(basePath *field.Path) ([]string, field.ErrorList) { + // Get deprecated fields list (without old values for CREATE) + deprecatedFieldsUpdate := spec.getDeprecatedFields(nil) + + // Convert to DeprecatedField list for CREATE validation + deprecatedFields := make([]common_webhook.DeprecatedField, len(deprecatedFieldsUpdate)) + for i, df := range deprecatedFieldsUpdate { + deprecatedFields[i] = common_webhook.DeprecatedField{ + DeprecatedFieldName: df.DeprecatedFieldName, + NewFieldPath: df.NewFieldPath, + DeprecatedValue: df.NewDeprecatedValue, + NewValue: df.NewValue, + } + } + + return common_webhook.ValidateDeprecatedFieldsCreate(deprecatedFields, basePath), nil +} + +// validateDeprecatedFieldsUpdate validates deprecated fields during UPDATE operations +func (spec *CinderSpecBase) validateDeprecatedFieldsUpdate(old CinderSpecBase, basePath *field.Path) ([]string, field.ErrorList) { + // Get deprecated fields list with old values + deprecatedFields := spec.getDeprecatedFields(&old) + return common_webhook.ValidateDeprecatedFieldsUpdate(deprecatedFields, basePath) +} + var _ webhook.Validator = &Cinder{} // ValidateCreate implements webhook.Validator so a webhook will be registered for the type @@ -179,6 +255,11 @@ func (spec *CinderSpec) ValidateCreate( var allErrs field.ErrorList var allWarns admission.Warnings + // Validate deprecated fields using shared helper + warns, errs := spec.CinderSpecBase.validateDeprecatedFieldsCreate(basePath) + allWarns = append(allWarns, warns...) + allErrs = append(allErrs, errs...) + // validate the service override key is valid allErrs = append(allErrs, service.ValidateRoutedOverrides( basePath.Child("cinderAPI").Child("override").Child("service"), @@ -202,6 +283,11 @@ func (spec *CinderSpecCore) ValidateCreate( var allErrs field.ErrorList var allWarns admission.Warnings + // Validate deprecated fields using shared helper + warns, errs := spec.CinderSpecBase.validateDeprecatedFieldsCreate(basePath) + allWarns = append(allWarns, warns...) + allErrs = append(allErrs, errs...) + // validate the service override key is valid allErrs = append(allErrs, service.ValidateRoutedOverrides( basePath.Child("cinderAPI").Child("override").Child("service"), @@ -275,6 +361,11 @@ func (spec *CinderSpec) ValidateUpdate( var allErrs field.ErrorList var allWarns []string + // Validate deprecated fields using shared helper + warns, errs := spec.CinderSpecBase.validateDeprecatedFieldsUpdate(old.CinderSpecBase, basePath) + allWarns = append(allWarns, warns...) + allErrs = append(allErrs, errs...) + // validate the service override key is valid allErrs = append(allErrs, service.ValidateRoutedOverrides( basePath.Child("cinderAPI").Child("override").Child("service"), @@ -301,6 +392,11 @@ func (spec *CinderSpecCore) ValidateUpdate( var allErrs field.ErrorList var allWarns []string + // Validate deprecated fields using shared helper + warns, errs := spec.CinderSpecBase.validateDeprecatedFieldsUpdate(old.CinderSpecBase, basePath) + allWarns = append(allWarns, warns...) + allErrs = append(allErrs, errs...) + // validate the service override key is valid allErrs = append(allErrs, service.ValidateRoutedOverrides( basePath.Child("cinderAPI").Child("override").Child("service"), diff --git a/api/v1beta1/zz_generated.deepcopy.go b/api/v1beta1/zz_generated.deepcopy.go index 538b093bc..0914910aa 100644 --- a/api/v1beta1/zz_generated.deepcopy.go +++ b/api/v1beta1/zz_generated.deepcopy.go @@ -21,6 +21,7 @@ limitations under the License. package v1beta1 import ( + rabbitmqv1beta1 "github.com/openstack-k8s-operators/infra-operator/apis/rabbitmq/v1beta1" topologyv1beta1 "github.com/openstack-k8s-operators/infra-operator/apis/topology/v1beta1" "github.com/openstack-k8s-operators/lib-common/modules/common/condition" "github.com/openstack-k8s-operators/lib-common/modules/common/service" @@ -798,6 +799,12 @@ func (in *CinderSpec) DeepCopy() *CinderSpec { func (in *CinderSpecBase) DeepCopyInto(out *CinderSpecBase) { *out = *in out.CinderTemplate = in.CinderTemplate + out.MessagingBus = in.MessagingBus + if in.NotificationsBus != nil { + in, out := &in.NotificationsBus, &out.NotificationsBus + *out = new(rabbitmqv1beta1.RabbitMqConfig) + **out = **in + } if in.ExtraMounts != nil { in, out := &in.ExtraMounts, &out.ExtraMounts *out = make([]CinderExtraVolMounts, len(*in)) diff --git a/config/crd/bases/cinder.openstack.org_cinders.yaml b/config/crd/bases/cinder.openstack.org_cinders.yaml index 6f35e4d5c..0052f2b5e 100644 --- a/config/crd/bases/cinder.openstack.org_cinders.yaml +++ b/config/crd/bases/cinder.openstack.org_cinders.yaml @@ -2032,6 +2032,22 @@ spec: default: memcached description: Memcached instance name. type: string + messagingBus: + description: MessagingBus configuration (username, vhost, and cluster) + properties: + cluster: + description: Name of the cluster + minLength: 1 + type: string + user: + description: User - RabbitMQ username + type: string + vhost: + description: Vhost - RabbitMQ vhost name + type: string + required: + - cluster + type: object nodeSelector: additionalProperties: type: string @@ -2040,10 +2056,28 @@ spec: NodeSelector here acts as a default value and can be overridden by service specific NodeSelector Settings. type: object + notificationsBus: + description: NotificationsBus configuration (username, vhost, and + cluster) for notifications + properties: + cluster: + description: Name of the cluster + minLength: 1 + type: string + user: + description: User - RabbitMQ username + type: string + vhost: + description: Vhost - RabbitMQ vhost name + type: string + required: + - cluster + type: object notificationsBusInstance: description: |- RabbitMQ instance name used to request a transportURL that is used for notification purposes + Deprecated: Use NotificationsBus.Cluster instead type: string passwordSelectors: default: @@ -2067,6 +2101,7 @@ spec: description: |- RabbitMQ instance name Needed to request a transportURL that is created and used in Cinder + Deprecated: Use MessagingBus.Cluster instead type: string secret: description: Secret containing OpenStack password information diff --git a/internal/controller/cinder_controller.go b/internal/controller/cinder_controller.go index 3aa489e0d..fd9dbf30a 100644 --- a/internal/controller/cinder_controller.go +++ b/internal/controller/cinder_controller.go @@ -207,7 +207,7 @@ func (r *CinderReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res condition.UnknownCondition(condition.RoleBindingReadyCondition, condition.InitReason, condition.RoleBindingReadyInitMessage), ) - if instance.Spec.NotificationsBusInstance != nil { + if instance.Spec.NotificationsBus != nil && instance.Spec.NotificationsBus.Cluster != "" { c := condition.UnknownCondition( condition.NotificationBusInstanceReadyCondition, condition.InitReason, @@ -539,7 +539,7 @@ func (r *CinderReconciler) reconcileNormal(ctx context.Context, instance *cinder // create RabbitMQ transportURL CR and get the actual URL from the associated secret that is created // - transportURL, op, err := r.transportURLCreateOrUpdate(ctx, instance, serviceLabels, "") + transportURL, op, err := r.transportURLCreateOrUpdate(ctx, instance, serviceLabels, false, instance.Spec.MessagingBus) if err != nil { instance.Status.Conditions.Set(condition.FalseCondition( condition.RabbitMqTransportURLReadyCondition, @@ -575,19 +575,17 @@ func (r *CinderReconciler) reconcileNormal(ctx context.Context, instance *cinder // associated secret that is created // - // Request TransportURL when the parameter is provided in the CR - // and it does not match with the existing RabbitMqClusterName - if instance.Spec.NotificationsBusInstance != nil { + // Determine if notifications are enabled by checking NotificationsBus.Cluster + // (the webhook defaults this from the deprecated NotificationsBusInstance field) + if instance.Spec.NotificationsBus != nil && instance.Spec.NotificationsBus.Cluster != "" { // init .Status.NotificationURLSecret instance.Status.NotificationsURLSecret = ptr.To("") - // setting notificationBusName to an empty string ensures that we do not - // request a new transportURL unless the two spec fields do not match - var notificationBusName string - if *instance.Spec.NotificationsBusInstance != instance.Spec.RabbitMqClusterName { - notificationBusName = *instance.Spec.NotificationsBusInstance - } - notificationBusInstanceURL, op, err := r.transportURLCreateOrUpdate(ctx, instance, serviceLabels, notificationBusName) + // Use NotificationsBus config (never fall back to MessagingBus to ensure separation) + notificationsRabbitMqConfig := *instance.Spec.NotificationsBus + // A separate TransportURL is always created for notifications, + // even when using the same cluster as messaging (to allow different vhost/user) + notificationBusInstanceURL, op, err := r.transportURLCreateOrUpdate(ctx, instance, serviceLabels, true, notificationsRabbitMqConfig) if err != nil { instance.Status.Conditions.Set(condition.FalseCondition( condition.NotificationBusInstanceReadyCondition, @@ -605,7 +603,7 @@ func (r *CinderReconciler) reconcileNormal(ctx context.Context, instance *cinder *instance.Status.NotificationsURLSecret = notificationBusInstanceURL.Status.SecretName if instance.Status.NotificationsURLSecret == nil { - Log.Info(fmt.Sprintf("Waiting for NotificationBusInstanceURL %s secret to be created", transportURL.Name)) + Log.Info(fmt.Sprintf("Waiting for NotificationBusInstanceURL %s secret to be created", notificationBusInstanceURL.Name)) instance.Status.Conditions.Set(condition.FalseCondition( condition.NotificationBusInstanceReadyCondition, condition.RequestedReason, @@ -617,7 +615,7 @@ func (r *CinderReconciler) reconcileNormal(ctx context.Context, instance *cinder instance.Status.Conditions.MarkTrue(condition.NotificationBusInstanceReadyCondition, condition.NotificationBusInstanceReadyMessage) } else { // make sure we do not have an entry in the status if - // .Spec.NotificationsURLSecret is not provided + // notifications are not enabled instance.Status.NotificationsURLSecret = nil } @@ -1165,17 +1163,13 @@ func (r *CinderReconciler) generateServiceConfigs( var notificationInstanceURLSecret *corev1.Secret if instance.Status.NotificationsURLSecret != nil { - // Get a notificationInstanceURLSecret only if rabbitMQ referenced in - // the spec is different, otherwise inherits the existing transport_url - if instance.Spec.RabbitMqClusterName != *instance.Spec.NotificationsBusInstance { - notificationInstanceURLSecret, _, err = secret.GetSecret(ctx, h, *instance.Status.NotificationsURLSecret, instance.Namespace) - if err != nil { - return err - } - templateParameters["NotificationsURL"] = string(notificationInstanceURLSecret.Data["transport_url"]) - } else { - templateParameters["NotificationsURL"] = transportURLSecretData + // A separate TransportURL is always created for notifications (even when using the same cluster) + // to allow different vhost/user configuration for isolation, so always use the dedicated secret + notificationInstanceURLSecret, _, err = secret.GetSecret(ctx, h, *instance.Status.NotificationsURLSecret, instance.Namespace) + if err != nil { + return err } + templateParameters["NotificationsURL"] = string(notificationInstanceURLSecret.Data["transport_url"]) } configTemplates := []util.Template{ @@ -1229,20 +1223,20 @@ func (r *CinderReconciler) transportURLCreateOrUpdate( ctx context.Context, instance *cinderv1beta1.Cinder, serviceLabels map[string]string, - rabbitMqClusterName string, + isNotifications bool, + rabbitMqConfig rabbitmqv1.RabbitMqConfig, ) (*rabbitmqv1.TransportURL, controllerutil.OperationResult, error) { - // Default values used for regular messagingBus transportURL and explicitly - // set here to ensure backward compatibility with the previous versions - rmqName := fmt.Sprintf("%s-cinder-transport", instance.Name) - transportURLName := instance.Spec.RabbitMqClusterName - - // When a rabbitMqClusterName is passed as input (notificationBus use case) - // update the default rmqName and transportURLName values - if rabbitMqClusterName != "" { - rmqName = fmt.Sprintf("%s-cinder-transport-%s", instance.Name, rabbitMqClusterName) - transportURLName = rabbitMqClusterName + // Set the TransportURL name to ensure uniqueness between messaging and notifications + // A separate TransportURL is always created for notifications, + // even when using the same cluster/user/vhost as messaging. + var rmqName string + if isNotifications { + rmqName = fmt.Sprintf("%s-cinder-notifications-transport", instance.Name) + } else { + rmqName = fmt.Sprintf("%s-cinder-transport", instance.Name) } + transportURL := &rabbitmqv1.TransportURL{ ObjectMeta: metav1.ObjectMeta{ Name: rmqName, @@ -1252,10 +1246,15 @@ func (r *CinderReconciler) transportURLCreateOrUpdate( } op, err := controllerutil.CreateOrUpdate(ctx, r.Client, transportURL, func() error { - transportURL.Spec.RabbitmqClusterName = transportURLName - - err := controllerutil.SetControllerReference(instance, transportURL, r.Scheme) - return err + // Use the Cluster field from rabbitMqConfig directly + transportURL.Spec.RabbitmqClusterName = rabbitMqConfig.Cluster + // Always set Username and Vhost to allow clearing/resetting them + // The infra-operator TransportURL controller handles empty values: + // - Empty Username: uses default cluster admin credentials + // - Empty Vhost: defaults to "/" vhost + transportURL.Spec.Username = rabbitMqConfig.User + transportURL.Spec.Vhost = rabbitMqConfig.Vhost + return controllerutil.SetControllerReference(instance, transportURL, r.Scheme) }) return transportURL, op, err @@ -1292,7 +1291,7 @@ func (r *CinderReconciler) apiDeploymentCreateOrUpdate(ctx context.Context, inst op, err := controllerutil.CreateOrUpdate(ctx, r.Client, deployment, func() error { deployment.Spec = cinderAPISpec - if instance.Spec.NotificationsBusInstance != nil { + if instance.Spec.NotificationsBus != nil && instance.Spec.NotificationsBus.Cluster != "" { deployment.Spec.NotificationsURLSecret = *instance.Status.NotificationsURLSecret } @@ -1339,7 +1338,7 @@ func (r *CinderReconciler) schedulerDeploymentCreateOrUpdate(ctx context.Context op, err := controllerutil.CreateOrUpdate(ctx, r.Client, deployment, func() error { deployment.Spec = cinderSchedulerSpec - if instance.Spec.NotificationsBusInstance != nil { + if instance.Spec.NotificationsBus != nil && instance.Spec.NotificationsBus.Cluster != "" { deployment.Spec.NotificationsURLSecret = *instance.Status.NotificationsURLSecret } @@ -1391,7 +1390,7 @@ func (r *CinderReconciler) backupDeploymentCreateOrUpdate( op, err := controllerutil.CreateOrUpdate(ctx, r.Client, deployment, func() error { deployment.Spec = cinderBackupSpec - if instance.Spec.NotificationsBusInstance != nil { + if instance.Spec.NotificationsBus != nil && instance.Spec.NotificationsBus.Cluster != "" { deployment.Spec.NotificationsURLSecret = *instance.Status.NotificationsURLSecret } @@ -1469,7 +1468,7 @@ func (r *CinderReconciler) volumeDeploymentCreateOrUpdate(ctx context.Context, i op, err := controllerutil.CreateOrUpdate(ctx, r.Client, deployment, func() error { deployment.Spec = cinderVolumeSpec - if instance.Spec.NotificationsBusInstance != nil { + if instance.Spec.NotificationsBus != nil && instance.Spec.NotificationsBus.Cluster != "" { deployment.Spec.NotificationsURLSecret = *instance.Status.NotificationsURLSecret } diff --git a/test/functional/cinder_controller_test.go b/test/functional/cinder_controller_test.go index 25704813f..eb90fb486 100644 --- a/test/functional/cinder_controller_test.go +++ b/test/functional/cinder_controller_test.go @@ -38,6 +38,7 @@ import ( cinderv1 "github.com/openstack-k8s-operators/cinder-operator/api/v1beta1" "github.com/openstack-k8s-operators/cinder-operator/internal/cinder" memcachedv1 "github.com/openstack-k8s-operators/infra-operator/apis/memcached/v1beta1" + rabbitmqv1 "github.com/openstack-k8s-operators/infra-operator/apis/rabbitmq/v1beta1" topologyv1 "github.com/openstack-k8s-operators/infra-operator/apis/topology/v1beta1" keystonev1 "github.com/openstack-k8s-operators/keystone-operator/api/v1beta1" condition "github.com/openstack-k8s-operators/lib-common/modules/common/condition" @@ -1325,6 +1326,12 @@ var _ = Describe("Cinder controller", func() { ), ) infra.SimulateTransportURLReady(cinderTest.CinderTransportURL) + // Simulate the notifications TransportURL being ready (separate from messaging) + notificationsTransportURLName := types.NamespacedName{ + Namespace: cinderTest.Instance.Namespace, + Name: fmt.Sprintf("%s-cinder-notifications-transport", cinderTest.Instance.Name), + } + infra.SimulateTransportURLReady(notificationsTransportURLName) DeferCleanup(infra.DeleteMemcached, infra.CreateMemcached(namespace, cinderTest.MemcachedInstance, memcachedSpec)) infra.SimulateMemcachedReady(cinderTest.CinderMemcached) keystoneAPIName := keystone.CreateKeystoneAPI(cinderTest.Instance.Namespace) @@ -1342,50 +1349,26 @@ var _ = Describe("Cinder controller", func() { corev1.ConditionTrue, ) + // Verify that separate TransportURL CRs exist even with same cluster Eventually(func(g Gomega) { - cinder := GetCinder(cinderTest.Instance) - g.Expect(cinder.Status.TransportURLSecret).ToNot(Equal("")) - g.Expect(cinder.Status.NotificationsURLSecret).ToNot(BeNil()) - g.Expect(cinder.Status.TransportURLSecret).To(Equal( - *cinder.Status.NotificationsURLSecret)) - }, timeout, interval).Should(Succeed()) - }) - It("overrides cinder CR notifications", func() { - // add new-rabbit in cinder CR - DeferCleanup(k8sClient.Delete, ctx, CreateCinderMessageBusSecret(cinderTest.Instance.Namespace, cinderTest.NotificationSecretName)) - - // update cinder CR to point to the new (dedicated) rabbit instance - Eventually(func(g Gomega) { - cinder := GetCinder(cinderTest.Instance) - *cinder.Spec.NotificationsBusInstance = "rabbitmq-notification" - g.Expect(k8sClient.Update(ctx, cinder)).To(Succeed()) - }, timeout, interval).Should(Succeed()) + mainTransportURL := infra.GetTransportURL(cinderTest.CinderTransportURL) + g.Expect(mainTransportURL).ToNot(BeNil()) - th.ExpectCondition( - cinderTest.Instance, - ConditionGetterFunc(CinderConditionGetter), - condition.NotificationBusInstanceReadyCondition, - corev1.ConditionTrue, - ) - - Eventually(func(g Gomega) { - cinder := GetCinder(cinderTest.Instance) - g.Expect(*cinder.Status.NotificationsURLSecret).ToNot( - Equal(cinder.Status.TransportURLSecret)) - }, timeout, interval).Should(Succeed()) - }) + notificationsTransportURLName := types.NamespacedName{ + Namespace: cinderTest.Instance.Namespace, + Name: fmt.Sprintf("%s-cinder-notifications-transport", cinderTest.Instance.Name), + } + notificationsTransportURL := infra.GetTransportURL(notificationsTransportURLName) + g.Expect(notificationsTransportURL).ToNot(BeNil()) - It("updates cinder CR and disable notifications", func() { - Eventually(func(g Gomega) { - cinder := GetCinder(cinderTest.Instance) - cinder.Spec.NotificationsBusInstance = nil - g.Expect(k8sClient.Update(ctx, cinder)).To(Succeed()) + // Verify they are different TransportURL CRs + g.Expect(mainTransportURL.Name).ToNot(Equal(notificationsTransportURL.Name)) }, timeout, interval).Should(Succeed()) Eventually(func(g Gomega) { cinder := GetCinder(cinderTest.Instance) - g.Expect(cinder.Status.NotificationsURLSecret).To(BeNil()) g.Expect(cinder.Status.TransportURLSecret).ToNot(Equal("")) + g.Expect(cinder.Status.NotificationsURLSecret).ToNot(BeNil()) }, timeout, interval).Should(Succeed()) }) }) @@ -2037,3 +2020,449 @@ var _ = Describe("Cinder Webhook", func() { }) }) }) + +var _ = Describe("Cinder with RabbitMQ custom vhost and user", func() { + var memcachedSpec memcachedv1.MemcachedSpec + + BeforeEach(func() { + err := os.Setenv("OPERATOR_TEMPLATES", "../../templates") + Expect(err).NotTo(HaveOccurred()) + + memcachedSpec = memcachedv1.MemcachedSpec{ + MemcachedSpecCore: memcachedv1.MemcachedSpecCore{ + Replicas: ptr.To(int32(3)), + }, + } + }) + + When("Cinder is created with custom RabbitMQ vhost and user", func() { + BeforeEach(func() { + spec := GetDefaultCinderSpec() + spec["messagingBus"] = map[string]any{ + "user": "custom-user", + "vhost": "custom-vhost", + } + DeferCleanup(th.DeleteInstance, CreateCinder(cinderTest.Instance, spec)) + DeferCleanup(k8sClient.Delete, ctx, CreateCinderMessageBusSecret(cinderTest.Instance.Namespace, cinderTest.RabbitmqSecretName)) + DeferCleanup( + mariadb.DeleteDBService, + mariadb.CreateDBService( + cinderTest.Instance.Namespace, + GetCinder(cinderTest.Instance).Spec.DatabaseInstance, + corev1.ServiceSpec{ + Ports: []corev1.ServicePort{{Port: 3306}}, + }, + ), + ) + infra.SimulateTransportURLReady(cinderTest.CinderTransportURL) + DeferCleanup(infra.DeleteMemcached, infra.CreateMemcached(namespace, cinderTest.MemcachedInstance, memcachedSpec)) + infra.SimulateMemcachedReady(cinderTest.CinderMemcached) + DeferCleanup(keystone.DeleteKeystoneAPI, keystone.CreateKeystoneAPI(cinderTest.Instance.Namespace)) + mariadb.SimulateMariaDBAccountCompleted(cinderTest.Database) + mariadb.SimulateMariaDBDatabaseCompleted(cinderTest.Database) + }) + + It("should create TransportURL with custom vhost and user", func() { + Eventually(func(g Gomega) { + transportURL := infra.GetTransportURL(cinderTest.CinderTransportURL) + g.Expect(transportURL.Spec.Username).To(Equal("custom-user")) + g.Expect(transportURL.Spec.Vhost).To(Equal("custom-vhost")) + }, timeout, interval).Should(Succeed()) + }) + }) + + When("Cinder is created with default RabbitMQ configuration", func() { + BeforeEach(func() { + DeferCleanup(th.DeleteInstance, CreateCinder(cinderTest.Instance, GetDefaultCinderSpec())) + DeferCleanup(k8sClient.Delete, ctx, CreateCinderMessageBusSecret(cinderTest.Instance.Namespace, cinderTest.RabbitmqSecretName)) + DeferCleanup( + mariadb.DeleteDBService, + mariadb.CreateDBService( + cinderTest.Instance.Namespace, + GetCinder(cinderTest.Instance).Spec.DatabaseInstance, + corev1.ServiceSpec{ + Ports: []corev1.ServicePort{{Port: 3306}}, + }, + ), + ) + infra.SimulateTransportURLReady(cinderTest.CinderTransportURL) + DeferCleanup(infra.DeleteMemcached, infra.CreateMemcached(namespace, cinderTest.MemcachedInstance, memcachedSpec)) + infra.SimulateMemcachedReady(cinderTest.CinderMemcached) + DeferCleanup(keystone.DeleteKeystoneAPI, keystone.CreateKeystoneAPI(cinderTest.Instance.Namespace)) + mariadb.SimulateMariaDBAccountCompleted(cinderTest.Database) + mariadb.SimulateMariaDBDatabaseCompleted(cinderTest.Database) + }) + + It("should create TransportURL with default vhost and user", func() { + Eventually(func(g Gomega) { + transportURL := infra.GetTransportURL(cinderTest.CinderTransportURL) + g.Expect(transportURL.Spec.Username).To(Equal("")) + g.Expect(transportURL.Spec.Vhost).To(Equal("")) + }, timeout, interval).Should(Succeed()) + }) + }) + + When("Cinder is created with only custom RabbitMQ user", func() { + BeforeEach(func() { + spec := GetDefaultCinderSpec() + spec["messagingBus"] = map[string]any{ + "user": "custom-user-only", + } + DeferCleanup(th.DeleteInstance, CreateCinder(cinderTest.Instance, spec)) + DeferCleanup(k8sClient.Delete, ctx, CreateCinderMessageBusSecret(cinderTest.Instance.Namespace, cinderTest.RabbitmqSecretName)) + DeferCleanup( + mariadb.DeleteDBService, + mariadb.CreateDBService( + cinderTest.Instance.Namespace, + GetCinder(cinderTest.Instance).Spec.DatabaseInstance, + corev1.ServiceSpec{ + Ports: []corev1.ServicePort{{Port: 3306}}, + }, + ), + ) + infra.SimulateTransportURLReady(cinderTest.CinderTransportURL) + DeferCleanup(infra.DeleteMemcached, infra.CreateMemcached(namespace, cinderTest.MemcachedInstance, memcachedSpec)) + infra.SimulateMemcachedReady(cinderTest.CinderMemcached) + DeferCleanup(keystone.DeleteKeystoneAPI, keystone.CreateKeystoneAPI(cinderTest.Instance.Namespace)) + mariadb.SimulateMariaDBAccountCompleted(cinderTest.Database) + mariadb.SimulateMariaDBDatabaseCompleted(cinderTest.Database) + }) + + It("should create TransportURL with custom user and default vhost", func() { + Eventually(func(g Gomega) { + transportURL := infra.GetTransportURL(cinderTest.CinderTransportURL) + g.Expect(transportURL.Spec.Username).To(Equal("custom-user-only")) + g.Expect(transportURL.Spec.Vhost).To(Equal("")) + }, timeout, interval).Should(Succeed()) + }) + }) + + When("Cinder is created with only custom RabbitMQ vhost", func() { + BeforeEach(func() { + spec := GetDefaultCinderSpec() + spec["messagingBus"] = map[string]any{ + "vhost": "custom-vhost-only", + } + DeferCleanup(th.DeleteInstance, CreateCinder(cinderTest.Instance, spec)) + DeferCleanup(k8sClient.Delete, ctx, CreateCinderMessageBusSecret(cinderTest.Instance.Namespace, cinderTest.RabbitmqSecretName)) + DeferCleanup( + mariadb.DeleteDBService, + mariadb.CreateDBService( + cinderTest.Instance.Namespace, + GetCinder(cinderTest.Instance).Spec.DatabaseInstance, + corev1.ServiceSpec{ + Ports: []corev1.ServicePort{{Port: 3306}}, + }, + ), + ) + infra.SimulateTransportURLReady(cinderTest.CinderTransportURL) + DeferCleanup(infra.DeleteMemcached, infra.CreateMemcached(namespace, cinderTest.MemcachedInstance, memcachedSpec)) + infra.SimulateMemcachedReady(cinderTest.CinderMemcached) + DeferCleanup(keystone.DeleteKeystoneAPI, keystone.CreateKeystoneAPI(cinderTest.Instance.Namespace)) + mariadb.SimulateMariaDBAccountCompleted(cinderTest.Database) + mariadb.SimulateMariaDBDatabaseCompleted(cinderTest.Database) + }) + + It("should create TransportURL with custom vhost and default user", func() { + Eventually(func(g Gomega) { + transportURL := infra.GetTransportURL(cinderTest.CinderTransportURL) + g.Expect(transportURL.Spec.Username).To(Equal("")) + g.Expect(transportURL.Spec.Vhost).To(Equal("custom-vhost-only")) + }, timeout, interval).Should(Succeed()) + }) + }) + + When("Cinder RabbitMQ configuration is updated", func() { + BeforeEach(func() { + spec := GetDefaultCinderSpec() + spec["messagingBus"] = map[string]any{ + "user": "initial-user", + "vhost": "initial-vhost", + } + DeferCleanup(th.DeleteInstance, CreateCinder(cinderTest.Instance, spec)) + DeferCleanup(k8sClient.Delete, ctx, CreateCinderMessageBusSecret(cinderTest.Instance.Namespace, cinderTest.RabbitmqSecretName)) + DeferCleanup( + mariadb.DeleteDBService, + mariadb.CreateDBService( + cinderTest.Instance.Namespace, + GetCinder(cinderTest.Instance).Spec.DatabaseInstance, + corev1.ServiceSpec{ + Ports: []corev1.ServicePort{{Port: 3306}}, + }, + ), + ) + infra.SimulateTransportURLReady(cinderTest.CinderTransportURL) + DeferCleanup(infra.DeleteMemcached, infra.CreateMemcached(namespace, cinderTest.MemcachedInstance, memcachedSpec)) + infra.SimulateMemcachedReady(cinderTest.CinderMemcached) + DeferCleanup(keystone.DeleteKeystoneAPI, keystone.CreateKeystoneAPI(cinderTest.Instance.Namespace)) + mariadb.SimulateMariaDBAccountCompleted(cinderTest.Database) + mariadb.SimulateMariaDBDatabaseCompleted(cinderTest.Database) + }) + + It("should update TransportURL when RabbitMQ configuration changes", func() { + // Verify initial configuration + Eventually(func(g Gomega) { + transportURL := infra.GetTransportURL(cinderTest.CinderTransportURL) + g.Expect(transportURL.Spec.Username).To(Equal("initial-user")) + g.Expect(transportURL.Spec.Vhost).To(Equal("initial-vhost")) + }, timeout, interval).Should(Succeed()) + + // Update the Cinder CR with new RabbitMQ configuration + Eventually(func(g Gomega) { + cinder := GetCinder(cinderTest.Instance) + cinder.Spec.MessagingBus.User = "updated-user" + cinder.Spec.MessagingBus.Vhost = "updated-vhost" + g.Expect(k8sClient.Update(ctx, cinder)).To(Succeed()) + }, timeout, interval).Should(Succeed()) + + // Verify the TransportURL is updated + Eventually(func(g Gomega) { + transportURL := infra.GetTransportURL(cinderTest.CinderTransportURL) + g.Expect(transportURL.Spec.Username).To(Equal("updated-user")) + g.Expect(transportURL.Spec.Vhost).To(Equal("updated-vhost")) + }, timeout, interval).Should(Succeed()) + }) + }) + + When("Cinder is created with different RabbitMQ configs for main and notifications", func() { + BeforeEach(func() { + spec := GetDefaultCinderSpec() + spec["messagingBus"] = map[string]any{ + "user": "main-user", + "vhost": "main-vhost", + } + spec["notificationsBus"] = map[string]any{ + "user": "notifications-user", + "vhost": "notifications-vhost", + } + spec["notificationsBusInstance"] = "rabbitmq-notifications" + DeferCleanup(th.DeleteInstance, CreateCinder(cinderTest.Instance, spec)) + DeferCleanup(k8sClient.Delete, ctx, CreateCinderMessageBusSecret(cinderTest.Instance.Namespace, cinderTest.RabbitmqSecretName)) + DeferCleanup(k8sClient.Delete, ctx, CreateCinderMessageBusSecret(cinderTest.Instance.Namespace, "rabbitmq-notifications-secret")) + DeferCleanup( + mariadb.DeleteDBService, + mariadb.CreateDBService( + cinderTest.Instance.Namespace, + GetCinder(cinderTest.Instance).Spec.DatabaseInstance, + corev1.ServiceSpec{ + Ports: []corev1.ServicePort{{Port: 3306}}, + }, + ), + ) + infra.SimulateTransportURLReady(cinderTest.CinderTransportURL) + DeferCleanup(infra.DeleteMemcached, infra.CreateMemcached(namespace, cinderTest.MemcachedInstance, memcachedSpec)) + infra.SimulateMemcachedReady(cinderTest.CinderMemcached) + DeferCleanup(keystone.DeleteKeystoneAPI, keystone.CreateKeystoneAPI(cinderTest.Instance.Namespace)) + mariadb.SimulateMariaDBAccountCompleted(cinderTest.Database) + mariadb.SimulateMariaDBDatabaseCompleted(cinderTest.Database) + }) + + It("should use different credentials for main and notifications TransportURLs", func() { + // Verify main TransportURL has main-specific config + Eventually(func(g Gomega) { + transportURL := infra.GetTransportURL(cinderTest.CinderTransportURL) + g.Expect(transportURL.Spec.Username).To(Equal("main-user")) + g.Expect(transportURL.Spec.Vhost).To(Equal("main-vhost")) + g.Expect(transportURL.Spec.RabbitmqClusterName).To(Equal(cinderTest.RabbitmqClusterName)) + }, timeout, interval).Should(Succeed()) + + // Verify notifications TransportURL has notifications-specific config + notificationsTransportURLName := types.NamespacedName{ + Namespace: cinderTest.Instance.Namespace, + Name: fmt.Sprintf("%s-cinder-notifications-transport", cinderTest.Instance.Name), + } + infra.SimulateTransportURLReady(notificationsTransportURLName) + + Eventually(func(g Gomega) { + notificationsTransportURL := infra.GetTransportURL(notificationsTransportURLName) + g.Expect(notificationsTransportURL.Spec.Username).To(Equal("notifications-user")) + g.Expect(notificationsTransportURL.Spec.Vhost).To(Equal("notifications-vhost")) + g.Expect(notificationsTransportURL.Spec.RabbitmqClusterName).To(Equal("rabbitmq-notifications")) + }, timeout, interval).Should(Succeed()) + }) + }) + + When("Cinder is created with notifications bus using default credentials", func() { + BeforeEach(func() { + spec := GetDefaultCinderSpec() + spec["notificationsBusInstance"] = "rabbitmq-notifications" + DeferCleanup(th.DeleteInstance, CreateCinder(cinderTest.Instance, spec)) + DeferCleanup(k8sClient.Delete, ctx, CreateCinderMessageBusSecret(cinderTest.Instance.Namespace, cinderTest.RabbitmqSecretName)) + DeferCleanup(k8sClient.Delete, ctx, CreateCinderMessageBusSecret(cinderTest.Instance.Namespace, "rabbitmq-notifications-secret")) + DeferCleanup( + mariadb.DeleteDBService, + mariadb.CreateDBService( + cinderTest.Instance.Namespace, + GetCinder(cinderTest.Instance).Spec.DatabaseInstance, + corev1.ServiceSpec{ + Ports: []corev1.ServicePort{{Port: 3306}}, + }, + ), + ) + infra.SimulateTransportURLReady(cinderTest.CinderTransportURL) + DeferCleanup(infra.DeleteMemcached, infra.CreateMemcached(namespace, cinderTest.MemcachedInstance, memcachedSpec)) + infra.SimulateMemcachedReady(cinderTest.CinderMemcached) + DeferCleanup(keystone.DeleteKeystoneAPI, keystone.CreateKeystoneAPI(cinderTest.Instance.Namespace)) + mariadb.SimulateMariaDBAccountCompleted(cinderTest.Database) + mariadb.SimulateMariaDBDatabaseCompleted(cinderTest.Database) + }) + + It("should create both TransportURLs with default credentials", func() { + // Verify main TransportURL has default config + Eventually(func(g Gomega) { + transportURL := infra.GetTransportURL(cinderTest.CinderTransportURL) + g.Expect(transportURL.Spec.Username).To(Equal("")) + g.Expect(transportURL.Spec.Vhost).To(Equal("")) + }, timeout, interval).Should(Succeed()) + + // Verify notifications TransportURL also has default config + notificationsTransportURLName := types.NamespacedName{ + Namespace: cinderTest.Instance.Namespace, + Name: fmt.Sprintf("%s-cinder-notifications-transport", cinderTest.Instance.Name), + } + infra.SimulateTransportURLReady(notificationsTransportURLName) + + Eventually(func(g Gomega) { + notificationsTransportURL := infra.GetTransportURL(notificationsTransportURLName) + g.Expect(notificationsTransportURL.Spec.Username).To(Equal("")) + g.Expect(notificationsTransportURL.Spec.Vhost).To(Equal("")) + g.Expect(notificationsTransportURL.Spec.RabbitmqClusterName).To(Equal("rabbitmq-notifications")) + }, timeout, interval).Should(Succeed()) + }) + }) + + When("Cinder is created with same RabbitMQ cluster but different vhost/user for notifications", func() { + BeforeEach(func() { + spec := GetDefaultCinderSpec() + spec["messagingBus"] = map[string]any{ + "cluster": "rabbitmq", + "user": "main-user", + "vhost": "main-vhost", + } + spec["notificationsBus"] = map[string]any{ + "cluster": "rabbitmq", // Same cluster as messaging + "user": "notifications-user", + "vhost": "notifications-vhost", + } + DeferCleanup(th.DeleteInstance, CreateCinder(cinderTest.Instance, spec)) + DeferCleanup(k8sClient.Delete, ctx, CreateCinderMessageBusSecret(cinderTest.Instance.Namespace, cinderTest.RabbitmqSecretName)) + DeferCleanup( + mariadb.DeleteDBService, + mariadb.CreateDBService( + cinderTest.Instance.Namespace, + GetCinder(cinderTest.Instance).Spec.DatabaseInstance, + corev1.ServiceSpec{ + Ports: []corev1.ServicePort{{Port: 3306}}, + }, + ), + ) + infra.SimulateTransportURLReady(cinderTest.CinderTransportURL) + DeferCleanup(infra.DeleteMemcached, infra.CreateMemcached(namespace, cinderTest.MemcachedInstance, memcachedSpec)) + infra.SimulateMemcachedReady(cinderTest.CinderMemcached) + DeferCleanup(keystone.DeleteKeystoneAPI, keystone.CreateKeystoneAPI(cinderTest.Instance.Namespace)) + mariadb.SimulateMariaDBAccountCompleted(cinderTest.Database) + mariadb.SimulateMariaDBDatabaseCompleted(cinderTest.Database) + }) + + It("should create separate TransportURLs for main and notifications even with same cluster", func() { + // Verify main TransportURL has main-specific config + Eventually(func(g Gomega) { + transportURL := infra.GetTransportURL(cinderTest.CinderTransportURL) + g.Expect(transportURL.Spec.Username).To(Equal("main-user")) + g.Expect(transportURL.Spec.Vhost).To(Equal("main-vhost")) + g.Expect(transportURL.Spec.RabbitmqClusterName).To(Equal(cinderTest.RabbitmqClusterName)) + }, timeout, interval).Should(Succeed()) + + // Verify notifications TransportURL has notifications-specific config + // Even though using same cluster, it should create a separate TransportURL CR + notificationsTransportURLName := types.NamespacedName{ + Namespace: cinderTest.Instance.Namespace, + Name: fmt.Sprintf("%s-cinder-notifications-transport", cinderTest.Instance.Name), + } + infra.SimulateTransportURLReady(notificationsTransportURLName) + + Eventually(func(g Gomega) { + notificationsTransportURL := infra.GetTransportURL(notificationsTransportURLName) + g.Expect(notificationsTransportURL.Spec.Username).To(Equal("notifications-user")) + g.Expect(notificationsTransportURL.Spec.Vhost).To(Equal("notifications-vhost")) + g.Expect(notificationsTransportURL.Spec.RabbitmqClusterName).To(Equal("rabbitmq")) + }, timeout, interval).Should(Succeed()) + + // Verify that both TransportURLs exist and are different CRs + Eventually(func(g Gomega) { + mainTransportURL := infra.GetTransportURL(cinderTest.CinderTransportURL) + notificationsTransportURL := infra.GetTransportURL(notificationsTransportURLName) + + // They should have the same cluster name + g.Expect(mainTransportURL.Spec.RabbitmqClusterName).To(Equal(notificationsTransportURL.Spec.RabbitmqClusterName)) + + // But different vhosts and users + g.Expect(mainTransportURL.Spec.Vhost).ToNot(Equal(notificationsTransportURL.Spec.Vhost)) + g.Expect(mainTransportURL.Spec.Username).ToNot(Equal(notificationsTransportURL.Spec.Username)) + }, timeout, interval).Should(Succeed()) + }) + }) + + When("Cinder notifications are disabled after creation", func() { + BeforeEach(func() { + spec := GetDefaultCinderSpec() + // Start with notifications enabled + spec["notificationsBusInstance"] = "rabbitmq-notifications" + DeferCleanup(th.DeleteInstance, CreateCinder(cinderTest.Instance, spec)) + DeferCleanup(k8sClient.Delete, ctx, CreateCinderMessageBusSecret(cinderTest.Instance.Namespace, cinderTest.RabbitmqSecretName)) + DeferCleanup(k8sClient.Delete, ctx, CreateCinderMessageBusSecret(cinderTest.Instance.Namespace, "rabbitmq-notifications-secret")) + DeferCleanup( + mariadb.DeleteDBService, + mariadb.CreateDBService( + cinderTest.Instance.Namespace, + GetCinder(cinderTest.Instance).Spec.DatabaseInstance, + corev1.ServiceSpec{ + Ports: []corev1.ServicePort{{Port: 3306}}, + }, + ), + ) + infra.SimulateTransportURLReady(cinderTest.CinderTransportURL) + DeferCleanup(infra.DeleteMemcached, infra.CreateMemcached(namespace, cinderTest.MemcachedInstance, memcachedSpec)) + infra.SimulateMemcachedReady(cinderTest.CinderMemcached) + DeferCleanup(keystone.DeleteKeystoneAPI, keystone.CreateKeystoneAPI(cinderTest.Instance.Namespace)) + mariadb.SimulateMariaDBAccountCompleted(cinderTest.Database) + mariadb.SimulateMariaDBDatabaseCompleted(cinderTest.Database) + }) + + It("should remove NotificationsURLSecret when notificationsBus is set to empty", func() { + // Wait for the notifications TransportURL to be created and simulate it being ready + notificationsTransportURLName := types.NamespacedName{ + Namespace: cinderTest.Instance.Namespace, + Name: fmt.Sprintf("%s-cinder-notifications-transport", cinderTest.Instance.Name), + } + Eventually(func(g Gomega) { + transportURL := &rabbitmqv1.TransportURL{} + err := k8sClient.Get(ctx, notificationsTransportURLName, transportURL) + g.Expect(err).ToNot(HaveOccurred()) + }, timeout, interval).Should(Succeed()) + infra.SimulateTransportURLReady(notificationsTransportURLName) + + // Verify notifications are enabled + Eventually(func(g Gomega) { + cinder := GetCinder(cinderTest.Instance) + g.Expect(cinder.Status.NotificationsURLSecret).ToNot(BeNil()) + g.Expect(*cinder.Status.NotificationsURLSecret).ToNot(Equal(cinder.Status.TransportURLSecret)) + }, timeout, interval).Should(Succeed()) + + // Now disable notifications by clearing both the deprecated field and the new field + Eventually(func(g Gomega) { + cinder := GetCinder(cinderTest.Instance) + cinder.Spec.NotificationsBusInstance = nil + cinder.Spec.NotificationsBus = nil + g.Expect(k8sClient.Update(ctx, cinder)).To(Succeed()) + }, timeout, interval).Should(Succeed()) + + // Verify NotificationsURLSecret is now nil + Eventually(func(g Gomega) { + cinder := GetCinder(cinderTest.Instance) + g.Expect(cinder.Status.NotificationsURLSecret).To(BeNil()) + g.Expect(cinder.Status.TransportURLSecret).ToNot(Equal("")) + }, timeout, interval).Should(Succeed()) + }) + }) +}) diff --git a/test/functional/cinder_webhook_test.go b/test/functional/cinder_webhook_test.go new file mode 100644 index 000000000..01ccaa681 --- /dev/null +++ b/test/functional/cinder_webhook_test.go @@ -0,0 +1,118 @@ +/* +Copyright 2025. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package functional + +import ( + "errors" + + . "github.com/onsi/ginkgo/v2" //revive:disable:dot-imports + . "github.com/onsi/gomega" //revive:disable:dot-imports + + k8s_errors "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" +) + +var _ = Describe("Cinder webhook", func() { + It("rejects update to deprecated rabbitMqClusterName field", func() { + spec := GetDefaultCinderSpec() + spec["rabbitMqClusterName"] = "rabbitmq" + + raw := map[string]any{ + "apiVersion": "cinder.openstack.org/v1beta1", + "kind": "Cinder", + "metadata": map[string]any{ + "name": cinderName.Name, + "namespace": cinderName.Namespace, + }, + "spec": spec, + } + + // Create the Cinder instance + unstructuredObj := &unstructured.Unstructured{Object: raw} + _, err := controllerutil.CreateOrPatch( + ctx, k8sClient, unstructuredObj, func() error { return nil }) + Expect(err).ShouldNot(HaveOccurred()) + + DeferCleanup(func() { + _ = k8sClient.Delete(ctx, unstructuredObj) + }) + + // Try to update rabbitMqClusterName + Eventually(func(g Gomega) { + g.Expect(k8sClient.Get(ctx, cinderName, unstructuredObj)).Should(Succeed()) + specMap := unstructuredObj.Object["spec"].(map[string]any) + specMap["rabbitMqClusterName"] = "rabbitmq2" + err := k8sClient.Update(ctx, unstructuredObj) + g.Expect(err).Should(HaveOccurred()) + + var statusError *k8s_errors.StatusError + g.Expect(errors.As(err, &statusError)).To(BeTrue()) + g.Expect(statusError.ErrStatus.Details.Kind).To(Equal("Cinder")) + g.Expect(statusError.ErrStatus.Message).To( + ContainSubstring("field \"spec.rabbitMqClusterName\" is deprecated")) + g.Expect(statusError.ErrStatus.Message).To( + ContainSubstring("use \"spec.messagingBus.cluster\" instead")) + }, timeout, interval).Should(Succeed()) + }) + + It("rejects update to deprecated notificationsBusInstance field", func() { + spec := GetDefaultCinderSpec() + notificationsBusInstance := "notifications-rabbitmq" + spec["notificationsBusInstance"] = notificationsBusInstance + + cinderName2 := cinderName + cinderName2.Name = cinderName.Name + "-notifications" + + raw := map[string]any{ + "apiVersion": "cinder.openstack.org/v1beta1", + "kind": "Cinder", + "metadata": map[string]any{ + "name": cinderName2.Name, + "namespace": cinderName2.Namespace, + }, + "spec": spec, + } + + // Create the Cinder instance + unstructuredObj := &unstructured.Unstructured{Object: raw} + _, err := controllerutil.CreateOrPatch( + ctx, k8sClient, unstructuredObj, func() error { return nil }) + Expect(err).ShouldNot(HaveOccurred()) + + DeferCleanup(func() { + _ = k8sClient.Delete(ctx, unstructuredObj) + }) + + // Try to update notificationsBusInstance + Eventually(func(g Gomega) { + g.Expect(k8sClient.Get(ctx, cinderName2, unstructuredObj)).Should(Succeed()) + specMap := unstructuredObj.Object["spec"].(map[string]any) + specMap["notificationsBusInstance"] = "notifications-rabbitmq2" + err := k8sClient.Update(ctx, unstructuredObj) + g.Expect(err).Should(HaveOccurred()) + + var statusError *k8s_errors.StatusError + g.Expect(errors.As(err, &statusError)).To(BeTrue()) + g.Expect(statusError.ErrStatus.Details.Kind).To(Equal("Cinder")) + g.Expect(statusError.ErrStatus.Message).To( + ContainSubstring("field \"spec.notificationsBusInstance\" is deprecated")) + g.Expect(statusError.ErrStatus.Message).To( + ContainSubstring("use \"spec.notificationsBus.cluster\" instead")) + }, timeout, interval).Should(Succeed()) + }) +}) From cac3f48d9aa27a01f404688293d85fce35902302 Mon Sep 17 00:00:00 2001 From: Luca Miccini Date: Thu, 29 Jan 2026 11:51:31 +0100 Subject: [PATCH 2/2] Make deprecated parameter optional --- api/bases/cinder.openstack.org_cinders.yaml | 2 - api/go.sum | 13 ------ api/v1beta1/cinder_types.go | 5 +- api/v1beta1/cinder_webhook.go | 20 ++------ .../bases/cinder.openstack.org_cinders.yaml | 2 - test/functional/cinder_controller_test.go | 46 ++++++++++++------- .../common/assert_sample_deployment.yaml | 3 +- .../common/assert_tls_sample_deployment.yaml | 3 +- test/kuttl/tests/cinder_scale/04-assert.yaml | 3 +- test/kuttl/tests/cinder_scale/05-assert.yaml | 3 +- test/kuttl/tests/cinder_scale/07-assert.yaml | 3 +- test/kuttl/tests/cinder_scale/08-assert.yaml | 3 +- test/kuttl/tests/cinder_scale/09-assert.yaml | 3 +- test/kuttl/tests/cinder_scale/10-assert.yaml | 3 +- test/kuttl/tests/cinder_scale/11-assert.yaml | 3 +- 15 files changed, 54 insertions(+), 61 deletions(-) diff --git a/api/bases/cinder.openstack.org_cinders.yaml b/api/bases/cinder.openstack.org_cinders.yaml index 0052f2b5e..b752efaba 100644 --- a/api/bases/cinder.openstack.org_cinders.yaml +++ b/api/bases/cinder.openstack.org_cinders.yaml @@ -2097,7 +2097,6 @@ spec: e.g. to check logs type: boolean rabbitMqClusterName: - default: rabbitmq description: |- RabbitMQ instance name Needed to request a transportURL that is created and used in Cinder @@ -2133,7 +2132,6 @@ spec: - cinderScheduler - databaseInstance - memcachedInstance - - rabbitMqClusterName - secret type: object status: diff --git a/api/go.sum b/api/go.sum index bb6072ff7..4838ff7f6 100644 --- a/api/go.sum +++ b/api/go.sum @@ -75,7 +75,6 @@ github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee h1:W5t00kpgFd github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= -<<<<<<< HEAD github.com/onsi/ginkgo/v2 v2.28.1 h1:S4hj+HbZp40fNKuLUQOYLDgZLwNUVn19N3Atb98NCyI= github.com/onsi/ginkgo/v2 v2.28.1/go.mod h1:CLtbVInNckU3/+gC8LzkGUb9oF+e8W8TdUsxPwvdOgE= github.com/onsi/gomega v1.39.1 h1:1IJLAad4zjPn2PsnhH70V4DKRFlrCzGBNrNaru+Vf28= @@ -86,20 +85,8 @@ github.com/openstack-k8s-operators/lib-common/modules/common v0.6.1-0.2026012814 github.com/openstack-k8s-operators/lib-common/modules/common v0.6.1-0.20260128142552-e2c25eccae5a/go.mod h1:ndqfy1KbVorHH6+zlUFPIrCRhMSxO3ImYJUGaooE0x0= github.com/openstack-k8s-operators/lib-common/modules/storage v0.6.1-0.20260128142552-e2c25eccae5a h1:teKxfVLDxJD9ahjeh29GlKHiXNUFDkVRmkpJdeKAvGE= github.com/openstack-k8s-operators/lib-common/modules/storage v0.6.1-0.20260128142552-e2c25eccae5a/go.mod h1:jofj+VqDszxLCZSBYo794KGkCjMo01xzhQ/gffYzf3I= -======= -github.com/onsi/ginkgo/v2 v2.27.5 h1:ZeVgZMx2PDMdJm/+w5fE/OyG6ILo1Y3e+QX4zSR0zTE= -github.com/onsi/ginkgo/v2 v2.27.5/go.mod h1:ArE1D/XhNXBXCBkKOLkbsb2c81dQHCRcF5zwn/ykDRo= -github.com/onsi/gomega v1.39.0 h1:y2ROC3hKFmQZJNFeGAMeHZKkjBL65mIZcvrLQBF9k6Q= -github.com/onsi/gomega v1.39.0/go.mod h1:ZCU1pkQcXDO5Sl9/VVEGlDyp+zm0m1cmeG5TOzLgdh4= -github.com/openstack-k8s-operators/infra-operator/apis v0.6.1-0.20260124124521-834dd78eceb9 h1:87CTyKbobvZPGOqiigSO+/PPbMnFHoYecjwBdnxxEGw= -github.com/openstack-k8s-operators/infra-operator/apis v0.6.1-0.20260124124521-834dd78eceb9/go.mod h1:ZXwFlspJCdZEUjMbmaf61t5AMB4u2vMyAMMoe/vJroE= -github.com/openstack-k8s-operators/lib-common/modules/common v0.6.1-0.20260126081203-efc2df9207eb h1:S7tnYO/E1f1KQfcp7N5bam8+ax/ExDTOhZ1WqG4Bfu0= -github.com/openstack-k8s-operators/lib-common/modules/common v0.6.1-0.20260126081203-efc2df9207eb/go.mod h1:ndqfy1KbVorHH6+zlUFPIrCRhMSxO3ImYJUGaooE0x0= -github.com/openstack-k8s-operators/lib-common/modules/storage v0.6.1-0.20260126081203-efc2df9207eb h1:0kP9V1pKfRno6ss7qAy3GcfVK29CobWym6WA7AYA7wY= -github.com/openstack-k8s-operators/lib-common/modules/storage v0.6.1-0.20260126081203-efc2df9207eb/go.mod h1:jofj+VqDszxLCZSBYo794KGkCjMo01xzhQ/gffYzf3I= github.com/openstack-k8s-operators/rabbitmq-cluster-operator/v2 v2.6.1-0.20250929174222-a0d328fa4dec h1:saovr368HPAKHN0aRPh8h8n9s9dn3d8Frmfua0UYRlc= github.com/openstack-k8s-operators/rabbitmq-cluster-operator/v2 v2.6.1-0.20250929174222-a0d328fa4dec/go.mod h1:Nh2NEePLjovUQof2krTAg4JaAoLacqtPTZQXK6izNfg= ->>>>>>> 7e5a34a (Rabbitmq vhost and user support) github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= diff --git a/api/v1beta1/cinder_types.go b/api/v1beta1/cinder_types.go index 723df6206..c02d4d048 100644 --- a/api/v1beta1/cinder_types.go +++ b/api/v1beta1/cinder_types.go @@ -65,12 +65,11 @@ type CinderSpecBase struct { // Might not be required in future DatabaseInstance string `json:"databaseInstance"` - // +kubebuilder:validation:Required - // +kubebuilder:default=rabbitmq + // +kubebuilder:validation:Optional // RabbitMQ instance name // Needed to request a transportURL that is created and used in Cinder // Deprecated: Use MessagingBus.Cluster instead - RabbitMqClusterName string `json:"rabbitMqClusterName" deprecated:"messagingBus.cluster"` + RabbitMqClusterName string `json:"rabbitMqClusterName,omitempty"` // +kubebuilder:validation:Optional // MessagingBus configuration (username, vhost, and cluster) diff --git a/api/v1beta1/cinder_webhook.go b/api/v1beta1/cinder_webhook.go index e66882dd5..e8784099a 100644 --- a/api/v1beta1/cinder_webhook.go +++ b/api/v1beta1/cinder_webhook.go @@ -26,7 +26,6 @@ import ( "golang.org/x/exp/maps" - rabbitmqv1 "github.com/openstack-k8s-operators/infra-operator/apis/rabbitmq/v1beta1" topologyv1 "github.com/openstack-k8s-operators/infra-operator/apis/topology/v1beta1" "github.com/openstack-k8s-operators/lib-common/modules/common/service" "github.com/openstack-k8s-operators/lib-common/modules/common/util" @@ -112,23 +111,14 @@ func (r *Cinder) Default() { // Default - set defaults for this Cinder spec func (spec *CinderSpecBase) Default() { - // Only migrate from deprecated field if the new field is not already set + // Default MessagingBus.Cluster if not set + // Migration from deprecated fields is handled by openstack-operator if spec.MessagingBus.Cluster == "" { - rabbitmqv1.DefaultRabbitMqConfig(&spec.MessagingBus, spec.RabbitMqClusterName) + spec.MessagingBus.Cluster = "rabbitmq" } - // Default NotificationsBus if NotificationsBusInstance is specified - if spec.NotificationsBusInstance != nil && *spec.NotificationsBusInstance != "" { - if spec.NotificationsBus == nil { - // Initialize empty NotificationsBus - credentials will be created dynamically - // to ensure separation from MessagingBus (RPC and notifications should never share credentials) - spec.NotificationsBus = &rabbitmqv1.RabbitMqConfig{} - } - // Always default the Cluster field from NotificationsBusInstance if it's empty - if spec.NotificationsBus.Cluster == "" { - rabbitmqv1.DefaultRabbitMqConfig(spec.NotificationsBus, *spec.NotificationsBusInstance) - } - } + // NotificationsBus.Cluster is not defaulted - it must be explicitly set if NotificationsBus is configured + // This ensures users make a conscious choice about which cluster to use for notifications if spec.DBPurge.Age == 0 { spec.DBPurge.Age = cinderDefaults.DBPurgeAge diff --git a/config/crd/bases/cinder.openstack.org_cinders.yaml b/config/crd/bases/cinder.openstack.org_cinders.yaml index 0052f2b5e..b752efaba 100644 --- a/config/crd/bases/cinder.openstack.org_cinders.yaml +++ b/config/crd/bases/cinder.openstack.org_cinders.yaml @@ -2097,7 +2097,6 @@ spec: e.g. to check logs type: boolean rabbitMqClusterName: - default: rabbitmq description: |- RabbitMQ instance name Needed to request a transportURL that is created and used in Cinder @@ -2133,7 +2132,6 @@ spec: - cinderScheduler - databaseInstance - memcachedInstance - - rabbitMqClusterName - secret type: object status: diff --git a/test/functional/cinder_controller_test.go b/test/functional/cinder_controller_test.go index eb90fb486..0fee4df22 100644 --- a/test/functional/cinder_controller_test.go +++ b/test/functional/cinder_controller_test.go @@ -89,7 +89,7 @@ var _ = Describe("Cinder controller", func() { Expect(Cinder.Spec.DatabaseInstance).Should(Equal("openstack")) Expect(Cinder.Spec.DatabaseAccount).Should(Equal(cinderTest.CinderDataBaseAccount)) Expect(Cinder.Spec.MemcachedInstance).Should(Equal(cinderTest.MemcachedInstance)) - Expect(Cinder.Spec.RabbitMqClusterName).Should(Equal(cinderTest.RabbitmqClusterName)) + Expect(Cinder.Spec.MessagingBus.Cluster).Should(Equal(cinderTest.RabbitmqClusterName)) Expect(Cinder.Spec.ServiceUser).Should(Equal(cinderTest.CinderServiceUser)) }) It("should have the Status fields initialized", func() { @@ -467,9 +467,11 @@ var _ = Describe("Cinder controller", func() { } rawSpec := map[string]any{ - "secret": SecretName, - "databaseInstance": "openstack", - "rabbitMqClusterName": "rabbitmq", + "secret": SecretName, + "databaseInstance": "openstack", + "messagingBus": map[string]any{ + "cluster": "rabbitmq", + }, "cinderAPI": map[string]any{ "containerImage": cinderv1.CinderAPIContainerImage, "networkAttachments": []string{"internalapi"}, @@ -1232,10 +1234,12 @@ var _ = Describe("Cinder controller", func() { When("Cinder CR instance is built with ExtraMounts", func() { BeforeEach(func() { rawSpec := map[string]any{ - "secret": SecretName, - "databaseInstance": "openstack", - "rabbitMqClusterName": "rabbitmq", - "extraMounts": GetExtraMounts(), + "secret": SecretName, + "databaseInstance": "openstack", + "messagingBus": map[string]any{ + "cluster": "rabbitmq", + }, + "extraMounts": GetExtraMounts(), "cinderAPI": map[string]any{ "containerImage": cinderv1.CinderAPIContainerImage, }, @@ -1296,10 +1300,14 @@ var _ = Describe("Cinder controller", func() { When("Cinder instance has notifications enabled", func() { BeforeEach(func() { rawSpec := map[string]any{ - "secret": SecretName, - "databaseInstance": "openstack", - "rabbitMqClusterName": "rabbitmq", - "notificationsBusInstance": "rabbitmq", + "secret": SecretName, + "databaseInstance": "openstack", + "messagingBus": map[string]any{ + "cluster": "rabbitmq", + }, + "notificationsBus": map[string]any{ + "cluster": "rabbitmq", + }, "cinderAPI": map[string]any{ "containerImage": cinderv1.CinderAPIContainerImage, }, @@ -2232,10 +2240,10 @@ var _ = Describe("Cinder with RabbitMQ custom vhost and user", func() { "vhost": "main-vhost", } spec["notificationsBus"] = map[string]any{ - "user": "notifications-user", - "vhost": "notifications-vhost", + "cluster": "rabbitmq-notifications", + "user": "notifications-user", + "vhost": "notifications-vhost", } - spec["notificationsBusInstance"] = "rabbitmq-notifications" DeferCleanup(th.DeleteInstance, CreateCinder(cinderTest.Instance, spec)) DeferCleanup(k8sClient.Delete, ctx, CreateCinderMessageBusSecret(cinderTest.Instance.Namespace, cinderTest.RabbitmqSecretName)) DeferCleanup(k8sClient.Delete, ctx, CreateCinderMessageBusSecret(cinderTest.Instance.Namespace, "rabbitmq-notifications-secret")) @@ -2285,7 +2293,9 @@ var _ = Describe("Cinder with RabbitMQ custom vhost and user", func() { When("Cinder is created with notifications bus using default credentials", func() { BeforeEach(func() { spec := GetDefaultCinderSpec() - spec["notificationsBusInstance"] = "rabbitmq-notifications" + spec["notificationsBus"] = map[string]any{ + "cluster": "rabbitmq-notifications", + } DeferCleanup(th.DeleteInstance, CreateCinder(cinderTest.Instance, spec)) DeferCleanup(k8sClient.Delete, ctx, CreateCinderMessageBusSecret(cinderTest.Instance.Namespace, cinderTest.RabbitmqSecretName)) DeferCleanup(k8sClient.Delete, ctx, CreateCinderMessageBusSecret(cinderTest.Instance.Namespace, "rabbitmq-notifications-secret")) @@ -2407,7 +2417,9 @@ var _ = Describe("Cinder with RabbitMQ custom vhost and user", func() { BeforeEach(func() { spec := GetDefaultCinderSpec() // Start with notifications enabled - spec["notificationsBusInstance"] = "rabbitmq-notifications" + spec["notificationsBus"] = map[string]any{ + "cluster": "rabbitmq-notifications", + } DeferCleanup(th.DeleteInstance, CreateCinder(cinderTest.Instance, spec)) DeferCleanup(k8sClient.Delete, ctx, CreateCinderMessageBusSecret(cinderTest.Instance.Namespace, cinderTest.RabbitmqSecretName)) DeferCleanup(k8sClient.Delete, ctx, CreateCinderMessageBusSecret(cinderTest.Instance.Namespace, "rabbitmq-notifications-secret")) diff --git a/test/kuttl/common/assert_sample_deployment.yaml b/test/kuttl/common/assert_sample_deployment.yaml index 0186ffbb6..c17d469f8 100644 --- a/test/kuttl/common/assert_sample_deployment.yaml +++ b/test/kuttl/common/assert_sample_deployment.yaml @@ -9,7 +9,8 @@ spec: debug = true databaseInstance: openstack databaseAccount: cinder - rabbitMqClusterName: rabbitmq + messagingBus: + cluster: rabbitmq cinderAPI: replicas: 1 cinderScheduler: diff --git a/test/kuttl/common/assert_tls_sample_deployment.yaml b/test/kuttl/common/assert_tls_sample_deployment.yaml index 9a0b7ec30..0c12347e6 100644 --- a/test/kuttl/common/assert_tls_sample_deployment.yaml +++ b/test/kuttl/common/assert_tls_sample_deployment.yaml @@ -9,7 +9,8 @@ spec: debug = true databaseInstance: openstack databaseAccount: cinder - rabbitMqClusterName: rabbitmq + messagingBus: + cluster: rabbitmq cinderAPI: replicas: 1 tls: diff --git a/test/kuttl/tests/cinder_scale/04-assert.yaml b/test/kuttl/tests/cinder_scale/04-assert.yaml index a1a4f2d77..5e80a7d20 100644 --- a/test/kuttl/tests/cinder_scale/04-assert.yaml +++ b/test/kuttl/tests/cinder_scale/04-assert.yaml @@ -9,7 +9,8 @@ spec: debug = true databaseInstance: openstack databaseAccount: cinder - rabbitMqClusterName: rabbitmq + messagingBus: + cluster: rabbitmq cinderAPI: replicas: 1 cinderScheduler: diff --git a/test/kuttl/tests/cinder_scale/05-assert.yaml b/test/kuttl/tests/cinder_scale/05-assert.yaml index 6b2fe0165..f34d6e26f 100644 --- a/test/kuttl/tests/cinder_scale/05-assert.yaml +++ b/test/kuttl/tests/cinder_scale/05-assert.yaml @@ -9,7 +9,8 @@ spec: debug = true databaseInstance: openstack databaseAccount: cinder - rabbitMqClusterName: rabbitmq + messagingBus: + cluster: rabbitmq cinderAPI: replicas: 1 cinderScheduler: diff --git a/test/kuttl/tests/cinder_scale/07-assert.yaml b/test/kuttl/tests/cinder_scale/07-assert.yaml index 5e6ec4441..0b52100f7 100644 --- a/test/kuttl/tests/cinder_scale/07-assert.yaml +++ b/test/kuttl/tests/cinder_scale/07-assert.yaml @@ -9,7 +9,8 @@ spec: debug = true databaseInstance: openstack databaseAccount: cinder - rabbitMqClusterName: rabbitmq + messagingBus: + cluster: rabbitmq cinderAPI: replicas: 1 cinderScheduler: diff --git a/test/kuttl/tests/cinder_scale/08-assert.yaml b/test/kuttl/tests/cinder_scale/08-assert.yaml index a6debe4f9..853d60db4 100644 --- a/test/kuttl/tests/cinder_scale/08-assert.yaml +++ b/test/kuttl/tests/cinder_scale/08-assert.yaml @@ -9,7 +9,8 @@ spec: debug = true databaseInstance: openstack databaseAccount: cinder - rabbitMqClusterName: rabbitmq + messagingBus: + cluster: rabbitmq cinderAPI: replicas: 1 cinderScheduler: diff --git a/test/kuttl/tests/cinder_scale/09-assert.yaml b/test/kuttl/tests/cinder_scale/09-assert.yaml index ae9c96a0a..7a62026ad 100644 --- a/test/kuttl/tests/cinder_scale/09-assert.yaml +++ b/test/kuttl/tests/cinder_scale/09-assert.yaml @@ -9,7 +9,8 @@ spec: debug = true databaseInstance: openstack databaseAccount: cinder - rabbitMqClusterName: rabbitmq + messagingBus: + cluster: rabbitmq cinderAPI: replicas: 0 cinderScheduler: diff --git a/test/kuttl/tests/cinder_scale/10-assert.yaml b/test/kuttl/tests/cinder_scale/10-assert.yaml index ba85590b8..85ca0d32f 100644 --- a/test/kuttl/tests/cinder_scale/10-assert.yaml +++ b/test/kuttl/tests/cinder_scale/10-assert.yaml @@ -9,7 +9,8 @@ spec: debug = true databaseInstance: openstack databaseAccount: cinder - rabbitMqClusterName: rabbitmq + messagingBus: + cluster: rabbitmq cinderAPI: replicas: 3 cinderScheduler: diff --git a/test/kuttl/tests/cinder_scale/11-assert.yaml b/test/kuttl/tests/cinder_scale/11-assert.yaml index 52c4612c1..40806d62c 100644 --- a/test/kuttl/tests/cinder_scale/11-assert.yaml +++ b/test/kuttl/tests/cinder_scale/11-assert.yaml @@ -9,7 +9,8 @@ spec: debug = true databaseInstance: openstack databaseAccount: cinder - rabbitMqClusterName: rabbitmq + messagingBus: + cluster: rabbitmq cinderAPI: replicas: 1 cinderScheduler: