fix(controller): canonicalise peer order in the agent state - #16
Merged
Conversation
The reconciler rewrites the Wireguard Secret whenever the marshalled state differs from what is stored, and every rewrite patches wgConfigLastUpdated on EVERY tenant's WG pod. filteredPeers is built by iterating peers.Items from a controller-runtime CACHED List, which returns informer-map iteration order, so it reshuffles between reconciles even when no peer changed -- and that reshuffle alone makes the marshalled bytes differ. Measured on managed-superopti: 340 "Updating secret with new config" in 37h (~9/h) against 40 peers whose set never changed, and 8 consecutive pushes carried ONE peer set in EIGHT distinct orders. Normalising only the order collapsed all 8 to a single hash -- no other field, and specifically not resourceVersion/generation/managedFields, differed between them. The churn is entirely ordering, so nothing else is touched here. Extract stateForAgent() and sort a copy of the peers by name: names are unique within a namespace and always set, so name alone is a total order. Copy rather than sort in place -- the caller built the slice and may still be using it. #15 already made the agent skip the iptables-restore when the rendered ruleset is unchanged, so these pushes are no longer harmful. This removes the pointless secret rewrites and the fleet-wide pod pokes behind them. Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #15. Manager-side counterpart: stops the pointless state pushes at the source.
Why
The reconciler rewrites the Wireguard Secret whenever the marshalled state differs from what's stored, and every rewrite patches
wgConfigLastUpdatedon every tenant's WG pod.filteredPeersis built by iteratingpeers.Itemsfrom a controller-runtime cachedr.List, which returns informer-map iteration order — so it reshuffles between reconciles even when no peer changed, and the reshuffle alone makes the bytes differ.Measured on
managed-superopti: 340Updating secret with new configin 37h (~9/h) against 40 peers whose set never changed. Per-tenant counts track peer count (pivit 90, internal 82, optimised 80, wavesquared 51, indigo 47, legion 12, edgecentres 4).Scope, narrowed by evidence
I originally intended to also strip volatile peer
metadata(resourceVersion,generation,managedFields) from the marshalled state. Re-checking 8 capturedstate.jsonsnapshots says that isn't warranted:Sorting alone accounts for all of it —
resourceVersion/generation/managedFieldswere identical across every push. Narrowing the state format the agent consumes would be risk for no measured benefit, so this change is the sort only.Change
Extract
stateForAgent()(pure refactor of two identical construction sites) and sort a copy of the peers by name — unique within a namespace and always set, so a total order. Copying keeps the caller's slice untouched.The manager only needs a stable order; the agent re-sorts on
(Address, AddressV6, Name)for rendering regardless.Tests
TestStateForAgentIsByteIdenticalRegardlessOfPeerOrder— asserts the invariant that actually decides whether the Secret is rewritten. Watched failing:marshalled state depends on peer list order.TestStateForAgentDoesNotReorderCallerPeers— guards against sorting in place.make testgreen, including theinternal/controllerenvtest suite (85s, 75.8% coverage).Not urgent
#15 already stopped the harm — the agent skips the restore when the ruleset is unchanged, so these pushes cost nothing but wasted work. This is cleanup, and is deliberately not being deployed yet: containers stays pinned at
0ce1117.🤖 Generated with Claude Code