Commit Graph
34 Commits
Author SHA1 Message Date
“Naeel” 919e84396c fix(reconciler): propagate SA/executor errors to NamespaceManager so failed NSes are retried
Problem
-------
The namespace reconciler (RunReconciler, added previously) retries namespaces
in NamespacePhaseFailed every 30s by calling DispatchResync. But the phase
could never actually reach NamespacePhaseFailed for the executor component
because the executor's NamespaceSubscriber always returned nil — swallowing
any SA-provisioning or informer-init errors. The reconciler was dead code for
the executor path.

Root cause chain
----------------
1. setupSAAndRoleBindings() — void, errors only logged internally.
2. EnsureNamespaceSA()      — void, just called setupSAAndRoleBindings.
3. registerNamespace()      — void, errors from both functions lost.
4. Executor AddFunc/ResyncFunc — always returned nil to dispatch().
5. dispatch() marks parts Active unconditionally   → NamespacePhaseFailed
   is never triggered for executor   → RunReconciler never fires for executor.

Consequence: if EnsureNamespaceSA failed (transient k8s 503, RBAC webhook
timeout, etc.) the namespace appeared Active in the manager but the fetcher
ServiceAccount was missing. Pool pods would CrashLoopBackOff on every call
to that namespace until a full process restart.

Changes
-------
pkg/utils/serviceaccount.go
  - setupSAAndRoleBindings: void → error. Returns the first k8s API error
    so callers can decide whether to retry.
  - runSACheck: ignores the error with _ = (same behaviour as before, it's
    a periodic background loop that already logs internally).
  - EnsureNamespaceSA: void → error, propagates setupSAAndRoleBindings.
    Updated godoc to explain the retry contract.

pkg/executor/multitenant/ns_watcher.go
  - registerNamespace: void → error.
    * EnsureNamespaceSA error → wrapped as 'EnsureNamespaceSA: ...' and returned.
    * registerExecutorTypes error → wrapped as 'registerExecutorTypes: ...' and returned.
    * Success log line only emitted when both succeed.
  - Added 'fmt' import for error wrapping.

pkg/executor/multitenant/namespace_subscriber.go
  - AddFunc:    return registerNamespace(...) instead of ignoring its error.
  - ResyncFunc: same — plus a comment explaining why it is safe to call
    registerNamespace again (SA creation is idempotent, executor-type
    AddNamespace guards against duplicate informer creation).

pkg/utils/namespace_manager.go
  - RunReconciler interface signature: added *zap.Logger parameter.
    Callers pass the component logger so retries are visible in prod logs.
  - RunReconciler implementation:
    * Accepts logger; falls back to zap.NewNop() if nil.
    * Skips the tick entirely when no failed namespaces are found (no log spam).
    * Logs 'retrying failed namespaces' with count + list when found.
    * Logs per-namespace 'dispatching resync'.
    * Logs 'resync succeeded' or 'resync still failing, will retry' with error.
  - RunManagedNamespaceWatcher: passes logger to RunReconciler.

End-to-end flow after this fix
-------------------------------
1. EnsureNamespaceSA fails (k8s 503).
2. registerNamespace returns error.
3. Executor AddFunc returns error.
4. dispatch() calls MarkPartFailed("executor") → deriveNamespacePhase →
   NamespacePhaseFailed.
5. RunReconciler tick (30s) finds the namespace → DispatchResync →
   registerNamespace called again → EnsureNamespaceSA (idempotent) →
   if API recovered: success → MarkPartActive → NamespacePhaseActive.
6. Log line 'namespace reconciler: resync succeeded' confirms recovery.

Backward compatibility
----------------------
- NamespaceManager interface: RunReconciler gained a *zap.Logger param.
  There is exactly one implementation (inMemoryNamespaceManager) and one
  call site (RunManagedNamespaceWatcher). No external mocks.
- EnsureNamespaceSA: callers outside this codebase (if any) that ignore
  the error will still compile (Go allows ignoring return values).
- All 26 affected tests pass: go test ./pkg/utils/... ./pkg/executor/...
  ./pkg/buildermgr/... ./pkg/router/...
2026-05-18 11:10:46 +04:00
“Naeel” 3b93c5dc8b fix(namespace): harden lifecycle — RemoveNamespace, parallel dispatch, reconciler
- DefaultNSResolver.RemoveNamespace(): removes NS from global map on label removal
  so Snapshot() and idleObjectReaper stop iterating deleted namespaces.
  Fixes class of dirty-state bugs when NS name is reused by new tenant.

- HandleWatcherNamespaceRemoval: call RemoveNamespace on both TrackOnly and
  DispatchRemove strategies — global resolver cleanup is always required.

- dispatch(): parallel subscriber execution via goroutine per subscriber +
  sync.WaitGroup. Reduces onboarding latency from O(N_subscribers × API_latency)
  to O(max(API_latency)). Safe: MarkPart* are internally mutex-protected.

- inMemoryNamespaceManager.RunReconciler(): 30s ticker scans for
  NamespacePhaseFailed records and retries via DispatchResync. Started
  automatically by RunManagedNamespaceWatcher. Fixes permanent stuck-failed
  state caused by transient k8s API errors.

Analysis source: FORENSIC_ARCHITECTURE_AUDIT.md §Deep Risk Analysis
2026-05-18 08:45:31 +04:00
Naeel 7b6ff84188 layer1: harden namespace watcher logging path 2026-04-26 16:27:20 +03:00
Naeel 55d0b5a9e7 layer1: log namespace watcher startup summary 2026-04-26 16:24:29 +03:00
Naeel 813617ffd1 layer1: log watcher namespace transitions 2026-04-26 11:52:06 +03:00
Naeel 7d7fe561a8 layer1: log namespace summary active flag 2026-04-26 11:51:03 +03:00
Naeel 9f0e911b9d layer1: stabilize namespace summary contract 2026-04-26 11:46:15 +03:00
Naeel 90924cdec7 layer1: checkpoint namespace manager runtime series 2026-04-26 11:44:36 +03:00
Naeel 6e037a506d layer1: add default watcher config helper 2026-04-26 11:03:39 +03:00
Naeel d2ff55f9e0 layer1: add managed watcher config 2026-04-26 11:02:09 +03:00
Naeel b9236698f3 layer1: run managed namespace watchers 2026-04-26 11:01:05 +03:00
Naeel 073f2c1504 layer1: add live namespace counts 2026-04-26 10:59:39 +03:00
Naeel b93e720e12 layer1: add namespace source counts 2026-04-26 10:59:01 +03:00
Naeel f16aa030db layer1: log namespace manager summary 2026-04-26 10:57:29 +03:00
Naeel 5c481b7293 layer1: add namespace manager summary 2026-04-26 10:55:56 +03:00
Naeel 2bbed95c2a layer1: prepare managed namespace watchers 2026-04-26 10:54:24 +03:00
Naeel a1517ba4b2 layer1: share managed namespace watcher startup 2026-04-26 10:51:31 +03:00
Naeel 49be1db3a0 layer1: share namespace watcher event handlers 2026-04-26 10:50:28 +03:00
Naeel c3b161da83 layer1: share update removal policy 2026-04-26 10:49:26 +03:00
Naeel 0755319fac layer1: formalize namespace removal strategy 2026-04-26 10:48:42 +03:00
Naeel 340b9cae84 layer1: centralize namespace watcher handlers 2026-04-26 10:47:12 +03:00
Naeel 4cd4bc9507 layer1: share namespace watcher lifecycle helpers 2026-04-26 10:45:03 +03:00
Naeel 0e08664ef6 layer1: share watcher namespace manager bootstrap 2026-04-26 10:41:14 +03:00
Naeel d09bee3431 layer1: add namespace remove dispatch step 33 2026-04-26 10:36:48 +03:00
Naeel ad0f83fd4b layer1: add namespace bootstrap dispatch step 26 2026-04-26 10:32:43 +03:00
Naeel bcf34d6b1a layer1: add namespace subscriber adapter step 19 2026-04-26 10:24:01 +03:00
Naeel b1e2e6462d layer1: add namespace dispatch step 17 2026-04-26 10:22:35 +03:00
Naeel ae8275d0a7 layer1: add namespace lifecycle subscribers step 16 2026-04-26 10:21:26 +03:00
Naeel db4499d8c7 layer1: add namespace part helpers step 14 2026-04-26 10:05:12 +03:00
Naeel 5e5058ba0e layer1: add namespace manager bridge step 12 2026-04-26 10:03:09 +03:00
Naeel 42acce308f layer1: add namespace bootstrap step 11 2026-04-26 10:02:37 +03:00
Naeel 66a3dc2a3c layer1: derive namespace phases step 10 2026-04-26 10:02:03 +03:00
Naeel 910f65b6d4 layer1: add namespace manager subscribers step 9 2026-04-26 10:01:21 +03:00
Naeel 114d5b99af layer1: add namespace manager skeleton step 8 2026-04-26 10:00:45 +03:00