Skip to content

Commit e2c9f69

Browse files
lmicciniopenshift-merge-bot[bot]
authored andcommitted
Make deprecated parameter optional
1 parent 150d016 commit e2c9f69

19 files changed

Lines changed: 174 additions & 187 deletions

File tree

‎api/bases/watcher.openstack.org_watchers.yaml‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,6 @@ spec:
689689
description: Secret containing prometheus connection parameters
690690
type: string
691691
rabbitMqClusterName:
692-
default: rabbitmq
693692
description: |-
694693
RabbitMQ instance name
695694
Needed to request a transportURL that is created and used in Watcher
@@ -729,7 +728,6 @@ spec:
729728
- databaseInstance
730729
- decisionengineContainerImageURL
731730
- decisionengineServiceTemplate
732-
- rabbitMqClusterName
733731
type: object
734732
status:
735733
description: WatcherStatus defines the observed state of Watcher

‎api/v1beta1/common_types.go‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,11 @@ type WatcherSpecCore struct {
9191
// NotificationsBus configuration (username, vhost, and cluster) for notifications
9292
NotificationsBus *rabbitmqv1.RabbitMqConfig `json:"notificationsBus,omitempty"`
9393

94-
// +kubebuilder:validation:Required
95-
// +kubebuilder:default=rabbitmq
94+
// +kubebuilder:validation:Optional
9695
// RabbitMQ instance name
9796
// Needed to request a transportURL that is created and used in Watcher
9897
// Deprecated: Use MessagingBus.Cluster instead
99-
RabbitMqClusterName *string `json:"rabbitMqClusterName"`
98+
RabbitMqClusterName *string `json:"rabbitMqClusterName,omitempty"`
10099

101100
// +kubebuilder:validation:Optional
102101
// +kubebuilder:default=osp-secret

‎api/v1beta1/watcher_webhook.go‎

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ package v1beta1
1919
import (
2020
"fmt"
2121

22-
rabbitmqv1 "github.com/openstack-k8s-operators/infra-operator/apis/rabbitmq/v1beta1"
2322
topologyv1 "github.com/openstack-k8s-operators/infra-operator/apis/topology/v1beta1"
23+
common_webhook "github.com/openstack-k8s-operators/lib-common/modules/common/webhook"
2424
apierrors "k8s.io/apimachinery/pkg/api/errors"
2525
"k8s.io/apimachinery/pkg/runtime"
2626
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -29,7 +29,6 @@ import (
2929
logf "sigs.k8s.io/controller-runtime/pkg/log"
3030
"sigs.k8s.io/controller-runtime/pkg/webhook"
3131
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
32-
common_webhook "github.com/openstack-k8s-operators/lib-common/modules/common/webhook"
3332
)
3433

3534
// WatcherDefaults -
@@ -67,28 +66,14 @@ func (spec *WatcherSpec) Default() {
6766

6867
// Default - set defaults for this WatcherSpecCore spec.
6968
func (spec *WatcherSpecCore) Default() {
70-
// Apply kubebuilder default for RabbitMqClusterName if not set
71-
if spec.RabbitMqClusterName == nil {
72-
spec.RabbitMqClusterName = ptr.To("rabbitmq")
73-
}
74-
75-
// Default MessagingBus.Cluster from RabbitMqClusterName if not already set
69+
// Default MessagingBus.Cluster if not set
70+
// Migration from deprecated fields is handled by openstack-operator
7671
if spec.MessagingBus.Cluster == "" {
77-
spec.MessagingBus.Cluster = *spec.RabbitMqClusterName
72+
spec.MessagingBus.Cluster = "rabbitmq"
7873
}
7974

80-
// Default NotificationsBus if NotificationsBusInstance is specified
81-
if spec.NotificationsBusInstance != nil && *spec.NotificationsBusInstance != "" {
82-
if spec.NotificationsBus == nil {
83-
// Initialize empty NotificationsBus - credentials will be created dynamically
84-
// to ensure separation from MessagingBus (RPC and notifications should never share credentials)
85-
spec.NotificationsBus = &rabbitmqv1.RabbitMqConfig{}
86-
}
87-
// Default cluster name if not already set
88-
if spec.NotificationsBus.Cluster == "" {
89-
spec.NotificationsBus.Cluster = *spec.NotificationsBusInstance
90-
}
91-
}
75+
// NotificationsBus.Cluster is not defaulted - it must be explicitly set if NotificationsBus is configured
76+
// This ensures users make a conscious choice about which cluster to use for notifications
9277
}
9378

9479
// getDeprecatedFields returns the centralized list of deprecated fields for WatcherSpecCore
@@ -140,7 +125,7 @@ func (r *Watcher) ValidateCreate() (admission.Warnings, error) {
140125
}
141126

142127
if len(allErrs) != 0 {
143-
return nil, apierrors.NewInvalid(
128+
return allWarns, apierrors.NewInvalid(
144129
schema.GroupKind{Group: "watcher.openstack.org", Kind: "Watcher"},
145130
r.Name, allErrs)
146131
}
@@ -233,7 +218,7 @@ func (r *Watcher) ValidateUpdate(old runtime.Object) (admission.Warnings, error)
233218
}
234219

235220
if len(allErrs) != 0 {
236-
return nil, apierrors.NewInvalid(
221+
return allWarns, apierrors.NewInvalid(
237222
schema.GroupKind{Group: "watcher.openstack.org", Kind: "Watcher"},
238223
r.Name, allErrs)
239224
}

‎ci/scenarios/edpm.yml‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,10 @@ cifmw_edpm_prepare_kustomizations:
6363
metadata:
6464
name: controlplane
6565
spec:
66-
notificationsBusInstance: null
6766
notificationsBus:
6867
cluster: rabbitmq-notifications
6968
target:
7069
kind: OpenStackControlPlane
71-
- patch: |-
72-
apiVersion: core.openstack.org/v1beta1
73-
kind: OpenStackControlPlane
74-
metadata:
75-
name: controlplane
76-
spec:
77-
keystone:
78-
template:
79-
rabbitMqClusterName: ""
80-
target:
81-
kind: OpenStackControlPlane
8270
- patch: |-
8371
apiVersion: core.openstack.org/v1beta1
8472
kind: OpenStackControlPlane

‎config/crd/bases/watcher.openstack.org_watchers.yaml‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,6 @@ spec:
689689
description: Secret containing prometheus connection parameters
690690
type: string
691691
rabbitMqClusterName:
692-
default: rabbitmq
693692
description: |-
694693
RabbitMQ instance name
695694
Needed to request a transportURL that is created and used in Watcher
@@ -729,7 +728,6 @@ spec:
729728
- databaseInstance
730729
- decisionengineContainerImageURL
731730
- decisionengineServiceTemplate
732-
- rabbitMqClusterName
733731
type: object
734732
status:
735733
description: WatcherStatus defines the observed state of Watcher

‎internal/controller/watcher_controller.go‎

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package controller
1919
import (
2020
"context"
2121
"fmt"
22+
"strings"
2223
"time"
2324

2425
"github.com/go-logr/logr"
@@ -234,7 +235,6 @@ func (r *WatcherReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re
234235
notificationURLSecret := &corev1.Secret{}
235236

236237
// Determine if notifications are enabled by checking NotificationsBus.Cluster
237-
// (the webhook defaults this from the deprecated NotificationsBusInstance field)
238238
if instance.Spec.NotificationsBus != nil && instance.Spec.NotificationsBus.Cluster != "" {
239239
instance.Status.Conditions.Set(condition.FalseCondition(
240240
watcherv1beta1.WatcherNotificationTransportURLReadyCondition,
@@ -248,6 +248,7 @@ func (r *WatcherReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re
248248

249249
// Append cluster name to the TransportURL name to make it unique when using different clusters
250250
notificationTransportURLName := fmt.Sprintf("%s-watcher-notification-%s", instance.Name, notificationsRabbitMqConfig.Cluster)
251+
251252
notificationURL, op, err := r.ensureMQ(ctx, instance, helper, notificationTransportURLName, notificationsRabbitMqConfig, serviceLabels)
252253
if err != nil {
253254
instance.Status.Conditions.Set(condition.FalseCondition(
@@ -271,6 +272,27 @@ func (r *WatcherReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re
271272
}
272273
instance.Status.Conditions.MarkTrue(watcherv1beta1.WatcherNotificationTransportURLReadyCondition, watcherv1beta1.WatcherNotificationTransportURLReadyMessage)
273274

275+
// Cleanup orphaned notification TransportURLs that don't match the current name
276+
// This happens AFTER successful creation to avoid deleting resources if creation fails
277+
transportURLList := &rabbitmqv1.TransportURLList{}
278+
listOpts := []client.ListOption{
279+
client.InNamespace(instance.Namespace),
280+
}
281+
if err := r.Client.List(ctx, transportURLList, listOpts...); err != nil {
282+
return ctrl.Result{}, err
283+
}
284+
285+
notificationTransportPrefix := fmt.Sprintf("%s-watcher-notification-", instance.Name)
286+
for _, url := range transportURLList.Items {
287+
// Delete notification TransportURLs that don't match the current name
288+
if strings.HasPrefix(url.Name, notificationTransportPrefix) && url.Name != notificationTransportURLName {
289+
err = r.ensureMQDeleted(ctx, instance, url.Name)
290+
if err != nil {
291+
return ctrl.Result{}, err
292+
}
293+
}
294+
}
295+
274296
// NotificationURL Secret
275297
hashNotificationURL, _, notificationSecret, err := ensureSecret(
276298
ctx,
@@ -288,6 +310,27 @@ func (r *WatcherReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re
288310
}
289311
notificationURLSecret = &notificationSecret
290312
_ = op
313+
} else {
314+
instance.Status.Conditions.Remove(watcherv1beta1.WatcherNotificationTransportURLReadyCondition)
315+
316+
// Ensure to delete all notification TransportURLs when notifications are disabled
317+
transportURLList := &rabbitmqv1.TransportURLList{}
318+
listOpts := []client.ListOption{
319+
client.InNamespace(instance.Namespace),
320+
}
321+
if err := r.Client.List(ctx, transportURLList, listOpts...); err != nil {
322+
return ctrl.Result{}, err
323+
}
324+
325+
notificationTransportPrefix := fmt.Sprintf("%s-watcher-notification-", instance.Name)
326+
for _, url := range transportURLList.Items {
327+
if strings.HasPrefix(url.Name, notificationTransportPrefix) {
328+
err = r.ensureMQDeleted(ctx, instance, url.Name)
329+
if err != nil {
330+
return ctrl.Result{}, err
331+
}
332+
}
333+
}
291334
}
292335

293336
// end of Notification TransportURL creation
@@ -1404,3 +1447,27 @@ func (r *WatcherReconciler) findObjectsForSrc(ctx context.Context, src client.Ob
14041447

14051448
return requests
14061449
}
1450+
1451+
func (r *WatcherReconciler) ensureMQDeleted(
1452+
ctx context.Context,
1453+
instance *watcherv1beta1.Watcher,
1454+
transportURLName string,
1455+
) error {
1456+
Log := r.GetLogger(ctx)
1457+
transportURL := &rabbitmqv1.TransportURL{
1458+
ObjectMeta: metav1.ObjectMeta{
1459+
Name: transportURLName,
1460+
Namespace: instance.Namespace,
1461+
},
1462+
}
1463+
1464+
err := r.Client.Delete(ctx, transportURL)
1465+
if err != nil && !k8s_errors.IsNotFound(err) {
1466+
Log.Info(fmt.Sprintf("Could not delete TransportURL %s err: %s", transportURLName, err))
1467+
return err
1468+
}
1469+
1470+
Log.Info("Deleted TransportURL", "name", transportURLName)
1471+
1472+
return nil
1473+
}

‎test/functional/watcher_controller_test.go‎

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ var _ = Describe("Watcher controller with minimal spec values", func() {
5252
Expect(*(Watcher.Spec.DatabaseAccount)).Should(Equal("watcher"))
5353
Expect(*(Watcher.Spec.Secret)).Should(Equal("osp-secret"))
5454
Expect(*(Watcher.Spec.PasswordSelectors.Service)).Should(Equal("WatcherPassword"))
55-
Expect(*(Watcher.Spec.RabbitMqClusterName)).Should(Equal("rabbitmq"))
55+
Expect(Watcher.Spec.MessagingBus.Cluster).Should(Equal("rabbitmq"))
5656
Expect(*(Watcher.Spec.ServiceUser)).Should(Equal("watcher"))
5757
Expect(Watcher.Spec.PreserveJobs).Should(BeFalse())
5858
Expect(Watcher.Spec.APIServiceTemplate.TLS.CaBundleSecretName).Should(Equal(""))
@@ -101,7 +101,7 @@ var _ = Describe("Watcher controller", func() {
101101
Expect(*(Watcher.Spec.DatabaseAccount)).Should(Equal("watcher"))
102102
Expect(*(Watcher.Spec.ServiceUser)).Should(Equal("watcher"))
103103
Expect(*(Watcher.Spec.Secret)).Should(Equal("test-osp-secret"))
104-
Expect(*(Watcher.Spec.RabbitMqClusterName)).Should(Equal("rabbitmq"))
104+
Expect(Watcher.Spec.MessagingBus.Cluster).Should(Equal("rabbitmq"))
105105
Expect(Watcher.Spec.PreserveJobs).Should(BeFalse())
106106
Expect(*(Watcher.Spec.APITimeout)).To(Equal(60))
107107

@@ -711,7 +711,9 @@ var _ = Describe("Watcher controller", func() {
711711
It("should raise an error for empty databaseInstance", func() {
712712
spec := GetDefaultWatcherAPISpec()
713713
spec["databaseInstance"] = ""
714-
spec["rabbitMqClusterName"] = "rabbitmq"
714+
spec["messagingBus"] = map[string]any{
715+
"cluster": "rabbitmq",
716+
}
715717

716718
raw := map[string]any{
717719
"apiVersion": "watcher.openstack.org/v1beta1",
@@ -743,7 +745,9 @@ var _ = Describe("Watcher controller", func() {
743745

744746
spec := GetDefaultWatcherAPISpec()
745747
spec["topologyRef"] = map[string]any{"name": "foo", "namespace": "bar"}
746-
spec["rabbitMqClusterName"] = "rabbitmq"
748+
spec["messagingBus"] = map[string]any{
749+
"cluster": "rabbitmq",
750+
}
747751

748752
raw := map[string]any{
749753
"apiVersion": "watcher.openstack.org/v1beta1",
@@ -771,34 +775,18 @@ var _ = Describe("Watcher controller", func() {
771775
})
772776

773777
When("Watcher is created with empty messagingBus.cluster", func() {
774-
It("should raise an error for empty messagingBus.cluster", func() {
775-
spec := GetDefaultWatcherAPISpec()
776-
spec["rabbitMqClusterName"] = ""
778+
It("should default messagingBus.cluster to rabbitmq", func() {
779+
spec := GetDefaultWatcherSpec()
777780
spec["messagingBus"] = map[string]any{
778781
"cluster": "",
779782
}
783+
DeferCleanup(th.DeleteInstance, CreateWatcher(watcherTest.Instance, spec))
780784

781-
raw := map[string]any{
782-
"apiVersion": "watcher.openstack.org/v1beta1",
783-
"kind": "watcher",
784-
"metadata": map[string]any{
785-
"name": watcherName.Name,
786-
"namespace": watcherName.Namespace,
787-
},
788-
"spec": spec,
789-
}
790-
791-
unstructuredObj := &unstructured.Unstructured{Object: raw}
792-
_, err := controllerutil.CreateOrPatch(
793-
th.Ctx, th.K8sClient, unstructuredObj, func() error { return nil })
794-
Expect(err).To(HaveOccurred())
795-
// Error comes from CRD schema validation (minLength: 1) before webhook validation
796-
Expect(err.Error()).To(
797-
ContainSubstring(
798-
"Watcher.watcher.openstack.org \"watcher\" is invalid: " +
799-
"spec.messagingBus.cluster: Invalid value: \"\": " +
800-
"spec.messagingBus.cluster in body should be at least 1 chars long"),
801-
)
785+
// Webhook should default empty cluster to "rabbitmq"
786+
Eventually(func(g Gomega) {
787+
watcher := GetWatcher(watcherTest.Instance)
788+
g.Expect(watcher.Spec.MessagingBus.Cluster).To(Equal("rabbitmq"))
789+
}, timeout, interval).Should(Succeed())
802790
})
803791
})
804792

@@ -851,7 +839,7 @@ var _ = Describe("Watcher controller", func() {
851839
Expect(*(Watcher.Spec.ServiceUser)).Should(Equal("fakeuser"))
852840
Expect(*(Watcher.Spec.Secret)).Should(Equal("test-osp-secret"))
853841
Expect(Watcher.Spec.PreserveJobs).Should(BeTrue())
854-
Expect(*(Watcher.Spec.RabbitMqClusterName)).Should(Equal("rabbitmq"))
842+
Expect(Watcher.Spec.MessagingBus.Cluster).Should(Equal("rabbitmq"))
855843
Expect(Watcher.Spec.APIServiceTemplate.TLS.CaBundleSecretName).Should(Equal("combined-ca-bundle"))
856844
Expect(Watcher.Spec.CustomServiceConfig).Should(Equal("# Global config"))
857845
Expect(*(Watcher.Spec.PrometheusSecret)).Should(Equal("custom-prometheus-config"))
@@ -1633,7 +1621,9 @@ var _ = Describe("Watcher controller", func() {
16331621
When("Watcher with notification bus instance is created", func() {
16341622
BeforeEach(func() {
16351623
spec := GetDefaultWatcherSpec()
1636-
spec["notificationsBusInstance"] = ptr.To("rabbitmq-notification")
1624+
spec["notificationsBus"] = map[string]any{
1625+
"cluster": "rabbitmq-notification",
1626+
}
16371627
DeferCleanup(th.DeleteInstance, CreateWatcher(watcherTest.Instance, spec))
16381628
DeferCleanup(k8sClient.Delete, ctx, CreateWatcherMessageBusSecret(watcherTest.Instance.Namespace, "rabbitmq-secret"))
16391629
memcachedSpec := memcachedv1.MemcachedSpec{
@@ -1676,8 +1666,8 @@ var _ = Describe("Watcher controller", func() {
16761666

16771667
It("should have the Spec fields with the expected values", func() {
16781668
Watcher := GetWatcher(watcherTest.Instance)
1679-
Expect(*(Watcher.Spec.RabbitMqClusterName)).Should(Equal("rabbitmq"))
1680-
Expect(*(Watcher.Spec.NotificationsBusInstance)).Should(Equal("rabbitmq-notification"))
1669+
Expect(Watcher.Spec.MessagingBus.Cluster).Should(Equal("rabbitmq"))
1670+
Expect(Watcher.Spec.NotificationsBus.Cluster).Should(Equal("rabbitmq-notification"))
16811671
})
16821672

16831673
It("should have the condition WatcherNotificationTransportURLReadyCondition set to false", func() {
@@ -2015,9 +2005,6 @@ var _ = Describe("Watcher controller", func() {
20152005
When("Watcher with custom messagingBus and notificationsBus is created", func() {
20162006
BeforeEach(func() {
20172007
spec := GetDefaultWatcherSpec()
2018-
// Null out deprecated fields before setting new bus fields
2019-
spec["rabbitMqClusterName"] = ""
2020-
spec["notificationsBusInstance"] = ""
20212008
spec["messagingBus"] = map[string]any{
20222009
"cluster": "custom-rabbitmq",
20232010
"user": "custom-rpc-user",

0 commit comments

Comments
 (0)