|
| 1 | +package controllers_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "time" |
| 5 | + |
| 6 | + "k8s.io/apimachinery/pkg/types" |
| 7 | + "k8s.io/utils/pointer" |
| 8 | + |
| 9 | + appsv1 "k8s.io/api/apps/v1" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + |
| 12 | + . "github.com/onsi/ginkgo" |
| 13 | + . "github.com/onsi/gomega" |
| 14 | + rabbitmqv1beta1 "github.com/rabbitmq/cluster-operator/api/v1beta1" |
| 15 | +) |
| 16 | + |
| 17 | +var _ = Describe("Reconcile post deploy", func() { |
| 18 | + var ( |
| 19 | + cluster *rabbitmqv1beta1.RabbitmqCluster |
| 20 | + annotations map[string]string |
| 21 | + defaultNamespace = "default" |
| 22 | + ) |
| 23 | + |
| 24 | + BeforeEach(func() { |
| 25 | + annotations = map[string]string{} |
| 26 | + }) |
| 27 | + |
| 28 | + AfterEach(func() { |
| 29 | + Expect(client.Delete(ctx, cluster)).To(Succeed()) |
| 30 | + waitForClusterDeletion(ctx, cluster, client) |
| 31 | + }) |
| 32 | + |
| 33 | + When("cluster is created", func() { |
| 34 | + var sts *appsv1.StatefulSet |
| 35 | + BeforeEach(func() { |
| 36 | + cluster = &rabbitmqv1beta1.RabbitmqCluster{ |
| 37 | + ObjectMeta: metav1.ObjectMeta{ |
| 38 | + Name: "rabbitmq-feature-flags", |
| 39 | + Namespace: defaultNamespace, |
| 40 | + }, |
| 41 | + } |
| 42 | + Expect(client.Create(ctx, cluster)).To(Succeed()) |
| 43 | + waitForClusterCreation(ctx, cluster, client) |
| 44 | + }) |
| 45 | + |
| 46 | + It("enables all feature flags", func() { |
| 47 | + By("annotating that StatefulSet got created") |
| 48 | + sts = statefulSet(ctx, cluster) |
| 49 | + value := sts.ObjectMeta.Annotations["rabbitmq.com/createdAt"] |
| 50 | + _, err := time.Parse(time.RFC3339, value) |
| 51 | + Expect(err).NotTo(HaveOccurred(), "annotation rabbitmq.com/createdAt was not a valid RFC3339 timestamp") |
| 52 | + |
| 53 | + By("enabling all feature flags once all Pods are up, and removing the annotation") |
| 54 | + sts.Status.Replicas = 1 |
| 55 | + sts.Status.ReadyReplicas = 1 |
| 56 | + Expect(client.Status().Update(ctx, sts)).To(Succeed()) |
| 57 | + Eventually(func() map[string]string { |
| 58 | + Expect(client.Get(ctx, types.NamespacedName{Namespace: cluster.Namespace, Name: cluster.ChildResourceName("server")}, sts)).To(Succeed()) |
| 59 | + return sts.ObjectMeta.Annotations |
| 60 | + }, 5).ShouldNot(HaveKey("rabbitmq.com/createdAt")) |
| 61 | + Expect(fakeExecutor.ExecutedCommands()).To(ContainElement(command{"bash", "-c", |
| 62 | + "set -eo pipefail; rabbitmqctl --silent list_feature_flags | (grep disabled || true) | cut -f 1 | xargs -r -n1 rabbitmqctl enable_feature_flag"})) |
| 63 | + }) |
| 64 | + }) |
| 65 | + |
| 66 | + When("the cluster is configured to run post-deploy steps", func() { |
| 67 | + BeforeEach(func() { |
| 68 | + cluster = &rabbitmqv1beta1.RabbitmqCluster{ |
| 69 | + ObjectMeta: metav1.ObjectMeta{ |
| 70 | + Name: "rabbitmq-three", |
| 71 | + Namespace: defaultNamespace, |
| 72 | + }, |
| 73 | + Spec: rabbitmqv1beta1.RabbitmqClusterSpec{ |
| 74 | + Replicas: pointer.Int32Ptr(3), |
| 75 | + }, |
| 76 | + } |
| 77 | + Expect(client.Create(ctx, cluster)).To(Succeed()) |
| 78 | + waitForClusterCreation(ctx, cluster, client) |
| 79 | + }) |
| 80 | + When("the cluster is updated", func() { |
| 81 | + var sts *appsv1.StatefulSet |
| 82 | + |
| 83 | + BeforeEach(func() { |
| 84 | + sts = statefulSet(ctx, cluster) |
| 85 | + sts.Status.Replicas = 3 |
| 86 | + sts.Status.CurrentReplicas = 2 |
| 87 | + sts.Status.CurrentRevision = "some-old-revision" |
| 88 | + sts.Status.UpdatedReplicas = 1 |
| 89 | + sts.Status.UpdateRevision = "some-new-revision" |
| 90 | + |
| 91 | + Expect(client.Status().Update(ctx, sts)).To(Succeed()) |
| 92 | + }) |
| 93 | + |
| 94 | + It("triggers the controller to run rabbitmq-queues rebalance all", func() { |
| 95 | + By("setting an annotation on the CR") |
| 96 | + Eventually(func() map[string]string { |
| 97 | + rmq := &rabbitmqv1beta1.RabbitmqCluster{} |
| 98 | + Expect(client.Get(ctx, types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace}, rmq)).To(Succeed()) |
| 99 | + annotations = rmq.ObjectMeta.Annotations |
| 100 | + return annotations |
| 101 | + }, 5).Should(HaveKey("rabbitmq.com/queueRebalanceNeededAt")) |
| 102 | + _, err := time.Parse(time.RFC3339, annotations["rabbitmq.com/queueRebalanceNeededAt"]) |
| 103 | + Expect(err).NotTo(HaveOccurred(), "annotation rabbitmq.com/queueRebalanceNeededAt was not a valid RFC3339 timestamp") |
| 104 | + |
| 105 | + By("not removing the annotation when all replicas are updated but not yet ready") |
| 106 | + sts.Status.CurrentReplicas = 3 |
| 107 | + sts.Status.CurrentRevision = "some-new-revision" |
| 108 | + sts.Status.UpdatedReplicas = 3 |
| 109 | + sts.Status.UpdateRevision = "some-new-revision" |
| 110 | + sts.Status.ReadyReplicas = 2 |
| 111 | + Expect(client.Status().Update(ctx, sts)).To(Succeed()) |
| 112 | + Eventually(func() map[string]string { |
| 113 | + rmq := &rabbitmqv1beta1.RabbitmqCluster{} |
| 114 | + err := client.Get(ctx, types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace}, rmq) |
| 115 | + Expect(err).To(BeNil()) |
| 116 | + annotations = rmq.ObjectMeta.Annotations |
| 117 | + return annotations |
| 118 | + }, 5).Should(HaveKey("rabbitmq.com/queueRebalanceNeededAt")) |
| 119 | + Expect(fakeExecutor.ExecutedCommands()).NotTo(ContainElement(command{"sh", "-c", "rabbitmq-queues rebalance all"})) |
| 120 | + _, err = time.Parse(time.RFC3339, annotations["rabbitmq.com/queueRebalanceNeededAt"]) |
| 121 | + Expect(err).NotTo(HaveOccurred(), "Annotation rabbitmq.com/queueRebalanceNeededAt was not a valid RFC3339 timestamp") |
| 122 | + |
| 123 | + By("removing the annotation once all Pods are up, and triggering the queue rebalance") |
| 124 | + sts.Status.ReadyReplicas = 3 |
| 125 | + Expect(client.Status().Update(ctx, sts)).To(Succeed()) |
| 126 | + Eventually(func() map[string]string { |
| 127 | + rmq := &rabbitmqv1beta1.RabbitmqCluster{} |
| 128 | + err := client.Get(ctx, types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace}, rmq) |
| 129 | + Expect(err).To(BeNil()) |
| 130 | + return rmq.ObjectMeta.Annotations |
| 131 | + }, 5).ShouldNot(HaveKey("rabbitmq.com/queueRebalanceNeededAt")) |
| 132 | + Expect(fakeExecutor.ExecutedCommands()).To(ContainElement(command{"sh", "-c", "rabbitmq-queues rebalance all"})) |
| 133 | + }) |
| 134 | + }) |
| 135 | + }) |
| 136 | + |
| 137 | + When("the cluster is not configured to run post-deploy steps", func() { |
| 138 | + BeforeEach(func() { |
| 139 | + cluster = &rabbitmqv1beta1.RabbitmqCluster{ |
| 140 | + ObjectMeta: metav1.ObjectMeta{ |
| 141 | + Name: "rabbitmq-three-no-post-deploy", |
| 142 | + Namespace: defaultNamespace, |
| 143 | + }, |
| 144 | + Spec: rabbitmqv1beta1.RabbitmqClusterSpec{ |
| 145 | + Replicas: pointer.Int32Ptr(3), |
| 146 | + SkipPostDeploySteps: true, |
| 147 | + }, |
| 148 | + } |
| 149 | + Expect(client.Create(ctx, cluster)).To(Succeed()) |
| 150 | + waitForClusterCreation(ctx, cluster, client) |
| 151 | + }) |
| 152 | + When("the cluster is updated", func() { |
| 153 | + var sts *appsv1.StatefulSet |
| 154 | + |
| 155 | + BeforeEach(func() { |
| 156 | + sts = statefulSet(ctx, cluster) |
| 157 | + sts.Status.Replicas = 3 |
| 158 | + sts.Status.CurrentReplicas = 2 |
| 159 | + sts.Status.CurrentRevision = "some-old-revision" |
| 160 | + sts.Status.UpdatedReplicas = 1 |
| 161 | + sts.Status.UpdateRevision = "some-new-revision" |
| 162 | + |
| 163 | + Expect(client.Status().Update(ctx, sts)).To(Succeed()) |
| 164 | + }) |
| 165 | + |
| 166 | + It("does not trigger the controller to run rabbitmq-queues rebalance all", func() { |
| 167 | + By("never setting the annotation on the CR", func() { |
| 168 | + Consistently(func() map[string]string { |
| 169 | + rmq := &rabbitmqv1beta1.RabbitmqCluster{} |
| 170 | + err := client.Get(ctx, types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace}, rmq) |
| 171 | + Expect(err).To(BeNil()) |
| 172 | + return rmq.ObjectMeta.Annotations |
| 173 | + }, 5).ShouldNot(HaveKey("rabbitmq.com/queueRebalanceNeededAt")) |
| 174 | + }) |
| 175 | + |
| 176 | + By("not running the rebalance command once all nodes are up") |
| 177 | + sts.Status.CurrentReplicas = 3 |
| 178 | + sts.Status.CurrentRevision = "some-new-revision" |
| 179 | + sts.Status.UpdatedReplicas = 3 |
| 180 | + sts.Status.UpdateRevision = "some-new-revision" |
| 181 | + sts.Status.ReadyReplicas = 3 |
| 182 | + Expect(client.Status().Update(ctx, sts)).To(Succeed()) |
| 183 | + Consistently(func() map[string]string { |
| 184 | + rmq := &rabbitmqv1beta1.RabbitmqCluster{} |
| 185 | + err := client.Get(ctx, types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace}, rmq) |
| 186 | + Expect(err).To(BeNil()) |
| 187 | + return rmq.ObjectMeta.Annotations |
| 188 | + }, 5).ShouldNot(HaveKey("rabbitmq.com/queueRebalanceNeededAt")) |
| 189 | + Expect(fakeExecutor.ExecutedCommands()).NotTo(ContainElement(command{"sh", "-c", "rabbitmq-queues rebalance all"})) |
| 190 | + }) |
| 191 | + }) |
| 192 | + }) |
| 193 | + |
| 194 | + When("the cluster is a single node cluster", func() { |
| 195 | + BeforeEach(func() { |
| 196 | + cluster = &rabbitmqv1beta1.RabbitmqCluster{ |
| 197 | + ObjectMeta: metav1.ObjectMeta{ |
| 198 | + Name: "rabbitmq-one-rebalance", |
| 199 | + Namespace: defaultNamespace, |
| 200 | + }, |
| 201 | + Spec: rabbitmqv1beta1.RabbitmqClusterSpec{ |
| 202 | + Replicas: pointer.Int32Ptr(1), |
| 203 | + SkipPostDeploySteps: false, |
| 204 | + }, |
| 205 | + } |
| 206 | + Expect(client.Create(ctx, cluster)).To(Succeed()) |
| 207 | + waitForClusterCreation(ctx, cluster, client) |
| 208 | + }) |
| 209 | + When("the cluster is updated", func() { |
| 210 | + var sts *appsv1.StatefulSet |
| 211 | + |
| 212 | + BeforeEach(func() { |
| 213 | + sts = statefulSet(ctx, cluster) |
| 214 | + sts.Status.Replicas = 1 |
| 215 | + sts.Status.CurrentReplicas = 1 |
| 216 | + sts.Status.CurrentRevision = "some-old-revision" |
| 217 | + sts.Status.UpdatedReplicas = 0 |
| 218 | + sts.Status.UpdateRevision = "some-new-revision" |
| 219 | + sts.Status.ReadyReplicas = 0 |
| 220 | + |
| 221 | + Expect(client.Status().Update(ctx, sts)).To(Succeed()) |
| 222 | + }) |
| 223 | + |
| 224 | + It("does not trigger the controller to run rabbitmq-queues rebalance all", func() { |
| 225 | + By("never setting the annotation on the CR") |
| 226 | + Consistently(func() map[string]string { |
| 227 | + rmq := &rabbitmqv1beta1.RabbitmqCluster{} |
| 228 | + err := client.Get(ctx, types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace}, rmq) |
| 229 | + Expect(err).To(BeNil()) |
| 230 | + return rmq.ObjectMeta.Annotations |
| 231 | + }, 5).ShouldNot(HaveKey("rabbitmq.com/queueRebalanceNeededAt")) |
| 232 | + |
| 233 | + By("not running the rebalance command once all nodes are up") |
| 234 | + sts.Status.CurrentReplicas = 1 |
| 235 | + sts.Status.CurrentRevision = "some-new-revision" |
| 236 | + sts.Status.UpdatedReplicas = 1 |
| 237 | + sts.Status.UpdateRevision = "some-new-revision" |
| 238 | + sts.Status.ReadyReplicas = 1 |
| 239 | + Expect(client.Status().Update(ctx, sts)).To(Succeed()) |
| 240 | + Consistently(func() map[string]string { |
| 241 | + rmq := &rabbitmqv1beta1.RabbitmqCluster{} |
| 242 | + err := client.Get(ctx, types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace}, rmq) |
| 243 | + Expect(err).To(BeNil()) |
| 244 | + return rmq.ObjectMeta.Annotations |
| 245 | + }, 5).ShouldNot(HaveKey("rabbitmq.com/queueRebalanceNeededAt")) |
| 246 | + Expect(fakeExecutor.ExecutedCommands()).NotTo(ContainElement(command{"sh", "-c", "rabbitmq-queues rebalance all"})) |
| 247 | + }) |
| 248 | + }) |
| 249 | + }) |
| 250 | +}) |
0 commit comments