Skip to content

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

Open
amalsaidov wants to merge 1 commit into
projectsveltos:mainfrom
amalsaidov:fix-set-maxreplicas-off-by-one
Open

amalsaidov wants to merge 1 commit into
projectsveltos:mainfrom
amalsaidov:fix-set-maxreplicas-off-by-one

Conversation

@amalsaidov

Copy link
Copy Markdown

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.

This branch has not been deployed

No deployments
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.

1 participant