GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(controllers): keep EtcdCluster status truthful when etcd is unrea… · cozystack/etcd-operator@a24698b

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • controllers
    • etcdcluster_controller.go
    • etcdcluster_controller_test.go
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎controllers/etcdcluster_controller.go‎

Copy file name to clipboardExpand all lines: controllers/etcdcluster_controller.go
+43-7Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ func (r *EtcdClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
241241
// to discovery to latch ClusterID once etcd answers.
242242
if desired == 0 {
243243
log.Info("cluster declared paused from the start; not bootstrapping")
244-
return r.updateStatus(ctx, cluster, active)
244+
return r.updateStatus(ctx, cluster, active, nil)
245245
}
246246
if current == 0 || hasPendingBootstrap(running) {
247247
log.Info("bootstrapping single-node cluster")
@@ -321,6 +321,15 @@ func (r *EtcdClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
321321
// so scaleUp won't run again on its own). We need a promote attempt
322322
// here too. Cheap: list etcd once and try to promote any learner;
323323
// no-op if none.
324+
//
325+
// A transient requeue from either the promote or the auth attempt
326+
// (etcd unreachable, learner not yet promotable, auth just latched)
327+
// is threaded into updateStatus via `pending` rather than returned
328+
// here. Returning early would skip updateStatus, freezing the
329+
// cluster's Available/Degraded conditions at their last-healthy value
330+
// while every EtcdMember has already flipped Ready=False — a down
331+
// cluster would keep reporting QuorumHealthy indefinitely.
332+
var pending *ctrl.Result
324333
if cluster.Status.ClusterID != "" && len(running) > 0 {
325334
endpoints := memberEndpoints(clusterClientScheme(cluster), running, cluster.Namespace)
326335
tlsCfg, tlsErr := buildOperatorTLSConfig(ctx, r.Client, cluster)
@@ -345,9 +354,7 @@ func (r *EtcdClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
345354
if perr != nil {
346355
return ctrl.Result{}, perr
347356
}
348-
if res != nil {
349-
return *res, nil
350-
}
357+
pending = res
351358
}
352359
}
353360
// Fall through to updateStatus — the next reconcile will retry
@@ -364,7 +371,7 @@ func (r *EtcdClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
364371
if res, err := r.reconcileAuth(ctx, cluster, running); err != nil {
365372
return ctrl.Result{}, err
366373
} else if res != nil {
367-
return *res, nil
374+
pending = res
368375
}
369376

370377
// ── Steady state ───────────────────────────────────────────────────
@@ -376,7 +383,7 @@ func (r *EtcdClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
376383
// dormant member with a real PVC exists. updateStatus re-derives
377384
// `running` internally for its accounting, so passing `active`
378385
// here is the correct shape.
379-
return r.updateStatus(ctx, cluster, active)
386+
return r.updateStatus(ctx, cluster, active, pending)
380387
}
381388

382389
// ── Bootstrap ────────────────────────────────────────────────────────────
@@ -1331,10 +1338,18 @@ func hasPendingBootstrap(members []lll.EtcdMember) bool {
13311338
// including any dormant member. It extracts the running subset for the
13321339
// per-condition accounting and uses the dormant member separately for
13331340
// the Paused message's PVC name.
1341+
// updateStatus recomputes the cluster's cached status fields and conditions
1342+
// from the current EtcdMember set and writes them. It is the single exit
1343+
// point of a converged reconcile, so callers with a transient requeue to
1344+
// honour (promote/auth retries) pass it as `pending` instead of returning
1345+
// early: the status write still happens, and the returned Result carries
1346+
// whichever requeue fires sooner — `pending` or updateStatus's own steady-
1347+
// state cadence. `pending` is nil when there is nothing to thread through.
13341348
func (r *EtcdClusterReconciler) updateStatus(
13351349
ctx context.Context,
13361350
cluster *lll.EtcdCluster,
13371351
members []lll.EtcdMember,
1352+
pending *ctrl.Result,
13381353
) (ctrl.Result, error) {
13391354
desired := cluster.Status.Observed.Replicas
13401355
running := filterRunningMembers(members)
@@ -1489,7 +1504,28 @@ func (r *EtcdClusterReconciler) updateStatus(
14891504
}
14901505
}
14911506

1492-
return ctrl.Result{RequeueAfter: 30 * time.Second}, nil
1507+
return soonerRequeue(ctrl.Result{RequeueAfter: 30 * time.Second}, pending), nil
1508+
}
1509+
1510+
// soonerRequeue returns whichever of the two results asks the controller to
1511+
// come back sooner. `base` is updateStatus's own steady-state cadence and
1512+
// always requeues; `pending` is an optional transient retry (nil when none).
1513+
// A Requeue=true (requeue-now) beats any RequeueAfter delay; between two
1514+
// delays the shorter wins.
1515+
func soonerRequeue(base ctrl.Result, pending *ctrl.Result) ctrl.Result {
1516+
if pending == nil {
1517+
return base
1518+
}
1519+
if pending.Requeue && !base.Requeue {
1520+
return *pending
1521+
}
1522+
if !pending.Requeue && base.Requeue {
1523+
return base
1524+
}
1525+
if pending.RequeueAfter > 0 && pending.RequeueAfter < base.RequeueAfter {
1526+
return *pending
1527+
}
1528+
return base
14931529
}
14941530

14951531
// pdbMinAvailable returns the eviction floor: quorum (n/2+1) of
Collapse file

‎controllers/etcdcluster_controller_test.go‎

Copy file name to clipboardExpand all lines: controllers/etcdcluster_controller_test.go
+99-4Lines changed: 99 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,101 @@ func TestTryDiscoverCluster_AuthCredentialsRejected(t *testing.T) {
590590
}
591591
}
592592

593+
// TestReconcile_UnreachableEtcdDoesNotFreezeStatus pins the fix for #367:
594+
// on a converged cluster (ClusterID latched, current==desired) whose etcd
595+
// has gone unreachable, the steady-state promote attempt returns a transient
596+
// requeue. That requeue must be threaded through updateStatus rather than
597+
// returned early — otherwise the cluster's Available/Degraded conditions
598+
// freeze at their last-healthy value while every member reports Ready=False,
599+
// and a fully down cluster keeps advertising QuorumHealthy.
600+
func TestReconcile_UnreachableEtcdDoesNotFreezeStatus(t *testing.T) {
601+
ctx := context.Background()
602+
cluster := &lll.EtcdCluster{
603+
ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "ns"},
604+
Spec: lll.EtcdClusterSpec{
605+
Replicas: ptrInt32(3),
606+
Version: "3.5.17",
607+
Storage: lll.StorageSpec{Size: quickQty(t, "1Gi")},
608+
},
609+
Status: lll.EtcdClusterStatus{
610+
ClusterToken: "test",
611+
ClusterID: "deadbeef",
612+
Observed: &lll.ObservedClusterSpec{
613+
Replicas: 3,
614+
Version: "3.5.17",
615+
Storage: lll.StorageSpec{Size: quickQty(t, "1Gi")},
616+
},
617+
ProgressDeadline: &metav1.Time{Time: metav1.Now().Add(60 * 60 * 1e9)},
618+
// Stale last-healthy snapshot: this is what must NOT survive a
619+
// reconcile once etcd is unreachable and members are Ready=False.
620+
ReadyMembers: 3,
621+
Conditions: []metav1.Condition{{
622+
Type: lll.ClusterAvailable, Status: metav1.ConditionTrue,
623+
Reason: "QuorumHealthy", Message: "All members are ready",
624+
LastTransitionTime: metav1.Now(),
625+
}},
626+
},
627+
}
628+
objs := []client.Object{cluster}
629+
// Three members, all Ready=False — the member controller has already
630+
// observed the down etcd and flipped them, exactly as reported in #367.
631+
for i := 0; i < 3; i++ {
632+
objs = append(objs, &lll.EtcdMember{
633+
ObjectMeta: metav1.ObjectMeta{
634+
Name: fmt.Sprintf("test-%d", i),
635+
Namespace: "ns",
636+
Labels: memberLabels("test", fmt.Sprintf("test-%d", i)),
637+
},
638+
Spec: lll.EtcdMemberSpec{ClusterName: "test", Version: "3.5.17", Storage: lll.StorageSpec{Size: quickQty(t, "1Gi")}, InitialCluster: "x", ClusterToken: "test"},
639+
Status: lll.EtcdMemberStatus{
640+
PodName: fmt.Sprintf("test-%d", i),
641+
MemberID: "abc",
642+
Conditions: []metav1.Condition{{
643+
Type: lll.MemberReady, Status: metav1.ConditionFalse, Reason: "PodNotReady",
644+
LastTransitionTime: metav1.Now(),
645+
}},
646+
},
647+
})
648+
}
649+
c, _ := newTestClient(t, objs...)
650+
// Dialable client whose MemberList errors: this is the etcd-unreachable
651+
// shape (a lazy clientv3 dial succeeds; the RPC is where it fails).
652+
fe := newFakeEtcd(0xdeadbeef)
653+
fe.listErr = errors.New("context deadline exceeded")
654+
r := &EtcdClusterReconciler{
655+
Client: c,
656+
Scheme: testScheme(t),
657+
EtcdClientFactory: factoryReturning(fe),
658+
}
659+
660+
res, err := r.Reconcile(ctx, ctrl.Request{NamespacedName: types.NamespacedName{Name: "test", Namespace: "ns"}})
661+
if err != nil {
662+
t.Fatalf("Reconcile: %v", err)
663+
}
664+
665+
mustGet(t, c, "test", "ns", cluster)
666+
var available *metav1.Condition
667+
for i := range cluster.Status.Conditions {
668+
if cluster.Status.Conditions[i].Type == lll.ClusterAvailable {
669+
available = &cluster.Status.Conditions[i]
670+
}
671+
}
672+
if available == nil {
673+
t.Fatalf("no Available condition after reconcile")
674+
}
675+
if available.Status != metav1.ConditionFalse {
676+
t.Fatalf("Available = %v/%q, want False (status must not freeze at QuorumHealthy)", available.Status, available.Reason)
677+
}
678+
if cluster.Status.ReadyMembers != 0 {
679+
t.Fatalf("ReadyMembers = %d, want 0 (recomputed from member conditions)", cluster.Status.ReadyMembers)
680+
}
681+
// The promote attempt's 10s transient requeue must survive: it is sooner
682+
// than updateStatus's 30s cadence, so it wins.
683+
if res.RequeueAfter != 10*time.Second {
684+
t.Fatalf("RequeueAfter = %v, want 10s (promote requeue threaded through updateStatus)", res.RequeueAfter)
685+
}
686+
}
687+
593688
// TestUpdateStatus_SurfacesBrokenCount covers reviewer issue #6: the isBroken
594689
// stub must have a tested call site so the predicate is actually exercised.
595690
// Today it always returns false, so the count must always be 0 — this test
@@ -1185,7 +1280,7 @@ func TestUpdateStatus_PausedClusterReportsPausedCondition(t *testing.T) {
11851280
c, _ := newTestClient(t, cluster, &dormant)
11861281
r := &EtcdClusterReconciler{Client: c, Scheme: testScheme(t), EtcdClientFactory: factoryReturning(newFakeEtcd(0xdead))}
11871282

1188-
if _, err := r.updateStatus(ctx, cluster, []lll.EtcdMember{dormant}); err != nil {
1283+
if _, err := r.updateStatus(ctx, cluster, []lll.EtcdMember{dormant}, nil); err != nil {
11891284
t.Fatalf("updateStatus: %v", err)
11901285
}
11911286
mustGet(t, c, "test", "ns", cluster)
@@ -1241,7 +1336,7 @@ func TestUpdateStatus_PausedFreshZeroMessageDifferentiates(t *testing.T) {
12411336
c, _ := newTestClient(t, cluster)
12421337
r := &EtcdClusterReconciler{Client: c, Scheme: testScheme(t), EtcdClientFactory: factoryReturning(newFakeEtcd(0xdead))}
12431338

1244-
if _, err := r.updateStatus(ctx, cluster, nil); err != nil {
1339+
if _, err := r.updateStatus(ctx, cluster, nil, nil); err != nil {
12451340
t.Fatalf("updateStatus: %v", err)
12461341
}
12471342
mustGet(t, c, "test", "ns", cluster)
@@ -1301,7 +1396,7 @@ func TestUpdateStatus_PausedMessageHonestForMemoryMember(t *testing.T) {
13011396
c, _ := newTestClient(t, cluster, dormant)
13021397
r := &EtcdClusterReconciler{Client: c, Scheme: testScheme(t), EtcdClientFactory: factoryReturning(newFakeEtcd(0xdead))}
13031398

1304-
if _, err := r.updateStatus(ctx, cluster, []lll.EtcdMember{*dormant}); err != nil {
1399+
if _, err := r.updateStatus(ctx, cluster, []lll.EtcdMember{*dormant}, nil); err != nil {
13051400
t.Fatalf("updateStatus: %v", err)
13061401
}
13071402
got := mustGet(t, c, "c", "ns", &lll.EtcdCluster{})
@@ -3154,7 +3249,7 @@ func TestUpdateStatus_SetsScaleSelector(t *testing.T) {
31543249
c, _ := newTestClient(t, cluster)
31553250
r := &EtcdClusterReconciler{Client: c, Scheme: testScheme(t), EtcdClientFactory: factoryReturning(newFakeEtcd(0xabc))}
31563251

3157-
if _, err := r.updateStatus(ctx, cluster, nil); err != nil {
3252+
if _, err := r.updateStatus(ctx, cluster, nil, nil); err != nil {
31583253
t.Fatalf("updateStatus: %v", err)
31593254
}
31603255

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.