Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GroupClusters should sort by score and availableReplica count #5144

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion pkg/scheduler/core/spreadconstraint/group_clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package spreadconstraint

import (
"k8s.io/utils/ptr"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Worth noting - the linter was failing because the original import (k8s.io/utils/pointer) is deprecated. This new import will work for newer karmada versions, but is not currently compatible with the version we use (v1.9.0) because it uses an older k8s-util version.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the information.
I wonder if you maintain some private commits based on v1.9.0?
I'm asking because I want to know if this going to be a problem for you. I mean if this patch not present on release 1.9.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We have a couple private commits on our v1.9.0 branch, but most of them we are planning to contribute via our published proposals. I think we can try updating to the v1.10.0 release branch which will have the updated k8s-util version and can pick up this commit. I'll test v1.10 with out setup and confirm.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Confirmed changes work with v1.10 - I think we can move forward with upgrading our local Karmada version.


clusterv1alpha1 "github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1"
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
Expand Down Expand Up @@ -143,7 +145,12 @@ func (info *GroupClustersInfo) generateClustersInfo(clustersScore framework.Clus
info.Clusters[i].AvailableReplicas += int64(rbSpec.AssignedReplicasForCluster(clustersReplica.Name))
}

sortClusters(info.Clusters)
sortClusters(info.Clusters, func(i *ClusterDetailInfo, j *ClusterDetailInfo) *bool {
if i.AvailableReplicas != j.AvailableReplicas {
return ptr.To(i.AvailableReplicas > j.AvailableReplicas)
}
return nil
})
}

func (info *GroupClustersInfo) generateZoneInfo(spreadConstraints []policyv1alpha1.SpreadConstraint) {
Expand Down
50 changes: 45 additions & 5 deletions pkg/scheduler/core/spreadconstraint/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"reflect"
"testing"

"k8s.io/utils/ptr"

policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
)

Expand Down Expand Up @@ -75,9 +77,10 @@ func TestIsSpreadConstraintExisted(t *testing.T) {

func Test_sortClusters(t *testing.T) {
tests := []struct {
name string
infos []ClusterDetailInfo
want []ClusterDetailInfo
name string
infos []ClusterDetailInfo
want []ClusterDetailInfo
compareFunction func(*ClusterDetailInfo, *ClusterDetailInfo) *bool
}{
{
name: "different scores",
Expand All @@ -103,7 +106,7 @@ func Test_sortClusters(t *testing.T) {
},
},
{
name: "same score",
name: "same score, default to sort by name",
infos: []ClusterDetailInfo{
{
Name: "b",
Expand All @@ -129,10 +132,47 @@ func Test_sortClusters(t *testing.T) {
},
},
},
{
name: "same score, sort by availableReplicas",
infos: []ClusterDetailInfo{
{
Name: "a",
Score: 1,
AvailableReplicas: 5,
},
{
Name: "b",
Score: 1,
AvailableReplicas: 10,
},
},
want: []ClusterDetailInfo{
{
Name: "b",
Score: 1,
AvailableReplicas: 10,
},
{
Name: "a",
Score: 1,
AvailableReplicas: 5,
},
},
compareFunction: func(i *ClusterDetailInfo, j *ClusterDetailInfo) *bool {
if i.AvailableReplicas != j.AvailableReplicas {
return ptr.To(i.AvailableReplicas > j.AvailableReplicas)
}
return nil
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sortClusters(tt.infos)
if tt.compareFunction != nil {
sortClusters(tt.infos, tt.compareFunction)
} else {
sortClusters(tt.infos)
}
if !reflect.DeepEqual(tt.infos, tt.want) {
t.Errorf("sortClusters() = %v, want %v", tt.infos, tt.want)
}
Expand Down