|
| 1 | +package controllers_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + . "github.com/onsi/ginkgo/v2" |
| 8 | + . "github.com/onsi/gomega" |
| 9 | + rabbitmqv1beta1 "github.com/rabbitmq/cluster-operator/v2/api/v1beta1" |
| 10 | + "github.com/rabbitmq/cluster-operator/v2/internal/status" |
| 11 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 12 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 13 | + "k8s.io/apimachinery/pkg/types" |
| 14 | + "k8s.io/utils/ptr" |
| 15 | + runtimeClient "sigs.k8s.io/controller-runtime/pkg/client" |
| 16 | +) |
| 17 | + |
| 18 | +var _ = Describe("Cluster scale to zero", func() { |
| 19 | + var ( |
| 20 | + cluster *rabbitmqv1beta1.RabbitmqCluster |
| 21 | + defaultNamespace = "default" |
| 22 | + ctx = context.Background() |
| 23 | + ) |
| 24 | + |
| 25 | + AfterEach(func() { |
| 26 | + Expect(client.Delete(ctx, cluster)).To(Succeed()) |
| 27 | + waitForClusterDeletion(ctx, cluster, client) |
| 28 | + Eventually(func() bool { |
| 29 | + rmq := &rabbitmqv1beta1.RabbitmqCluster{} |
| 30 | + err := client.Get(ctx, types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace}, rmq) |
| 31 | + return apierrors.IsNotFound(err) |
| 32 | + }).Should(BeTrue()) |
| 33 | + }) |
| 34 | + |
| 35 | + It("scale to zero", func() { |
| 36 | + By("update statefulSet replicas to zero", func() { |
| 37 | + cluster = &rabbitmqv1beta1.RabbitmqCluster{ |
| 38 | + ObjectMeta: metav1.ObjectMeta{ |
| 39 | + Name: "rabbitmq-to-zero", |
| 40 | + Namespace: defaultNamespace, |
| 41 | + }, |
| 42 | + Spec: rabbitmqv1beta1.RabbitmqClusterSpec{ |
| 43 | + Replicas: ptr.To(int32(2)), |
| 44 | + }, |
| 45 | + } |
| 46 | + Expect(client.Create(ctx, cluster)).To(Succeed()) |
| 47 | + waitForClusterCreation(ctx, cluster, client) |
| 48 | + |
| 49 | + Expect(updateWithRetry(cluster, func(r *rabbitmqv1beta1.RabbitmqCluster) { |
| 50 | + r.Spec.Replicas = ptr.To(int32(0)) |
| 51 | + })).To(Succeed()) |
| 52 | + |
| 53 | + Eventually(func() int32 { |
| 54 | + sts, err := clientSet.AppsV1().StatefulSets(defaultNamespace).Get(ctx, cluster.ChildResourceName("server"), metav1.GetOptions{}) |
| 55 | + Expect(err).NotTo(HaveOccurred()) |
| 56 | + return *sts.Spec.Replicas |
| 57 | + }, 10, 1).Should(Equal(int32(0))) |
| 58 | + |
| 59 | + }) |
| 60 | + |
| 61 | + By("setting ReconcileSuccess to 'true'", func() { |
| 62 | + Eventually(func() string { |
| 63 | + rabbit := &rabbitmqv1beta1.RabbitmqCluster{} |
| 64 | + Expect(client.Get(ctx, runtimeClient.ObjectKey{ |
| 65 | + Name: cluster.Name, |
| 66 | + Namespace: defaultNamespace, |
| 67 | + }, rabbit)).To(Succeed()) |
| 68 | + |
| 69 | + for i := range rabbit.Status.Conditions { |
| 70 | + if rabbit.Status.Conditions[i].Type == status.ReconcileSuccess { |
| 71 | + return fmt.Sprintf( |
| 72 | + "ReconcileSuccess status: %s, with reason: %s and message: %s", |
| 73 | + rabbit.Status.Conditions[i].Status, |
| 74 | + rabbit.Status.Conditions[i].Reason, |
| 75 | + rabbit.Status.Conditions[i].Message) |
| 76 | + } |
| 77 | + } |
| 78 | + return "ReconcileSuccess status: condition not present" |
| 79 | + }, 0).Should(Equal("ReconcileSuccess status: True, " + |
| 80 | + "with reason: Success " + |
| 81 | + "and message: Finish reconciling")) |
| 82 | + }) |
| 83 | + }) |
| 84 | +}) |
| 85 | + |
| 86 | +var _ = Describe("Cluster scale from zero", func() { |
| 87 | + var ( |
| 88 | + cluster *rabbitmqv1beta1.RabbitmqCluster |
| 89 | + defaultNamespace = "default" |
| 90 | + ctx = context.Background() |
| 91 | + ) |
| 92 | + |
| 93 | + AfterEach(func() { |
| 94 | + Expect(client.Delete(ctx, cluster)).To(Succeed()) |
| 95 | + waitForClusterDeletion(ctx, cluster, client) |
| 96 | + Eventually(func() bool { |
| 97 | + rmq := &rabbitmqv1beta1.RabbitmqCluster{} |
| 98 | + err := client.Get(ctx, types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace}, rmq) |
| 99 | + return apierrors.IsNotFound(err) |
| 100 | + }).Should(BeTrue()) |
| 101 | + }) |
| 102 | + |
| 103 | + It("scale from zero", func() { |
| 104 | + By("update statefulSet replicas from zero", func() { |
| 105 | + cluster = &rabbitmqv1beta1.RabbitmqCluster{ |
| 106 | + ObjectMeta: metav1.ObjectMeta{ |
| 107 | + Name: "rabbitmq-from-zero", |
| 108 | + Namespace: defaultNamespace, |
| 109 | + Annotations: map[string]string{ |
| 110 | + "rabbitmq.com/before-zero-replicas-configured": "2", |
| 111 | + }, |
| 112 | + }, |
| 113 | + Spec: rabbitmqv1beta1.RabbitmqClusterSpec{ |
| 114 | + Replicas: ptr.To(int32(0)), |
| 115 | + }, |
| 116 | + } |
| 117 | + Expect(client.Create(ctx, cluster)).To(Succeed()) |
| 118 | + waitForClusterCreation(ctx, cluster, client) |
| 119 | + |
| 120 | + Expect(updateWithRetry(cluster, func(r *rabbitmqv1beta1.RabbitmqCluster) { |
| 121 | + r.Spec.Replicas = ptr.To(int32(2)) |
| 122 | + })).To(Succeed()) |
| 123 | + |
| 124 | + Eventually(func() int32 { |
| 125 | + sts, err := clientSet.AppsV1().StatefulSets(defaultNamespace).Get(ctx, cluster.ChildResourceName("server"), metav1.GetOptions{}) |
| 126 | + Expect(err).NotTo(HaveOccurred()) |
| 127 | + return *sts.Spec.Replicas |
| 128 | + }, 10, 1).Should(Equal(int32(2))) |
| 129 | + |
| 130 | + }) |
| 131 | + |
| 132 | + By("setting ReconcileSuccess to 'true'", func() { |
| 133 | + Eventually(func() string { |
| 134 | + rabbit := &rabbitmqv1beta1.RabbitmqCluster{} |
| 135 | + Expect(client.Get(ctx, runtimeClient.ObjectKey{ |
| 136 | + Name: cluster.Name, |
| 137 | + Namespace: defaultNamespace, |
| 138 | + }, rabbit)).To(Succeed()) |
| 139 | + |
| 140 | + for i := range rabbit.Status.Conditions { |
| 141 | + if rabbit.Status.Conditions[i].Type == status.ReconcileSuccess { |
| 142 | + return fmt.Sprintf( |
| 143 | + "ReconcileSuccess status: %s, with reason: %s and message: %s", |
| 144 | + rabbit.Status.Conditions[i].Status, |
| 145 | + rabbit.Status.Conditions[i].Reason, |
| 146 | + rabbit.Status.Conditions[i].Message) |
| 147 | + } |
| 148 | + } |
| 149 | + return "ReconcileSuccess status: condition not present" |
| 150 | + }, 0).Should(Equal("ReconcileSuccess status: True, " + |
| 151 | + "with reason: Success " + |
| 152 | + "and message: Finish reconciling")) |
| 153 | + }) |
| 154 | + }) |
| 155 | +}) |
| 156 | + |
| 157 | +var _ = Describe("Cluster scale from zero to less replicas configured", Ordered, func() { |
| 158 | + var ( |
| 159 | + cluster *rabbitmqv1beta1.RabbitmqCluster |
| 160 | + defaultNamespace = "default" |
| 161 | + ctx = context.Background() |
| 162 | + ) |
| 163 | + |
| 164 | + AfterEach(func() { |
| 165 | + Expect(client.Delete(ctx, cluster)).To(Succeed()) |
| 166 | + waitForClusterDeletion(ctx, cluster, client) |
| 167 | + Eventually(func() bool { |
| 168 | + rmq := &rabbitmqv1beta1.RabbitmqCluster{} |
| 169 | + err := client.Get(ctx, types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace}, rmq) |
| 170 | + return apierrors.IsNotFound(err) |
| 171 | + }).Should(BeTrue()) |
| 172 | + }) |
| 173 | + |
| 174 | + It("scale from zero to less replicas", func() { |
| 175 | + By("update statefulSet replicas from zero", func() { |
| 176 | + cluster = &rabbitmqv1beta1.RabbitmqCluster{ |
| 177 | + ObjectMeta: metav1.ObjectMeta{ |
| 178 | + Name: "rabbitmq-from-zero-to-less", |
| 179 | + Namespace: defaultNamespace, |
| 180 | + Annotations: map[string]string{ |
| 181 | + "rabbitmq.com/before-zero-replicas-configured": "2", |
| 182 | + }, |
| 183 | + }, |
| 184 | + Spec: rabbitmqv1beta1.RabbitmqClusterSpec{ |
| 185 | + Replicas: ptr.To(int32(0)), |
| 186 | + }, |
| 187 | + } |
| 188 | + Expect(client.Create(ctx, cluster)).To(Succeed()) |
| 189 | + waitForClusterCreation(ctx, cluster, client) |
| 190 | + |
| 191 | + Expect(updateWithRetry(cluster, func(r *rabbitmqv1beta1.RabbitmqCluster) { |
| 192 | + r.Spec.Replicas = ptr.To(int32(1)) |
| 193 | + })).To(Succeed()) |
| 194 | + |
| 195 | + Consistently(func() int32 { |
| 196 | + sts, err := clientSet.AppsV1().StatefulSets(defaultNamespace).Get(ctx, cluster.ChildResourceName("server"), metav1.GetOptions{}) |
| 197 | + Expect(err).NotTo(HaveOccurred()) |
| 198 | + return *sts.Spec.Replicas |
| 199 | + }, 10, 1).Should(Equal(int32(0))) |
| 200 | + |
| 201 | + }) |
| 202 | + |
| 203 | + By("setting 'Warning' events", func() { |
| 204 | + Expect(aggregateEventMsgs(ctx, cluster, "UnsupportedOperation")).To( |
| 205 | + ContainSubstring("Unsupported operation")) |
| 206 | + }) |
| 207 | + |
| 208 | + By("setting ReconcileSuccess to 'false'", func() { |
| 209 | + Eventually(func() string { |
| 210 | + rabbit := &rabbitmqv1beta1.RabbitmqCluster{} |
| 211 | + Expect(client.Get(ctx, runtimeClient.ObjectKey{ |
| 212 | + Name: cluster.Name, |
| 213 | + Namespace: defaultNamespace, |
| 214 | + }, rabbit)).To(Succeed()) |
| 215 | + |
| 216 | + for i := range rabbit.Status.Conditions { |
| 217 | + if rabbit.Status.Conditions[i].Type == status.ReconcileSuccess { |
| 218 | + return fmt.Sprintf( |
| 219 | + "ReconcileSuccess status: %s, with reason: %s and message: %s", |
| 220 | + rabbit.Status.Conditions[i].Status, |
| 221 | + rabbit.Status.Conditions[i].Reason, |
| 222 | + rabbit.Status.Conditions[i].Message) |
| 223 | + } |
| 224 | + } |
| 225 | + return "ReconcileSuccess status: condition not present" |
| 226 | + }, 0).Should(Equal("ReconcileSuccess status: False, " + |
| 227 | + "with reason: UnsupportedOperation " + |
| 228 | + "and message: Unsupported operation; when scaling from zero, you can only restore the previous number of replicas (2)")) |
| 229 | + }) |
| 230 | + }) |
| 231 | +}) |
0 commit comments