Skip to content

Commit cc9cc6a

Browse files
feat: update code to align with pr comments
1 parent 0a57c86 commit cc9cc6a

File tree

4 files changed

+16
-13
lines changed

4 files changed

+16
-13
lines changed

controllers/rabbitmqcluster_controller.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ func (r *RabbitmqClusterReconciler) Reconcile(ctx context.Context, req ctrl.Requ
213213
}
214214
}
215215
if r.scaleFromZero(current, sts) {
216-
if r.scaleDownFromZero(ctx, rabbitmqCluster, sts) {
216+
if r.scaleFromZeroToBeforeReplicasConfigured(ctx, rabbitmqCluster, sts) {
217217
// return when cluster scale down from zero detected; unsupported operation
218218
return ctrl.Result{}, nil
219219
}
@@ -227,7 +227,6 @@ func (r *RabbitmqClusterReconciler) Reconcile(ctx context.Context, req ctrl.Requ
227227
return ctrl.Result{}, err
228228
}
229229
}
230-
231230
var operationResult controllerutil.OperationResult
232231
err = clientretry.RetryOnConflict(clientretry.DefaultRetry, func() error {
233232
var apiError error
@@ -263,9 +262,9 @@ func (r *RabbitmqClusterReconciler) Reconcile(ctx context.Context, req ctrl.Requ
263262
}
264263
return ctrl.Result{RequeueAfter: requeueAfter}, err
265264
}
266-
267265
// Set ReconcileSuccess to true and update observedGeneration after all reconciliation steps have finished with no error
268266
rabbitmqCluster.Status.ObservedGeneration = rabbitmqCluster.GetGeneration()
267+
269268
r.setReconcileSuccess(ctx, rabbitmqCluster, corev1.ConditionTrue, "Success", "Finish reconciling")
270269

271270
logger.Info("Finished reconciling")

controllers/reconcile_scale_zero.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func (r *RabbitmqClusterReconciler) scaleFromZero(current, sts *appsv1.StatefulS
3131
}
3232

3333
// scaleDownFromZero checks if the current replicas is desired replicas would be greatter than replicas configured before zero state.
34-
func (r *RabbitmqClusterReconciler) scaleDownFromZero(ctx context.Context, cluster *v1beta1.RabbitmqCluster, sts *appsv1.StatefulSet) bool {
34+
func (r *RabbitmqClusterReconciler) scaleFromZeroToBeforeReplicasConfigured(ctx context.Context, cluster *v1beta1.RabbitmqCluster, sts *appsv1.StatefulSet) bool {
3535
var err error
3636
var beforeZeroReplicas int64
3737
desiredReplicas := *sts.Spec.Replicas
@@ -50,9 +50,8 @@ func (r *RabbitmqClusterReconciler) scaleDownFromZero(ctx context.Context, clust
5050
}
5151
return true
5252
}
53-
54-
if desiredReplicas < int32(beforeZeroReplicas) {
55-
msg := fmt.Sprintf("Cluster Scale down not supported; tried to scale cluster from %d nodes to %d nodes", int32(beforeZeroReplicas), desiredReplicas)
53+
if desiredReplicas != int32(beforeZeroReplicas) {
54+
msg := fmt.Sprintf("Cluster Scale from zero to other replicas than before configured not supported; tried to scale cluster from %d nodes to %d nodes", int32(beforeZeroReplicas), desiredReplicas)
5655
reason := "UnsupportedOperation"
5756
err = r.recordEventsAndSetCondition(ctx, cluster, status.ReconcileSuccess, corev1.ConditionFalse, corev1.EventTypeWarning, reason, msg)
5857
if err != nil {
@@ -61,6 +60,7 @@ func (r *RabbitmqClusterReconciler) scaleDownFromZero(ctx context.Context, clust
6160
return true
6261
}
6362
return false
63+
6464
}
6565

6666
// saveReplicasBeforeZero saves the current replicas count in an annotation before scaling down to zero.
@@ -90,9 +90,6 @@ func (r *RabbitmqClusterReconciler) recordEventsAndSetCondition(ctx context.Cont
9090
logger.Error(errors.New(reason), msg)
9191
r.Recorder.Event(cluster, eventType, reason, msg)
9292
cluster.Status.SetCondition(condType, condStatus, reason, msg)
93-
if statusErr := r.Status().Update(ctx, cluster); statusErr != nil {
94-
logger.Error(statusErr, "Failed to update ReconcileSuccess condition state")
95-
}
93+
statusErr = r.Status().Update(ctx, cluster)
9694
return statusErr
97-
9895
}

controllers/reconcile_scale_zero_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ var _ = Describe("Cluster scale from zero to less replicas configured", Ordered,
202202

203203
By("setting 'Warning' events", func() {
204204
Expect(aggregateEventMsgs(ctx, cluster, "UnsupportedOperation")).To(
205-
ContainSubstring("Cluster Scale down not supported"))
205+
ContainSubstring("Cluster Scale from zero to other replicas than before configured not supported"))
206206
})
207207

208208
By("setting ReconcileSuccess to 'false'", func() {
@@ -225,7 +225,7 @@ var _ = Describe("Cluster scale from zero to less replicas configured", Ordered,
225225
return "ReconcileSuccess status: condition not present"
226226
}, 0).Should(Equal("ReconcileSuccess status: False, " +
227227
"with reason: UnsupportedOperation " +
228-
"and message: Cluster Scale down not supported; tried to scale cluster from 2 nodes to 1 nodes"))
228+
"and message: Cluster Scale from zero to other replicas than before configured not supported; tried to scale cluster from 2 nodes to 1 nodes"))
229229
})
230230
})
231231
})

internal/status/all_replicas_ready.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ func AllReplicasReadyCondition(resources []runtime.Object,
4141
if resource.Spec.Replicas != nil {
4242
desiredReplicas = *resource.Spec.Replicas
4343
}
44+
45+
if desiredReplicas == 0 {
46+
condition.Status = corev1.ConditionFalse
47+
condition.Reason = "ScaledToZero"
48+
goto assignLastTransitionTime
49+
}
50+
4451
if desiredReplicas == resource.Status.ReadyReplicas {
4552
condition.Status = corev1.ConditionTrue
4653
condition.Reason = "AllPodsAreReady"

0 commit comments

Comments
 (0)