Skip to content

Commit

Permalink
add plugin clusterLocality to favor cluster
Browse files Browse the repository at this point in the history
Signed-off-by: huone1 <huwanxing@huawei.com>
  • Loading branch information
huone1 committed Mar 25, 2022
1 parent 042762b commit 0a02032
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 2 deletions.
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

0 comments on commit 0a02032

Please sign in to comment.