Skip to content

Commit 041ac0b

Browse files
committed
Make deprecated parameter optional
1 parent 8b30c5e commit 041ac0b

18 files changed

Lines changed: 175 additions & 173 deletions

File tree

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,6 @@ spec:
680680
description: Secret containing prometheus connection parameters
681681
type: string
682682
rabbitMqClusterName:
683-
default: rabbitmq
684683
description: |-
685684
RabbitMQ instance name
686685
Needed to request a transportURL that is created and used in Watcher
@@ -720,7 +719,6 @@ spec:
720719
- databaseInstance
721720
- decisionengineContainerImageURL
722721
- decisionengineServiceTemplate
723-
- rabbitMqClusterName
724722
type: object
725723
status:
726724
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: 9 additions & 22 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,27 +66,15 @@ 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-
}
75+
// Default NotificationsBus.Cluster if NotificationsBus is present but Cluster is empty
76+
if spec.NotificationsBus != nil && spec.NotificationsBus.Cluster == "" {
77+
spec.NotificationsBus.Cluster = "rabbitmq"
9178
}
9279
}
9380

@@ -140,7 +127,7 @@ func (r *Watcher) ValidateCreate() (admission.Warnings, error) {
140127
}
141128

142129
if len(allErrs) != 0 {
143-
return nil, apierrors.NewInvalid(
130+
return allWarns, apierrors.NewInvalid(
144131
schema.GroupKind{Group: "watcher.openstack.org", Kind: "Watcher"},
145132
r.Name, allErrs)
146133
}
@@ -233,7 +220,7 @@ func (r *Watcher) ValidateUpdate(old runtime.Object) (admission.Warnings, error)
233220
}
234221

235222
if len(allErrs) != 0 {
236-
return nil, apierrors.NewInvalid(
223+
return allWarns, apierrors.NewInvalid(
237224
schema.GroupKind{Group: "watcher.openstack.org", Kind: "Watcher"},
238225
r.Name, allErrs)
239226
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,6 @@ spec:
680680
description: Secret containing prometheus connection parameters
681681
type: string
682682
rabbitMqClusterName:
683-
default: rabbitmq
684683
description: |-
685684
RabbitMQ instance name
686685
Needed to request a transportURL that is created and used in Watcher
@@ -720,7 +719,6 @@ spec:
720719
- databaseInstance
721720
- decisionengineContainerImageURL
722721
- decisionengineServiceTemplate
723-
- rabbitMqClusterName
724722
type: object
725723
status:
726724
description: WatcherStatus defines the observed state of Watcher

‎internal/controller/watcher_controller.go‎

Lines changed: 68 additions & 0 deletions
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"
@@ -248,6 +249,28 @@ func (r *WatcherReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re
248249

249250
// Append cluster name to the TransportURL name to make it unique when using different clusters
250251
notificationTransportURLName := fmt.Sprintf("%s-watcher-notification-%s", instance.Name, notificationsRabbitMqConfig.Cluster)
252+
253+
// Cleanup orphaned notification TransportURLs that don't match the current name
254+
// This handles the case where notificationsBus.cluster was changed
255+
transportURLList := &rabbitmqv1.TransportURLList{}
256+
listOpts := []client.ListOption{
257+
client.InNamespace(instance.Namespace),
258+
}
259+
if err := r.Client.List(ctx, transportURLList, listOpts...); err != nil {
260+
return ctrl.Result{}, err
261+
}
262+
263+
notificationTransportPrefix := fmt.Sprintf("%s-watcher-notification-", instance.Name)
264+
for _, url := range transportURLList.Items {
265+
// Delete notification TransportURLs that don't match the current name
266+
if strings.HasPrefix(url.Name, notificationTransportPrefix) && url.Name != notificationTransportURLName {
267+
err = r.ensureMQDeleted(ctx, instance, url.Name)
268+
if err != nil {
269+
return ctrl.Result{}, err
270+
}
271+
}
272+
}
273+
251274
notificationURL, op, err := r.ensureMQ(ctx, instance, helper, notificationTransportURLName, notificationsRabbitMqConfig, serviceLabels)
252275
if err != nil {
253276
instance.Status.Conditions.Set(condition.FalseCondition(
@@ -288,6 +311,27 @@ func (r *WatcherReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re
288311
}
289312
notificationURLSecret = &notificationSecret
290313
_ = op
314+
} else {
315+
instance.Status.Conditions.Remove(watcherv1beta1.WatcherNotificationTransportURLReadyCondition)
316+
317+
// Ensure to delete all notification TransportURLs when notifications are disabled
318+
transportURLList := &rabbitmqv1.TransportURLList{}
319+
listOpts := []client.ListOption{
320+
client.InNamespace(instance.Namespace),
321+
}
322+
if err := r.Client.List(ctx, transportURLList, listOpts...); err != nil {
323+
return ctrl.Result{}, err
324+
}
325+
326+
notificationTransportPrefix := fmt.Sprintf("%s-watcher-notification-", instance.Name)
327+
for _, url := range transportURLList.Items {
328+
if strings.HasPrefix(url.Name, notificationTransportPrefix) {
329+
err = r.ensureMQDeleted(ctx, instance, url.Name)
330+
if err != nil {
331+
return ctrl.Result{}, err
332+
}
333+
}
334+
}
291335
}
292336

293337
// end of Notification TransportURL creation
@@ -1336,3 +1380,27 @@ func (r *WatcherReconciler) findObjectsForSrc(ctx context.Context, src client.Ob
13361380

13371381
return requests
13381382
}
1383+
1384+
func (r *WatcherReconciler) ensureMQDeleted(
1385+
ctx context.Context,
1386+
instance *watcherv1beta1.Watcher,
1387+
transportURLName string,
1388+
) error {
1389+
Log := r.GetLogger(ctx)
1390+
transportURL := &rabbitmqv1.TransportURL{
1391+
ObjectMeta: metav1.ObjectMeta{
1392+
Name: transportURLName,
1393+
Namespace: instance.Namespace,
1394+
},
1395+
}
1396+
1397+
err := r.Client.Delete(ctx, transportURL)
1398+
if err != nil && !k8s_errors.IsNotFound(err) {
1399+
Log.Info(fmt.Sprintf("Could not delete TransportURL %s err: %s", transportURLName, err))
1400+
return err
1401+
}
1402+
1403+
Log.Info("Deleted TransportURL", "name", transportURLName)
1404+
1405+
return nil
1406+
}

‎test/functional/watcher_controller_test.go‎

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

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

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

743745
spec := GetDefaultWatcherAPISpec()
744746
spec["topologyRef"] = map[string]any{"name": "foo", "namespace": "bar"}
745-
spec["rabbitMqClusterName"] = "rabbitmq"
747+
spec["messagingBus"] = map[string]any{
748+
"cluster": "rabbitmq",
749+
}
746750

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

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

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

@@ -850,7 +838,7 @@ var _ = Describe("Watcher controller", func() {
850838
Expect(*(Watcher.Spec.ServiceUser)).Should(Equal("fakeuser"))
851839
Expect(*(Watcher.Spec.Secret)).Should(Equal("test-osp-secret"))
852840
Expect(Watcher.Spec.PreserveJobs).Should(BeTrue())
853-
Expect(*(Watcher.Spec.RabbitMqClusterName)).Should(Equal("rabbitmq"))
841+
Expect(Watcher.Spec.MessagingBus.Cluster).Should(Equal("rabbitmq"))
854842
Expect(Watcher.Spec.APIServiceTemplate.TLS.CaBundleSecretName).Should(Equal("combined-ca-bundle"))
855843
Expect(Watcher.Spec.CustomServiceConfig).Should(Equal("# Global config"))
856844
Expect(*(Watcher.Spec.PrometheusSecret)).Should(Equal("custom-prometheus-config"))
@@ -1632,7 +1620,9 @@ var _ = Describe("Watcher controller", func() {
16321620
When("Watcher with notification bus instance is created", func() {
16331621
BeforeEach(func() {
16341622
spec := GetDefaultWatcherSpec()
1635-
spec["notificationsBusInstance"] = ptr.To("rabbitmq-notification")
1623+
spec["notificationsBus"] = map[string]any{
1624+
"cluster": "rabbitmq-notification",
1625+
}
16361626
DeferCleanup(th.DeleteInstance, CreateWatcher(watcherTest.Instance, spec))
16371627
DeferCleanup(k8sClient.Delete, ctx, CreateWatcherMessageBusSecret(watcherTest.Instance.Namespace, "rabbitmq-secret"))
16381628
memcachedSpec := memcachedv1.MemcachedSpec{
@@ -1675,8 +1665,8 @@ var _ = Describe("Watcher controller", func() {
16751665

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

16821672
It("should have the condition WatcherNotificationTransportURLReadyCondition set to false", func() {
@@ -1762,9 +1752,6 @@ var _ = Describe("Watcher controller", func() {
17621752
When("Watcher with custom messagingBus and notificationsBus is created", func() {
17631753
BeforeEach(func() {
17641754
spec := GetDefaultWatcherSpec()
1765-
// Null out deprecated fields before setting new bus fields
1766-
spec["rabbitMqClusterName"] = ""
1767-
spec["notificationsBusInstance"] = ""
17681755
spec["messagingBus"] = map[string]any{
17691756
"cluster": "custom-rabbitmq",
17701757
"user": "custom-rpc-user",

0 commit comments

Comments
 (0)