Skip to content

Commit

Permalink
Merge pull request #53 from soma00333/enhance-reconcile-logic
Browse files Browse the repository at this point in the history
Feat(controller): Fix inconsistency between etcd cluster and statefulset
  • Loading branch information
ahrtr authored Jan 28, 2025
2 parents c1211f8 + cf38149 commit 073a14a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@ linters-settings:
revive:
rules:
- name: comment-spacings
gocyclo:
min-complexity: 35
28 changes: 17 additions & 11 deletions internal/controller/etcdcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,21 +142,27 @@ func (r *EtcdClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}
targetReplica := *sts.Spec.Replicas // Start with the current size of the stateful set

// TODO: finish the logic later
// The number of replicas in the StatefulSet doesn't match the number of etcd members in the cluster.
if int(targetReplica) != memberCnt {
// TODO: finish the logic later
// nolint:staticcheck // Temporarily disable staticcheck
logger.Info("The expected number of replicas doesn't match the number of etcd members in the cluster", "targetReplica", targetReplica, "memberCnt", memberCnt)
if int(targetReplica) < memberCnt {
// a new added learner hasn't started yet

// re-generate configuration for the new learner member;
// increase statefulsets's replica by 1
logger.Info("An etcd member was added into the cluster, but the StatefulSet hasn't scaled out yet")
newReplicaCount := targetReplica + 1
logger.Info("Increasing StatefulSet replicas to match the etcd cluster member count", "oldReplicaCount", targetReplica, "newReplicaCount", newReplicaCount)
_, err = reconcileStatefulSet(ctx, logger, etcdCluster, r.Client, newReplicaCount, r.Scheme)
if err != nil {
return ctrl.Result{}, err
}
} else {
// an already removed member hasn't stopped yet.

// Decrease the statefulsets's replica by 1
logger.Info("An etcd member was removed from the cluster, but the StatefulSet hasn't scaled in yet")
newReplicaCount := targetReplica - 1
logger.Info("Decreasing StatefulSet replicas to remove the unneeded Pod.", "oldReplicaCount", targetReplica, "newReplicaCount", newReplicaCount)
_, err = reconcileStatefulSet(ctx, logger, etcdCluster, r.Client, newReplicaCount, r.Scheme)
if err != nil {
return ctrl.Result{}, err
}
}
// return
return ctrl.Result{RequeueAfter: requeueDuration}, nil
}

var (
Expand Down

0 comments on commit 073a14a

Please sign in to comment.