Skip to content

Commit 13a76bf

Browse files
committed
feat(controller): fix inconsistency between etcd cluster and statefullset
Signed-off-by: soma00333 <[email protected]>
1 parent c2343a0 commit 13a76bf

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

internal/controller/etcdcluster_controller.go

+22-11
Original file line numberDiff line numberDiff line change
@@ -142,21 +142,32 @@ func (r *EtcdClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
142142
}
143143
targetReplica := *sts.Spec.Replicas // Start with the current size of the stateful set
144144

145-
// TODO: finish the logic later
145+
// The number of replicas in the StatefulSet doesn't match the number of etcd members in the cluster.
146146
if int(targetReplica) != memberCnt {
147-
// TODO: finish the logic later
148-
// nolint:staticcheck // Temporarily disable staticcheck
147+
logger.Info("The expected number of replicas doesn't match the number of etcd members in the cluster", "targetReplica", targetReplica, "memberCnt", memberCnt)
149148
if int(targetReplica) < memberCnt {
150-
// a new added learner hasn't started yet
151-
152-
// re-generate configuration for the new learner member;
153-
// increase statefulsets's replica by 1
149+
// A new member has been added to the etcd cluster
150+
// but the corresponding Pod hasn't been created yet in the StatefulSet.
151+
// Increase the StatefulSet replicas by 1 to match the new cluster member.
152+
newReplicaCount := targetReplica + 1
153+
logger.Info("Increasing StatefulSet replicas to match the new etcd learner.", "oldReplicaCount", targetReplica, "newReplicaCount", newReplicaCount)
154+
_, err = reconcileStatefulSet(ctx, logger, etcdCluster, r.Client, newReplicaCount, r.Scheme)
155+
if err != nil {
156+
return ctrl.Result{}, err
157+
}
154158
} else {
155-
// an already removed member hasn't stopped yet.
156-
157-
// Decrease the statefulsets's replica by 1
159+
// A member has been removed from the etcd cluster
160+
// but the corresponding Pod is still running.
161+
// Decrease the StatefulSet replicas by 1 to remove the unneeded Pod.
162+
logger.Info("An etcd member was removed from the cluster, but the StatefulSet hasn't scaled down yet.")
163+
newReplicaCount := targetReplica - 1
164+
logger.Info("Decreasing StatefulSet replicas to remove the unneeded Pod.", "oldReplicaCount", targetReplica, "newReplicaCount", newReplicaCount)
165+
_, err = reconcileStatefulSet(ctx, logger, etcdCluster, r.Client, newReplicaCount, r.Scheme)
166+
if err != nil {
167+
return ctrl.Result{}, err
168+
}
158169
}
159-
// return
170+
return ctrl.Result{RequeueAfter: requeueDuration}, nil
160171
}
161172

162173
var (

0 commit comments

Comments
 (0)