Skip to content

refactor(sac): keep highest scope state for duplicate clusters and ns#19506

Draft
rhybrillou wants to merge 1 commit intomaster-yann/ROX-33511/scope-selection-by-cluster-idfrom
master-yann/ROX-33511/update-tree-nodes-on-duplicate-cluster-or-namespace
Draft

refactor(sac): keep highest scope state for duplicate clusters and ns#19506
rhybrillou wants to merge 1 commit intomaster-yann/ROX-33511/scope-selection-by-cluster-idfrom
master-yann/ROX-33511/update-tree-nodes-on-duplicate-cluster-or-namespace

Conversation

@rhybrillou
Copy link
Contributor

@rhybrillou rhybrillou commented Mar 19, 2026

Description

This PR is part of the split of #19351
The split results in the following stack of PRs:

The code changed here is unlikely to be actually called in production, as in that case, the list of clusters and namespaces are provided by the database, which has unicity constraints on the cluster name, and relays kubernetes namespace information from the clusters (the latter ones should enforce namespace unicity too).
Nevertheless, if some duplicate entries for a given node of the access scope tree (root -> cluster -> namespace) were provided in input (that is database/cache lookup at the start of processing of a service call), then the highest selection result (excluded < partial < included) is kept for that node instead of the last seen one.

User-facing documentation

Testing and quality

  • the change is production ready: the change is GA, or otherwise the functionality is gated by a feature flag
  • CI results are inspected

Automated testing

  • added unit tests

How I validated my change

Manual CI run.

There should be no duplicate cluster or namespace in the access scope computation for actual service calls. E2e validation of this change might be tricky. However, there should be no change in behaviour for the nominal case, and the current SAC e2e tests should catch changes in behaviour.

@openshift-ci
Copy link

openshift-ci bot commented Mar 19, 2026

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • In populateStateForCluster, when a cluster with an existing name but a different ID is processed, clusterIDToName is no longer updated (due to the early return), which changes previous behavior where the last-seen cluster ID for that name was recorded; consider whether this regression is acceptable or if all IDs for a given name should be tracked.
  • The new logic in populateStateForNamespace only updates the state of an existing namespace entry but never refreshes its attributes, unlike the cluster-level logic; if attributes are expected to reflect the latest selection/detail context, you may want to update them there as well for consistency.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `populateStateForCluster`, when a cluster with an existing name but a different ID is processed, `clusterIDToName` is no longer updated (due to the early return), which changes previous behavior where the last-seen cluster ID for that name was recorded; consider whether this regression is acceptable or if all IDs for a given name should be tracked.
- The new logic in `populateStateForNamespace` only updates the state of an existing namespace entry but never refreshes its attributes, unlike the cluster-level logic; if attributes are expected to reflect the latest selection/detail context, you may want to update them there as well for consistency.

## Individual Comments

### Comment 1
<location path="pkg/sac/effectiveaccessscope/effective_access_scope.go" line_range="321-327" />
<code_context>
 	// Match the cluster.
 	clusterState := ruleSelectors.matchCluster(cluster)

+	// Set the cluster state to the pre-existing state.
+	if clusterSubTree := root.Clusters[clusterName]; clusterSubTree != nil {
+		if clusterSubTree.State < clusterState {
+			clusterSubTree.State = clusterState
+			clusterSubTree.Attributes = nodeAttributesForCluster(cluster, detail)
+		}
+		return
+	}
+
</code_context>
<issue_to_address>
**issue (bug_risk):** Reusing existing cluster subtree skips clusterIDToName updates, which may break lookups for additional cluster IDs with the same name.

With this early return, later clusters that share `clusterName` but have lower/equal `clusterState` will skip `root.clusterIDToName[clusterID]` updates. Previously, even when reusing/overwriting the subtree, the new `clusterID` was still mapped to `clusterName`. If callers expect all cluster IDs to resolve via `clusterIDToName`, those lookups can now fail. You can avoid this by updating `root.clusterIDToName[clusterID]` before returning, regardless of whether the state changes.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +321 to +327
// Set the cluster state to the pre-existing state.
if clusterSubTree := root.Clusters[clusterName]; clusterSubTree != nil {
if clusterSubTree.State < clusterState {
clusterSubTree.State = clusterState
clusterSubTree.Attributes = nodeAttributesForCluster(cluster, detail)
}
return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Reusing existing cluster subtree skips clusterIDToName updates, which may break lookups for additional cluster IDs with the same name.

With this early return, later clusters that share clusterName but have lower/equal clusterState will skip root.clusterIDToName[clusterID] updates. Previously, even when reusing/overwriting the subtree, the new clusterID was still mapped to clusterName. If callers expect all cluster IDs to resolve via clusterIDToName, those lookups can now fail. You can avoid this by updating root.clusterIDToName[clusterID] before returning, regardless of whether the state changes.

@rhacs-bot
Copy link
Contributor

rhacs-bot commented Mar 19, 2026

Images are ready for the commit at 100b9a2.

To use with deploy scripts, first export MAIN_IMAGE_TAG=4.11.x-329-g100b9a2236.

@codecov
Copy link

codecov bot commented Mar 19, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 49.72%. Comparing base (ff9a4f5) to head (6773139).

Additional details and impacted files
@@                                   Coverage Diff                                   @@
##           master-yann/ROX-33511/scope-selection-by-cluster-id   #19506      +/-   ##
=======================================================================================
+ Coverage                                                49.69%   49.72%   +0.03%     
=======================================================================================
  Files                                                     2701     2701              
  Lines                                                   203514   203524      +10     
=======================================================================================
+ Hits                                                    101136   101211      +75     
+ Misses                                                   94845    94788      -57     
+ Partials                                                  7533     7525       -8     
Flag Coverage Δ
go-unit-tests 49.72% <100.00%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@rhybrillou rhybrillou changed the title refactor(sac): keep highest scope state for duplicate clusters and na… refactor(sac): keep highest scope state for duplicate clusters and ns Mar 19, 2026
@rhybrillou rhybrillou force-pushed the master-yann/ROX-33511/scope-selection-by-cluster-id branch from ff9a4f5 to 8abc271 Compare March 19, 2026 17:31
@rhybrillou rhybrillou force-pushed the master-yann/ROX-33511/update-tree-nodes-on-duplicate-cluster-or-namespace branch from 6773139 to 9602a55 Compare March 19, 2026 17:31
@rhybrillou rhybrillou force-pushed the master-yann/ROX-33511/scope-selection-by-cluster-id branch from 8abc271 to 9c74804 Compare March 19, 2026 17:48
@rhybrillou rhybrillou force-pushed the master-yann/ROX-33511/update-tree-nodes-on-duplicate-cluster-or-namespace branch from 9602a55 to cbc8794 Compare March 19, 2026 17:48
@rhybrillou rhybrillou force-pushed the master-yann/ROX-33511/scope-selection-by-cluster-id branch from 9c74804 to 38cb56c Compare March 19, 2026 17:54
@rhybrillou rhybrillou force-pushed the master-yann/ROX-33511/update-tree-nodes-on-duplicate-cluster-or-namespace branch from cbc8794 to 100b9a2 Compare March 19, 2026 17:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants