Skip to content

Commit aae58d2

Browse files
feat: add changes asked in pr
1 parent 1037a6a commit aae58d2

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

controllers/rabbitmqcluster_controller.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func (r *RabbitmqClusterReconciler) Reconcile(ctx context.Context, req ctrl.Requ
201201
if err := builder.Update(sts); err != nil {
202202
return ctrl.Result{}, err
203203
}
204-
if r.scaleToZero(current, sts) {
204+
if ScaleToZero(current, sts) {
205205
err := r.saveReplicasBeforeZero(ctx, rabbitmqCluster, current)
206206
if err != nil {
207207
return ctrl.Result{}, err
@@ -212,7 +212,7 @@ func (r *RabbitmqClusterReconciler) Reconcile(ctx context.Context, req ctrl.Requ
212212
return ctrl.Result{}, nil
213213
}
214214
}
215-
if r.scaleFromZero(current, sts) {
215+
if ScaleFromZero(current, sts) {
216216
if r.scaleFromZeroToBeforeReplicasConfigured(ctx, rabbitmqCluster, sts) {
217217
// return when cluster scale down from zero detected; unsupported operation
218218
return ctrl.Result{}, nil
@@ -226,6 +226,7 @@ func (r *RabbitmqClusterReconciler) Reconcile(ctx context.Context, req ctrl.Requ
226226
r.setReconcileSuccess(ctx, rabbitmqCluster, corev1.ConditionFalse, "FailedReconcilePVC", err.Error())
227227
return ctrl.Result{}, err
228228
}
229+
229230
}
230231
var operationResult controllerutil.OperationResult
231232
err = clientretry.RetryOnConflict(clientretry.DefaultRetry, func() error {
@@ -262,9 +263,9 @@ func (r *RabbitmqClusterReconciler) Reconcile(ctx context.Context, req ctrl.Requ
262263
}
263264
return ctrl.Result{RequeueAfter: requeueAfter}, err
264265
}
266+
265267
// Set ReconcileSuccess to true and update observedGeneration after all reconciliation steps have finished with no error
266268
rabbitmqCluster.Status.ObservedGeneration = rabbitmqCluster.GetGeneration()
267-
268269
r.setReconcileSuccess(ctx, rabbitmqCluster, corev1.ConditionTrue, "Success", "Finish reconciling")
269270

270271
logger.Info("Finished reconciling")

controllers/reconcile_scale_zero.go

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package controllers
22

33
import (
44
"context"
5-
"errors"
65
"fmt"
76
"strconv"
87

@@ -16,22 +15,23 @@ import (
1615

1716
const beforeZeroReplicasConfigured = "rabbitmq.com/before-zero-replicas-configured"
1817

19-
// scaleToZero checks if the desired replicas is zero and the current replicas is not zero.
20-
func (r *RabbitmqClusterReconciler) scaleToZero(current, sts *appsv1.StatefulSet) bool {
18+
// ScaleToZero checks if the desired replicas is zero and the current replicas is not zero.
19+
func ScaleToZero(current, sts *appsv1.StatefulSet) bool {
2120
currentReplicas := *current.Spec.Replicas
2221
desiredReplicas := *sts.Spec.Replicas
2322
return desiredReplicas == 0 && currentReplicas > 0
2423
}
2524

26-
// scaleFromZero checks if the current replicas is zero and the desired replicas is greater than zero.
27-
func (r *RabbitmqClusterReconciler) scaleFromZero(current, sts *appsv1.StatefulSet) bool {
25+
// ScaleFromZero checks if the current replicas is zero and the desired replicas is greater than zero.
26+
func ScaleFromZero(current, sts *appsv1.StatefulSet) bool {
2827
currentReplicas := *current.Spec.Replicas
2928
desiredReplicas := *sts.Spec.Replicas
3029
return currentReplicas == 0 && desiredReplicas > 0
3130
}
3231

3332
// scaleDownFromZero checks if the current replicas is desired replicas would be greatter than replicas configured before zero state.
3433
func (r *RabbitmqClusterReconciler) scaleFromZeroToBeforeReplicasConfigured(ctx context.Context, cluster *v1beta1.RabbitmqCluster, sts *appsv1.StatefulSet) bool {
34+
logger := ctrl.LoggerFrom(ctx)
3535
var err error
3636
var beforeZeroReplicas int64
3737
desiredReplicas := *sts.Spec.Replicas
@@ -46,7 +46,7 @@ func (r *RabbitmqClusterReconciler) scaleFromZeroToBeforeReplicasConfigured(ctx
4646
reason := "TransformErrorOperation"
4747
err = r.recordEventsAndSetCondition(ctx, cluster, status.ReconcileSuccess, corev1.ConditionFalse, corev1.EventTypeWarning, reason, msg)
4848
if err != nil {
49-
return true
49+
logger.V(1).Info(err.Error())
5050
}
5151
return true
5252
}
@@ -55,7 +55,7 @@ func (r *RabbitmqClusterReconciler) scaleFromZeroToBeforeReplicasConfigured(ctx
5555
reason := "UnsupportedOperation"
5656
err = r.recordEventsAndSetCondition(ctx, cluster, status.ReconcileSuccess, corev1.ConditionFalse, corev1.EventTypeWarning, reason, msg)
5757
if err != nil {
58-
return true
58+
logger.V(1).Info(err.Error())
5959
}
6060
return true
6161
}
@@ -66,15 +66,13 @@ func (r *RabbitmqClusterReconciler) scaleFromZeroToBeforeReplicasConfigured(ctx
6666
// saveReplicasBeforeZero saves the current replicas count in an annotation before scaling down to zero.
6767
// This is used to prevent scaling down when the cluster change from zero replicas to a number less than the saved replicas count.
6868
func (r *RabbitmqClusterReconciler) saveReplicasBeforeZero(ctx context.Context, cluster *v1beta1.RabbitmqCluster, current *appsv1.StatefulSet) error {
69-
var err error
7069
currentReplicas := *current.Spec.Replicas
7170
logger := ctrl.LoggerFrom(ctx)
72-
msg := "Cluster Scale down to 0 replicas."
71+
msg := "Cluster Scale down to 0 replicas"
7372
reason := "ScaleDownToZero"
7473
logger.Info(msg)
75-
err = r.updateAnnotation(ctx, cluster, cluster.Namespace, cluster.Name, beforeZeroReplicasConfigured, fmt.Sprint(currentReplicas))
7674
r.Recorder.Event(cluster, corev1.EventTypeNormal, reason, msg)
77-
return err
75+
return r.updateAnnotation(ctx, cluster, cluster.Namespace, cluster.Name, beforeZeroReplicasConfigured, fmt.Sprint(currentReplicas))
7876
}
7977

8078
// If the annotation rabbitmq.com/before-zero-replicas-configured exists it will be deleted.
@@ -85,11 +83,7 @@ func (r *RabbitmqClusterReconciler) removeReplicasBeforeZeroAnnotationIfExists(c
8583
}
8684

8785
func (r *RabbitmqClusterReconciler) recordEventsAndSetCondition(ctx context.Context, cluster *v1beta1.RabbitmqCluster, condType status.RabbitmqClusterConditionType, condStatus corev1.ConditionStatus, eventType, reason, msg string) error {
88-
logger := ctrl.LoggerFrom(ctx)
89-
var statusErr error
90-
logger.Error(errors.New(reason), msg)
9186
r.Recorder.Event(cluster, eventType, reason, msg)
9287
cluster.Status.SetCondition(condType, condStatus, reason, msg)
93-
statusErr = r.Status().Update(ctx, cluster)
94-
return statusErr
88+
return r.Status().Update(ctx, cluster)
9589
}

0 commit comments

Comments
 (0)