From 7c5d2d1603ee63f7835ac3d3b8dea66f646d7570 Mon Sep 17 00:00:00 2001 From: Timofei Larkin Date: Wed, 10 Apr 2024 23:09:07 +0400 Subject: [PATCH] Simplify the condition transition rules (#129) When the cluster is initializing, either a quorum has not yet been established, so if the sts isn't ready, we don't change the reason for etcd not ready, or the cluster has initialized, so we set the status based on the sts state. This reduces the amount of nesting and repeated code in the condition/reason checks. --- internal/controller/etcdcluster_controller.go | 40 +++++++++---------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/internal/controller/etcdcluster_controller.go b/internal/controller/etcdcluster_controller.go index dee8e8b3..1ca84255 100644 --- a/internal/controller/etcdcluster_controller.go +++ b/internal/controller/etcdcluster_controller.go @@ -99,30 +99,26 @@ func (r *EtcdClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) // set cluster readiness condition existingCondition := factory.GetCondition(instance, etcdaenixiov1alpha1.EtcdConditionReady) - if existingCondition.Reason == string(etcdaenixiov1alpha1.EtcdCondTypeWaitingForFirstQuorum) { - // we should change from "waiting for first quorum establishment" to "StatefulSet ready / not ready" - // only after sts gets ready first time - if clusterReady { - factory.SetCondition(instance, factory.NewCondition(etcdaenixiov1alpha1.EtcdConditionReady). - WithStatus(true). - WithReason(string(etcdaenixiov1alpha1.EtcdCondTypeStatefulSetReady)). - WithMessage(string(etcdaenixiov1alpha1.EtcdReadyCondPosMessage)). - Complete()) - } - } else { - reason := etcdaenixiov1alpha1.EtcdCondTypeStatefulSetNotReady - message := etcdaenixiov1alpha1.EtcdReadyCondNegMessage - if clusterReady { - reason = etcdaenixiov1alpha1.EtcdCondTypeStatefulSetReady - message = etcdaenixiov1alpha1.EtcdReadyCondPosMessage - } + if existingCondition.Reason == string(etcdaenixiov1alpha1.EtcdCondTypeWaitingForFirstQuorum) && !clusterReady { + // if we are still "waiting for first quorum establishment" and the StatefulSet + // isn't ready yet, don't update the EtcdConditionReady, but circuit-break. + return r.updateStatus(ctx, instance) + } - factory.SetCondition(instance, factory.NewCondition(etcdaenixiov1alpha1.EtcdConditionReady). - WithStatus(clusterReady). - WithReason(string(reason)). - WithMessage(string(message)). - Complete()) + // otherwise, EtcdConditionReady is set to true/false with the reason that the + // StatefulSet is or isn't ready. + reason := etcdaenixiov1alpha1.EtcdCondTypeStatefulSetNotReady + message := etcdaenixiov1alpha1.EtcdReadyCondNegMessage + if clusterReady { + reason = etcdaenixiov1alpha1.EtcdCondTypeStatefulSetReady + message = etcdaenixiov1alpha1.EtcdReadyCondPosMessage } + + factory.SetCondition(instance, factory.NewCondition(etcdaenixiov1alpha1.EtcdConditionReady). + WithStatus(clusterReady). + WithReason(string(reason)). + WithMessage(string(message)). + Complete()) return r.updateStatus(ctx, instance) }