Merged
fix(cube): sync create persist and reclaim Cubelet orphans (#71) #289
zhoudong01Add on 19 hours ago
fix(cube): sync create persist and reclaim Cubelet orphans (#71) #289
Merged
Total: 4 files changed+242-36
| @@ -100,8 +100,9 @@ type Engine struct { | |||
| 100 | hideLabelKey string | 100 | hideLabelKey string |
| 101 | hideLabelValue string | 101 | hideLabelValue string |
| 102 | 102 | ||
| 103 | - // Background persist coalesces whole-map snapshots so Create only does a | 103 | + // Background persist coalesces whole-map snapshots. Create/Resume wait for |
| 104 | - // short in-memory map insert (e2b tracker style). Pause/Destroy/Close wait. | 104 | + // durability (KASandbox #71); Pause/Destroy/Close also wait. Other updates |
| 105 | + // may schedule without waiting. | ||
| 105 | persistMu sync.Mutex | 106 | persistMu sync.Mutex |
| 106 | persistCond *sync.Cond | 107 | persistCond *sync.Cond |
| 107 | persistDirty bool | 108 | persistDirty bool |
| @@ -111,6 +112,11 @@ type Engine struct { | |||
| 111 | persistCh chan struct{} | 112 | persistCh chan struct{} |
| 112 | persistStop chan struct{} | 113 | persistStop chan struct{} |
| 113 | persistDone chan struct{} | 114 | persistDone chan struct{} |
| 115 | + | ||
| 116 | + // orphanSeen tracks Cubelet sandbox IDs first observed without a local | ||
| 117 | + // record, so Reconcile can honor GracePeriod before destroying them. | ||
| 118 | + orphanMu sync.Mutex | ||
| 119 | + orphanSeen map[string]time.Time | ||
| 114 | } | 120 | } |
| 115 | 121 | ||
| 116 | var _ engine.RuntimeEngine = (*Engine)(nil) | 122 | var _ engine.RuntimeEngine = (*Engine)(nil) |
| @@ -153,6 +159,7 @@ func newEngine(cfg Config, client cubeClient) *Engine { | |||
| 153 | persistCh: make(chan struct{}, 1), | 159 | persistCh: make(chan struct{}, 1), |
| 154 | persistStop: make(chan struct{}), | 160 | persistStop: make(chan struct{}), |
| 155 | persistDone: make(chan struct{}), | 161 | persistDone: make(chan struct{}), |
| 162 | + orphanSeen: map[string]time.Time{}, | ||
| 156 | } | 163 | } |
| 157 | if hide := strings.TrimSpace(cfg.HideLabel); hide != "" { | 164 | if hide := strings.TrimSpace(cfg.HideLabel); hide != "" { |
| 158 | key, value, found := strings.Cut(hide, "=") | 165 | key, value, found := strings.Cut(hide, "=") |
| @@ -229,10 +236,18 @@ func (e *Engine) RestoreState(ctx context.Context) error { | |||
| 229 | return nil | 236 | return nil |
| 230 | } | 237 | } |
| 231 | paused := make(map[string]bool, len(listed)) | 238 | paused := make(map[string]bool, len(listed)) |
| 239 | + known := e.knownCubeletIDs() | ||
| 240 | + var orphans []string | ||
| 232 | for _, item := range listed { | 241 | for _, item := range listed { |
| 242 | + if item.ID == "" { | ||
| 243 | + continue | ||
| 244 | + } | ||
| 233 | if item.Paused { | 245 | if item.Paused { |
| 234 | paused[item.ID] = true | 246 | paused[item.ID] = true |
| 235 | } | 247 | } |
| 248 | + if !known[item.ID] { | ||
| 249 | + orphans = append(orphans, item.ID) | ||
| 250 | + } | ||
| 236 | } | 251 | } |
| 237 | e.mu.Lock() | 252 | e.mu.Lock() |
| 238 | var changed []CubePodState | 253 | var changed []CubePodState |
| @@ -248,6 +263,14 @@ func (e *Engine) RestoreState(ctx context.Context) error { | |||
| 248 | return err | 263 | return err |
| 249 | } | 264 | } |
| 250 | } | 265 | } |
| 266 | + // Startup: Cubelet sandboxes with no local record are unreclaimable via CRI | ||
| 267 | + // — destroy them immediately (KASandbox #71). Periodic Reconcile uses grace. | ||
| 268 | + for _, id := range orphans { | ||
| 269 | + log.Printf("[CubeEngine] restore reclaiming orphan cubelet sandbox=%s", id) | ||
| 270 | + if err := e.destroyCubeletSandbox(ctx, id, "restore-orphan"); err != nil { | ||
| 271 | + log.Printf("[CubeEngine] WARNING: restore orphan destroy sandbox=%s: %v", id, err) | ||
| 272 | + } | ||
| 273 | + } | ||
| 251 | return nil | 274 | return nil |
| 252 | } | 275 | } |
| 253 | 276 | ||
| @@ -342,8 +365,10 @@ func (e *Engine) coldCreate(ctx context.Context, req *runtime.RunPodSandboxReque | |||
| 342 | } | 365 | } |
| 343 | buildMs := time.Since(buildStart).Milliseconds() | 366 | buildMs := time.Since(buildStart).Milliseconds() |
| 344 | commitStart := time.Now() | 367 | commitStart := time.Now() |
| 345 | - if err := e.commit(pod); err != nil { | 368 | + if err := e.commitSync(pod); err != nil { |
| 346 | - return nil, err | 369 | + _ = e.client.Destroy(ctx, result.SandboxID, destroyAnnotations(podRunning)) |
| 370 | + _ = e.forget(id) | ||
| 371 | + return nil, status.Errorf(codes.Internal, "persist cube sandbox state: %v", err) | ||
| 347 | } | 372 | } |
| 348 | commitMs := time.Since(commitStart).Milliseconds() | 373 | commitMs := time.Since(commitStart).Milliseconds() |
| 349 | totalMs := time.Since(totalStart).Milliseconds() | 374 | totalMs := time.Since(totalStart).Milliseconds() |
| @@ -385,8 +410,9 @@ func (e *Engine) resume(ctx context.Context, pod *CubePodState) (*runtime.RunPod | |||
| 385 | if result.SandboxUUID != "" { | 410 | if result.SandboxUUID != "" { |
| 386 | pod.SandboxUUID = result.SandboxUUID | 411 | pod.SandboxUUID = result.SandboxUUID |
| 387 | } | 412 | } |
| 388 | - if err := e.commit(*pod); err != nil { | 413 | + if err := e.commitSync(*pod); err != nil { |
| 389 | - return nil, err | 414 | + _ = e.client.Destroy(ctx, result.SandboxID, destroyAnnotations(podRunning)) |
| 415 | + return nil, status.Errorf(codes.Internal, "persist cube sandbox state: %v", err) | ||
| 390 | } | 416 | } |
| 391 | log.Printf("[CubeEngine] resumed cri=%s sandbox=%s ip=%s", pod.CRISandboxID, pod.SandboxID, result.SandboxIP) | 417 | log.Printf("[CubeEngine] resumed cri=%s sandbox=%s ip=%s", pod.CRISandboxID, pod.SandboxID, result.SandboxIP) |
| 392 | return &runtime.RunPodSandboxResponse{PodSandboxId: pod.CRISandboxID}, nil | 418 | return &runtime.RunPodSandboxResponse{PodSandboxId: pod.CRISandboxID}, nil |
| @@ -493,16 +519,10 @@ func (e *Engine) destroy(ctx context.Context, id string) error { | |||
| 493 | // missing, and that confirm window is too slow for the idle check. The | 519 | // missing, and that confirm window is too slow for the idle check. The |
| 494 | // record stays listable so the agent reports phase stopped. | 520 | // record stays listable so the agent reports phase stopped. |
| 495 | func (e *Engine) hardDestroy(ctx context.Context, pod *CubePodState) error { | 521 | func (e *Engine) hardDestroy(ctx context.Context, pod *CubePodState) error { |
| 496 | - annotations := map[string]string{ | 522 | + if err := e.client.Destroy(ctx, pod.SandboxID, destroyAnnotations(pod.State)); err != nil { |
| 497 | - annInstanceType: instanceTypeCubebox, | ||
| 498 | - "cube.master.instance.kill_reason": "request", | ||
| 499 | - } | ||
| 500 | - if pod.State == podPaused || pod.State == podParked { | ||
| 501 | - annotations[annDeleteTombstone] = "true" | ||
| 502 | - } | ||
| 503 | - if err := e.client.Destroy(ctx, pod.SandboxID, annotations); err != nil { | ||
| 504 | return err | 523 | return err |
| 505 | } | 524 | } |
| 525 | + e.clearOrphanSeen(pod.SandboxID) | ||
| 506 | pod.State = podStopped | 526 | pod.State = podStopped |
| 507 | pod.StoppedAt = time.Now() | 527 | pod.StoppedAt = time.Now() |
| 508 | pod.SandboxIP = "" | 528 | pod.SandboxIP = "" |
| @@ -723,7 +743,7 @@ func (e *Engine) podByContainer(id string) (CubePodState, bool) { | |||
| 723 | } | 743 | } |
| 724 | 744 | ||
| 725 | // commit updates the in-memory map under a short lock and schedules a | 745 | // commit updates the in-memory map under a short lock and schedules a |
| 726 | -// background persist. Create/Resume never wait on disk (e2b-style). | 746 | +// background persist without waiting. |
| 727 | func (e *Engine) commit(pod CubePodState) error { | 747 | func (e *Engine) commit(pod CubePodState) error { |
| 728 | cp := clonePod(pod) | 748 | cp := clonePod(pod) |
| 729 | e.mu.Lock() | 749 | e.mu.Lock() |
| @@ -733,8 +753,8 @@ func (e *Engine) commit(pod CubePodState) error { | |||
| 733 | return nil | 753 | return nil |
| 734 | } | 754 | } |
| 735 | 755 | ||
| 736 | -// commitSync is commit + wait for the coalesced persist worker. Use when the | 756 | +// commitSync is commit + wait for the coalesced persist worker. Create/Resume |
| 737 | -// new state must survive a crash before the RPC returns (Pause snap id). | 757 | +// and Pause use this so a successful RPC implies durable local state (#71). |
| 738 | func (e *Engine) commitSync(pod CubePodState) error { | 758 | func (e *Engine) commitSync(pod CubePodState) error { |
| 739 | cp := clonePod(pod) | 759 | cp := clonePod(pod) |
| 740 | e.mu.Lock() | 760 | e.mu.Lock() |
| @@ -854,11 +874,73 @@ func (e *Engine) ensureEnabled() error { | |||
| 854 | return nil | 874 | return nil |
| 855 | } | 875 | } |
| 856 | 876 | ||
| 877 | +func destroyAnnotations(state string) map[string]string { | ||
| 878 | + annotations := map[string]string{ | ||
| 879 | + annInstanceType: instanceTypeCubebox, | ||
| 880 | + "cube.master.instance.kill_reason": "request", | ||
| 881 | + } | ||
| 882 | + if state == podPaused || state == podParked { | ||
| 883 | + annotations[annDeleteTombstone] = "true" | ||
| 884 | + } | ||
| 885 | + return annotations | ||
| 886 | +} | ||
| 887 | + | ||
| 888 | +func (e *Engine) knownCubeletIDs() map[string]bool { | ||
| 889 | + e.mu.Lock() | ||
| 890 | + defer e.mu.Unlock() | ||
| 891 | + out := make(map[string]bool, len(e.pods)) | ||
| 892 | + for _, pod := range e.pods { | ||
| 893 | + if pod != nil && pod.SandboxID != "" { | ||
| 894 | + out[pod.SandboxID] = true | ||
| 895 | + } | ||
| 896 | + } | ||
| 897 | + return out | ||
| 898 | +} | ||
| 899 | + | ||
| 900 | +func (e *Engine) clearOrphanSeen(sandboxID string) { | ||
| 901 | + if e == nil || sandboxID == "" { | ||
| 902 | + return | ||
| 903 | + } | ||
| 904 | + e.orphanMu.Lock() | ||
| 905 | + delete(e.orphanSeen, sandboxID) | ||
| 906 | + e.orphanMu.Unlock() | ||
| 907 | +} | ||
| 908 | + | ||
| 909 | +// destroyCubeletSandbox destroys by Cubelet ID without requiring a local CRI record. | ||
| 910 | +func (e *Engine) destroyCubeletSandbox(ctx context.Context, sandboxID, reason string) error { | ||
| 911 | + if e == nil || e.client == nil || sandboxID == "" { | ||
| 912 | + return nil | ||
| 913 | + } | ||
| 914 | + log.Printf("[CubeEngine] destroy cubelet sandbox=%s reason=%s", sandboxID, reason) | ||
| 915 | + if err := e.client.Destroy(ctx, sandboxID, destroyAnnotations(podRunning)); err != nil { | ||
| 916 | + return err | ||
| 917 | + } | ||
| 918 | + e.clearOrphanSeen(sandboxID) | ||
| 919 | + return nil | ||
| 920 | +} | ||
| 921 | + | ||
| 857 | func (e *Engine) CleanupSandbox(ctx context.Context, sandboxID, reason string) error { | 922 | func (e *Engine) CleanupSandbox(ctx context.Context, sandboxID, reason string) error { |
| 858 | if e == nil || !e.cfg.Enabled || sandboxID == "" { | 923 | if e == nil || !e.cfg.Enabled || sandboxID == "" { |
| 859 | return nil | 924 | return nil |
| 860 | } | 925 | } |
| 861 | - return e.destroy(ctx, sandboxID) | 926 | + // Prefer CRI-id lookup; if missing, treat the id as a Cubelet sandbox id |
| 927 | + // so Restore/Reconcile orphan cleanup tasks can still succeed. | ||
| 928 | + if _, ok := e.get(sandboxID); ok { | ||
| 929 | + return e.destroy(ctx, sandboxID) | ||
| 930 | + } | ||
| 931 | + e.mu.Lock() | ||
| 932 | + var cri string | ||
| 933 | + for id, pod := range e.pods { | ||
| 934 | + if pod != nil && pod.SandboxID == sandboxID { | ||
| 935 | + cri = id | ||
| 936 | + break | ||
| 937 | + } | ||
| 938 | + } | ||
| 939 | + e.mu.Unlock() | ||
| 940 | + if cri != "" { | ||
| 941 | + return e.destroy(ctx, cri) | ||
| 942 | + } | ||
| 943 | + return e.destroyCubeletSandbox(ctx, sandboxID, reason) | ||
| 862 | } | 944 | } |
| 863 | 945 | ||
| 864 | func (e *Engine) reconcilePause(pod *CubePodState, snap string) { | 946 | func (e *Engine) reconcilePause(pod *CubePodState, snap string) { |
| @@ -131,7 +131,7 @@ func TestRunPodSandboxRequiresTemplate(t *testing.T) { | |||
| 131 | } | 131 | } |
| 132 | } | 132 | } |
| 133 | 133 | ||
| 134 | -func TestColdCreateNotBlockedByStateFlush(t *testing.T) { | 134 | +func TestColdCreateWaitsForStateFlush(t *testing.T) { |
| 135 | store, err := engine.NewJSONStateStore(t.TempDir()) | 135 | store, err := engine.NewJSONStateStore(t.TempDir()) |
| 136 | if err != nil { | 136 | if err != nil { |
| 137 | t.Fatal(err) | 137 | t.Fatal(err) |
| @@ -177,22 +177,27 @@ func TestColdCreateNotBlockedByStateFlush(t *testing.T) { | |||
| 177 | done <- err | 177 | done <- err |
| 178 | }() | 178 | }() |
| 179 | select { | 179 | select { |
| 180 | + case <-started: | ||
| 181 | + case <-time.After(2 * time.Second): | ||
| 182 | + t.Fatal("state flush was not scheduled") | ||
| 183 | + } | ||
| 184 | + select { | ||
| 185 | + case <-done: | ||
| 186 | + t.Fatal("RunPodSandbox returned before state flush finished") | ||
| 187 | + case <-time.After(100 * time.Millisecond): | ||
| 188 | + } | ||
| 189 | + release <- nil | ||
| 190 | + select { | ||
| 180 | case err := <-done: | 191 | case err := <-done: |
| 181 | if err != nil { | 192 | if err != nil { |
| 182 | t.Fatalf("create: %v", err) | 193 | t.Fatalf("create: %v", err) |
| 183 | } | 194 | } |
| 184 | case <-time.After(2 * time.Second): | 195 | case <-time.After(2 * time.Second): |
| 185 | - t.Fatal("RunPodSandbox blocked on state flush") | 196 | + t.Fatal("RunPodSandbox did not finish after flush") |
| 186 | } | 197 | } |
| 187 | - select { | ||
| 188 | - case <-started: | ||
| 189 | - case <-time.After(2 * time.Second): | ||
| 190 | - t.Fatal("state flush was not scheduled") | ||
| 191 | - } | ||
| 192 | - release <- nil | ||
| 193 | } | 198 | } |
| 194 | 199 | ||
| 195 | -func TestColdCreatePersistDoesNotFailCreate(t *testing.T) { | 200 | +func TestColdCreatePersistFailureDestroysSandbox(t *testing.T) { |
| 196 | store, err := engine.NewJSONStateStore(t.TempDir()) | 201 | store, err := engine.NewJSONStateStore(t.TempDir()) |
| 197 | if err != nil { | 202 | if err != nil { |
| 198 | t.Fatal(err) | 203 | t.Fatal(err) |
| @@ -200,26 +205,26 @@ func TestColdCreatePersistDoesNotFailCreate(t *testing.T) { | |||
| 200 | store.SetWriteSnapshotForTest(func([]byte) error { | 205 | store.SetWriteSnapshotForTest(func([]byte) error { |
| 201 | return context.DeadlineExceeded | 206 | return context.DeadlineExceeded |
| 202 | }) | 207 | }) |
| 208 | + fake := &fakeClient{} | ||
| 203 | eng := newEngine(Config{ | 209 | eng := newEngine(Config{ |
| 204 | Enabled: true, | 210 | Enabled: true, |
| 205 | CubeletAddr: "127.0.0.1:9999", | 211 | CubeletAddr: "127.0.0.1:9999", |
| 206 | NodeName: "node-a", | 212 | NodeName: "node-a", |
| 207 | Timeout: time.Second, | 213 | Timeout: time.Second, |
| 208 | StateStore: store, | 214 | StateStore: store, |
| 209 | - }, &fakeClient{}) | 215 | + }, fake) |
| 210 | attachStubEnvd(t, eng) | 216 | attachStubEnvd(t, eng) |
| 211 | t.Cleanup(func() { _ = eng.Close() }) | 217 | t.Cleanup(func() { _ = eng.Close() }) |
| 212 | 218 | ||
| 213 | - resp, err := eng.RunPodSandbox(context.Background(), podRequest()) | 219 | + _, err = eng.RunPodSandbox(context.Background(), podRequest()) |
| 214 | - if err != nil { | 220 | + if err == nil { |
| 215 | - t.Fatalf("create should ignore persist failure: %v", err) | 221 | + t.Fatal("expected create to fail when persist fails") |
| 216 | } | 222 | } |
| 217 | - if resp.GetPodSandboxId() != "uid-1" { | 223 | + if len(fake.destroys) == 0 || fake.destroys[0].id != "sbx-1" { |
| 218 | - t.Fatalf("cri id = %s", resp.GetPodSandboxId()) | 224 | + t.Fatalf("expected cubelet destroy after persist fail, got %+v", fake.destroys) |
| 219 | } | 225 | } |
| 220 | - st, err := eng.PodSandboxStatus(context.Background(), &runtime.PodSandboxStatusRequest{PodSandboxId: "uid-1"}) | 226 | + if _, ok := eng.get("uid-1"); ok { |
| 221 | - if err != nil || st.GetStatus().GetNetwork().GetIp() != "10.1.0.2" { | 227 | + t.Fatal("local record should be cleared after persist fail") |
| 222 | - t.Fatalf("in-memory status after persist fail: err=%v status=%+v", err, st.GetStatus()) | ||
| 223 | } | 228 | } |
| 224 | } | 229 | } |
| 225 | 230 | ||
| @@ -502,6 +507,56 @@ func TestRestoreReconcilesPausedFromList(t *testing.T) { | |||
| 502 | } | 507 | } |
| 503 | } | 508 | } |
| 504 | 509 | ||
| 510 | +func TestRestoreDestroysUnknownCubeletSandbox(t *testing.T) { | ||
| 511 | + store, err := engine.NewJSONStateStore(t.TempDir()) | ||
| 512 | + if err != nil { | ||
| 513 | + t.Fatal(err) | ||
| 514 | + } | ||
| 515 | + fake := &fakeClient{list: []listedSandbox{{ID: "orphan-sbx"}}} | ||
| 516 | + eng := newEngine(Config{ | ||
| 517 | + Enabled: true, | ||
| 518 | + CubeletAddr: "127.0.0.1:9999", | ||
| 519 | + NodeName: "node-a", | ||
| 520 | + Timeout: time.Second, | ||
| 521 | + StateStore: store, | ||
| 522 | + }, fake) | ||
| 523 | + t.Cleanup(func() { _ = eng.Close() }) | ||
| 524 | + if err := eng.RestoreState(context.Background()); err != nil { | ||
| 525 | + t.Fatal(err) | ||
| 526 | + } | ||
| 527 | + if len(fake.destroys) != 1 || fake.destroys[0].id != "orphan-sbx" { | ||
| 528 | + t.Fatalf("destroys = %+v", fake.destroys) | ||
| 529 | + } | ||
| 530 | +} | ||
| 531 | + | ||
| 532 | +func TestReconcileDestroysOrphanAfterGrace(t *testing.T) { | ||
| 533 | + fake := &fakeClient{list: []listedSandbox{{ID: "orphan-sbx"}}} | ||
| 534 | + eng := newEngine(Config{ | ||
| 535 | + Enabled: true, | ||
| 536 | + CubeletAddr: "127.0.0.1:9999", | ||
| 537 | + NodeName: "node-a", | ||
| 538 | + Timeout: time.Second, | ||
| 539 | + StateStore: nil, | ||
| 540 | + }, fake) | ||
| 541 | + t.Cleanup(func() { _ = eng.Close() }) | ||
| 542 | + | ||
| 543 | + if err := eng.Reconcile(context.Background(), engine.ReconcileOptions{GracePeriod: time.Hour}); err != nil { | ||
| 544 | + t.Fatal(err) | ||
| 545 | + } | ||
| 546 | + if len(fake.destroys) != 0 { | ||
| 547 | + t.Fatalf("should honor grace, destroys=%+v", fake.destroys) | ||
| 548 | + } | ||
| 549 | + eng.orphanMu.Lock() | ||
| 550 | + eng.orphanSeen["orphan-sbx"] = time.Now().Add(-2 * time.Hour) | ||
| 551 | + eng.orphanMu.Unlock() | ||
| 552 | + if err := eng.Reconcile(context.Background(), engine.ReconcileOptions{GracePeriod: time.Hour}); err != nil { | ||
| 553 | + t.Fatal(err) | ||
| 554 | + } | ||
| 555 | + if len(fake.destroys) != 1 || fake.destroys[0].id != "orphan-sbx" { | ||
| 556 | + t.Fatalf("destroys = %+v", fake.destroys) | ||
| 557 | + } | ||
| 558 | +} | ||
| 559 | + | ||
| 505 | func TestAdminPauseCheckpointAndRuntime(t *testing.T) { | 560 | func TestAdminPauseCheckpointAndRuntime(t *testing.T) { |
| 506 | eng := newTestEngine(t, &fakeClient{}) | 561 | eng := newTestEngine(t, &fakeClient{}) |
| 507 | if _, err := eng.RunPodSandbox(context.Background(), podRequest()); err != nil { | 562 | if _, err := eng.RunPodSandbox(context.Background(), podRequest()); err != nil { |
| @@ -12,6 +12,7 @@ func (e *Engine) Module() engine.BackendModule { | |||
| 12 | Descriptor: Descriptor, | 12 | Descriptor: Descriptor, |
| 13 | Engine: e, | 13 | Engine: e, |
| 14 | Cleaner: e, | 14 | Cleaner: e, |
| 15 | + Reconciler: e, | ||
| 15 | Restorer: e, | 16 | Restorer: e, |
| 16 | } | 17 | } |
| 17 | } | 18 | } |
| @@ -19,6 +20,7 @@ func (e *Engine) Module() engine.BackendModule { | |||
| 19 | var ( | 20 | var ( |
| 20 | _ engine.RuntimeEngine = (*Engine)(nil) | 21 | _ engine.RuntimeEngine = (*Engine)(nil) |
| 21 | _ engine.Cleaner = (*Engine)(nil) | 22 | _ engine.Cleaner = (*Engine)(nil) |
| 23 | + _ engine.Reconciler = (*Engine)(nil) | ||
| 22 | _ engine.StateRestorer = (*Engine)(nil) | 24 | _ engine.StateRestorer = (*Engine)(nil) |
| 23 | _ engine.AdminPauser = (*Engine)(nil) | 25 | _ engine.AdminPauser = (*Engine)(nil) |
| 24 | _ engine.AdminCheckpointer = (*Engine)(nil) | 26 | _ engine.AdminCheckpointer = (*Engine)(nil) |
| @@ -0,0 +1,67 @@ | |||
| 1 | +package cube | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "context" | ||
| 5 | + "errors" | ||
| 6 | + "log" | ||
| 7 | + "time" | ||
| 8 | + | ||
| 9 | + "github.com/cri-multiplex/pkg/engine" | ||
| 10 | +) | ||
| 11 | + | ||
| 12 | +// Reconcile reclaims Cubelet sandboxes that have no local CRI record so they | ||
| 13 | +// cannot become permanent orphans after state loss (KASandbox #71). | ||
| 14 | +func (e *Engine) Reconcile(ctx context.Context, opts engine.ReconcileOptions) error { | ||
| 15 | + if e == nil || !e.cfg.Enabled || e.client == nil { | ||
| 16 | + return nil | ||
| 17 | + } | ||
| 18 | + listCtx, cancel := context.WithTimeout(ctx, 5*time.Second) | ||
| 19 | + defer cancel() | ||
| 20 | + listed, err := e.client.List(listCtx) | ||
| 21 | + if err != nil { | ||
| 22 | + log.Printf("[CubeEngine] reconcile list skipped: %v", err) | ||
| 23 | + return nil | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + known := e.knownCubeletIDs() | ||
| 27 | + now := time.Now() | ||
| 28 | + var ( | ||
| 29 | + errs []error | ||
| 30 | + reclaim []string | ||
| 31 | + ) | ||
| 32 | + | ||
| 33 | + e.orphanMu.Lock() | ||
| 34 | + for id := range e.orphanSeen { | ||
| 35 | + if known[id] { | ||
| 36 | + delete(e.orphanSeen, id) | ||
| 37 | + } | ||
| 38 | + } | ||
| 39 | + for _, item := range listed { | ||
| 40 | + id := item.ID | ||
| 41 | + if id == "" || known[id] { | ||
| 42 | + continue | ||
| 43 | + } | ||
| 44 | + first, ok := e.orphanSeen[id] | ||
| 45 | + if !ok { | ||
| 46 | + e.orphanSeen[id] = now | ||
| 47 | + continue | ||
| 48 | + } | ||
| 49 | + if opts.GracePeriod > 0 && now.Sub(first) < opts.GracePeriod { | ||
| 50 | + continue | ||
| 51 | + } | ||
| 52 | + reclaim = append(reclaim, id) | ||
| 53 | + } | ||
| 54 | + e.orphanMu.Unlock() | ||
| 55 | + | ||
| 56 | + for _, id := range reclaim { | ||
| 57 | + if opts.DryRun { | ||
| 58 | + log.Printf("[CubeEngine] dry-run orphan cubelet sandbox=%s", id) | ||
| 59 | + continue | ||
| 60 | + } | ||
| 61 | + log.Printf("[CubeEngine] reclaiming orphan cubelet sandbox=%s", id) | ||
| 62 | + if err := e.destroyCubeletSandbox(ctx, id, "reconcile-orphan"); err != nil { | ||
| 63 | + errs = append(errs, err) | ||
| 64 | + } | ||
| 65 | + } | ||
| 66 | + return errors.Join(errs...) | ||
| 67 | +} | ||