Skip to content

Commit

Permalink
Set ReconcileSuccess after necessary cli commands succeed
Browse files Browse the repository at this point in the history
- ReconcileSuccess was set to true when set plugins command
failed
- ReconcileSuccess will only be set to true if all necessary
commands (set plugins, feature flags, rebalance) are
run successfullly
  • Loading branch information
ChunyiLyu committed Feb 10, 2021
1 parent c273d60 commit 376e493
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 34 deletions.
19 changes: 12 additions & 7 deletions controllers/rabbitmqcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,23 +186,28 @@ func (r *RabbitmqClusterReconciler) Reconcile(ctx context.Context, req ctrl.Requ
return ctrl.Result{RequeueAfter: requeueAfter}, err
}

// Set ReconcileSuccess to true here because all CRUD operations to Kube API related
// to child resources returned no error
rabbitmqCluster.Status.SetCondition(status.ReconcileSuccess, corev1.ConditionTrue, "Success", "Created or Updated all child resources")
if writerErr := r.Status().Update(ctx, rabbitmqCluster); writerErr != nil {
logger.Error(writerErr, "Failed to Update Custom Resource status")
}

if err := r.setDefaultUserStatus(ctx, rabbitmqCluster); err != nil {
return ctrl.Result{}, err
}

// By this point the StatefulSet may have finished deploying. Run any
// 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")
}
}
return ctrl.Result{RequeueAfter: requeueAfter}, err
}

// Set ReconcileSuccess to true after all reconciliation steps have finished with no error
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")
}

logger.Info("Finished reconciling")

return ctrl.Result{}, nil
Expand Down
27 changes: 0 additions & 27 deletions controllers/rabbitmqcluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,33 +676,6 @@ var _ = Describe("RabbitmqClusterController", func() {
return "ReconcileSuccess status: condition not present"
}, 5).Should(Equal("ReconcileSuccess status: False"))
})

By("transitioning to True when a valid spec in updated", func() {
// We have to Get() the CR again because Reconcile() changes the object
// If we try to Update() without getting the latest version of the CR
// We are very likely to hit a Conflict error
Expect(client.Get(ctx, runtimeClient.ObjectKey{
Name: crName,
Namespace: defaultNamespace,
}, cluster)).To(Succeed())
cluster.Spec.Service.Annotations = map[string]string{"thisIs": "valid"}
Expect(client.Update(ctx, cluster)).To(Succeed())

Eventually(func() string {
someRabbit := &rabbitmqv1beta1.RabbitmqCluster{}
Expect(client.Get(ctx, runtimeClient.ObjectKey{
Name: crName,
Namespace: defaultNamespace,
}, someRabbit)).To(Succeed())

for i := range someRabbit.Status.Conditions {
if someRabbit.Status.Conditions[i].Type == status.ReconcileSuccess {
return fmt.Sprintf("ReconcileSuccess status: %s", someRabbit.Status.Conditions[i].Status)
}
}
return "ReconcileSuccess status: condition not present"
}, 5).Should(Equal("ReconcileSuccess status: True"))
})
})
})

Expand Down

0 comments on commit 376e493

Please sign in to comment.