-
Notifications
You must be signed in to change notification settings - Fork 819
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add plugin clusterLocality to favor cluster
Signed-off-by: huone1 <huwanxing@huawei.com>
- Loading branch information
Showing
5 changed files
with
79 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
pkg/scheduler/framework/plugins/clusterlocality/cluster_locality.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package clusterlocality | ||
|
||
import ( | ||
"context" | ||
|
||
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" | ||
"github.com/karmada-io/karmada/pkg/scheduler/framework" | ||
"github.com/karmada-io/karmada/pkg/util" | ||
) | ||
|
||
const ( | ||
// Name is the name of the plugin used in the plugin registry and configurations. | ||
Name = "ClusterLocality" | ||
) | ||
|
||
// ClusterLocality is a score plugin that favors cluster that already have requested. | ||
type ClusterLocality struct{} | ||
|
||
var _ framework.ScorePlugin = &ClusterLocality{} | ||
|
||
// New instantiates the clusteraffinity plugin. | ||
func New() framework.Plugin { | ||
return &ClusterLocality{} | ||
} | ||
|
||
// Name returns the plugin name. | ||
func (p *ClusterLocality) Name() string { | ||
return Name | ||
} | ||
|
||
// Score calculates the score on the candidate cluster. | ||
// if cluster object is exist in resourceBinding.Spec.Clusters, Score is 100, otherwise it is 0. | ||
func (p *ClusterLocality) Score(ctx context.Context, placement *policyv1alpha1.Placement, | ||
spec *workv1alpha2.ResourceBindingSpec, cluster *clusterv1alpha1.Cluster) (int64, *framework.Result) { | ||
if len(spec.Clusters) == 0 { | ||
return framework.MinClusterScore, framework.NewResult(framework.Success) | ||
} | ||
|
||
replicas := util.GetSumOfReplicas(spec.Clusters) | ||
if replicas <= 0 { | ||
return framework.MinClusterScore, framework.NewResult(framework.Success) | ||
} | ||
|
||
if isClusterScheduled(cluster.Name, spec.Clusters) { | ||
return framework.MaxClusterScore, framework.NewResult(framework.Success) | ||
} | ||
|
||
return framework.MinClusterScore, framework.NewResult(framework.Success) | ||
} | ||
|
||
// ScoreExtensions of the Score plugin. | ||
func (p *ClusterLocality) ScoreExtensions() framework.ScoreExtensions { | ||
return nil | ||
} | ||
|
||
func isClusterScheduled(candidate string, schedulerClusters []workv1alpha2.TargetCluster) bool { | ||
for _, cluster := range schedulerClusters { | ||
if candidate == cluster.Name { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters