target: cache compiled LabelSelector per ClusterGroup to reduce CPU#5445
Open
himabindugit wants to merge 3 commits into
Open
target: cache compiled LabelSelector per ClusterGroup to reduce CPU#5445himabindugit wants to merge 3 commits into
himabindugit wants to merge 3 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR optimizes target selection by caching compiled Kubernetes LabelSelector objects per ClusterGroup (keyed by namespace/name@resourceVersion) to reduce repeated metav1.LabelSelectorAsSelector compilations during reconcile loops.
Changes:
- Implement per-
Managerselector compilation caching viasync.MapinclusterGroupsForCluster. - Add unit tests for selector matching behavior and caching behavior.
- Extend the
target.Managerstruct to hold the selector cache.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| internal/cmd/controller/target/query.go | Adds selector caching in clusterGroupsForCluster to reduce CPU/allocations during target queries. |
| internal/cmd/controller/target/query_test.go | Adds unit tests for selector matching/caching behavior. |
| internal/cmd/controller/target/builder.go | Adds a sync.Map field to Manager to store the selector cache. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.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.
#5444
Refers to #5444
Problem
clusterGroupsForClustercallsmetav1.LabelSelectorAsSelectoron every reconcile loop for every ClusterGroup, with no caching. At scale this causes an allocation storm.With ~3900 clusters × 10 ClusterGroups: 39,130
LabelSelectorAsSelectorcompilations perOnBundleChangeevent. pprof showedLabelSelectorAsSelectorconsuming 61% CPU on the fleet-controller, leading to OOM and lease-lock failures.Reported in: #5444
Fix
Cache the compiled
labels.Selectorin async.Mapon theManager, keyed bynamespace/name@resourceVersion. TheResourceVersionensures the selector is recompiled when a ClusterGroup is updated.ClusterGroups change infrequently and their count is small, so stale entries from old ResourceVersions are acceptable and not evicted.
Added unit tests covering:
Additional Information
Checklist