Skip to content

Commit

Permalink
Fix error
Browse files Browse the repository at this point in the history
  • Loading branch information
hexiaofeng committed Aug 14, 2024
1 parent 7eab13a commit b048562
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
1 change: 1 addition & 0 deletions pkg/scheduler/core/spreadconstraint/group_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ func born(parent *groupNode, constraints []policyv1alpha1.SpreadConstraint, inde
born(child, constraints, index+1)
} else {
child.Leaf = true
child.compute()
}
}
parent.compute()
Expand Down
17 changes: 8 additions & 9 deletions pkg/scheduler/core/spreadconstraint/group_election.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ import (
// Elect method to select clusters based on the required replicas
func (root *groupRoot) Elect() ([]*clusterv1alpha1.Cluster, error) {
selects := make(map[string]*clusterDesc)
_, err := root.selectCluster(root.Replicas, selects)
i, err := root.selectCluster(root.Replicas, selects)
if err != nil {
return nil, err
} else if root.Replicas > 0 && i < root.Replicas {
return nil, fmt.Errorf("no enough resource when selecting %d %ss", root.MaxGroups, root.Constraint)
}
var clusters []*clusterDesc
for _, cluster := range selects {
Expand Down Expand Up @@ -73,7 +75,7 @@ func (node *groupNode) selectByGroups(replicas int32, selects map[string]*cluste
remain := replicas
adds := int32(0)
groups := len(node.Groups)
if groups <= node.MinGroups {
if groups < node.MinGroups {
return 0, errors.New("the number of feasible clusters is less than spreadConstraint.MinGroups")
}
maxGroups := node.MaxGroups
Expand All @@ -97,16 +99,13 @@ func (node *groupNode) selectByGroups(replicas int32, selects map[string]*cluste
for i := maxGroups; i < groups; i++ {
_, _ = node.Groups[i].selectByClusters(InvalidReplicas, left)
}
i, err := node.supplement(selects, left, remain)
if err != nil {
return 0, err
}
i := node.supplement(selects, left, remain)
return adds + i, nil
}

// supplement attempts to balance the cluster nodes by supplementing the selected nodes
// with the remaining nodes to meet the required replicas.
func (node *groupNode) supplement(selects map[string]*clusterDesc, remains map[string]*clusterDesc, replicas int32) (int32, error) {
func (node *groupNode) supplement(selects map[string]*clusterDesc, remains map[string]*clusterDesc, replicas int32) int32 {
adds := int32(0)
var selectArray []*clusterDesc
for _, cluster := range selects {
Expand Down Expand Up @@ -138,11 +137,11 @@ func (node *groupNode) supplement(selects map[string]*clusterDesc, remains map[s
delete(selects, selectArray[j].Name)
selects[remainArray[maxIndex].Name] = remainArray[maxIndex]
if replicas <= 0 {
return adds, nil
break
}
selectArray[j], remainArray[maxIndex] = remainArray[maxIndex], selectArray[j]
}
}

return 0, fmt.Errorf("no enough resource when selecting %d %ss", node.MaxGroups, node.Constraint)
return adds
}
31 changes: 31 additions & 0 deletions pkg/scheduler/core/spreadconstraint/select_clusters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,37 @@ func TestSelectBestClusters(t *testing.T) {
clusterScores[3].Cluster,
},
},
{
name: "select clusters by region and cluster",
ctx: SelectionCtx{
ClusterScores: clusterScores,
Placement: &policyv1alpha1.Placement{
SpreadConstraints: []policyv1alpha1.SpreadConstraint{
{
SpreadByField: policyv1alpha1.SpreadByFieldCluster,
MaxGroups: 1,
MinGroups: 1,
},
{
SpreadByField: policyv1alpha1.SpreadByFieldRegion,
MaxGroups: 2,
MinGroups: 2,
},
},
ReplicaScheduling: &policyv1alpha1.ReplicaSchedulingStrategy{
ReplicaSchedulingType: policyv1alpha1.ReplicaSchedulingTypeDivided,
},
},
Spec: &workv1alpha2.ResourceBindingSpec{
Replicas: 120,
},
ReplicasFunc: replicasFunc,
},
want: []*clusterv1alpha1.Cluster{
clusterScores[1].Cluster,
clusterScores[2].Cluster,
},
},
{
name: "select clusters by cluster score and satisfy available resources",
ctx: SelectionCtx{
Expand Down

0 comments on commit b048562

Please sign in to comment.