Skip to content

fix(cni): guarantee the local storage delete on CNI DEL (#261) - #262

Merged
bpalermo merged 1 commit into
mainfrom
fix/cni-del-guaranteed-storage-delete
Jun 22, 2026
Merged

bpalermo merged 1 commit into
mainfrom
fix/cni-del-guaranteed-storage-delete

Conversation

@bpalermo

Copy link
Copy Markdown
Owner

The prevention layer for #261 (ghost storage entries → dead-netns listeners → connection_error).

Root cause

RemovePod removed local storage last, gated behind a fatal, request-ctx-bound UnregisterEndpoints (and the listener removal). So when the request ctx is canceled — the plugin's del timeout, a kubelet cancel, or agent SIGTERM (the xDS server force-Stop()s in-flight RPCs after ShutdownTimeout) — or the registrar is briefly unavailable (we observed UnregisterEndpoints fail context canceled during a registrar roll), the handler returns before storage.RemoveResource. That leaves a ghost storage entry whose netns pin lingers (so the #245 stale-netns guard can't prune it) and whose listener is re-emitted against a dead netns on agent restart.

AddPod is already robust the right way: it stores first and treats RegisterEndpoint as best-effort. RemovePod was the asymmetric opposite.

Fix

Make DEL mirror ADD:

  • Detached context (context.WithoutCancel) so request-ctx cancellation can't skip the removal.
  • Remove local storage FIRST — the authoritative local truth, and the only fatal error (kubelet retries the DEL). Once gone, the liveness loop can't resurrect the endpoint and the ghost sweep deregisters whatever's left in the registry.
  • Listener removal + endpoint deregistration are best-effort (logged, not fatal); the snapshot rebuild/restart drops the listener and the ghost sweep reconciles the registry. The deregister is bounded (unregisterTimeout) so a rolling registrar can't hang the DEL.

Why this is safe (the reconcile question)

The ghost sweep already deregisters any registry endpoint with no live local pod (every 60s + on registrar reconnect), with local storage as the source of truth. So removing storage-first + best-effort unregister self-heals: a failed unregister leaves an orphan that the next sweep (this agent, or its successor after SIGTERM) deregisters. No resurrection — the sweep only re-registers live local pods, and this one is gone.

Tests

  • The old unregister failure returns error case now asserts the DEL is tolerated (SUCCESS).
  • New TestRemovePodDurableDeleteOnCanceledCtx: with an already-canceled ctx AND a down registrar, the DEL succeeds and local storage is still removed.

Code-only; bazel build //... green; cni-server tests pass. The reconcile (cure) and startup taint (cold-start) are separate PRs on #261.

RemovePod removed local storage LAST, gated behind a fatal, request-context-bound
UnregisterEndpoints (and the listener removal). So a canceled request ctx (the
plugin's del timeout, a kubelet cancel, or agent SIGTERM — the xDS server
force-stops in-flight RPCs after ShutdownTimeout) OR a transient registrar
outage (observed during a registrar roll: UnregisterEndpoints fails with
"context canceled") made the handler return BEFORE storage.RemoveResource —
leaving a ghost storage entry whose netns pin lingers (so the #245 stale-netns
guard can't prune it) and whose listener is re-emitted on agent restart against
a dead netns. This is the prevention root cause behind the prober/edge
connection_error degradation.

Make DEL mirror AddPod (which stores first + treats registration as best-effort):
- Run the removal on a detached context (context.WithoutCancel) so request-ctx
  cancellation can't skip it.
- Remove LOCAL STORAGE FIRST — the authoritative local truth. It's the only fatal
  error (kubelet retries the DEL). Once gone, the liveness loop can't resurrect
  the endpoint and the ghost sweep deregisters whatever is left in the registry.
- Listener removal + endpoint deregistration are now BEST-EFFORT (logged, not
  fatal); the snapshot rebuild / restart drops the listener and the ghost sweep
  reconciles the registry. The deregister is bounded (unregisterTimeout) so it
  can't hang the DEL when the registrar is rolling.

Tests: the old "unregister failure returns error" case now asserts the DEL is
tolerated (SUCCESS); new TestRemovePodDurableDeleteOnCanceledCtx proves storage
is still removed with an already-canceled ctx AND a down registrar.

This is the prevention layer; the storage<->pods reconcile (cure) and the
startup taint (cold-start) are separate PRs on #261.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01N7oY7W8oDXrqDzFgoZh25S
@bpalermo
bpalermo merged commit 94ae752 into main Jun 22, 2026
14 checks passed
@bpalermo
bpalermo deleted the fix/cni-del-guaranteed-storage-delete branch June 22, 2026 17:31
bpalermo added a commit that referenced this pull request Jun 22, 2026
…sue #261) (#264)

The ghost sweep reconciled the registry against local storage but never
reconciled local storage against the node's real pods, so two desync classes
went uncured (talos worker-01, 2026-06-22):

  - Orphan ghost: a missed CNI DEL that left the netns bind-mount pin behind.
    netnsExists() stats the lingering pin and reports present, so the existing
    stale-prune never removed it; the agent kept emitting a listener bound to a
    dead netns (prober-vhbp8) — a connection sink and a latent #245 segfault.
  - Missing pod: a live, mesh-managed pod with no storage entry (a lost CNI ADD)
    that therefore had no listener and 100% connection_error (prober-k7vsm), and
    never self-healed.

The agent's manager cache is already scoped to spec.nodeName=<this node>, so
listing the node's pods is local and cheap (no new RBAC — it already has
pods get/list/watch). The sweep now:

  - Prunes a stored pod when EITHER its netns is gone (existing) OR its
    Kubernetes pod no longer exists — by pod identity, regardless of a lingering
    pin. The pruned pod's registry endpoint then falls through to ghost
    deregistration as before. If the pod list fails we skip prune-by-absence
    (an empty list would nuke every entry) but still run the netns prune.
  - Surfaces live, Running, mesh-managed pods that storage has no entry for: the
    agent can't synthesize the CNI data to self-heal, so it emits a metric +
    warning (the pod must be rolled) instead of silently serving no listener.

New metrics: ghost_sweep.orphans_pruned, ghost_sweep.missing_storage. Tests:
TestSweepPrunesOrphanedPods (lingering-pin orphan pruned + ghost-deregistered,
live pod kept) and TestIsMeshManagedK8sPod; existing sweep tests keep a nil
k8sClient and skip the pod reconcile via a defensive guard.

This is the cure layer; the guaranteed-delete reorder (#262) is the prevention
and the startup taint (#263) closes the cold-start window.


Claude-Session: https://claude.ai/code/session_01N7oY7W8oDXrqDzFgoZh25S

Co-authored-by: Claude Opus 4.8 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant