Repository navigation
perf(xds): cache mTLS-injected clusters per entry (drop per-snapshot proto.Clone) - #548
Merged
Merged
Conversation
clustersEndpointsAndVhosts ran proto.Clone + SPIFFE SAN rendering +
serviceref.ParseKey + waypoint-SNI concat + InjectUpstreamMTLS (or the edge
transport socket) for every cluster entry on EVERY snapshot generation —
O(clusters) deep proto copies per push, on every node, constantly during
churn — even though none of those inputs change between entry mutations.
Move the work to entry build/invalidation time:
- clusterEntry gains sanURIs (the rendered expected-server SPIFFE IDs) and
mtlsCluster (the fully injected cluster proto). refreshEntryMTLSLocked
rebuilds both from a point-in-time localMTLSState snapshot; the snapshot
path now just emits entry.mtlsCluster (or the bare cluster while the node
SVID is unset — same gate as before).
- Invalidation points (every writer of the consumed inputs):
- LoadClustersFromRegistry recomputes inline under the same clusterMu hold
that rebuilds the entry map (covers sanNamespaces/service/sni changes and
retained grace-period entries);
- setLocalWorkload / removeLocalWorkload / the LoadListenersFromStorage
merge (netns→SPIFFE-ID map + trust domain);
- SetNodeIdentity (node SVID arrival/rotation);
- SetEdgeIdentity (edge SVID + trust domain).
- The cached proto is built fresh (clone + inject) on every refresh and only
ever REPLACED, never mutated in place: snapshots already handed to
go-control-plane alias it and xDS goroutines marshal without clusterMu.
- captureTCPClusters / edgeTCPClusters reuse the cached sanURIs (same inputs,
same recompute points), dropping their duplicate per-snapshot sprintf/parse;
their few TCP-floor cluster builds are otherwise unchanged.
- Lock order for the nested read is clusterMu (outer) → localMu (inner); the
local state is snapshotted inside the clusterMu critical section so the
last recompute to serialize always renders from the final local state.
snapshotMu scope and the unified full-snapshot model are untouched.
Emitted xDS resources are byte-identical: the new
TestCachedMTLSClusterMatchesInlineInjection table test proves proto equality
against the old inline injection for node, node+waypoint, and edge modes, and
the new invalidation tests cover workload add/remove, late node SVID, and
endpoint-namespace (SAN) changes. //agent/... green, race-clean.
Fixes #537
Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01EioJVuoepwStjHoRLB52WR
This was referenced Jul 19, 2026
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.
What
clustersEndpointsAndVhosts()ran, for every cluster entry with a node SPIFFE ID on every snapshot generation:proto.Clone(entry.cluster), per-namespacefmt.SprintfSPIFFE SAN rendering,serviceref.ParseKey, waypoint-SNI concat, andproxy.InjectUpstreamMTLS/proxy.EdgeUpstreamTransportSocket. None of those inputs change per snapshot — they change when the entry is (re)built, whensanNamespaceschange, or when the local netns→ID map / node SVID / trust domain change. That was O(clusters) proto deep-copies per push, on every node, constantly during churn.This moves the work to entry build/invalidation time (new
mtls.go):clusterEntrygainssanURIs(rendered expected-server SPIFFE IDs) andmtlsCluster(the fully injected cluster proto).refreshEntryMTLSLockedrebuilds both from a point-in-timelocalMTLSState. The snapshot path now just emitsentry.mtlsCluster, falling back to the bare cluster while the node SVID is unset (same gate as before).captureTCPClusters/edgeTCPClustersreuse the cachedsanURIs(same inputs, same recompute points), dropping their duplicate per-snapshot sprintf/parse (capture.go:262-265and the edge twin).Invalidation points (every writer of the consumed inputs)
LoadClustersFromRegistry— recomputes inline under the sameclusterMuhold that rebuilds the entry map (coverssanNamespaces/service/snichanges, freshly built AND retained grace-period entries).setLocalWorkload/removeLocalWorkload/ theLoadListenersFromStoragemerge — netns→SPIFFE-ID map + trust domain changes (pod add/remove, startup load).SetNodeIdentity— node SVID arrival/rotation (gates injection entirely).SetEdgeIdentity— edge SVID + trust domain.Lock order for the nested read is
clusterMu(outer) →localMu(inner); the local state is snapshotted inside theclusterMucritical section, so with every mutation followed by a recompute, whichever recompute serializes last renders from the final local state.Safety
clusterMu.snapshotMuscope/semantics and the unified full-snapshot model are untouched.TestCachedMTLSClusterMatchesInlineInjectionproves proto equality against the old inline injection for node, node+waypoint, and edge modes; new tests also cover invalidation on workload add/remove, late node SVID, and endpoint-namespace (SAN) changes.Validation
bazel test //agent/...— 23/23 pass.//agent/internal/xds/cache:cache_testalso green under--@rules_go//go/config:race=true(includesraces_test.go).agent.snapshot.generateduration metric to drop visibly once deployed.Fixes #537
🤖 Generated with Claude Code
https://claude.ai/code/session_01EioJVuoepwStjHoRLB52WR