Merge pull request #5144 from mszacillo/sort-clusterinfo
GroupClusters should sort by score and availableReplica count
This commit is contained in:
commit
15df251200
|
@ -17,6 +17,8 @@ limitations under the License.
|
||||||
package spreadconstraint
|
package spreadconstraint
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"k8s.io/utils/ptr"
|
||||||
|
|
||||||
clusterv1alpha1 "github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1"
|
clusterv1alpha1 "github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1"
|
||||||
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
|
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
|
||||||
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
|
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
|
||||||
|
@ -143,7 +145,12 @@ func (info *GroupClustersInfo) generateClustersInfo(clustersScore framework.Clus
|
||||||
info.Clusters[i].AvailableReplicas += int64(rbSpec.AssignedReplicasForCluster(clustersReplica.Name))
|
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) {
|
func (info *GroupClustersInfo) generateZoneInfo(spreadConstraints []policyv1alpha1.SpreadConstraint) {
|
||||||
|
|
|
@ -20,6 +20,8 @@ import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"k8s.io/utils/ptr"
|
||||||
|
|
||||||
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
|
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -75,9 +77,10 @@ func TestIsSpreadConstraintExisted(t *testing.T) {
|
||||||
|
|
||||||
func Test_sortClusters(t *testing.T) {
|
func Test_sortClusters(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
infos []ClusterDetailInfo
|
infos []ClusterDetailInfo
|
||||||
want []ClusterDetailInfo
|
want []ClusterDetailInfo
|
||||||
|
compareFunction func(*ClusterDetailInfo, *ClusterDetailInfo) *bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "different scores",
|
name: "different scores",
|
||||||
|
@ -103,7 +106,7 @@ func Test_sortClusters(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "same score",
|
name: "same score, default to sort by name",
|
||||||
infos: []ClusterDetailInfo{
|
infos: []ClusterDetailInfo{
|
||||||
{
|
{
|
||||||
Name: "b",
|
Name: "b",
|
||||||
|
@ -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 {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
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) {
|
if !reflect.DeepEqual(tt.infos, tt.want) {
|
||||||
t.Errorf("sortClusters() = %v, want %v", tt.infos, tt.want)
|
t.Errorf("sortClusters() = %v, want %v", tt.infos, tt.want)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue