2014-08-20 21:34:55 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors.
|
2014-08-20 21:34:55 +00:00
|
|
|
|
|
|
|
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 factory can set up a scheduler. This code is here instead of
|
|
|
|
// plugin/cmd/scheduler for both testability and reuse.
|
|
|
|
package factory
|
|
|
|
|
|
|
|
import (
|
2015-09-23 22:24:49 +00:00
|
|
|
"fmt"
|
2014-08-20 21:34:55 +00:00
|
|
|
"time"
|
|
|
|
|
2017-01-31 16:03:46 +00:00
|
|
|
"github.com/golang/glog"
|
2017-01-13 17:48:50 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/errors"
|
2017-01-11 14:09:48 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-01-19 14:50:16 +00:00
|
|
|
"k8s.io/apimachinery/pkg/fields"
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/types"
|
|
|
|
"k8s.io/apimachinery/pkg/util/runtime"
|
|
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
2017-01-17 10:38:25 +00:00
|
|
|
genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
|
2017-01-24 14:11:51 +00:00
|
|
|
"k8s.io/client-go/tools/cache"
|
2016-11-18 20:52:35 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/v1"
|
2017-01-10 08:49:34 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/clientset_generated/clientset"
|
2017-02-19 07:28:09 +00:00
|
|
|
appsinformers "k8s.io/kubernetes/pkg/client/informers/informers_generated/externalversions/apps/v1beta1"
|
2017-02-21 20:00:57 +00:00
|
|
|
coreinformers "k8s.io/kubernetes/pkg/client/informers/informers_generated/externalversions/core/v1"
|
|
|
|
extensionsinformers "k8s.io/kubernetes/pkg/client/informers/informers_generated/externalversions/extensions/v1beta1"
|
2017-02-19 07:28:09 +00:00
|
|
|
appslisters "k8s.io/kubernetes/pkg/client/listers/apps/v1beta1"
|
2017-02-21 20:00:57 +00:00
|
|
|
corelisters "k8s.io/kubernetes/pkg/client/listers/core/v1"
|
|
|
|
extensionslisters "k8s.io/kubernetes/pkg/client/listers/extensions/v1beta1"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/plugin/pkg/scheduler"
|
|
|
|
"k8s.io/kubernetes/plugin/pkg/scheduler/algorithm"
|
2015-11-26 08:57:26 +00:00
|
|
|
"k8s.io/kubernetes/plugin/pkg/scheduler/algorithm/predicates"
|
2015-08-05 22:03:47 +00:00
|
|
|
schedulerapi "k8s.io/kubernetes/plugin/pkg/scheduler/api"
|
|
|
|
"k8s.io/kubernetes/plugin/pkg/scheduler/api/validation"
|
2017-01-31 16:03:46 +00:00
|
|
|
"k8s.io/kubernetes/plugin/pkg/scheduler/core"
|
2016-03-14 04:58:28 +00:00
|
|
|
"k8s.io/kubernetes/plugin/pkg/scheduler/schedulercache"
|
2017-01-13 23:51:38 +00:00
|
|
|
"k8s.io/kubernetes/plugin/pkg/scheduler/util"
|
2014-08-20 21:34:55 +00:00
|
|
|
)
|
|
|
|
|
2015-11-27 09:07:17 +00:00
|
|
|
const (
|
2017-01-05 23:47:54 +00:00
|
|
|
initialGetBackoff = 100 * time.Millisecond
|
|
|
|
maximalGetBackoff = time.Minute
|
2015-11-27 09:07:17 +00:00
|
|
|
)
|
|
|
|
|
2017-01-13 23:51:38 +00:00
|
|
|
// ConfigFactory is the default implementation of the scheduler.Configurator interface.
|
|
|
|
// TODO make this private if possible, so that only its interface is externally used.
|
2014-12-01 19:49:13 +00:00
|
|
|
type ConfigFactory struct {
|
2017-01-13 23:51:38 +00:00
|
|
|
client clientset.Interface
|
2014-11-19 16:16:25 +00:00
|
|
|
// queue for pods that need scheduling
|
2017-01-13 23:51:38 +00:00
|
|
|
podQueue *cache.FIFO
|
2015-03-13 00:28:00 +00:00
|
|
|
// a means to list all known scheduled pods.
|
2017-02-21 20:00:57 +00:00
|
|
|
scheduledPodLister corelisters.PodLister
|
2015-03-13 00:28:00 +00:00
|
|
|
// a means to list all known scheduled pods and pods assumed to have been scheduled.
|
2017-01-13 23:51:38 +00:00
|
|
|
podLister algorithm.PodLister
|
2015-09-10 08:40:22 +00:00
|
|
|
// a means to list all nodes
|
2017-02-21 20:00:57 +00:00
|
|
|
nodeLister corelisters.NodeLister
|
2015-11-29 19:00:49 +00:00
|
|
|
// a means to list all PersistentVolumes
|
2017-02-21 20:00:57 +00:00
|
|
|
pVLister corelisters.PersistentVolumeLister
|
2015-11-29 19:00:49 +00:00
|
|
|
// a means to list all PersistentVolumeClaims
|
2017-02-21 20:00:57 +00:00
|
|
|
pVCLister corelisters.PersistentVolumeClaimLister
|
2014-12-12 22:29:20 +00:00
|
|
|
// a means to list all services
|
2017-02-21 20:00:57 +00:00
|
|
|
serviceLister corelisters.ServiceLister
|
2015-07-01 14:26:54 +00:00
|
|
|
// a means to list all controllers
|
2017-02-21 20:00:57 +00:00
|
|
|
controllerLister corelisters.ReplicationControllerLister
|
2016-02-15 16:13:38 +00:00
|
|
|
// a means to list all replicasets
|
2017-02-21 20:00:57 +00:00
|
|
|
replicaSetLister extensionslisters.ReplicaSetLister
|
2017-02-19 07:28:09 +00:00
|
|
|
// a means to list all statefulsets
|
|
|
|
statefulSetLister appslisters.StatefulSetLister
|
2015-03-13 00:28:00 +00:00
|
|
|
|
2015-04-07 23:44:08 +00:00
|
|
|
// Close this to stop all reflectors
|
|
|
|
StopEverything chan struct{}
|
|
|
|
|
2017-01-12 13:45:53 +00:00
|
|
|
scheduledPodPopulator cache.Controller
|
2016-04-21 08:24:12 +00:00
|
|
|
|
|
|
|
schedulerCache schedulercache.Cache
|
2015-11-27 09:07:17 +00:00
|
|
|
|
|
|
|
// SchedulerName of a scheduler is used to select which pods will be
|
2017-01-05 23:47:54 +00:00
|
|
|
// processed by this scheduler, based on pods's "spec.SchedulerName".
|
2017-01-13 23:51:38 +00:00
|
|
|
schedulerName string
|
2016-05-04 06:50:31 +00:00
|
|
|
|
|
|
|
// RequiredDuringScheduling affinity is not symmetric, but there is an implicit PreferredDuringScheduling affinity rule
|
|
|
|
// corresponding to every RequiredDuringScheduling affinity rule.
|
|
|
|
// HardPodAffinitySymmetricWeight represents the weight of implicit PreferredDuringScheduling affinity rule, in the range 0-100.
|
2017-01-13 23:51:38 +00:00
|
|
|
hardPodAffinitySymmetricWeight int
|
2016-05-04 06:50:31 +00:00
|
|
|
|
2016-10-11 15:13:35 +00:00
|
|
|
// Equivalence class cache
|
2017-01-31 16:03:46 +00:00
|
|
|
equivalencePodCache *core.EquivalenceCache
|
2014-11-19 16:16:25 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 23:51:38 +00:00
|
|
|
// NewConfigFactory initializes the default implementation of a Configurator To encourage eventual privatization of the struct type, we only
|
|
|
|
// return the interface.
|
2017-02-21 20:00:57 +00:00
|
|
|
func NewConfigFactory(
|
|
|
|
schedulerName string,
|
|
|
|
client clientset.Interface,
|
|
|
|
nodeInformer coreinformers.NodeInformer,
|
|
|
|
pvInformer coreinformers.PersistentVolumeInformer,
|
|
|
|
pvcInformer coreinformers.PersistentVolumeClaimInformer,
|
|
|
|
replicationControllerInformer coreinformers.ReplicationControllerInformer,
|
|
|
|
replicaSetInformer extensionsinformers.ReplicaSetInformer,
|
2017-02-19 07:28:09 +00:00
|
|
|
statefulSetInformer appsinformers.StatefulSetInformer,
|
2017-02-21 20:00:57 +00:00
|
|
|
serviceInformer coreinformers.ServiceInformer,
|
|
|
|
hardPodAffinitySymmetricWeight int,
|
|
|
|
) scheduler.Configurator {
|
2016-03-14 04:58:28 +00:00
|
|
|
stopEverything := make(chan struct{})
|
|
|
|
schedulerCache := schedulercache.New(30*time.Second, stopEverything)
|
|
|
|
|
2015-03-13 00:28:00 +00:00
|
|
|
c := &ConfigFactory{
|
2017-02-21 20:00:57 +00:00
|
|
|
client: client,
|
|
|
|
podLister: schedulerCache,
|
|
|
|
podQueue: cache.NewFIFO(cache.MetaNamespaceKeyFunc),
|
|
|
|
pVLister: pvInformer.Lister(),
|
2017-01-13 23:51:38 +00:00
|
|
|
pVCLister: pvcInformer.Lister(),
|
2017-02-21 20:00:57 +00:00
|
|
|
serviceLister: serviceInformer.Lister(),
|
|
|
|
controllerLister: replicationControllerInformer.Lister(),
|
|
|
|
replicaSetLister: replicaSetInformer.Lister(),
|
2017-02-19 07:28:09 +00:00
|
|
|
statefulSetLister: statefulSetInformer.Lister(),
|
2016-05-04 06:50:31 +00:00
|
|
|
schedulerCache: schedulerCache,
|
|
|
|
StopEverything: stopEverything,
|
2017-01-13 23:51:38 +00:00
|
|
|
schedulerName: schedulerName,
|
|
|
|
hardPodAffinitySymmetricWeight: hardPodAffinitySymmetricWeight,
|
2014-11-19 16:16:25 +00:00
|
|
|
}
|
2016-03-14 04:58:28 +00:00
|
|
|
|
2015-04-07 23:44:08 +00:00
|
|
|
// On add/delete to the scheduled pods, remove from the assumed pods.
|
|
|
|
// We construct this here instead of in CreateFromKeys because
|
|
|
|
// ScheduledPodLister is something we provide to plug in functions that
|
|
|
|
// they may need to call.
|
2017-02-21 20:00:57 +00:00
|
|
|
var scheduledPodIndexer cache.Indexer
|
|
|
|
scheduledPodIndexer, c.scheduledPodPopulator = cache.NewIndexerInformer(
|
2015-12-21 12:33:42 +00:00
|
|
|
c.createAssignedNonTerminatedPodLW(),
|
2016-11-18 20:52:35 +00:00
|
|
|
&v1.Pod{},
|
2015-04-07 23:44:08 +00:00
|
|
|
0,
|
2016-09-14 18:35:38 +00:00
|
|
|
cache.ResourceEventHandlerFuncs{
|
2016-04-21 08:24:12 +00:00
|
|
|
AddFunc: c.addPodToCache,
|
|
|
|
UpdateFunc: c.updatePodInCache,
|
|
|
|
DeleteFunc: c.deletePodFromCache,
|
|
|
|
},
|
2016-04-07 12:15:21 +00:00
|
|
|
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc},
|
2016-04-21 08:24:12 +00:00
|
|
|
)
|
2017-02-21 20:00:57 +00:00
|
|
|
c.scheduledPodLister = corelisters.NewPodLister(scheduledPodIndexer)
|
2016-04-21 08:24:12 +00:00
|
|
|
|
2017-02-21 20:00:57 +00:00
|
|
|
// Only nodes in the "Ready" condition with status == "True" are schedulable
|
|
|
|
nodeInformer.Informer().AddEventHandlerWithResyncPeriod(
|
2016-09-14 18:35:38 +00:00
|
|
|
cache.ResourceEventHandlerFuncs{
|
2016-04-21 08:24:12 +00:00
|
|
|
AddFunc: c.addNodeToCache,
|
|
|
|
UpdateFunc: c.updateNodeInCache,
|
|
|
|
DeleteFunc: c.deleteNodeFromCache,
|
2015-04-07 23:44:08 +00:00
|
|
|
},
|
2016-10-11 15:13:35 +00:00
|
|
|
0,
|
|
|
|
)
|
2017-02-21 20:00:57 +00:00
|
|
|
c.nodeLister = nodeInformer.Lister()
|
2016-10-11 15:13:35 +00:00
|
|
|
|
2017-02-21 20:00:57 +00:00
|
|
|
// TODO(harryz) need to fill all the handlers here and below for equivalence cache
|
2016-10-11 15:13:35 +00:00
|
|
|
|
2015-03-13 00:28:00 +00:00
|
|
|
return c
|
2014-12-10 04:25:45 +00:00
|
|
|
}
|
2014-11-19 16:16:25 +00:00
|
|
|
|
2017-01-13 23:51:38 +00:00
|
|
|
// GetNodeStore provides the cache to the nodes, mostly internal use, but may also be called by mock-tests.
|
2017-02-21 20:00:57 +00:00
|
|
|
func (c *ConfigFactory) GetNodeLister() corelisters.NodeLister {
|
|
|
|
return c.nodeLister
|
2017-01-13 23:51:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ConfigFactory) GetHardPodAffinitySymmetricWeight() int {
|
|
|
|
return c.hardPodAffinitySymmetricWeight
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *ConfigFactory) GetSchedulerName() string {
|
|
|
|
return f.schedulerName
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetClient provides a kubernetes client, mostly internal use, but may also be called by mock-tests.
|
|
|
|
func (f *ConfigFactory) GetClient() clientset.Interface {
|
|
|
|
return f.client
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetScheduledPodListerIndexer provides a pod lister, mostly internal use, but may also be called by mock-tests.
|
2017-02-21 20:00:57 +00:00
|
|
|
func (c *ConfigFactory) GetScheduledPodLister() corelisters.PodLister {
|
|
|
|
return c.scheduledPodLister
|
2017-01-13 23:51:38 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 08:59:30 +00:00
|
|
|
// TODO(resouer) need to update all the handlers here and below for equivalence cache
|
2016-04-21 08:24:12 +00:00
|
|
|
func (c *ConfigFactory) addPodToCache(obj interface{}) {
|
2016-11-18 20:52:35 +00:00
|
|
|
pod, ok := obj.(*v1.Pod)
|
2016-04-21 08:24:12 +00:00
|
|
|
if !ok {
|
2016-11-18 20:52:35 +00:00
|
|
|
glog.Errorf("cannot convert to *v1.Pod: %v", obj)
|
2016-04-21 08:24:12 +00:00
|
|
|
return
|
|
|
|
}
|
2016-10-11 15:13:35 +00:00
|
|
|
|
2016-04-21 08:24:12 +00:00
|
|
|
if err := c.schedulerCache.AddPod(pod); err != nil {
|
|
|
|
glog.Errorf("scheduler cache AddPod failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ConfigFactory) updatePodInCache(oldObj, newObj interface{}) {
|
2016-11-18 20:52:35 +00:00
|
|
|
oldPod, ok := oldObj.(*v1.Pod)
|
2016-04-21 08:24:12 +00:00
|
|
|
if !ok {
|
2016-11-18 20:52:35 +00:00
|
|
|
glog.Errorf("cannot convert oldObj to *v1.Pod: %v", oldObj)
|
2016-04-21 08:24:12 +00:00
|
|
|
return
|
|
|
|
}
|
2016-11-18 20:52:35 +00:00
|
|
|
newPod, ok := newObj.(*v1.Pod)
|
2016-04-21 08:24:12 +00:00
|
|
|
if !ok {
|
2016-11-18 20:52:35 +00:00
|
|
|
glog.Errorf("cannot convert newObj to *v1.Pod: %v", newObj)
|
2016-04-21 08:24:12 +00:00
|
|
|
return
|
|
|
|
}
|
2016-10-11 15:13:35 +00:00
|
|
|
|
2016-04-21 08:24:12 +00:00
|
|
|
if err := c.schedulerCache.UpdatePod(oldPod, newPod); err != nil {
|
|
|
|
glog.Errorf("scheduler cache UpdatePod failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ConfigFactory) deletePodFromCache(obj interface{}) {
|
2016-11-18 20:52:35 +00:00
|
|
|
var pod *v1.Pod
|
2016-04-21 08:24:12 +00:00
|
|
|
switch t := obj.(type) {
|
2016-11-18 20:52:35 +00:00
|
|
|
case *v1.Pod:
|
2016-04-21 08:24:12 +00:00
|
|
|
pod = t
|
|
|
|
case cache.DeletedFinalStateUnknown:
|
|
|
|
var ok bool
|
2016-11-18 20:52:35 +00:00
|
|
|
pod, ok = t.Obj.(*v1.Pod)
|
2016-04-21 08:24:12 +00:00
|
|
|
if !ok {
|
2016-11-18 20:52:35 +00:00
|
|
|
glog.Errorf("cannot convert to *v1.Pod: %v", t.Obj)
|
2016-04-21 08:24:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
default:
|
2016-11-18 20:52:35 +00:00
|
|
|
glog.Errorf("cannot convert to *v1.Pod: %v", t)
|
2016-04-21 08:24:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := c.schedulerCache.RemovePod(pod); err != nil {
|
|
|
|
glog.Errorf("scheduler cache RemovePod failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ConfigFactory) addNodeToCache(obj interface{}) {
|
2016-11-18 20:52:35 +00:00
|
|
|
node, ok := obj.(*v1.Node)
|
2016-04-21 08:24:12 +00:00
|
|
|
if !ok {
|
2016-11-18 20:52:35 +00:00
|
|
|
glog.Errorf("cannot convert to *v1.Node: %v", obj)
|
2016-04-21 08:24:12 +00:00
|
|
|
return
|
|
|
|
}
|
2016-10-11 15:13:35 +00:00
|
|
|
|
2016-04-21 08:24:12 +00:00
|
|
|
if err := c.schedulerCache.AddNode(node); err != nil {
|
|
|
|
glog.Errorf("scheduler cache AddNode failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ConfigFactory) updateNodeInCache(oldObj, newObj interface{}) {
|
2016-11-18 20:52:35 +00:00
|
|
|
oldNode, ok := oldObj.(*v1.Node)
|
2016-04-21 08:24:12 +00:00
|
|
|
if !ok {
|
2016-11-18 20:52:35 +00:00
|
|
|
glog.Errorf("cannot convert oldObj to *v1.Node: %v", oldObj)
|
2016-04-21 08:24:12 +00:00
|
|
|
return
|
|
|
|
}
|
2016-11-18 20:52:35 +00:00
|
|
|
newNode, ok := newObj.(*v1.Node)
|
2016-04-21 08:24:12 +00:00
|
|
|
if !ok {
|
2016-11-18 20:52:35 +00:00
|
|
|
glog.Errorf("cannot convert newObj to *v1.Node: %v", newObj)
|
2016-04-21 08:24:12 +00:00
|
|
|
return
|
|
|
|
}
|
2016-10-11 15:13:35 +00:00
|
|
|
|
2016-04-21 08:24:12 +00:00
|
|
|
if err := c.schedulerCache.UpdateNode(oldNode, newNode); err != nil {
|
|
|
|
glog.Errorf("scheduler cache UpdateNode failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ConfigFactory) deleteNodeFromCache(obj interface{}) {
|
2016-11-18 20:52:35 +00:00
|
|
|
var node *v1.Node
|
2016-04-21 08:24:12 +00:00
|
|
|
switch t := obj.(type) {
|
2016-11-18 20:52:35 +00:00
|
|
|
case *v1.Node:
|
2016-04-21 08:24:12 +00:00
|
|
|
node = t
|
|
|
|
case cache.DeletedFinalStateUnknown:
|
|
|
|
var ok bool
|
2016-11-18 20:52:35 +00:00
|
|
|
node, ok = t.Obj.(*v1.Node)
|
2016-04-21 08:24:12 +00:00
|
|
|
if !ok {
|
2016-11-18 20:52:35 +00:00
|
|
|
glog.Errorf("cannot convert to *v1.Node: %v", t.Obj)
|
2016-04-21 08:24:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
default:
|
2016-11-18 20:52:35 +00:00
|
|
|
glog.Errorf("cannot convert to *v1.Node: %v", t)
|
2016-04-21 08:24:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := c.schedulerCache.RemoveNode(node); err != nil {
|
|
|
|
glog.Errorf("scheduler cache RemoveNode failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-10 04:25:45 +00:00
|
|
|
// Create creates a scheduler with the default algorithm provider.
|
|
|
|
func (f *ConfigFactory) Create() (*scheduler.Config, error) {
|
|
|
|
return f.CreateFromProvider(DefaultProvider)
|
2014-08-20 21:34:55 +00:00
|
|
|
}
|
|
|
|
|
2015-02-27 00:40:15 +00:00
|
|
|
// Creates a scheduler from the name of a registered algorithm provider.
|
2014-12-10 04:25:45 +00:00
|
|
|
func (f *ConfigFactory) CreateFromProvider(providerName string) (*scheduler.Config, error) {
|
2015-09-29 07:04:07 +00:00
|
|
|
glog.V(2).Infof("Creating scheduler from algorithm provider '%v'", providerName)
|
2014-12-10 04:25:45 +00:00
|
|
|
provider, err := GetAlgorithmProvider(providerName)
|
2014-11-19 16:16:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-09-04 06:50:14 +00:00
|
|
|
return f.CreateFromKeys(provider.FitPredicateKeys, provider.PriorityFunctionKeys, []algorithm.SchedulerExtender{})
|
2014-12-10 04:25:45 +00:00
|
|
|
}
|
|
|
|
|
2015-02-27 00:40:15 +00:00
|
|
|
// Creates a scheduler from the configuration file
|
2015-02-20 00:18:28 +00:00
|
|
|
func (f *ConfigFactory) CreateFromConfig(policy schedulerapi.Policy) (*scheduler.Config, error) {
|
2015-09-29 07:04:07 +00:00
|
|
|
glog.V(2).Infof("Creating scheduler from configuration: %v", policy)
|
2015-02-20 00:18:28 +00:00
|
|
|
|
2015-03-31 01:37:19 +00:00
|
|
|
// validate the policy configuration
|
|
|
|
if err := validation.ValidatePolicy(policy); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-09-09 17:45:01 +00:00
|
|
|
predicateKeys := sets.NewString()
|
2015-02-20 00:18:28 +00:00
|
|
|
for _, predicate := range policy.Predicates {
|
|
|
|
glog.V(2).Infof("Registering predicate: %s", predicate.Name)
|
2015-02-27 00:40:15 +00:00
|
|
|
predicateKeys.Insert(RegisterCustomFitPredicate(predicate))
|
2015-02-20 00:18:28 +00:00
|
|
|
}
|
|
|
|
|
2015-09-09 17:45:01 +00:00
|
|
|
priorityKeys := sets.NewString()
|
2015-02-20 00:18:28 +00:00
|
|
|
for _, priority := range policy.Priorities {
|
|
|
|
glog.V(2).Infof("Registering priority: %s", priority.Name)
|
|
|
|
priorityKeys.Insert(RegisterCustomPriorityFunction(priority))
|
|
|
|
}
|
|
|
|
|
2015-09-04 06:50:14 +00:00
|
|
|
extenders := make([]algorithm.SchedulerExtender, 0)
|
|
|
|
if len(policy.ExtenderConfigs) != 0 {
|
|
|
|
for ii := range policy.ExtenderConfigs {
|
|
|
|
glog.V(2).Infof("Creating extender with config %+v", policy.ExtenderConfigs[ii])
|
2017-02-01 06:57:42 +00:00
|
|
|
if extender, err := core.NewHTTPExtender(&policy.ExtenderConfigs[ii]); err != nil {
|
2015-09-04 06:50:14 +00:00
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
extenders = append(extenders, extender)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return f.CreateFromKeys(predicateKeys, priorityKeys, extenders)
|
2015-02-20 00:18:28 +00:00
|
|
|
}
|
|
|
|
|
2015-02-27 00:40:15 +00:00
|
|
|
// Creates a scheduler from a set of registered fit predicate keys and priority keys.
|
2015-09-04 06:50:14 +00:00
|
|
|
func (f *ConfigFactory) CreateFromKeys(predicateKeys, priorityKeys sets.String, extenders []algorithm.SchedulerExtender) (*scheduler.Config, error) {
|
2017-01-04 22:37:56 +00:00
|
|
|
glog.V(2).Infof("Creating scheduler with fit predicates '%v' and priority functions '%v", predicateKeys, priorityKeys)
|
2016-05-04 06:50:31 +00:00
|
|
|
|
2017-01-13 23:51:38 +00:00
|
|
|
if f.GetHardPodAffinitySymmetricWeight() < 0 || f.GetHardPodAffinitySymmetricWeight() > 100 {
|
|
|
|
return nil, fmt.Errorf("invalid hardPodAffinitySymmetricWeight: %d, must be in the range 0-100", f.GetHardPodAffinitySymmetricWeight())
|
2016-05-04 06:50:31 +00:00
|
|
|
}
|
|
|
|
|
2016-05-23 12:19:46 +00:00
|
|
|
predicateFuncs, err := f.GetPredicates(predicateKeys)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
priorityConfigs, err := f.GetPriorityFunctionConfigs(priorityKeys)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-09-13 09:54:13 +00:00
|
|
|
priorityMetaProducer, err := f.GetPriorityMetadataProducer()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-10-12 16:03:01 +00:00
|
|
|
predicateMetaProducer, err := f.GetPredicateMetadataProducer()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-05-23 12:19:46 +00:00
|
|
|
|
2016-10-12 16:03:01 +00:00
|
|
|
f.Run()
|
2017-02-15 08:59:30 +00:00
|
|
|
// TODO(resouer) use equivalence cache instead of nil here when #36238 get merged
|
|
|
|
algo := core.NewGenericScheduler(f.schedulerCache, nil, predicateFuncs, predicateMetaProducer, priorityConfigs, priorityMetaProducer, extenders)
|
2017-01-13 23:51:38 +00:00
|
|
|
podBackoff := util.CreateDefaultPodBackoff()
|
2016-05-23 12:19:46 +00:00
|
|
|
return &scheduler.Config{
|
|
|
|
SchedulerCache: f.schedulerCache,
|
|
|
|
// The scheduler only needs to consider schedulable nodes.
|
2017-02-21 20:00:57 +00:00
|
|
|
NodeLister: &nodePredicateLister{f.nodeLister},
|
2016-05-23 12:19:46 +00:00
|
|
|
Algorithm: algo,
|
2017-01-13 23:51:38 +00:00
|
|
|
Binder: &binder{f.client},
|
|
|
|
PodConditionUpdater: &podConditionUpdater{f.client},
|
2016-11-18 20:52:35 +00:00
|
|
|
NextPod: func() *v1.Pod {
|
2016-05-23 12:19:46 +00:00
|
|
|
return f.getNextPod()
|
|
|
|
},
|
2017-01-13 23:51:38 +00:00
|
|
|
Error: f.MakeDefaultErrorFunc(podBackoff, f.podQueue),
|
2016-05-23 12:19:46 +00:00
|
|
|
StopEverything: f.StopEverything,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-02-21 20:00:57 +00:00
|
|
|
type nodePredicateLister struct {
|
|
|
|
corelisters.NodeLister
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *nodePredicateLister) List() ([]*v1.Node, error) {
|
|
|
|
return n.ListWithPredicate(getNodeConditionPredicate())
|
|
|
|
}
|
|
|
|
|
2016-05-23 12:19:46 +00:00
|
|
|
func (f *ConfigFactory) GetPriorityFunctionConfigs(priorityKeys sets.String) ([]algorithm.PriorityConfig, error) {
|
|
|
|
pluginArgs, err := f.getPluginArgs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return getPriorityFunctionConfigs(priorityKeys, *pluginArgs)
|
|
|
|
}
|
|
|
|
|
2016-09-13 09:54:13 +00:00
|
|
|
func (f *ConfigFactory) GetPriorityMetadataProducer() (algorithm.MetadataProducer, error) {
|
|
|
|
pluginArgs, err := f.getPluginArgs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return getPriorityMetadataProducer(*pluginArgs)
|
|
|
|
}
|
|
|
|
|
2016-10-12 16:03:01 +00:00
|
|
|
func (f *ConfigFactory) GetPredicateMetadataProducer() (algorithm.MetadataProducer, error) {
|
|
|
|
pluginArgs, err := f.getPluginArgs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return getPredicateMetadataProducer(*pluginArgs)
|
|
|
|
}
|
|
|
|
|
2016-05-23 12:19:46 +00:00
|
|
|
func (f *ConfigFactory) GetPredicates(predicateKeys sets.String) (map[string]algorithm.FitPredicate, error) {
|
|
|
|
pluginArgs, err := f.getPluginArgs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return getFitPredicateFunctions(predicateKeys, *pluginArgs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *ConfigFactory) getPluginArgs() (*PluginFactoryArgs, error) {
|
|
|
|
return &PluginFactoryArgs{
|
2017-02-19 07:28:09 +00:00
|
|
|
PodLister: f.podLister,
|
|
|
|
ServiceLister: f.serviceLister,
|
|
|
|
ControllerLister: f.controllerLister,
|
|
|
|
ReplicaSetLister: f.replicaSetLister,
|
|
|
|
StatefulSetLister: f.statefulSetLister,
|
2015-05-02 00:00:37 +00:00
|
|
|
// All fit predicates only need to consider schedulable nodes.
|
2017-02-21 20:00:57 +00:00
|
|
|
NodeLister: &nodePredicateLister{f.nodeLister},
|
|
|
|
NodeInfo: &predicates.CachedNodeInfo{NodeLister: f.nodeLister},
|
|
|
|
PVInfo: &predicates.CachedPersistentVolumeInfo{PersistentVolumeLister: f.pVLister},
|
|
|
|
PVCInfo: &predicates.CachedPersistentVolumeClaimInfo{PersistentVolumeClaimLister: f.pVCLister},
|
2017-01-13 23:51:38 +00:00
|
|
|
HardPodAffinitySymmetricWeight: f.hardPodAffinitySymmetricWeight,
|
2016-05-23 12:19:46 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2014-11-19 16:16:25 +00:00
|
|
|
|
2016-05-23 12:19:46 +00:00
|
|
|
func (f *ConfigFactory) Run() {
|
2014-08-20 21:34:55 +00:00
|
|
|
// Watch and queue pods that need scheduling.
|
2017-01-13 23:51:38 +00:00
|
|
|
cache.NewReflector(f.createUnassignedNonTerminatedPodLW(), &v1.Pod{}, f.podQueue, 0).RunUntil(f.StopEverything)
|
2015-03-27 22:33:03 +00:00
|
|
|
|
2015-04-07 23:44:08 +00:00
|
|
|
// Begin populating scheduled pods.
|
2015-04-08 00:40:30 +00:00
|
|
|
go f.scheduledPodPopulator.Run(f.StopEverything)
|
2014-11-19 16:16:25 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 20:52:35 +00:00
|
|
|
func (f *ConfigFactory) getNextPod() *v1.Pod {
|
2015-11-27 09:07:17 +00:00
|
|
|
for {
|
2017-01-13 23:51:38 +00:00
|
|
|
pod := cache.Pop(f.podQueue).(*v1.Pod)
|
|
|
|
if f.ResponsibleForPod(pod) {
|
2015-11-27 09:07:17 +00:00
|
|
|
glog.V(4).Infof("About to try and schedule pod %v", pod.Name)
|
|
|
|
return pod
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-13 23:51:38 +00:00
|
|
|
func (f *ConfigFactory) ResponsibleForPod(pod *v1.Pod) bool {
|
2017-01-05 23:47:54 +00:00
|
|
|
return f.schedulerName == pod.Spec.SchedulerName
|
2015-11-27 09:07:17 +00:00
|
|
|
}
|
|
|
|
|
2017-02-21 20:00:57 +00:00
|
|
|
func getNodeConditionPredicate() corelisters.NodeConditionPredicate {
|
2016-11-18 20:52:35 +00:00
|
|
|
return func(node *v1.Node) bool {
|
2016-07-11 08:32:29 +00:00
|
|
|
for i := range node.Status.Conditions {
|
|
|
|
cond := &node.Status.Conditions[i]
|
2016-05-27 09:37:20 +00:00
|
|
|
// We consider the node for scheduling only when its:
|
|
|
|
// - NodeReady condition status is ConditionTrue,
|
|
|
|
// - NodeOutOfDisk condition status is ConditionFalse,
|
|
|
|
// - NodeNetworkUnavailable condition status is ConditionFalse.
|
2016-11-18 20:52:35 +00:00
|
|
|
if cond.Type == v1.NodeReady && cond.Status != v1.ConditionTrue {
|
2016-11-21 19:55:11 +00:00
|
|
|
glog.V(4).Infof("Ignoring node %v with %v condition status %v", node.Name, cond.Type, cond.Status)
|
|
|
|
return false
|
2016-11-18 20:52:35 +00:00
|
|
|
} else if cond.Type == v1.NodeOutOfDisk && cond.Status != v1.ConditionFalse {
|
2016-11-21 19:55:11 +00:00
|
|
|
glog.V(4).Infof("Ignoring node %v with %v condition status %v", node.Name, cond.Type, cond.Status)
|
|
|
|
return false
|
2016-11-18 20:52:35 +00:00
|
|
|
} else if cond.Type == v1.NodeNetworkUnavailable && cond.Status != v1.ConditionFalse {
|
2016-11-21 19:55:11 +00:00
|
|
|
glog.V(4).Infof("Ignoring node %v with %v condition status %v", node.Name, cond.Type, cond.Status)
|
|
|
|
return false
|
2015-10-22 19:47:43 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-03 21:35:50 +00:00
|
|
|
// Ignore nodes that are marked unschedulable
|
|
|
|
if node.Spec.Unschedulable {
|
|
|
|
glog.V(4).Infof("Ignoring node %v since it is unschedulable", node.Name)
|
|
|
|
return false
|
|
|
|
}
|
2015-10-22 19:47:43 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-27 00:40:15 +00:00
|
|
|
// Returns a cache.ListWatch that finds all pods that need to be
|
2014-09-16 21:08:57 +00:00
|
|
|
// scheduled.
|
2015-12-21 12:33:42 +00:00
|
|
|
func (factory *ConfigFactory) createUnassignedNonTerminatedPodLW() *cache.ListWatch {
|
2016-11-18 20:52:35 +00:00
|
|
|
selector := fields.ParseSelectorOrDie("spec.nodeName==" + "" + ",status.phase!=" + string(v1.PodSucceeded) + ",status.phase!=" + string(v1.PodFailed))
|
2017-01-22 03:36:02 +00:00
|
|
|
return cache.NewListWatchFromClient(factory.client.Core().RESTClient(), "pods", metav1.NamespaceAll, selector)
|
2014-09-16 21:08:57 +00:00
|
|
|
}
|
|
|
|
|
2015-02-27 00:40:15 +00:00
|
|
|
// Returns a cache.ListWatch that finds all pods that are
|
2014-09-16 21:08:57 +00:00
|
|
|
// already scheduled.
|
2015-01-07 21:12:29 +00:00
|
|
|
// TODO: return a ListerWatcher interface instead?
|
2015-12-21 12:33:42 +00:00
|
|
|
func (factory *ConfigFactory) createAssignedNonTerminatedPodLW() *cache.ListWatch {
|
2016-11-18 20:52:35 +00:00
|
|
|
selector := fields.ParseSelectorOrDie("spec.nodeName!=" + "" + ",status.phase!=" + string(v1.PodSucceeded) + ",status.phase!=" + string(v1.PodFailed))
|
2017-01-22 03:36:02 +00:00
|
|
|
return cache.NewListWatchFromClient(factory.client.Core().RESTClient(), "pods", metav1.NamespaceAll, selector)
|
2014-09-16 21:08:57 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 23:51:38 +00:00
|
|
|
func (factory *ConfigFactory) MakeDefaultErrorFunc(backoff *util.PodBackoff, podQueue *cache.FIFO) func(pod *v1.Pod, err error) {
|
2016-11-18 20:52:35 +00:00
|
|
|
return func(pod *v1.Pod, err error) {
|
2017-01-31 16:03:46 +00:00
|
|
|
if err == core.ErrNoNodesAvailable {
|
2015-05-16 23:46:50 +00:00
|
|
|
glog.V(4).Infof("Unable to schedule %v %v: no nodes are registered to the cluster; waiting", pod.Namespace, pod.Name)
|
|
|
|
} else {
|
|
|
|
glog.Errorf("Error scheduling %v %v: %v; retrying", pod.Namespace, pod.Name, err)
|
|
|
|
}
|
2017-01-13 23:51:38 +00:00
|
|
|
backoff.Gc()
|
2014-08-20 22:03:32 +00:00
|
|
|
// Retry asynchronously.
|
|
|
|
// Note that this is extremely rudimentary and we need a more real error handling path.
|
|
|
|
go func() {
|
2016-01-15 07:32:10 +00:00
|
|
|
defer runtime.HandleCrash()
|
2015-09-23 22:24:49 +00:00
|
|
|
podID := types.NamespacedName{
|
|
|
|
Namespace: pod.Namespace,
|
|
|
|
Name: pod.Name,
|
|
|
|
}
|
|
|
|
|
2017-01-13 23:51:38 +00:00
|
|
|
entry := backoff.GetEntry(podID)
|
|
|
|
if !entry.TryWait(backoff.MaxDuration()) {
|
2015-09-23 22:24:49 +00:00
|
|
|
glog.Warningf("Request for pod %v already in flight, abandoning", podID)
|
|
|
|
return
|
|
|
|
}
|
2014-08-20 22:03:32 +00:00
|
|
|
// Get the pod again; it may have changed/been scheduled already.
|
2016-07-18 09:47:22 +00:00
|
|
|
getBackoff := initialGetBackoff
|
|
|
|
for {
|
2017-01-13 23:51:38 +00:00
|
|
|
pod, err := factory.client.Core().Pods(podID.Namespace).Get(podID.Name, metav1.GetOptions{})
|
2016-09-14 04:33:28 +00:00
|
|
|
if err == nil {
|
|
|
|
if len(pod.Spec.NodeName) == 0 {
|
|
|
|
podQueue.AddIfNotPresent(pod)
|
|
|
|
}
|
2016-07-18 09:47:22 +00:00
|
|
|
break
|
2015-05-16 23:46:50 +00:00
|
|
|
}
|
2016-07-18 09:47:22 +00:00
|
|
|
if errors.IsNotFound(err) {
|
2016-07-19 16:10:50 +00:00
|
|
|
glog.Warningf("A pod %v no longer exists", podID)
|
2016-07-18 09:47:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
glog.Errorf("Error getting pod %v for retry: %v; retrying...", podID, err)
|
|
|
|
if getBackoff = getBackoff * 2; getBackoff > maximalGetBackoff {
|
|
|
|
getBackoff = maximalGetBackoff
|
|
|
|
}
|
|
|
|
time.Sleep(getBackoff)
|
2014-08-20 22:03:32 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2014-08-20 21:34:55 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 20:52:35 +00:00
|
|
|
// nodeEnumerator allows a cache.Poller to enumerate items in an v1.NodeList
|
2014-12-08 03:44:27 +00:00
|
|
|
type nodeEnumerator struct {
|
2016-11-18 20:52:35 +00:00
|
|
|
*v1.NodeList
|
2014-08-20 21:34:55 +00:00
|
|
|
}
|
|
|
|
|
2014-12-08 03:44:27 +00:00
|
|
|
// Len returns the number of items in the node list.
|
|
|
|
func (ne *nodeEnumerator) Len() int {
|
|
|
|
if ne.NodeList == nil {
|
2014-08-20 21:34:55 +00:00
|
|
|
return 0
|
|
|
|
}
|
2014-12-08 03:44:27 +00:00
|
|
|
return len(ne.Items)
|
2014-08-20 21:34:55 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// Get returns the item (and ID) with the particular index.
|
2015-01-26 21:44:53 +00:00
|
|
|
func (ne *nodeEnumerator) Get(index int) interface{} {
|
|
|
|
return &ne.Items[index]
|
2014-08-20 21:34:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type binder struct {
|
2016-10-05 08:40:39 +00:00
|
|
|
Client clientset.Interface
|
2014-08-20 21:34:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bind just does a POST binding RPC.
|
2016-11-18 20:52:35 +00:00
|
|
|
func (b *binder) Bind(binding *v1.Binding) error {
|
2016-04-20 11:35:06 +00:00
|
|
|
glog.V(3).Infof("Attempting to bind %v to %v", binding.Name, binding.Target.Name)
|
2017-01-02 14:07:36 +00:00
|
|
|
ctx := genericapirequest.WithNamespace(genericapirequest.NewContext(), binding.Namespace)
|
|
|
|
return b.Client.Core().RESTClient().Post().Namespace(genericapirequest.NamespaceValue(ctx)).Resource("bindings").Body(binding).Do().Error()
|
2015-03-04 20:55:41 +00:00
|
|
|
// TODO: use Pods interface for binding once clusters are upgraded
|
|
|
|
// return b.Pods(binding.Namespace).Bind(binding)
|
2014-08-20 21:34:55 +00:00
|
|
|
}
|
2014-10-03 16:59:39 +00:00
|
|
|
|
2016-04-18 12:26:16 +00:00
|
|
|
type podConditionUpdater struct {
|
2016-10-05 08:40:39 +00:00
|
|
|
Client clientset.Interface
|
2016-04-18 12:26:16 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 20:52:35 +00:00
|
|
|
func (p *podConditionUpdater) Update(pod *v1.Pod, condition *v1.PodCondition) error {
|
2016-04-18 12:26:16 +00:00
|
|
|
glog.V(2).Infof("Updating pod condition for %s/%s to (%s==%s)", pod.Namespace, pod.Name, condition.Type, condition.Status)
|
2016-11-18 20:52:35 +00:00
|
|
|
if v1.UpdatePodCondition(&pod.Status, condition) {
|
2016-10-05 08:40:39 +00:00
|
|
|
_, err := p.Client.Core().Pods(pod.Namespace).UpdateStatus(pod)
|
2016-04-18 12:26:16 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|