Skip to content

Commit

Permalink
Extract helper setReconcileSuccess in rmq controller
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunyiLyu committed Jun 8, 2021
1 parent ed868de commit 5936825
Showing 1 changed file with 17 additions and 20 deletions.
37 changes: 17 additions & 20 deletions controllers/rabbitmqcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,7 @@ func (r *RabbitmqClusterReconciler) Reconcile(ctx context.Context, req ctrl.Requ
return ctrl.Result{}, err
}
if err = r.reconcilePVC(ctx, rabbitmqCluster, current, sts); err != nil {
rabbitmqCluster.Status.SetCondition(status.ReconcileSuccess, corev1.ConditionFalse, "FailedReconcilePVC", err.Error())
if statusErr := r.Status().Update(ctx, rabbitmqCluster); statusErr != nil {
logger.Error(statusErr, "Failed to update ReconcileSuccess condition state")
}
r.setReconcileSuccess(ctx, rabbitmqCluster, corev1.ConditionFalse, "FailedReconcilePVC", err.Error())
return ctrl.Result{}, err
}
if r.scaleDown(ctx, rabbitmqCluster, current, sts) {
Expand All @@ -200,10 +197,7 @@ func (r *RabbitmqClusterReconciler) Reconcile(ctx context.Context, req ctrl.Requ
})
r.logAndRecordOperationResult(logger, rabbitmqCluster, resource, operationResult, err)
if err != nil {
rabbitmqCluster.Status.SetCondition(status.ReconcileSuccess, corev1.ConditionFalse, "Error", err.Error())
if writerErr := r.Status().Update(ctx, rabbitmqCluster); writerErr != nil {
logger.Error(writerErr, "Failed to update ReconcileSuccess condition state")
}
r.setReconcileSuccess(ctx, rabbitmqCluster, corev1.ConditionFalse, "Error", err.Error())
return ctrl.Result{}, err
}

Expand All @@ -227,26 +221,26 @@ func (r *RabbitmqClusterReconciler) Reconcile(ctx context.Context, req ctrl.Requ
// post-deploy steps if so, or requeue until the deployment is finished.
if requeueAfter, err := r.runRabbitmqCLICommandsIfAnnotated(ctx, rabbitmqCluster); err != nil || requeueAfter > 0 {
if err != nil {
rabbitmqCluster.Status.SetCondition(status.ReconcileSuccess, corev1.ConditionFalse, "FailedCLICommand", err.Error())
if writerErr := r.Status().Update(ctx, rabbitmqCluster); writerErr != nil {
logger.Error(writerErr, "Failed to update ReconcileSuccess condition state")
}
r.setReconcileSuccess(ctx, rabbitmqCluster, corev1.ConditionFalse, "FailedCLICommand", err.Error())
}
return ctrl.Result{RequeueAfter: requeueAfter}, err
}

// Set ReconcileSuccess to true and update observedGeneration after all reconciliation steps have finished with no error
rabbitmqCluster.Status.ObservedGeneration = rabbitmqCluster.GetGeneration()
rabbitmqCluster.Status.SetCondition(status.ReconcileSuccess, corev1.ConditionTrue, "Success", "Finish reconciling")
if writerErr := r.Status().Update(ctx, rabbitmqCluster); writerErr != nil {
logger.Error(writerErr, "Failed to Update Custom Resource status")
}
r.setReconcileSuccess(ctx, rabbitmqCluster, corev1.ConditionTrue, "Success", "Finish reconciling")

logger.Info("Finished reconciling")

return ctrl.Result{}, nil
}

func (r *RabbitmqClusterReconciler) getRabbitmqCluster(ctx context.Context, namespacedName types.NamespacedName) (*rabbitmqv1beta1.RabbitmqCluster, error) {
rabbitmqClusterInstance := &rabbitmqv1beta1.RabbitmqCluster{}
err := r.Get(ctx, namespacedName, rabbitmqClusterInstance)
return rabbitmqClusterInstance, err
}

// logAndRecordOperationResult - helper function to log and record events with message and error
// it logs and records 'updated' and 'created' OperationResult, and ignores OperationResult 'unchanged'
func (r *RabbitmqClusterReconciler) logAndRecordOperationResult(logger logr.Logger, rmq runtime.Object, resource runtime.Object, operationResult controllerutil.OperationResult, err error) {
Expand Down Expand Up @@ -323,10 +317,13 @@ func (r *RabbitmqClusterReconciler) getChildResources(ctx context.Context, rmq *
return []runtime.Object{sts, endPoints}, nil
}

func (r *RabbitmqClusterReconciler) getRabbitmqCluster(ctx context.Context, namespacedName types.NamespacedName) (*rabbitmqv1beta1.RabbitmqCluster, error) {
rabbitmqClusterInstance := &rabbitmqv1beta1.RabbitmqCluster{}
err := r.Get(ctx, namespacedName, rabbitmqClusterInstance)
return rabbitmqClusterInstance, err
func (r *RabbitmqClusterReconciler) setReconcileSuccess(ctx context.Context, rabbitmqCluster *rabbitmqv1beta1.RabbitmqCluster, condition corev1.ConditionStatus, reason, msg string) {
rabbitmqCluster.Status.SetCondition(status.ReconcileSuccess, condition, reason, msg)
if writerErr := r.Status().Update(ctx, rabbitmqCluster); writerErr != nil {
ctrl.LoggerFrom(ctx).Error(writerErr, "Failed to update Custom Resource status",
"namespace", rabbitmqCluster.Namespace,
"name", rabbitmqCluster.Name)
}
}

func (r *RabbitmqClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
Expand Down

0 comments on commit 5936825

Please sign in to comment.