Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add plugin clusterLocality to favor cluster #1334

Merged
merged 1 commit into from Mar 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions pkg/scheduler/core/generic_scheduler.go
Expand Up @@ -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)
RainbowMango marked this conversation as resolved.
Show resolved Hide resolved
}
}

result = make(framework.ClusterScoreList, len(clusters))
for i := range clusters {
result[i] = framework.ClusterScore{Cluster: clusters[i], Score: 0}
Expand Down
8 changes: 8 additions & 0 deletions pkg/scheduler/framework/interface.go
Expand Up @@ -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 {
Expand Down
Expand Up @@ -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.
Expand Down
@@ -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
}
2 changes: 2 additions & 0 deletions pkg/scheduler/framework/plugins/registry.go
Expand Up @@ -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"
)

Expand All @@ -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(),
}
}
3 changes: 2 additions & 1 deletion pkg/scheduler/scheduler.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down