Skip to content

perf(xds): cache mTLS-injected clusters per entry (drop per-snapshot proto.Clone) - #548

Merged
bpalermo merged 1 commit into
mainfrom
perf/537-cluster-entry-mtls-cache
Jul 19, 2026
Merged

bpalermo merged 1 commit into
mainfrom
perf/537-cluster-entry-mtls-cache

Conversation

@bpalermo

Copy link
Copy Markdown
Owner

What

clustersEndpointsAndVhosts() ran, for every cluster entry with a node SPIFFE ID on every snapshot generation: proto.Clone(entry.cluster), per-namespace fmt.Sprintf SPIFFE SAN rendering, serviceref.ParseKey, waypoint-SNI concat, and proxy.InjectUpstreamMTLS / proxy.EdgeUpstreamTransportSocket. None of those inputs change per snapshot — they change when the entry is (re)built, when sanNamespaces change, 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):

  • clusterEntry gains sanURIs (rendered expected-server SPIFFE IDs) and mtlsCluster (the fully injected cluster proto). refreshEntryMTLSLocked rebuilds both from a point-in-time localMTLSState. The snapshot path now just emits entry.mtlsCluster, falling back to the bare cluster while the node SVID is unset (same gate as before).
  • captureTCPClusters / edgeTCPClusters reuse the cached sanURIs (same inputs, same recompute points), dropping their duplicate per-snapshot sprintf/parse (capture.go:262-265 and the edge twin).

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, freshly built AND retained grace-period entries).
  • setLocalWorkload / removeLocalWorkload / the LoadListenersFromStorage merge — 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 the clusterMu critical section, so with every mutation followed by a recompute, whichever recompute serializes last renders from the final local state.

Safety

  • The cached proto is built fresh (clone + inject) on each refresh and only ever replaced on the entry, never mutated in place — snapshots already set on go-control-plane alias it and xDS server goroutines marshal it without holding clusterMu.
  • snapshotMu scope/semantics and the unified full-snapshot model are untouched.
  • Emitted xDS resources are byte-identical: TestCachedMTLSClusterMatchesInlineInjection proves 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_test also green under --@rules_go//go/config:race=true (includes races_test.go).
  • Expect the agent.snapshot.generate duration metric to drop visibly once deployed.

Fixes #537

🤖 Generated with Claude Code

https://claude.ai/code/session_01EioJVuoepwStjHoRLB52WR

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
@bpalermo
bpalermo merged commit 3510326 into main Jul 19, 2026
14 checks passed
@bpalermo
bpalermo deleted the perf/537-cluster-entry-mtls-cache branch July 19, 2026 14:18
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.

perf(xds): cache the mTLS-injected cluster per entry (drop per-snapshot proto.Clone)

1 participant