Skip to content

fix: keep MaxReplicas selected clusters when trimming a Set - #2107

Merged
gianlucam76 merged 1 commit into
projectsveltos:mainfrom
amalsaidov:fix-set-maxreplicas-off-by-one
Nov 19, 2026
Merged

gianlucam76 merged 1 commit into
projectsveltos:mainfrom
amalsaidov:fix-set-maxreplicas-off-by-one

Conversation

@amalsaidov

Copy link
Copy Markdown
Contributor

Problem

In selectClusters (controllers/set_utils.go), when a (Cluster)Set already has more selected clusters than MaxReplicas, the extra clusters are dropped with:

status.SelectedClusterRefs = currentSelectedClusters[:spec.MaxReplicas-1]

This keeps MaxReplicas-1 clusters instead of MaxReplicas, so the Set ends up one cluster short of the requested replica count. This branch is reached whenever len(currentSelectedClusters) > MaxReplicas, for example after a user lowers MaxReplicas on an existing Set that already had more clusters selected.

The rest of the function treats exactly MaxReplicas as the target: the early return fires on len(currentSelectedClusters) == spec.MaxReplicas, and selectMoreClusters stops once the count reaches MaxReplicas. The drop path is inconsistent with that.

Fix

Slice to [:spec.MaxReplicas] so exactly MaxReplicas clusters remain.

Test

Added a unit test covering the drop path (4 selected clusters, MaxReplicas of 2). It fails before the fix (selects 1) and passes after (selects 2).

Verified locally:

  • go test ./controllers/ (focused on Set utils) passes
  • gofmt and go vet clean

AI-assisted; directed, reviewed and tested by the author.

When a (Cluster)Set already has more selected clusters than MaxReplicas
(for example after MaxReplicas is lowered), selectClusters trimmed the
slice with currentSelectedClusters[:spec.MaxReplicas-1], leaving
MaxReplicas-1 clusters selected instead of MaxReplicas.

Slice to [:spec.MaxReplicas] so exactly MaxReplicas clusters remain,
matching the behavior of the select-more path and the == MaxReplicas
early return.

Add a unit test covering the drop path, which fails before the fix
(selects 1 instead of 2) and passes after.

AI-assisted; directed, reviewed and tested by the author.

@gianlucam76 gianlucam76 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@gianlucam76
gianlucam76 merged commit 289f574 into projectsveltos:main Nov 19, 2026
13 checks passed
@amalsaidov

Copy link
Copy Markdown
Contributor Author

Thanks for merging 🙏

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.

2 participants