Merge pull request #1334 from huone1/feature/clusterlocality

add plugin clusterLocality to favor cluster
This commit is contained in:
karmada-bot 2022-03-26 14:18:56 +08:00 committed by GitHub
commit 431677b1a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 85 additions and 2 deletions

View File

@ -115,6 +115,12 @@ func (g *genericScheduler) prioritizeClusters(
return result, err
}
if klog.V(4).Enabled() {
for plugin, nodeScoreList := range scoresMap {
klog.Infof("Plugin %s scores on %v/%v => %v", plugin, spec.Resource.Namespace, spec.Resource.Name, nodeScoreList)
}
}
result = make(framework.ClusterScoreList, len(clusters))
for i := range clusters {
result[i] = framework.ClusterScore{Cluster: clusters[i], Score: 0}

View File

@ -10,6 +10,14 @@ import (
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
)
const (
// MinClusterScore is the minimum score a Score plugin is expected to return.
MinClusterScore int64 = 0
// MaxClusterScore is the maximum score a Score plugin is expected to return.
MaxClusterScore int64 = 100
)
// Framework manages the set of plugins in use by the scheduling framework.
// Configured plugins are called at specified points in a scheduling context.
type Framework interface {

View File

@ -48,7 +48,7 @@ func (p *ClusterAffinity) Filter(ctx context.Context, placement *policyv1alpha1.
// Score calculates the score on the candidate cluster.
func (p *ClusterAffinity) Score(ctx context.Context, placement *policyv1alpha1.Placement,
spec *workv1alpha2.ResourceBindingSpec, cluster *clusterv1alpha1.Cluster) (int64, *framework.Result) {
return 0, framework.NewResult(framework.Success)
return framework.MinClusterScore, framework.NewResult(framework.Success)
}
// ScoreExtensions of the Score plugin.

View File

@ -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
}

View File

@ -4,6 +4,7 @@ import (
"github.com/karmada-io/karmada/pkg/scheduler/framework"
"github.com/karmada-io/karmada/pkg/scheduler/framework/plugins/apiinstalled"
"github.com/karmada-io/karmada/pkg/scheduler/framework/plugins/clusteraffinity"
"github.com/karmada-io/karmada/pkg/scheduler/framework/plugins/clusterlocality"
"github.com/karmada-io/karmada/pkg/scheduler/framework/plugins/tainttoleration"
)
@ -13,5 +14,6 @@ func NewPlugins() map[string]framework.Plugin {
clusteraffinity.Name: clusteraffinity.New(),
tainttoleration.Name: tainttoleration.New(),
apiinstalled.Name: apiinstalled.New(),
clusterlocality.Name: clusterlocality.New(),
}
}

View File

@ -36,6 +36,7 @@ import (
"github.com/karmada-io/karmada/pkg/scheduler/core"
"github.com/karmada-io/karmada/pkg/scheduler/framework/plugins/apiinstalled"
"github.com/karmada-io/karmada/pkg/scheduler/framework/plugins/clusteraffinity"
"github.com/karmada-io/karmada/pkg/scheduler/framework/plugins/clusterlocality"
"github.com/karmada-io/karmada/pkg/scheduler/framework/plugins/tainttoleration"
"github.com/karmada-io/karmada/pkg/scheduler/metrics"
"github.com/karmada-io/karmada/pkg/util"
@ -99,7 +100,7 @@ func NewScheduler(dynamicClient dynamic.Interface, karmadaClient karmadaclientse
queue := workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
schedulerCache := schedulercache.NewCache(clusterLister)
// TODO: make plugins as a flag
algorithm := core.NewGenericScheduler(schedulerCache, []string{clusteraffinity.Name, tainttoleration.Name, apiinstalled.Name})
algorithm := core.NewGenericScheduler(schedulerCache, []string{clusteraffinity.Name, tainttoleration.Name, apiinstalled.Name, clusterlocality.Name})
sched := &Scheduler{
DynamicClient: dynamicClient,
KarmadaClient: karmadaClient,