From 483d5d9fa03a0f155f99106739aa6d1e11df8b50 Mon Sep 17 00:00:00 2001 From: lihanbo Date: Thu, 13 May 2021 14:57:12 +0800 Subject: [PATCH] support schedule based on cluster taint toleration Signed-off-by: lihanbo --- .../propagationpolicy_taintToleration.yaml | 16 ++++++ pkg/scheduler/framework/plugins/registry.go | 2 + .../tainttoleration/taint_toleration.go | 55 +++++++++++++++++++ pkg/scheduler/scheduler.go | 3 +- 4 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 artifacts/example/propagationpolicy_taintToleration.yaml create mode 100644 pkg/scheduler/framework/plugins/tainttoleration/taint_toleration.go diff --git a/artifacts/example/propagationpolicy_taintToleration.yaml b/artifacts/example/propagationpolicy_taintToleration.yaml new file mode 100644 index 000000000..158228ae6 --- /dev/null +++ b/artifacts/example/propagationpolicy_taintToleration.yaml @@ -0,0 +1,16 @@ +apiVersion: policy.karmada.io/v1alpha1 +kind: PropagationPolicy +metadata: + name: nginx-propagation +spec: + resourceSelectors: + - apiVersion: apps/v1 + kind: Deployment + name: nginx + placement: + clusterAffinity: + clusterNames: + - member1 + - member2 + clusterTolerations: + - effect: "NoSchedule" diff --git a/pkg/scheduler/framework/plugins/registry.go b/pkg/scheduler/framework/plugins/registry.go index f7d366d7f..3d05bc602 100644 --- a/pkg/scheduler/framework/plugins/registry.go +++ b/pkg/scheduler/framework/plugins/registry.go @@ -3,11 +3,13 @@ package plugins import ( "github.com/karmada-io/karmada/pkg/scheduler/framework" "github.com/karmada-io/karmada/pkg/scheduler/framework/plugins/clusteraffinity" + "github.com/karmada-io/karmada/pkg/scheduler/framework/plugins/tainttoleration" ) // NewPlugins builds all the scheduling plugins. func NewPlugins() map[string]framework.Plugin { return map[string]framework.Plugin{ clusteraffinity.Name: clusteraffinity.New(), + tainttoleration.Name: tainttoleration.New(), } } diff --git a/pkg/scheduler/framework/plugins/tainttoleration/taint_toleration.go b/pkg/scheduler/framework/plugins/tainttoleration/taint_toleration.go new file mode 100644 index 000000000..44273e413 --- /dev/null +++ b/pkg/scheduler/framework/plugins/tainttoleration/taint_toleration.go @@ -0,0 +1,55 @@ +package tainttoleration + +import ( + "context" + "fmt" + + v1 "k8s.io/api/core/v1" + v1helper "k8s.io/kubernetes/pkg/apis/core/v1/helper" + + cluster "github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1" + "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1" + "github.com/karmada-io/karmada/pkg/scheduler/framework" +) + +const ( + // Name is the name of the plugin used in the plugin registry and configurations. + Name = "TaintToleration" +) + +// TaintToleration is a plugin that checks if a propagation policy tolerates a cluster's taints. +type TaintToleration struct{} + +var _ framework.FilterPlugin = &TaintToleration{} +var _ framework.ScorePlugin = &TaintToleration{} + +// New instantiates the TaintToleration plugin. +func New() framework.Plugin { + return &TaintToleration{} +} + +// Name returns the plugin name. +func (p *TaintToleration) Name() string { + return Name +} + +// Filter checks if the propagation policy tolerates a cluster's taints. +func (p *TaintToleration) Filter(ctx context.Context, placement *v1alpha1.Placement, cluster *cluster.Cluster) *framework.Result { + filterPredicate := func(t *v1.Taint) bool { + // only interested in NoSchedule taints. + return t.Effect == v1.TaintEffectNoSchedule + } + + taint, isUntolerated := v1helper.FindMatchingUntoleratedTaint(cluster.Spec.Taints, placement.ClusterTolerations, filterPredicate) + if !isUntolerated { + return framework.NewResult(framework.Success) + } + + return framework.NewResult(framework.Unschedulable, fmt.Sprintf("cluster had taint {%s: %s}, that the propagation policy didn't tolerate", + taint.Key, taint.Value)) +} + +// Score calculates the score on the candidate cluster. +func (p *TaintToleration) Score(ctx context.Context, placement *v1alpha1.Placement, cluster *cluster.Cluster) (float64, *framework.Result) { + return 0, framework.NewResult(framework.Success) +} diff --git a/pkg/scheduler/scheduler.go b/pkg/scheduler/scheduler.go index 0fbcb83b9..2c6a4b8c3 100644 --- a/pkg/scheduler/scheduler.go +++ b/pkg/scheduler/scheduler.go @@ -32,6 +32,7 @@ import ( schedulercache "github.com/karmada-io/karmada/pkg/scheduler/cache" "github.com/karmada-io/karmada/pkg/scheduler/core" "github.com/karmada-io/karmada/pkg/scheduler/framework/plugins/clusteraffinity" + "github.com/karmada-io/karmada/pkg/scheduler/framework/plugins/tainttoleration" "github.com/karmada-io/karmada/pkg/util" ) @@ -106,7 +107,7 @@ func NewScheduler(dynamicClient dynamic.Interface, karmadaClient karmadaclientse queue := workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()) schedulerCache := schedulercache.NewCache() // TODO: make plugins as a flag - algorithm := core.NewGenericScheduler(schedulerCache, policyLister, []string{clusteraffinity.Name}) + algorithm := core.NewGenericScheduler(schedulerCache, policyLister, []string{clusteraffinity.Name, tainttoleration.Name}) sched := &Scheduler{ DynamicClient: dynamicClient, KarmadaClient: karmadaClient,