@@ -20,6 +20,7 @@ import (
2020 "context"
2121 "fmt"
2222 "math"
23+ "sort"
2324
2425 "k8s.io/apimachinery/pkg/util/sets"
2526 "k8s.io/klog/v2"
@@ -53,22 +54,13 @@ func getDefaultWeightPreference(clusters []*clusterv1alpha1.Cluster) *policyv1al
5354
5455func calAvailableReplicas (clusters []* clusterv1alpha1.Cluster , spec * workv1alpha2.ResourceBindingSpec ) []workv1alpha2.TargetCluster {
5556 availableTargetClusters := make ([]workv1alpha2.TargetCluster , len (clusters ))
56- // availableResultPriority indicates which priority Estimator gives the scheduling result for each cluster, respectively
57- // 1. Higher-priority estimator can directly override the result given by lower-priority estimator
58- // 2. Lower replicas estimated will be chosen if the priority of estimator are same
59- // 3. Lower-priority estimator would be ignored if the result was already given by higher-priority estimator
60- availableResultPriority := make ([]estimatorclient.EstimatorPriority , len (clusters ))
6157
6258 // Set the boundary.
6359 for i := range availableTargetClusters {
6460 availableTargetClusters [i ].Name = clusters [i ].Name
6561 availableTargetClusters [i ].Replicas = math .MaxInt32
6662 }
6763
68- for i := range availableResultPriority {
69- availableResultPriority [i ] = estimatorclient .Undefined
70- }
71-
7264 // For non-workload, like ServiceAccount, ConfigMap, Secret and etc, it's unnecessary to calculate available replicas in member clusters.
7365 // See issue: https://github.com/karmada-io/karmada/issues/3743.
7466 if spec .Replicas == 0 {
@@ -79,30 +71,29 @@ func calAvailableReplicas(clusters []*clusterv1alpha1.Cluster, spec *workv1alpha
7971
8072 // Get the minimum value of MaxAvailableReplicas in terms of all estimators.
8173 estimators := estimatorclient .GetReplicaEstimators ()
74+ sort .Slice (estimators , func (i , j int ) bool {
75+ return estimators [i ].GetPriority () > estimators [j ].GetPriority ()
76+ })
77+
78+ priorityOfAvailableEstimator := estimatorclient .EstimatorPriority (math .MinInt32 )
8279 ctx := context .WithValue (context .TODO (), util .ContextKeyObject ,
8380 fmt .Sprintf ("kind=%s, name=%s/%s" , spec .Resource .Kind , spec .Resource .Namespace , spec .Resource .Name ))
8481 for _ , estimator := range estimators {
82+ if estimator .GetPriority () < priorityOfAvailableEstimator {
83+ break
84+ }
8585 res , err := estimator .MaxAvailableReplicas (ctx , clusters , spec .ReplicaRequirements )
8686 if err != nil {
8787 klog .Errorf ("Max cluster available replicas error: %v" , err )
8888 continue
8989 }
90+ priorityOfAvailableEstimator = estimator .GetPriority ()
9091 for i := range res {
91- if res [i ].Name != availableTargetClusters [i ].Name {
92- continue
93- }
9492 if res [i ].Replicas == estimatorclient .UnauthenticReplica {
9593 continue
9694 }
97- // means there are already replicas calculated by higher-priority estimator
98- if estimator .GetPriority () < availableResultPriority [i ] {
99- continue
100- }
101- // override if current estimator has higher priority and take lower replicas is estimator priority are same
102- if (estimator .GetPriority () == availableResultPriority [i ] && res [i ].Replicas < availableTargetClusters [i ].Replicas ) ||
103- estimator .GetPriority () > availableResultPriority [i ] {
95+ if availableTargetClusters [i ].Name == res [i ].Name && availableTargetClusters [i ].Replicas > res [i ].Replicas {
10496 availableTargetClusters [i ].Replicas = res [i ].Replicas
105- availableResultPriority [i ] = estimator .GetPriority ()
10697 }
10798 }
10899 }
0 commit comments