Commit d7ec5a9
feat: hub-side level-triggered companion GC (Component 6)
Add CompanionGCReconciler, a level-triggered backstop for referenced-data
companions stranded by interrupted finalization. When the
ReferencedDataController's WD finalizer is interrupted mid-flight (pod restart,
SIGKILL), a hub companion can be left with a referenced-by annotation pointing
at WDs that no longer exist on the hub. Without this controller, the companion
persists forever (leaking data including Secret bytes), its ResourceBinding keeps
a Work alive on the hub, and Karmada continuously re-creates the cell copy —
exactly the cm-pristine/secret-pristine lab scenario.
Design:
- Watches referenced-data-labeled ConfigMaps and Secrets on the hub. The
federationMgr cache for ConfigMaps and Secrets is restricted to objects
carrying the ReferencedDataLabel via cache.Options.ByObject (cmd/main.go).
This is the OOM guard: predicates filter events, not cache contents; without
the cache-level scope the informer would list-and-watch every ConfigMap/Secret
on the Karmada hub — the same unscoped-informer pattern that OOMKilled the
cell CompanionGCReconciler. The label predicate on the controller is kept as
belt-and-suspenders but is NOT the primary memory guard.
- On each reconcile, parses the referenced-by annotation and checks each
referenced WD by name in the companion's own hub namespace (ns-{project-uid})
via HubClient.Get — no MCManager needed because WDs are federated to the hub.
- If ALL referrers are absent: deletes the companion and its ResourceBinding via
the downstreamCompanionWriter path, driving the full Karmada cascade
(RB → Work → cell copy deleted permanently).
- Conservative safety: terminating WDs count as present; corrupt annotations
and malformed WD keys are handled by skip-not-delete.
- A companionGCPeriodicSweep backstop fires every 5 minutes to catch companions
stranded before the controller started.
- Wired in setupManagementControllers on the federationMgr alongside
OrphanRBReconciler and InstanceProjector.
Unit tests cover: orphaned CM/Secret deleted + RB torn down; live referrer
preserved; terminating referrer counts as present; all-absent multi-referrer
deleted; partial multi-referrer preserved; corrupt annotation preserved;
empty annotation preserved; unlabeled object unaffected; RB-already-gone
tolerated; periodic sweep drives reconciliation.
Federated e2e (test/e2e/referenced-data-delete-cascade/):
- HAPPY-PATH CASCADE: create WD + ConfigMap/Secret → assert companions on hub +
cell + RBs present → delete WD → assert hub companion, RBs, cell copies all
deleted and stay deleted (30 s anti-thrash poll).
- STRANDED COMPANION BACKSTOP: inject a labeled companion with a dead WD key →
assert CompanionGCReconciler reclaims it within the sweep interval.
The e2e test is correct and self-contained but requires the Kind+Karmada
harness (task e2e:up) which is not runnable headless.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
(cherry picked from commit e419b54)1 parent 0a7e512 commit d7ec5a9
4 files changed
Lines changed: 914 additions & 8 deletions
File tree
- cmd
- internal/controller
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
| 18 | + | |
17 | 19 | | |
18 | 20 | | |
19 | 21 | | |
20 | 22 | | |
21 | 23 | | |
22 | 24 | | |
23 | 25 | | |
| 26 | + | |
24 | 27 | | |
25 | 28 | | |
26 | 29 | | |
| |||
491 | 494 | | |
492 | 495 | | |
493 | 496 | | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
494 | 508 | | |
495 | | - | |
496 | | - | |
497 | | - | |
498 | | - | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
499 | 514 | | |
500 | 515 | | |
501 | 516 | | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
502 | 527 | | |
503 | 528 | | |
504 | 529 | | |
| |||
535 | 560 | | |
536 | 561 | | |
537 | 562 | | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
538 | 573 | | |
539 | 574 | | |
0 commit comments