GeezerVison is showing github.com — fix(controllers): keep EtcdCluster status truthful when etcd is unrea… · cozystack/etcd-operator@a24698b
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: controllers/etcdcluster_controller.go +43-7Lines changed: 43 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -241,7 +241,7 @@ func (r *EtcdClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) | 241 | 241 | // to discovery to latch ClusterID once etcd answers. | 242 | 242 | if desired == 0 { | 243 | 243 | 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) | 245 | 245 | } | 246 | 246 | if current == 0 || hasPendingBootstrap(running) { | 247 | 247 | log.Info("bootstrapping single-node cluster") | @@ -321,6 +321,15 @@ func (r *EtcdClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) | 321 | 321 | // so scaleUp won't run again on its own). We need a promote attempt | 322 | 322 | // here too. Cheap: list etcd once and try to promote any learner; | 323 | 323 | // 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 | 324 | 333 | if cluster.Status.ClusterID != "" && len(running) > 0 { | 325 | 334 | endpoints := memberEndpoints(clusterClientScheme(cluster), running, cluster.Namespace) | 326 | 335 | tlsCfg, tlsErr := buildOperatorTLSConfig(ctx, r.Client, cluster) | @@ -345,9 +354,7 @@ func (r *EtcdClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) | 345 | 354 | if perr != nil { | 346 | 355 | return ctrl.Result{}, perr | 347 | 356 | } | 348 | | - if res != nil { | 349 | | - return *res, nil | 350 | | - } | | 357 | + pending = res | 351 | 358 | } | 352 | 359 | } | 353 | 360 | // Fall through to updateStatus — the next reconcile will retry | @@ -364,7 +371,7 @@ func (r *EtcdClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) | 364 | 371 | if res, err := r.reconcileAuth(ctx, cluster, running); err != nil { | 365 | 372 | return ctrl.Result{}, err | 366 | 373 | } else if res != nil { | 367 | | - return *res, nil | | 374 | + pending = res | 368 | 375 | } | 369 | 376 |
| 370 | 377 | // ── Steady state ─────────────────────────────────────────────────── | @@ -376,7 +383,7 @@ func (r *EtcdClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) | 376 | 383 | // dormant member with a real PVC exists. updateStatus re-derives | 377 | 384 | // `running` internally for its accounting, so passing `active` | 378 | 385 | // here is the correct shape. | 379 | | - return r.updateStatus(ctx, cluster, active) | | 386 | + return r.updateStatus(ctx, cluster, active, pending) | 380 | 387 | } | 381 | 388 |
| 382 | 389 | // ── Bootstrap ──────────────────────────────────────────────────────────── | @@ -1331,10 +1338,18 @@ func hasPendingBootstrap(members []lll.EtcdMember) bool { | 1331 | 1338 | // including any dormant member. It extracts the running subset for the | 1332 | 1339 | // per-condition accounting and uses the dormant member separately for | 1333 | 1340 | // 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. | 1334 | 1348 | func (r *EtcdClusterReconciler) updateStatus( | 1335 | 1349 | ctx context.Context, | 1336 | 1350 | cluster *lll.EtcdCluster, | 1337 | 1351 | members []lll.EtcdMember, | | 1352 | + pending *ctrl.Result, | 1338 | 1353 | ) (ctrl.Result, error) { | 1339 | 1354 | desired := cluster.Status.Observed.Replicas | 1340 | 1355 | running := filterRunningMembers(members) | @@ -1489,7 +1504,28 @@ func (r *EtcdClusterReconciler) updateStatus( | 1489 | 1504 | } | 1490 | 1505 | } | 1491 | 1506 |
| 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 | 1493 | 1529 | } | 1494 | 1530 |
| 1495 | 1531 | // pdbMinAvailable returns the eviction floor: quorum (n/2+1) of | |
Collapse file Copy file name to clipboardExpand all lines: controllers/etcdcluster_controller_test.go +99-4Lines changed: 99 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -590,6 +590,101 @@ func TestTryDiscoverCluster_AuthCredentialsRejected(t *testing.T) { | 590 | 590 | } | 591 | 591 | } | 592 | 592 |
| | 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 | + | 593 | 688 | // TestUpdateStatus_SurfacesBrokenCount covers reviewer issue #6: the isBroken | 594 | 689 | // stub must have a tested call site so the predicate is actually exercised. | 595 | 690 | // Today it always returns false, so the count must always be 0 — this test | @@ -1185,7 +1280,7 @@ func TestUpdateStatus_PausedClusterReportsPausedCondition(t *testing.T) { | 1185 | 1280 | c, _ := newTestClient(t, cluster, &dormant) | 1186 | 1281 | r := &EtcdClusterReconciler{Client: c, Scheme: testScheme(t), EtcdClientFactory: factoryReturning(newFakeEtcd(0xdead))} | 1187 | 1282 |
| 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 { | 1189 | 1284 | t.Fatalf("updateStatus: %v", err) | 1190 | 1285 | } | 1191 | 1286 | mustGet(t, c, "test", "ns", cluster) | @@ -1241,7 +1336,7 @@ func TestUpdateStatus_PausedFreshZeroMessageDifferentiates(t *testing.T) { | 1241 | 1336 | c, _ := newTestClient(t, cluster) | 1242 | 1337 | r := &EtcdClusterReconciler{Client: c, Scheme: testScheme(t), EtcdClientFactory: factoryReturning(newFakeEtcd(0xdead))} | 1243 | 1338 |
| 1244 | | - if _, err := r.updateStatus(ctx, cluster, nil); err != nil { | | 1339 | + if _, err := r.updateStatus(ctx, cluster, nil, nil); err != nil { | 1245 | 1340 | t.Fatalf("updateStatus: %v", err) | 1246 | 1341 | } | 1247 | 1342 | mustGet(t, c, "test", "ns", cluster) | @@ -1301,7 +1396,7 @@ func TestUpdateStatus_PausedMessageHonestForMemoryMember(t *testing.T) { | 1301 | 1396 | c, _ := newTestClient(t, cluster, dormant) | 1302 | 1397 | r := &EtcdClusterReconciler{Client: c, Scheme: testScheme(t), EtcdClientFactory: factoryReturning(newFakeEtcd(0xdead))} | 1303 | 1398 |
| 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 { | 1305 | 1400 | t.Fatalf("updateStatus: %v", err) | 1306 | 1401 | } | 1307 | 1402 | got := mustGet(t, c, "c", "ns", &lll.EtcdCluster{}) | @@ -3154,7 +3249,7 @@ func TestUpdateStatus_SetsScaleSelector(t *testing.T) { | 3154 | 3249 | c, _ := newTestClient(t, cluster) | 3155 | 3250 | r := &EtcdClusterReconciler{Client: c, Scheme: testScheme(t), EtcdClientFactory: factoryReturning(newFakeEtcd(0xabc))} | 3156 | 3251 |
| 3157 | | - if _, err := r.updateStatus(ctx, cluster, nil); err != nil { | | 3252 | + if _, err := r.updateStatus(ctx, cluster, nil, nil); err != nil { | 3158 | 3253 | t.Fatalf("updateStatus: %v", err) | 3159 | 3254 | } | 3160 | 3255 |
| |
|