k3s/vendor/k8s.io/kubernetes/pkg/scheduler/factory.go

378 lines
14 KiB
Go
Raw Normal View History

2019-12-12 01:27:03 +00:00
/*
Copyright 2014 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package scheduler
import (
2020-03-26 21:07:15 +00:00
"context"
"errors"
2019-12-12 01:27:03 +00:00
"fmt"
"time"
2020-03-26 21:07:15 +00:00
"github.com/google/go-cmp/cmp"
2019-12-12 01:27:03 +00:00
v1 "k8s.io/api/core/v1"
2020-03-26 21:07:15 +00:00
apierrors "k8s.io/apimachinery/pkg/api/errors"
2019-12-12 01:27:03 +00:00
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2020-03-26 21:07:15 +00:00
"k8s.io/apimachinery/pkg/runtime"
2019-12-12 01:27:03 +00:00
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/informers"
clientset "k8s.io/client-go/kubernetes"
corelisters "k8s.io/client-go/listers/core/v1"
2020-08-10 17:43:49 +00:00
"k8s.io/klog/v2"
2020-03-26 21:07:15 +00:00
"k8s.io/kubernetes/pkg/scheduler/algorithmprovider"
2019-12-12 01:27:03 +00:00
schedulerapi "k8s.io/kubernetes/pkg/scheduler/apis/config"
"k8s.io/kubernetes/pkg/scheduler/apis/config/validation"
"k8s.io/kubernetes/pkg/scheduler/core"
"k8s.io/kubernetes/pkg/scheduler/framework"
2020-03-26 21:07:15 +00:00
frameworkplugins "k8s.io/kubernetes/pkg/scheduler/framework/plugins"
"k8s.io/kubernetes/pkg/scheduler/framework/plugins/defaultbinder"
"k8s.io/kubernetes/pkg/scheduler/framework/plugins/defaultpreemption"
2020-03-26 21:07:15 +00:00
"k8s.io/kubernetes/pkg/scheduler/framework/plugins/noderesources"
"k8s.io/kubernetes/pkg/scheduler/framework/plugins/queuesort"
2020-08-10 17:43:49 +00:00
frameworkruntime "k8s.io/kubernetes/pkg/scheduler/framework/runtime"
2019-12-12 01:27:03 +00:00
internalcache "k8s.io/kubernetes/pkg/scheduler/internal/cache"
cachedebugger "k8s.io/kubernetes/pkg/scheduler/internal/cache/debugger"
internalqueue "k8s.io/kubernetes/pkg/scheduler/internal/queue"
2020-03-26 21:07:15 +00:00
"k8s.io/kubernetes/pkg/scheduler/profile"
2019-12-12 01:27:03 +00:00
)
// Binder knows how to write a binding.
type Binder interface {
Bind(binding *v1.Binding) error
}
// Configurator defines I/O, caching, and other functionality needed to
// construct a new scheduler.
type Configurator struct {
client clientset.Interface
2020-03-26 21:07:15 +00:00
recorderFactory profile.RecorderFactory
2019-12-12 01:27:03 +00:00
informerFactory informers.SharedInformerFactory
// Close this to stop all reflectors
StopEverything <-chan struct{}
schedulerCache internalcache.Cache
2020-03-26 21:07:15 +00:00
// Always check all predicates even if the middle of one predicate fails.
alwaysCheckAllPredicates bool
2019-12-12 01:27:03 +00:00
// percentageOfNodesToScore specifies percentage of all nodes to score in each scheduling cycle.
percentageOfNodesToScore int32
podInitialBackoffSeconds int64
podMaxBackoffSeconds int64
2020-08-10 17:43:49 +00:00
profiles []schedulerapi.KubeSchedulerProfile
registry frameworkruntime.Registry
nodeInfoSnapshot *internalcache.Snapshot
extenders []schedulerapi.Extender
frameworkCapturer FrameworkCapturer
parallellism int32
2019-12-12 01:27:03 +00:00
}
2020-03-26 21:07:15 +00:00
// create a scheduler from a set of registered plugins.
func (c *Configurator) create() (*Scheduler, error) {
2020-08-10 17:43:49 +00:00
var extenders []framework.Extender
2020-03-26 21:07:15 +00:00
var ignoredExtendedResources []string
if len(c.extenders) != 0 {
2020-08-10 17:43:49 +00:00
var ignorableExtenders []framework.Extender
2020-03-26 21:07:15 +00:00
for ii := range c.extenders {
klog.V(2).InfoS("Creating extender", "extender", c.extenders[ii])
2020-03-26 21:07:15 +00:00
extender, err := core.NewHTTPExtender(&c.extenders[ii])
2019-12-12 01:27:03 +00:00
if err != nil {
return nil, err
}
if !extender.IsIgnorable() {
extenders = append(extenders, extender)
} else {
ignorableExtenders = append(ignorableExtenders, extender)
}
2020-03-26 21:07:15 +00:00
for _, r := range c.extenders[ii].ManagedResources {
2019-12-12 01:27:03 +00:00
if r.IgnoredByScheduler {
2020-03-26 21:07:15 +00:00
ignoredExtendedResources = append(ignoredExtendedResources, r.Name)
2019-12-12 01:27:03 +00:00
}
}
}
// place ignorable extenders to the tail of extenders
extenders = append(extenders, ignorableExtenders...)
}
2020-03-26 21:07:15 +00:00
// If there are any extended resources found from the Extenders, append them to the pluginConfig for each profile.
2020-08-10 17:43:49 +00:00
// This should only have an effect on ComponentConfig v1beta1, where it is possible to configure Extenders and
2020-03-26 21:07:15 +00:00
// plugin args (and in which case the extender ignored resources take precedence).
// For earlier versions, using both policy and custom plugin config is disallowed, so this should be the only
// plugin config for this plugin.
if len(ignoredExtendedResources) > 0 {
for i := range c.profiles {
prof := &c.profiles[i]
2020-08-10 17:43:49 +00:00
pc := schedulerapi.PluginConfig{
Name: noderesources.FitName,
Args: &schedulerapi.NodeResourcesFitArgs{
IgnoredResources: ignoredExtendedResources,
},
}
prof.PluginConfig = append(prof.PluginConfig, pc)
2020-03-26 21:07:15 +00:00
}
2019-12-12 01:27:03 +00:00
}
2020-08-10 17:43:49 +00:00
// The nominator will be passed all the way to framework instantiation.
nominator := internalqueue.NewPodNominator()
// It's a "cluster event" -> "plugin names" map.
clusterEventMap := make(map[framework.ClusterEvent]sets.String)
profiles, err := profile.NewMap(c.profiles, c.registry, c.recorderFactory,
frameworkruntime.WithClientSet(c.client),
frameworkruntime.WithInformerFactory(c.informerFactory),
frameworkruntime.WithSnapshotSharedLister(c.nodeInfoSnapshot),
frameworkruntime.WithRunAllFilters(c.alwaysCheckAllPredicates),
frameworkruntime.WithPodNominator(nominator),
frameworkruntime.WithCaptureProfile(frameworkruntime.CaptureProfile(c.frameworkCapturer)),
frameworkruntime.WithClusterEventMap(clusterEventMap),
frameworkruntime.WithParallelism(int(c.parallellism)),
)
2019-12-12 01:27:03 +00:00
if err != nil {
2020-03-26 21:07:15 +00:00
return nil, fmt.Errorf("initializing profiles: %v", err)
2019-12-12 01:27:03 +00:00
}
2020-03-26 21:07:15 +00:00
if len(profiles) == 0 {
return nil, errors.New("at least one profile is required")
2019-12-12 01:27:03 +00:00
}
2020-03-26 21:07:15 +00:00
// Profiles are required to have equivalent queue sort plugins.
lessFn := profiles[c.profiles[0].SchedulerName].QueueSortFunc()
2019-12-12 01:27:03 +00:00
podQueue := internalqueue.NewSchedulingQueue(
2020-03-26 21:07:15 +00:00
lessFn,
c.informerFactory,
2019-12-12 01:27:03 +00:00
internalqueue.WithPodInitialBackoffDuration(time.Duration(c.podInitialBackoffSeconds)*time.Second),
internalqueue.WithPodMaxBackoffDuration(time.Duration(c.podMaxBackoffSeconds)*time.Second),
2020-08-10 17:43:49 +00:00
internalqueue.WithPodNominator(nominator),
internalqueue.WithClusterEventMap(clusterEventMap),
2019-12-12 01:27:03 +00:00
)
// Setup cache debugger.
debugger := cachedebugger.New(
c.informerFactory.Core().V1().Nodes().Lister(),
c.informerFactory.Core().V1().Pods().Lister(),
2019-12-12 01:27:03 +00:00
c.schedulerCache,
podQueue,
)
debugger.ListenForSignal(c.StopEverything)
algo := core.NewGenericScheduler(
c.schedulerCache,
c.nodeInfoSnapshot,
extenders,
c.percentageOfNodesToScore,
)
return &Scheduler{
SchedulerCache: c.schedulerCache,
Algorithm: algo,
2020-03-26 21:07:15 +00:00
Profiles: profiles,
2019-12-12 01:27:03 +00:00
NextPod: internalqueue.MakeNextPodFunc(podQueue),
2020-08-10 17:43:49 +00:00
Error: MakeDefaultErrorFunc(c.client, c.informerFactory.Core().V1().Pods().Lister(), podQueue, c.schedulerCache),
2019-12-12 01:27:03 +00:00
StopEverything: c.StopEverything,
SchedulingQueue: podQueue,
}, nil
}
2020-03-26 21:07:15 +00:00
// createFromProvider creates a scheduler from the name of a registered algorithm provider.
func (c *Configurator) createFromProvider(providerName string) (*Scheduler, error) {
klog.V(2).InfoS("Creating scheduler from algorithm provider", "algorithmProvider", providerName)
2020-03-26 21:07:15 +00:00
r := algorithmprovider.NewRegistry()
defaultPlugins, exist := r[providerName]
if !exist {
return nil, fmt.Errorf("algorithm provider %q is not registered", providerName)
}
for i := range c.profiles {
prof := &c.profiles[i]
plugins := &schedulerapi.Plugins{}
plugins.Append(defaultPlugins)
plugins.Apply(prof.Plugins)
prof.Plugins = plugins
}
return c.create()
}
// createFromConfig creates a scheduler from the configuration file
// Only reachable when using v1alpha1 component config
func (c *Configurator) createFromConfig(policy schedulerapi.Policy) (*Scheduler, error) {
lr := frameworkplugins.NewLegacyRegistry()
args := &frameworkplugins.ConfigProducerArgs{}
klog.V(2).InfoS("Creating scheduler from configuration", "policy", policy)
2020-03-26 21:07:15 +00:00
// validate the policy configuration
if err := validation.ValidatePolicy(policy); err != nil {
return nil, err
}
predicateKeys := sets.NewString()
if policy.Predicates == nil {
klog.V(2).InfoS("Using predicates from algorithm provider", "algorithmProvider", schedulerapi.SchedulerDefaultProviderName)
2020-03-26 21:07:15 +00:00
predicateKeys = lr.DefaultPredicates
} else {
for _, predicate := range policy.Predicates {
klog.V(2).InfoS("Registering predicate", "predicate", predicate.Name)
predicateName, err := lr.ProcessPredicatePolicy(predicate, args)
if err != nil {
return nil, err
}
predicateKeys.Insert(predicateName)
2020-03-26 21:07:15 +00:00
}
}
priorityKeys := make(map[string]int64)
if policy.Priorities == nil {
klog.V(2).InfoS("Using default priorities")
2020-03-26 21:07:15 +00:00
priorityKeys = lr.DefaultPriorities
} else {
for _, priority := range policy.Priorities {
if priority.Name == frameworkplugins.EqualPriority {
klog.V(2).InfoS("Skip registering priority", "priority", priority.Name)
2020-03-26 21:07:15 +00:00
continue
2019-12-12 01:27:03 +00:00
}
klog.V(2).InfoS("Registering priority", "priority", priority.Name)
priorityName, err := lr.ProcessPriorityPolicy(priority, args)
if err != nil {
return nil, err
}
priorityKeys[priorityName] = priority.Weight
2019-12-12 01:27:03 +00:00
}
}
2020-03-26 21:07:15 +00:00
// HardPodAffinitySymmetricWeight in the policy config takes precedence over
// CLI configuration.
if policy.HardPodAffinitySymmetricWeight != 0 {
2020-08-10 17:43:49 +00:00
args.InterPodAffinityArgs = &schedulerapi.InterPodAffinityArgs{
HardPodAffinityWeight: policy.HardPodAffinitySymmetricWeight,
2020-03-26 21:07:15 +00:00
}
}
// When AlwaysCheckAllPredicates is set to true, scheduler checks all the configured
// predicates even after one or more of them fails.
if policy.AlwaysCheckAllPredicates {
c.alwaysCheckAllPredicates = policy.AlwaysCheckAllPredicates
}
klog.V(2).InfoS("Creating scheduler", "predicates", predicateKeys, "priorities", priorityKeys)
2020-03-26 21:07:15 +00:00
// Combine all framework configurations. If this results in any duplication, framework
// instantiation should fail.
// "PrioritySort", "DefaultPreemption" and "DefaultBinder" were neither predicates nor priorities
2020-03-26 21:07:15 +00:00
// before. We add them by default.
plugins := schedulerapi.Plugins{
QueueSort: schedulerapi.PluginSet{
2020-03-26 21:07:15 +00:00
Enabled: []schedulerapi.Plugin{{Name: queuesort.Name}},
},
PostFilter: schedulerapi.PluginSet{
Enabled: []schedulerapi.Plugin{{Name: defaultpreemption.Name}},
},
Bind: schedulerapi.PluginSet{
2020-03-26 21:07:15 +00:00
Enabled: []schedulerapi.Plugin{{Name: defaultbinder.Name}},
},
}
var pluginConfig []schedulerapi.PluginConfig
var err error
if plugins, pluginConfig, err = lr.AppendPredicateConfigs(predicateKeys, args, plugins, pluginConfig); err != nil {
return nil, err
}
if plugins, pluginConfig, err = lr.AppendPriorityConfigs(priorityKeys, args, plugins, pluginConfig); err != nil {
return nil, err
}
if pluginConfig, err = dedupPluginConfigs(pluginConfig); err != nil {
2020-03-26 21:07:15 +00:00
return nil, err
2019-12-12 01:27:03 +00:00
}
2020-03-26 21:07:15 +00:00
for i := range c.profiles {
prof := &c.profiles[i]
// Plugins and PluginConfig are empty when using Policy; overriding.
2020-03-26 21:07:15 +00:00
prof.Plugins = &schedulerapi.Plugins{}
prof.Plugins.Append(&plugins)
prof.PluginConfig = pluginConfig
2019-12-12 01:27:03 +00:00
}
2020-03-26 21:07:15 +00:00
return c.create()
}
// dedupPluginConfigs removes duplicates from pluginConfig, ensuring that,
2020-03-26 21:07:15 +00:00
// if a plugin name is repeated, the arguments are the same.
func dedupPluginConfigs(pc []schedulerapi.PluginConfig) ([]schedulerapi.PluginConfig, error) {
2020-08-10 17:43:49 +00:00
args := make(map[string]runtime.Object)
result := make([]schedulerapi.PluginConfig, 0, len(pc))
for _, c := range pc {
if v, found := args[c.Name]; !found {
result = append(result, c)
args[c.Name] = c.Args
} else if !cmp.Equal(v, c.Args) {
2020-03-26 21:07:15 +00:00
// This should be unreachable.
return nil, fmt.Errorf("inconsistent configuration produced for plugin %s", c.Name)
}
2019-12-12 01:27:03 +00:00
}
return result, nil
2019-12-12 01:27:03 +00:00
}
// MakeDefaultErrorFunc construct a function to handle pod scheduler error
2020-08-10 17:43:49 +00:00
func MakeDefaultErrorFunc(client clientset.Interface, podLister corelisters.PodLister, podQueue internalqueue.SchedulingQueue, schedulerCache internalcache.Cache) func(*framework.QueuedPodInfo, error) {
return func(podInfo *framework.QueuedPodInfo, err error) {
2019-12-12 01:27:03 +00:00
pod := podInfo.Pod
if err == core.ErrNoNodesAvailable {
2020-08-10 17:43:49 +00:00
klog.V(2).InfoS("Unable to schedule pod; no nodes are registered to the cluster; waiting", "pod", klog.KObj(pod))
} else if fitError, ok := err.(*framework.FitError); ok {
// Inject UnschedulablePlugins to PodInfo, which will be used later for moving Pods between queues efficiently.
podInfo.UnschedulablePlugins = fitError.Diagnosis.UnschedulablePlugins
2020-08-10 17:43:49 +00:00
klog.V(2).InfoS("Unable to schedule pod; no fit; waiting", "pod", klog.KObj(pod), "err", err)
} else if apierrors.IsNotFound(err) {
klog.V(2).InfoS("Unable to schedule pod, possibly due to node not found; waiting", "pod", klog.KObj(pod), "err", err)
2020-08-10 17:43:49 +00:00
if errStatus, ok := err.(apierrors.APIStatus); ok && errStatus.Status().Details.Kind == "node" {
nodeName := errStatus.Status().Details.Name
// when node is not found, We do not remove the node right away. Trying again to get
// the node and if the node is still not found, then remove it from the scheduler cache.
_, err := client.CoreV1().Nodes().Get(context.TODO(), nodeName, metav1.GetOptions{})
if err != nil && apierrors.IsNotFound(err) {
node := v1.Node{ObjectMeta: metav1.ObjectMeta{Name: nodeName}}
if err := schedulerCache.RemoveNode(&node); err != nil {
klog.V(4).InfoS("Node is not found; failed to remove it from the cache", "node", node.Name)
2019-12-12 01:27:03 +00:00
}
}
}
2020-08-10 17:43:49 +00:00
} else {
klog.ErrorS(err, "Error scheduling pod; retrying", "pod", klog.KObj(pod))
2019-12-12 01:27:03 +00:00
}
2020-08-10 17:43:49 +00:00
// Check if the Pod exists in informer cache.
cachedPod, err := podLister.Pods(pod.Namespace).Get(pod.Name)
if err != nil {
klog.InfoS("Pod doesn't exist in informer cache", "pod", klog.KObj(pod), "err", err)
2020-08-10 17:43:49 +00:00
return
}
// In the case of extender, the pod may have been bound successfully, but timed out returning its response to the scheduler.
// It could result in the live version to carry .spec.nodeName, and that's inconsistent with the internal-queued version.
if len(cachedPod.Spec.NodeName) != 0 {
klog.InfoS("Pod has been assigned to node. Abort adding it back to queue.", "pod", klog.KObj(pod), "node", cachedPod.Spec.NodeName)
return
}
2020-08-10 17:43:49 +00:00
// As <cachedPod> is from SharedInformer, we need to do a DeepCopy() here.
podInfo.PodInfo = framework.NewPodInfo(cachedPod.DeepCopy())
2020-08-10 17:43:49 +00:00
if err := podQueue.AddUnschedulableIfNotPresent(podInfo, podQueue.SchedulingCycle()); err != nil {
klog.ErrorS(err, "Error occurred")
2020-08-10 17:43:49 +00:00
}
2019-12-12 01:27:03 +00:00
}
}