Skip to content

refactor(agent): reduce cognitive complexity <=15 in cni server, hotrestart, spire (part of #556) - #561

Merged
bpalermo merged 1 commit into
mainfrom
refactor/cognit-lane3-agent-internals
Jul 19, 2026
Merged

bpalermo merged 1 commit into
mainfrom
refactor/cognit-lane3-agent-internals

Conversation

@bpalermo

Copy link
Copy Markdown
Owner

Summary

Lane 3 of the cognitive-complexity reduction campaign (#556). Reduces all non-test functions in agent/internal/cni/server/, agent/internal/proxy/hotrestart/, and agent/internal/spire/ to ≤15 via pure structural extraction — no behavior changes.

Per-function extraction summary

Function Before After Technique
(*CNIServer).sweepGhostEndpoints 75 9 Extracted listRegistryEndpoints, pruneStaleStoragePods, pruneOnePod, reportMissingStoragePods, classifyPods, deregisterGhostEndpoints, deregisterGhostEndpointsForService, registeredIPs, registerMissingEndpoints
(*Supervisor).Run 68 8 Extracted restartLoop struct with run, handleSignal, handleDebounce, handleChildExit methods; extracted handleShutdown, handleDebounce, handleChildExit, retryBindCollision on Supervisor
(*CNIServer).reconcileLiveness 39 10 Extracted applyPodLiveness, registerHealthTransition, livenessWant, livenessPrev
(*Supervisor).watchLiveness 36 12 Extracted onLiveEpoch, onNotLiveEpoch, checkWedgeWatchdogs
(*Bridge).Start 34 7 Extracted runBundleSubscriptionLoop, drainBundleStream, handleBundleSubscribeError, logBundleSubscribed
(*Bridge).SubscribePod 34 10 Extracted runSVIDSubscriptionLoop, drainSVIDStream, resubscribeSVIDs

Verification

$ $BIN -over 15 agent/internal/cni/server/ agent/internal/proxy/hotrestart/ agent/internal/spire/
(no output — all functions ≤15)
$ bazel test --test_output=errors //agent/...
26 tests pass.

make gazelle — no BUILD changes needed.
make format — imports ordered; no additional changes.

Notes

  • Supervisor.Run (originally 68): the event loop is complex by nature. Introduced a restartLoop helper struct to carry loop state (debounce timer, retry counters) so the select cases can be delegated to named methods without large parameter lists. The select case ordering is unchanged.
  • watchLiveness state transitions, channel ordering, and wedge watchdog sequence are preserved verbatim — only the dead-code blocks were moved to helpers.
  • sweepGhostEndpoints decision order (list → lock → prune stale → report missing → classify → deregister ghosts → register missing) is unchanged.
  • Bridge.Start/SubscribePod subscription/retry/cleanup ordering preserved exactly.

Part of #556

🤖 Generated with Claude Code

https://claude.ai/code/session_01EioJVuoepwStjHoRLB52WR

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