2015-09-08 18:50:39 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2015-09-08 18:50:39 +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 daemon
|
|
|
|
|
|
|
|
import (
|
2016-06-06 07:07:28 +00:00
|
|
|
"fmt"
|
2015-09-08 18:50:39 +00:00
|
|
|
"reflect"
|
|
|
|
"sort"
|
2016-01-28 06:13:05 +00:00
|
|
|
"sync"
|
2015-09-08 18:50:39 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/golang/glog"
|
2016-06-06 07:07:28 +00:00
|
|
|
|
2015-09-08 18:50:39 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2016-02-02 05:34:42 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/unversioned"
|
2015-10-09 22:04:41 +00:00
|
|
|
"k8s.io/kubernetes/pkg/apis/extensions"
|
2015-09-08 18:50:39 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/cache"
|
2016-02-05 21:58:03 +00:00
|
|
|
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
|
2016-03-29 21:52:43 +00:00
|
|
|
unversionedcore "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/unversioned"
|
|
|
|
unversionedextensions "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/extensions/unversioned"
|
2015-09-08 18:50:39 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/record"
|
|
|
|
"k8s.io/kubernetes/pkg/controller"
|
|
|
|
"k8s.io/kubernetes/pkg/controller/framework"
|
2016-04-19 12:45:00 +00:00
|
|
|
"k8s.io/kubernetes/pkg/controller/framework/informers"
|
2015-09-08 18:50:39 +00:00
|
|
|
"k8s.io/kubernetes/pkg/labels"
|
|
|
|
"k8s.io/kubernetes/pkg/runtime"
|
2016-04-13 18:38:32 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/metrics"
|
2016-01-15 07:32:10 +00:00
|
|
|
utilruntime "k8s.io/kubernetes/pkg/util/runtime"
|
2016-02-02 10:57:06 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/wait"
|
2015-09-08 18:50:39 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/workqueue"
|
|
|
|
"k8s.io/kubernetes/pkg/watch"
|
2016-01-28 06:13:05 +00:00
|
|
|
"k8s.io/kubernetes/plugin/pkg/scheduler/algorithm/predicates"
|
2016-07-01 17:02:51 +00:00
|
|
|
"k8s.io/kubernetes/plugin/pkg/scheduler/schedulercache"
|
2015-09-08 18:50:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Daemon sets will periodically check that their daemon pods are running as expected.
|
|
|
|
FullDaemonSetResyncPeriod = 30 * time.Second // TODO: Figure out if this time seems reasonable.
|
2015-10-01 09:16:08 +00:00
|
|
|
|
2015-10-20 10:47:46 +00:00
|
|
|
// Realistic value of the burstReplica field for the replication manager based off
|
|
|
|
// performance requirements for kubernetes 1.0.
|
|
|
|
BurstReplicas = 500
|
|
|
|
|
2015-10-01 09:16:08 +00:00
|
|
|
// We must avoid counting pods until the pod store has synced. If it hasn't synced, to
|
|
|
|
// avoid a hot loop, we'll wait this long between checks.
|
|
|
|
PodStoreSyncedPollPeriod = 100 * time.Millisecond
|
|
|
|
|
2015-09-08 18:50:39 +00:00
|
|
|
// If sending a status upate to API server fails, we retry a finite number of times.
|
|
|
|
StatusUpdateRetries = 1
|
|
|
|
)
|
|
|
|
|
|
|
|
// DaemonSetsController is responsible for synchronizing DaemonSet objects stored
|
|
|
|
// in the system with actual running pods.
|
|
|
|
type DaemonSetsController struct {
|
2016-03-18 23:14:07 +00:00
|
|
|
kubeClient clientset.Interface
|
|
|
|
eventRecorder record.EventRecorder
|
|
|
|
podControl controller.PodControlInterface
|
2015-09-08 18:50:39 +00:00
|
|
|
|
2016-04-19 12:45:00 +00:00
|
|
|
// internalPodInformer is used to hold a personal informer. If we're using
|
|
|
|
// a normal shared informer, then the informer will be started for us. If
|
|
|
|
// we have a personal informer, we must start it ourselves. If you start
|
|
|
|
// the controller using NewDaemonSetsController(passing SharedInformer), this
|
|
|
|
// will be null
|
|
|
|
internalPodInformer framework.SharedInformer
|
|
|
|
|
2015-10-20 10:47:46 +00:00
|
|
|
// An dsc is temporarily suspended after creating/deleting these many replicas.
|
|
|
|
// It resumes normal action after observing the watch events for them.
|
|
|
|
burstReplicas int
|
|
|
|
|
2015-09-08 18:50:39 +00:00
|
|
|
// To allow injection of syncDaemonSet for testing.
|
|
|
|
syncHandler func(dsKey string) error
|
|
|
|
// A TTLCache of pod creates/deletes each ds expects to see
|
|
|
|
expectations controller.ControllerExpectationsInterface
|
|
|
|
// A store of daemon sets
|
|
|
|
dsStore cache.StoreToDaemonSetLister
|
|
|
|
// A store of pods
|
|
|
|
podStore cache.StoreToPodLister
|
|
|
|
// A store of nodes
|
|
|
|
nodeStore cache.StoreToNodeLister
|
|
|
|
// Watches changes to all daemon sets.
|
|
|
|
dsController *framework.Controller
|
|
|
|
// Watches changes to all pods
|
2016-04-19 12:45:00 +00:00
|
|
|
podController framework.ControllerInterface
|
2015-09-08 18:50:39 +00:00
|
|
|
// Watches changes to all nodes.
|
|
|
|
nodeController *framework.Controller
|
2015-10-01 09:16:08 +00:00
|
|
|
// podStoreSynced returns true if the pod store has been synced at least once.
|
|
|
|
// Added as a member to the struct to allow injection for testing.
|
|
|
|
podStoreSynced func() bool
|
|
|
|
|
2016-02-26 03:39:43 +00:00
|
|
|
lookupCache *controller.MatchingCache
|
|
|
|
|
2015-09-08 18:50:39 +00:00
|
|
|
// Daemon sets that need to be synced.
|
|
|
|
queue *workqueue.Type
|
|
|
|
}
|
|
|
|
|
2016-04-07 12:15:21 +00:00
|
|
|
func NewDaemonSetsController(podInformer framework.SharedIndexInformer, kubeClient clientset.Interface, resyncPeriod controller.ResyncPeriodFunc, lookupCacheSize int) *DaemonSetsController {
|
2015-09-08 18:50:39 +00:00
|
|
|
eventBroadcaster := record.NewBroadcaster()
|
|
|
|
eventBroadcaster.StartLogging(glog.Infof)
|
2016-01-15 05:00:58 +00:00
|
|
|
// TODO: remove the wrapper when every clients have moved to use the clientset.
|
2016-03-23 23:45:24 +00:00
|
|
|
eventBroadcaster.StartRecordingToSink(&unversionedcore.EventSinkImpl{Interface: kubeClient.Core().Events("")})
|
2015-09-08 18:50:39 +00:00
|
|
|
|
2016-04-13 18:38:32 +00:00
|
|
|
if kubeClient != nil && kubeClient.Core().GetRESTClient().GetRateLimiter() != nil {
|
|
|
|
metrics.RegisterMetricAndTrackRateLimiterUsage("daemon_controller", kubeClient.Core().GetRESTClient().GetRateLimiter())
|
|
|
|
}
|
2015-09-08 18:50:39 +00:00
|
|
|
dsc := &DaemonSetsController{
|
2016-03-18 23:14:07 +00:00
|
|
|
kubeClient: kubeClient,
|
|
|
|
eventRecorder: eventBroadcaster.NewRecorder(api.EventSource{Component: "daemonset-controller"}),
|
2015-09-08 18:50:39 +00:00
|
|
|
podControl: controller.RealPodControl{
|
|
|
|
KubeClient: kubeClient,
|
|
|
|
Recorder: eventBroadcaster.NewRecorder(api.EventSource{Component: "daemon-set"}),
|
|
|
|
},
|
2015-10-20 10:47:46 +00:00
|
|
|
burstReplicas: BurstReplicas,
|
|
|
|
expectations: controller.NewControllerExpectations(),
|
|
|
|
queue: workqueue.New(),
|
2015-09-08 18:50:39 +00:00
|
|
|
}
|
|
|
|
// Manage addition/update of daemon sets.
|
|
|
|
dsc.dsStore.Store, dsc.dsController = framework.NewInformer(
|
|
|
|
&cache.ListWatch{
|
2015-12-10 09:39:03 +00:00
|
|
|
ListFunc: func(options api.ListOptions) (runtime.Object, error) {
|
2015-12-02 15:20:48 +00:00
|
|
|
return dsc.kubeClient.Extensions().DaemonSets(api.NamespaceAll).List(options)
|
2015-09-08 18:50:39 +00:00
|
|
|
},
|
2015-12-10 09:39:03 +00:00
|
|
|
WatchFunc: func(options api.ListOptions) (watch.Interface, error) {
|
2015-11-26 15:27:45 +00:00
|
|
|
return dsc.kubeClient.Extensions().DaemonSets(api.NamespaceAll).Watch(options)
|
2015-09-08 18:50:39 +00:00
|
|
|
},
|
|
|
|
},
|
2015-10-09 22:49:10 +00:00
|
|
|
&extensions.DaemonSet{},
|
2015-10-06 09:12:00 +00:00
|
|
|
// TODO: Can we have much longer period here?
|
2015-09-08 18:50:39 +00:00
|
|
|
FullDaemonSetResyncPeriod,
|
|
|
|
framework.ResourceEventHandlerFuncs{
|
|
|
|
AddFunc: func(obj interface{}) {
|
2015-10-09 22:49:10 +00:00
|
|
|
ds := obj.(*extensions.DaemonSet)
|
2015-09-08 18:50:39 +00:00
|
|
|
glog.V(4).Infof("Adding daemon set %s", ds.Name)
|
2016-03-18 23:14:07 +00:00
|
|
|
dsc.enqueueDaemonSet(ds)
|
2015-09-08 18:50:39 +00:00
|
|
|
},
|
|
|
|
UpdateFunc: func(old, cur interface{}) {
|
2015-10-09 22:49:10 +00:00
|
|
|
oldDS := old.(*extensions.DaemonSet)
|
2016-02-26 03:39:43 +00:00
|
|
|
curDS := cur.(*extensions.DaemonSet)
|
|
|
|
// We should invalidate the whole lookup cache if a DS's selector has been updated.
|
|
|
|
//
|
|
|
|
// Imagine that you have two RSs:
|
|
|
|
// * old DS1
|
|
|
|
// * new DS2
|
|
|
|
// You also have a pod that is attached to DS2 (because it doesn't match DS1 selector).
|
|
|
|
// Now imagine that you are changing DS1 selector so that it is now matching that pod,
|
|
|
|
// in such case we must invalidate the whole cache so that pod could be adopted by DS1
|
|
|
|
//
|
|
|
|
// This makes the lookup cache less helpful, but selector update does not happen often,
|
|
|
|
// so it's not a big problem
|
|
|
|
if !reflect.DeepEqual(oldDS.Spec.Selector, curDS.Spec.Selector) {
|
|
|
|
dsc.lookupCache.InvalidateAll()
|
|
|
|
}
|
|
|
|
|
2015-09-08 18:50:39 +00:00
|
|
|
glog.V(4).Infof("Updating daemon set %s", oldDS.Name)
|
2016-03-18 23:14:07 +00:00
|
|
|
dsc.enqueueDaemonSet(curDS)
|
2015-09-08 18:50:39 +00:00
|
|
|
},
|
2016-05-19 21:16:34 +00:00
|
|
|
DeleteFunc: dsc.deleteDaemonset,
|
2015-09-08 18:50:39 +00:00
|
|
|
},
|
|
|
|
)
|
2016-04-19 12:45:00 +00:00
|
|
|
|
2015-09-08 18:50:39 +00:00
|
|
|
// Watch for creation/deletion of pods. The reason we watch is that we don't want a daemon set to create/delete
|
|
|
|
// more pods until all the effects (expectations) of a daemon set's create/delete have been observed.
|
2016-04-19 12:45:00 +00:00
|
|
|
podInformer.AddEventHandler(framework.ResourceEventHandlerFuncs{
|
|
|
|
AddFunc: dsc.addPod,
|
|
|
|
UpdateFunc: dsc.updatePod,
|
|
|
|
DeleteFunc: dsc.deletePod,
|
|
|
|
})
|
2016-04-07 12:15:21 +00:00
|
|
|
dsc.podStore.Indexer = podInformer.GetIndexer()
|
2016-04-19 12:45:00 +00:00
|
|
|
dsc.podController = podInformer.GetController()
|
|
|
|
dsc.podStoreSynced = podInformer.HasSynced
|
|
|
|
|
2015-09-08 18:50:39 +00:00
|
|
|
// Watch for new nodes or updates to nodes - daemon pods are launched on new nodes, and possibly when labels on nodes change,
|
|
|
|
dsc.nodeStore.Store, dsc.nodeController = framework.NewInformer(
|
|
|
|
&cache.ListWatch{
|
2015-12-10 09:39:03 +00:00
|
|
|
ListFunc: func(options api.ListOptions) (runtime.Object, error) {
|
2016-02-03 21:21:05 +00:00
|
|
|
return dsc.kubeClient.Core().Nodes().List(options)
|
2015-09-08 18:50:39 +00:00
|
|
|
},
|
2015-12-10 09:39:03 +00:00
|
|
|
WatchFunc: func(options api.ListOptions) (watch.Interface, error) {
|
2016-02-03 21:21:05 +00:00
|
|
|
return dsc.kubeClient.Core().Nodes().Watch(options)
|
2015-09-08 18:50:39 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&api.Node{},
|
2015-10-06 09:12:00 +00:00
|
|
|
resyncPeriod(),
|
2015-09-08 18:50:39 +00:00
|
|
|
framework.ResourceEventHandlerFuncs{
|
|
|
|
AddFunc: dsc.addNode,
|
|
|
|
UpdateFunc: dsc.updateNode,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
dsc.syncHandler = dsc.syncDaemonSet
|
2016-02-26 03:39:43 +00:00
|
|
|
dsc.lookupCache = controller.NewMatchingCache(lookupCacheSize)
|
2015-09-08 18:50:39 +00:00
|
|
|
return dsc
|
|
|
|
}
|
|
|
|
|
2016-04-19 12:45:00 +00:00
|
|
|
func NewDaemonSetsControllerFromClient(kubeClient clientset.Interface, resyncPeriod controller.ResyncPeriodFunc, lookupCacheSize int) *DaemonSetsController {
|
2016-08-04 07:02:13 +00:00
|
|
|
podInformer := informers.NewPodInformer(kubeClient, resyncPeriod())
|
2016-04-19 12:45:00 +00:00
|
|
|
dsc := NewDaemonSetsController(podInformer, kubeClient, resyncPeriod, lookupCacheSize)
|
|
|
|
dsc.internalPodInformer = podInformer
|
|
|
|
|
|
|
|
return dsc
|
|
|
|
}
|
|
|
|
|
2016-05-19 21:16:34 +00:00
|
|
|
func (dsc *DaemonSetsController) deleteDaemonset(obj interface{}) {
|
|
|
|
ds, ok := obj.(*extensions.DaemonSet)
|
|
|
|
if !ok {
|
|
|
|
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
|
|
|
|
if !ok {
|
2016-06-14 12:04:38 +00:00
|
|
|
glog.Errorf("Couldn't get object from tombstone %#v", obj)
|
2016-05-19 21:16:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ds, ok = tombstone.Obj.(*extensions.DaemonSet)
|
|
|
|
if !ok {
|
2016-06-14 12:04:38 +00:00
|
|
|
glog.Errorf("Tombstone contained object that is not a DaemonSet %#v", obj)
|
2016-05-19 21:16:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
glog.V(4).Infof("Deleting daemon set %s", ds.Name)
|
|
|
|
dsc.enqueueDaemonSet(ds)
|
|
|
|
}
|
|
|
|
|
2015-09-08 18:50:39 +00:00
|
|
|
// Run begins watching and syncing daemon sets.
|
|
|
|
func (dsc *DaemonSetsController) Run(workers int, stopCh <-chan struct{}) {
|
2016-01-15 07:32:10 +00:00
|
|
|
defer utilruntime.HandleCrash()
|
2016-01-22 02:23:17 +00:00
|
|
|
glog.Infof("Starting Daemon Sets controller manager")
|
2015-09-08 18:50:39 +00:00
|
|
|
go dsc.dsController.Run(stopCh)
|
|
|
|
go dsc.podController.Run(stopCh)
|
|
|
|
go dsc.nodeController.Run(stopCh)
|
|
|
|
for i := 0; i < workers; i++ {
|
2016-06-06 06:29:57 +00:00
|
|
|
go wait.Until(dsc.runWorker, time.Second, stopCh)
|
2015-09-08 18:50:39 +00:00
|
|
|
}
|
2016-04-19 12:45:00 +00:00
|
|
|
|
|
|
|
if dsc.internalPodInformer != nil {
|
|
|
|
go dsc.internalPodInformer.Run(stopCh)
|
|
|
|
}
|
|
|
|
|
2015-09-08 18:50:39 +00:00
|
|
|
<-stopCh
|
|
|
|
glog.Infof("Shutting down Daemon Set Controller")
|
|
|
|
dsc.queue.ShutDown()
|
|
|
|
}
|
|
|
|
|
2016-06-06 06:29:57 +00:00
|
|
|
func (dsc *DaemonSetsController) runWorker() {
|
2015-09-08 18:50:39 +00:00
|
|
|
for {
|
2016-06-06 06:29:57 +00:00
|
|
|
dsKey, quit := dsc.queue.Get()
|
|
|
|
if quit {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err := dsc.syncHandler(dsKey.(string))
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("Error syncing daemon set with key %s: %v", dsKey.(string), err)
|
|
|
|
}
|
|
|
|
dsc.queue.Done(dsKey)
|
2015-09-08 18:50:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-18 23:14:07 +00:00
|
|
|
func (dsc *DaemonSetsController) enqueueDaemonSet(ds *extensions.DaemonSet) {
|
|
|
|
key, err := controller.KeyFunc(ds)
|
2015-09-08 18:50:39 +00:00
|
|
|
if err != nil {
|
2016-06-14 12:04:38 +00:00
|
|
|
glog.Errorf("Couldn't get key for object %#v: %v", ds, err)
|
2015-09-08 18:50:39 +00:00
|
|
|
return
|
|
|
|
}
|
2015-10-03 21:03:27 +00:00
|
|
|
|
|
|
|
// TODO: Handle overlapping controllers better. See comment in ReplicationManager.
|
2015-09-08 18:50:39 +00:00
|
|
|
dsc.queue.Add(key)
|
|
|
|
}
|
|
|
|
|
2015-10-09 22:49:10 +00:00
|
|
|
func (dsc *DaemonSetsController) getPodDaemonSet(pod *api.Pod) *extensions.DaemonSet {
|
2016-02-26 03:39:43 +00:00
|
|
|
// look up in the cache, if cached and the cache is valid, just return cached value
|
|
|
|
if obj, cached := dsc.lookupCache.GetMatchingObject(pod); cached {
|
|
|
|
ds, ok := obj.(*extensions.DaemonSet)
|
|
|
|
if !ok {
|
|
|
|
// This should not happen
|
|
|
|
glog.Errorf("lookup cache does not retuen a ReplicationController object")
|
|
|
|
return nil
|
|
|
|
}
|
2016-06-06 07:07:28 +00:00
|
|
|
if dsc.isCacheValid(pod, ds) {
|
2016-02-26 03:39:43 +00:00
|
|
|
return ds
|
|
|
|
}
|
|
|
|
}
|
2015-09-08 18:50:39 +00:00
|
|
|
sets, err := dsc.dsStore.GetPodDaemonSets(pod)
|
|
|
|
if err != nil {
|
|
|
|
glog.V(4).Infof("No daemon sets found for pod %v, daemon set controller will avoid syncing", pod.Name)
|
|
|
|
return nil
|
|
|
|
}
|
2015-09-18 19:19:07 +00:00
|
|
|
if len(sets) > 1 {
|
|
|
|
// More than two items in this list indicates user error. If two daemon
|
|
|
|
// sets overlap, sort by creation timestamp, subsort by name, then pick
|
|
|
|
// the first.
|
|
|
|
glog.Errorf("user error! more than one daemon is selecting pods with labels: %+v", pod.Labels)
|
|
|
|
sort.Sort(byCreationTimestamp(sets))
|
|
|
|
}
|
2016-02-26 03:39:43 +00:00
|
|
|
|
|
|
|
// update lookup cache
|
|
|
|
dsc.lookupCache.Update(pod, &sets[0])
|
|
|
|
|
2015-09-08 18:50:39 +00:00
|
|
|
return &sets[0]
|
|
|
|
}
|
|
|
|
|
2016-02-26 03:39:43 +00:00
|
|
|
// isCacheValid check if the cache is valid
|
|
|
|
func (dsc *DaemonSetsController) isCacheValid(pod *api.Pod, cachedDS *extensions.DaemonSet) bool {
|
|
|
|
_, exists, err := dsc.dsStore.Get(cachedDS)
|
|
|
|
// ds has been deleted or updated, cache is invalid
|
|
|
|
if err != nil || !exists || !isDaemonSetMatch(pod, cachedDS) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// isDaemonSetMatch take a Pod and DaemonSet, return whether the Pod and DaemonSet are matching
|
|
|
|
// TODO(mqliang): This logic is a copy from GetPodDaemonSets(), remove the duplication
|
|
|
|
func isDaemonSetMatch(pod *api.Pod, ds *extensions.DaemonSet) bool {
|
|
|
|
if ds.Namespace != pod.Namespace {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
selector, err := unversioned.LabelSelectorAsSelector(ds.Spec.Selector)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("invalid selector: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a ReplicaSet with a nil or empty selector creeps in, it should match nothing, not everything.
|
|
|
|
if selector.Empty() || !selector.Matches(labels.Set(pod.Labels)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-09-08 18:50:39 +00:00
|
|
|
func (dsc *DaemonSetsController) addPod(obj interface{}) {
|
|
|
|
pod := obj.(*api.Pod)
|
|
|
|
glog.V(4).Infof("Pod %s added.", pod.Name)
|
|
|
|
if ds := dsc.getPodDaemonSet(pod); ds != nil {
|
|
|
|
dsKey, err := controller.KeyFunc(ds)
|
|
|
|
if err != nil {
|
2016-06-14 12:04:38 +00:00
|
|
|
glog.Errorf("Couldn't get key for object %#v: %v", ds, err)
|
2015-09-08 18:50:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
dsc.expectations.CreationObserved(dsKey)
|
|
|
|
dsc.enqueueDaemonSet(ds)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// When a pod is updated, figure out what sets manage it and wake them
|
|
|
|
// up. If the labels of the pod have changed we need to awaken both the old
|
|
|
|
// and new set. old and cur must be *api.Pod types.
|
|
|
|
func (dsc *DaemonSetsController) updatePod(old, cur interface{}) {
|
|
|
|
if api.Semantic.DeepEqual(old, cur) {
|
|
|
|
// A periodic relist will send update events for all known pods.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
curPod := cur.(*api.Pod)
|
|
|
|
glog.V(4).Infof("Pod %s updated.", curPod.Name)
|
|
|
|
if curDS := dsc.getPodDaemonSet(curPod); curDS != nil {
|
|
|
|
dsc.enqueueDaemonSet(curDS)
|
|
|
|
}
|
|
|
|
oldPod := old.(*api.Pod)
|
|
|
|
// If the labels have not changed, then the daemon set responsible for
|
|
|
|
// the pod is the same as it was before. In that case we have enqueued the daemon
|
|
|
|
// set above, and do not have to enqueue the set again.
|
|
|
|
if !reflect.DeepEqual(curPod.Labels, oldPod.Labels) {
|
|
|
|
// It's ok if both oldDS and curDS are the same, because curDS will set
|
|
|
|
// the expectations on its run so oldDS will have no effect.
|
|
|
|
if oldDS := dsc.getPodDaemonSet(oldPod); oldDS != nil {
|
|
|
|
dsc.enqueueDaemonSet(oldDS)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dsc *DaemonSetsController) deletePod(obj interface{}) {
|
|
|
|
pod, ok := obj.(*api.Pod)
|
|
|
|
// When a delete is dropped, the relist will notice a pod in the store not
|
|
|
|
// in the list, leading to the insertion of a tombstone object which contains
|
|
|
|
// the deleted key/value. Note that this value might be stale. If the pod
|
2016-02-26 00:15:46 +00:00
|
|
|
// changed labels the new daemonset will not be woken up till the periodic
|
|
|
|
// resync.
|
2015-09-08 18:50:39 +00:00
|
|
|
if !ok {
|
|
|
|
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
|
|
|
|
if !ok {
|
2016-06-14 12:04:38 +00:00
|
|
|
glog.Errorf("Couldn't get object from tombstone %#v", obj)
|
2015-09-08 18:50:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
pod, ok = tombstone.Obj.(*api.Pod)
|
|
|
|
if !ok {
|
2016-06-14 12:04:38 +00:00
|
|
|
glog.Errorf("Tombstone contained object that is not a pod %#v", obj)
|
2015-09-08 18:50:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2016-02-26 00:15:46 +00:00
|
|
|
glog.V(4).Infof("Pod %s deleted.", pod.Name)
|
2015-09-08 18:50:39 +00:00
|
|
|
if ds := dsc.getPodDaemonSet(pod); ds != nil {
|
|
|
|
dsKey, err := controller.KeyFunc(ds)
|
|
|
|
if err != nil {
|
2016-06-14 12:04:38 +00:00
|
|
|
glog.Errorf("Couldn't get key for object %#v: %v", ds, err)
|
2015-09-08 18:50:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
dsc.expectations.DeletionObserved(dsKey)
|
|
|
|
dsc.enqueueDaemonSet(ds)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dsc *DaemonSetsController) addNode(obj interface{}) {
|
|
|
|
// TODO: it'd be nice to pass a hint with these enqueues, so that each ds would only examine the added node (unless it has other work to do, too).
|
2015-11-18 01:39:55 +00:00
|
|
|
dsList, err := dsc.dsStore.List()
|
|
|
|
if err != nil {
|
|
|
|
glog.V(4).Infof("Error enqueueing daemon sets: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
node := obj.(*api.Node)
|
|
|
|
for i := range dsList.Items {
|
|
|
|
ds := &dsList.Items[i]
|
2016-01-28 06:13:05 +00:00
|
|
|
shouldEnqueue := dsc.nodeShouldRunDaemonPod(node, ds)
|
2015-11-18 01:39:55 +00:00
|
|
|
if shouldEnqueue {
|
|
|
|
dsc.enqueueDaemonSet(ds)
|
|
|
|
}
|
|
|
|
}
|
2015-09-08 18:50:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (dsc *DaemonSetsController) updateNode(old, cur interface{}) {
|
|
|
|
oldNode := old.(*api.Node)
|
|
|
|
curNode := cur.(*api.Node)
|
|
|
|
if api.Semantic.DeepEqual(oldNode.Name, curNode.Name) && api.Semantic.DeepEqual(oldNode.Namespace, curNode.Namespace) && api.Semantic.DeepEqual(oldNode.Labels, curNode.Labels) {
|
|
|
|
// A periodic relist will send update events for all known pods.
|
|
|
|
return
|
|
|
|
}
|
2015-11-18 01:39:55 +00:00
|
|
|
dsList, err := dsc.dsStore.List()
|
|
|
|
if err != nil {
|
|
|
|
glog.V(4).Infof("Error enqueueing daemon sets: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for i := range dsList.Items {
|
|
|
|
ds := &dsList.Items[i]
|
2016-01-28 06:13:05 +00:00
|
|
|
shouldEnqueue := (dsc.nodeShouldRunDaemonPod(oldNode, ds) != dsc.nodeShouldRunDaemonPod(curNode, ds))
|
2015-11-18 01:39:55 +00:00
|
|
|
if shouldEnqueue {
|
|
|
|
dsc.enqueueDaemonSet(ds)
|
|
|
|
}
|
|
|
|
}
|
2015-09-08 18:50:39 +00:00
|
|
|
// TODO: it'd be nice to pass a hint with these enqueues, so that each ds would only examine the added node (unless it has other work to do, too).
|
|
|
|
}
|
|
|
|
|
|
|
|
// getNodesToDaemonSetPods returns a map from nodes to daemon pods (corresponding to ds) running on the nodes.
|
2015-10-09 22:49:10 +00:00
|
|
|
func (dsc *DaemonSetsController) getNodesToDaemonPods(ds *extensions.DaemonSet) (map[string][]*api.Pod, error) {
|
2015-09-08 18:50:39 +00:00
|
|
|
nodeToDaemonPods := make(map[string][]*api.Pod)
|
2016-02-02 05:34:42 +00:00
|
|
|
selector, err := unversioned.LabelSelectorAsSelector(ds.Spec.Selector)
|
2015-10-26 06:11:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
daemonPods, err := dsc.podStore.Pods(ds.Namespace).List(selector)
|
2015-09-08 18:50:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nodeToDaemonPods, err
|
|
|
|
}
|
|
|
|
for i := range daemonPods.Items {
|
|
|
|
nodeName := daemonPods.Items[i].Spec.NodeName
|
|
|
|
nodeToDaemonPods[nodeName] = append(nodeToDaemonPods[nodeName], &daemonPods.Items[i])
|
|
|
|
}
|
|
|
|
return nodeToDaemonPods, nil
|
|
|
|
}
|
|
|
|
|
2015-10-09 22:49:10 +00:00
|
|
|
func (dsc *DaemonSetsController) manage(ds *extensions.DaemonSet) {
|
2015-09-08 18:50:39 +00:00
|
|
|
// Find out which nodes are running the daemon pods selected by ds.
|
|
|
|
nodeToDaemonPods, err := dsc.getNodesToDaemonPods(ds)
|
|
|
|
if err != nil {
|
2016-06-14 12:04:38 +00:00
|
|
|
glog.Errorf("Error getting node to daemon pod mapping for daemon set %#v: %v", ds, err)
|
2015-09-08 18:50:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// For each node, if the node is running the daemon pod but isn't supposed to, kill the daemon
|
|
|
|
// pod. If the node is supposed to run the daemon pod, but isn't, create the daemon pod on the node.
|
|
|
|
nodeList, err := dsc.nodeStore.List()
|
|
|
|
if err != nil {
|
2016-06-14 12:04:38 +00:00
|
|
|
glog.Errorf("Couldn't get list of nodes when syncing daemon set %#v: %v", ds, err)
|
2015-09-08 18:50:39 +00:00
|
|
|
}
|
|
|
|
var nodesNeedingDaemonPods, podsToDelete []string
|
2015-11-16 16:09:43 +00:00
|
|
|
for _, node := range nodeList.Items {
|
2016-01-28 06:13:05 +00:00
|
|
|
shouldRun := dsc.nodeShouldRunDaemonPod(&node, ds)
|
2015-11-18 01:39:55 +00:00
|
|
|
|
2015-11-16 16:09:43 +00:00
|
|
|
daemonPods, isRunning := nodeToDaemonPods[node.Name]
|
|
|
|
|
2016-06-06 07:07:28 +00:00
|
|
|
switch {
|
|
|
|
case shouldRun && !isRunning:
|
2015-09-08 18:50:39 +00:00
|
|
|
// If daemon pod is supposed to be running on node, but isn't, create daemon pod.
|
2015-11-16 16:09:43 +00:00
|
|
|
nodesNeedingDaemonPods = append(nodesNeedingDaemonPods, node.Name)
|
2016-06-06 07:07:28 +00:00
|
|
|
case shouldRun && len(daemonPods) > 1:
|
2015-09-08 18:50:39 +00:00
|
|
|
// If daemon pod is supposed to be running on node, but more than 1 daemon pod is running, delete the excess daemon pods.
|
2015-09-29 09:07:43 +00:00
|
|
|
// Sort the daemon pods by creation time, so the the oldest is preserved.
|
|
|
|
sort.Sort(podByCreationTimestamp(daemonPods))
|
2015-09-08 18:50:39 +00:00
|
|
|
for i := 1; i < len(daemonPods); i++ {
|
|
|
|
podsToDelete = append(podsToDelete, daemonPods[i].Name)
|
|
|
|
}
|
2016-06-06 07:07:28 +00:00
|
|
|
case !shouldRun && isRunning:
|
2015-09-08 18:50:39 +00:00
|
|
|
// If daemon pod isn't supposed to run on node, but it is, delete all daemon pods on node.
|
|
|
|
for i := range daemonPods {
|
|
|
|
podsToDelete = append(podsToDelete, daemonPods[i].Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to set expectations before creating/deleting pods to avoid race conditions.
|
|
|
|
dsKey, err := controller.KeyFunc(ds)
|
|
|
|
if err != nil {
|
2016-06-14 12:04:38 +00:00
|
|
|
glog.Errorf("Couldn't get key for object %#v: %v", ds, err)
|
2015-09-08 18:50:39 +00:00
|
|
|
return
|
|
|
|
}
|
2015-10-20 10:47:46 +00:00
|
|
|
|
|
|
|
createDiff := len(nodesNeedingDaemonPods)
|
|
|
|
deleteDiff := len(podsToDelete)
|
|
|
|
|
|
|
|
if createDiff > dsc.burstReplicas {
|
|
|
|
createDiff = dsc.burstReplicas
|
|
|
|
}
|
|
|
|
if deleteDiff > dsc.burstReplicas {
|
|
|
|
deleteDiff = dsc.burstReplicas
|
2015-09-08 18:50:39 +00:00
|
|
|
}
|
|
|
|
|
2015-10-20 10:47:46 +00:00
|
|
|
dsc.expectations.SetExpectations(dsKey, createDiff, deleteDiff)
|
|
|
|
|
|
|
|
glog.V(4).Infof("Nodes needing daemon pods for daemon set %s: %+v, creating %d", ds.Name, nodesNeedingDaemonPods, createDiff)
|
|
|
|
createWait := sync.WaitGroup{}
|
|
|
|
createWait.Add(createDiff)
|
|
|
|
for i := 0; i < createDiff; i++ {
|
|
|
|
go func(ix int) {
|
|
|
|
defer createWait.Done()
|
2016-01-11 22:17:42 +00:00
|
|
|
if err := dsc.podControl.CreatePodsOnNode(nodesNeedingDaemonPods[ix], ds.Namespace, &ds.Spec.Template, ds); err != nil {
|
2015-10-20 10:47:46 +00:00
|
|
|
glog.V(2).Infof("Failed creation, decrementing expectations for set %q/%q", ds.Namespace, ds.Name)
|
|
|
|
dsc.expectations.CreationObserved(dsKey)
|
2016-01-15 07:32:10 +00:00
|
|
|
utilruntime.HandleError(err)
|
2015-10-20 10:47:46 +00:00
|
|
|
}
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
createWait.Wait()
|
|
|
|
|
|
|
|
glog.V(4).Infof("Pods to delete for daemon set %s: %+v, deleting %d", ds.Name, podsToDelete, deleteDiff)
|
|
|
|
deleteWait := sync.WaitGroup{}
|
|
|
|
deleteWait.Add(deleteDiff)
|
|
|
|
for i := 0; i < deleteDiff; i++ {
|
|
|
|
go func(ix int) {
|
|
|
|
defer deleteWait.Done()
|
|
|
|
if err := dsc.podControl.DeletePod(ds.Namespace, podsToDelete[ix], ds); err != nil {
|
|
|
|
glog.V(2).Infof("Failed deletion, decrementing expectations for set %q/%q", ds.Namespace, ds.Name)
|
|
|
|
dsc.expectations.DeletionObserved(dsKey)
|
2016-01-15 07:32:10 +00:00
|
|
|
utilruntime.HandleError(err)
|
2015-10-20 10:47:46 +00:00
|
|
|
}
|
|
|
|
}(i)
|
2015-09-08 18:50:39 +00:00
|
|
|
}
|
2015-10-20 10:47:46 +00:00
|
|
|
deleteWait.Wait()
|
2015-09-08 18:50:39 +00:00
|
|
|
}
|
|
|
|
|
2016-02-16 17:54:53 +00:00
|
|
|
func storeDaemonSetStatus(dsClient unversionedextensions.DaemonSetInterface, ds *extensions.DaemonSet, desiredNumberScheduled, currentNumberScheduled, numberMisscheduled int) error {
|
2016-06-07 02:28:05 +00:00
|
|
|
if int(ds.Status.DesiredNumberScheduled) == desiredNumberScheduled &&
|
|
|
|
int(ds.Status.CurrentNumberScheduled) == currentNumberScheduled &&
|
|
|
|
int(ds.Status.NumberMisscheduled) == numberMisscheduled {
|
2015-09-08 18:50:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-02-11 03:46:16 +00:00
|
|
|
|
2015-09-08 18:50:39 +00:00
|
|
|
var updateErr, getErr error
|
2016-06-07 02:28:05 +00:00
|
|
|
for i := 0; i < StatusUpdateRetries; i++ {
|
2016-04-27 04:35:14 +00:00
|
|
|
ds.Status.DesiredNumberScheduled = int32(desiredNumberScheduled)
|
|
|
|
ds.Status.CurrentNumberScheduled = int32(currentNumberScheduled)
|
|
|
|
ds.Status.NumberMisscheduled = int32(numberMisscheduled)
|
2015-11-04 07:29:31 +00:00
|
|
|
|
2016-06-07 02:28:05 +00:00
|
|
|
if _, updateErr = dsClient.UpdateStatus(ds); updateErr == nil {
|
2015-09-08 18:50:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-06-07 02:28:05 +00:00
|
|
|
|
2015-09-08 18:50:39 +00:00
|
|
|
// Update the set with the latest resource version for the next poll
|
|
|
|
if ds, getErr = dsClient.Get(ds.Name); getErr != nil {
|
|
|
|
// If the GET fails we can't trust status.Replicas anymore. This error
|
|
|
|
// is bound to be more interesting than the update failure.
|
|
|
|
return getErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return updateErr
|
|
|
|
}
|
|
|
|
|
2015-10-09 22:49:10 +00:00
|
|
|
func (dsc *DaemonSetsController) updateDaemonSetStatus(ds *extensions.DaemonSet) {
|
2015-09-21 22:40:57 +00:00
|
|
|
glog.V(4).Infof("Updating daemon set status")
|
2015-09-08 18:50:39 +00:00
|
|
|
nodeToDaemonPods, err := dsc.getNodesToDaemonPods(ds)
|
|
|
|
if err != nil {
|
2016-06-14 12:04:38 +00:00
|
|
|
glog.Errorf("Error getting node to daemon pod mapping for daemon set %#v: %v", ds, err)
|
2016-06-07 02:28:05 +00:00
|
|
|
return
|
2015-09-08 18:50:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nodeList, err := dsc.nodeStore.List()
|
|
|
|
if err != nil {
|
2016-06-14 12:04:38 +00:00
|
|
|
glog.Errorf("Couldn't get list of nodes when updating daemon set %#v: %v", ds, err)
|
2016-06-07 02:28:05 +00:00
|
|
|
return
|
2015-09-08 18:50:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var desiredNumberScheduled, currentNumberScheduled, numberMisscheduled int
|
|
|
|
for _, node := range nodeList.Items {
|
2016-01-28 06:13:05 +00:00
|
|
|
shouldRun := dsc.nodeShouldRunDaemonPod(&node, ds)
|
2015-10-07 00:01:54 +00:00
|
|
|
|
2016-06-07 02:28:05 +00:00
|
|
|
scheduled := len(nodeToDaemonPods[node.Name]) > 0
|
2015-09-08 18:50:39 +00:00
|
|
|
|
|
|
|
if shouldRun {
|
|
|
|
desiredNumberScheduled++
|
2016-06-07 02:28:05 +00:00
|
|
|
if scheduled {
|
|
|
|
currentNumberScheduled++
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if scheduled {
|
|
|
|
numberMisscheduled++
|
|
|
|
}
|
2015-09-08 18:50:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-12 18:18:50 +00:00
|
|
|
err = storeDaemonSetStatus(dsc.kubeClient.Extensions().DaemonSets(ds.Namespace), ds, desiredNumberScheduled, currentNumberScheduled, numberMisscheduled)
|
2015-09-08 18:50:39 +00:00
|
|
|
if err != nil {
|
2016-06-14 12:04:38 +00:00
|
|
|
glog.Errorf("Error storing status for daemon set %#v: %v", ds, err)
|
2015-09-08 18:50:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dsc *DaemonSetsController) syncDaemonSet(key string) error {
|
|
|
|
startTime := time.Now()
|
|
|
|
defer func() {
|
|
|
|
glog.V(4).Infof("Finished syncing daemon set %q (%v)", key, time.Now().Sub(startTime))
|
|
|
|
}()
|
2016-02-13 03:47:33 +00:00
|
|
|
|
|
|
|
if !dsc.podStoreSynced() {
|
|
|
|
// Sleep so we give the pod reflector goroutine a chance to run.
|
|
|
|
time.Sleep(PodStoreSyncedPollPeriod)
|
|
|
|
glog.Infof("Waiting for pods controller to sync, requeuing ds %v", key)
|
|
|
|
dsc.queue.Add(key)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-08 18:50:39 +00:00
|
|
|
obj, exists, err := dsc.dsStore.Store.GetByKey(key)
|
|
|
|
if err != nil {
|
|
|
|
glog.Infof("Unable to retrieve ds %v from store: %v", key, err)
|
|
|
|
dsc.queue.Add(key)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
glog.V(3).Infof("daemon set has been deleted %v", key)
|
|
|
|
dsc.expectations.DeleteExpectations(key)
|
|
|
|
return nil
|
|
|
|
}
|
2015-10-09 22:49:10 +00:00
|
|
|
ds := obj.(*extensions.DaemonSet)
|
2015-09-08 18:50:39 +00:00
|
|
|
|
2016-03-18 23:14:07 +00:00
|
|
|
everything := unversioned.LabelSelector{}
|
|
|
|
if reflect.DeepEqual(ds.Spec.Selector, &everything) {
|
2016-03-25 07:40:12 +00:00
|
|
|
dsc.eventRecorder.Eventf(ds, api.EventTypeWarning, "SelectingAll", "This daemon set is selecting all pods. A non-empty selector is required.")
|
2016-03-18 23:14:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-08 18:50:39 +00:00
|
|
|
// Don't process a daemon set until all its creations and deletions have been processed.
|
|
|
|
// For example if daemon set foo asked for 3 new daemon pods in the previous call to manage,
|
|
|
|
// then we do not want to call manage on foo until the daemon pods have been created.
|
|
|
|
dsKey, err := controller.KeyFunc(ds)
|
|
|
|
if err != nil {
|
2016-06-14 12:04:38 +00:00
|
|
|
glog.Errorf("Couldn't get key for object %#v: %v", ds, err)
|
2015-09-08 18:50:39 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
dsNeedsSync := dsc.expectations.SatisfiedExpectations(dsKey)
|
2016-06-15 13:34:14 +00:00
|
|
|
if dsNeedsSync && ds.DeletionTimestamp == nil {
|
2015-09-08 18:50:39 +00:00
|
|
|
dsc.manage(ds)
|
|
|
|
}
|
|
|
|
|
|
|
|
dsc.updateDaemonSetStatus(ds)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-28 06:13:05 +00:00
|
|
|
func (dsc *DaemonSetsController) nodeShouldRunDaemonPod(node *api.Node, ds *extensions.DaemonSet) bool {
|
2015-11-16 16:09:43 +00:00
|
|
|
// If the daemon set specifies a node name, check that it matches with node.Name.
|
2016-01-28 06:13:05 +00:00
|
|
|
if !(ds.Spec.Template.Spec.NodeName == "" || ds.Spec.Template.Spec.NodeName == node.Name) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-07-01 17:02:51 +00:00
|
|
|
// TODO: Move it to the predicates
|
2016-01-28 06:13:05 +00:00
|
|
|
for _, c := range node.Status.Conditions {
|
|
|
|
if c.Type == api.NodeOutOfDisk && c.Status == api.ConditionTrue {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-01 17:02:51 +00:00
|
|
|
newPod := &api.Pod{Spec: ds.Spec.Template.Spec, ObjectMeta: ds.Spec.Template.ObjectMeta}
|
2016-01-28 06:13:05 +00:00
|
|
|
newPod.Spec.NodeName = node.Name
|
2016-07-01 17:02:51 +00:00
|
|
|
|
|
|
|
pods := []*api.Pod{}
|
2015-11-16 16:09:43 +00:00
|
|
|
|
2016-04-07 12:15:21 +00:00
|
|
|
for _, m := range dsc.podStore.Indexer.List() {
|
2016-01-28 06:13:05 +00:00
|
|
|
pod := m.(*api.Pod)
|
|
|
|
if pod.Spec.NodeName != node.Name {
|
|
|
|
continue
|
|
|
|
}
|
2016-04-06 18:47:16 +00:00
|
|
|
if pod.Status.Phase == api.PodSucceeded || pod.Status.Phase == api.PodFailed {
|
|
|
|
continue
|
|
|
|
}
|
2016-03-03 06:15:20 +00:00
|
|
|
// ignore pods that belong to the daemonset when taking into account wheter
|
|
|
|
// a daemonset should bind to a node.
|
|
|
|
if pds := dsc.getPodDaemonSet(pod); pds != nil && ds.Name == pds.Name {
|
|
|
|
continue
|
|
|
|
}
|
2016-01-28 06:13:05 +00:00
|
|
|
pods = append(pods, pod)
|
|
|
|
}
|
2016-07-01 17:02:51 +00:00
|
|
|
|
|
|
|
nodeInfo := schedulercache.NewNodeInfo(pods...)
|
|
|
|
nodeInfo.SetNode(node)
|
|
|
|
fit, err := predicates.GeneralPredicates(newPod, nil, nodeInfo)
|
|
|
|
if err != nil {
|
|
|
|
if re, ok := err.(*predicates.PredicateFailureError); ok {
|
|
|
|
message := re.Error()
|
|
|
|
glog.V(2).Infof("Predicate failed on Pod: %s, for reason: %v", newPod.Name, message)
|
2016-01-28 06:13:05 +00:00
|
|
|
}
|
2016-07-01 17:02:51 +00:00
|
|
|
if re, ok := err.(*predicates.InsufficientResourceError); ok {
|
|
|
|
message := re.Error()
|
|
|
|
glog.V(2).Infof("Predicate failed on Pod: %s, for reason: %v", newPod.Name, message)
|
|
|
|
}
|
|
|
|
message := fmt.Sprintf("GeneralPredicates failed due to %v.", err)
|
|
|
|
glog.Warningf("Predicate failed on Pod %s - %s", newPod.Name, message)
|
2016-01-28 06:13:05 +00:00
|
|
|
}
|
2016-07-01 17:02:51 +00:00
|
|
|
return fit
|
2015-11-16 16:09:43 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 18:50:39 +00:00
|
|
|
// byCreationTimestamp sorts a list by creation timestamp, using their names as a tie breaker.
|
2015-10-09 22:49:10 +00:00
|
|
|
type byCreationTimestamp []extensions.DaemonSet
|
2015-09-08 18:50:39 +00:00
|
|
|
|
|
|
|
func (o byCreationTimestamp) Len() int { return len(o) }
|
|
|
|
func (o byCreationTimestamp) Swap(i, j int) { o[i], o[j] = o[j], o[i] }
|
|
|
|
|
|
|
|
func (o byCreationTimestamp) Less(i, j int) bool {
|
|
|
|
if o[i].CreationTimestamp.Equal(o[j].CreationTimestamp) {
|
|
|
|
return o[i].Name < o[j].Name
|
|
|
|
}
|
|
|
|
return o[i].CreationTimestamp.Before(o[j].CreationTimestamp)
|
|
|
|
}
|
2015-09-29 09:07:43 +00:00
|
|
|
|
|
|
|
type podByCreationTimestamp []*api.Pod
|
|
|
|
|
|
|
|
func (o podByCreationTimestamp) Len() int { return len(o) }
|
|
|
|
func (o podByCreationTimestamp) Swap(i, j int) { o[i], o[j] = o[j], o[i] }
|
|
|
|
|
|
|
|
func (o podByCreationTimestamp) Less(i, j int) bool {
|
|
|
|
if o[i].CreationTimestamp.Equal(o[j].CreationTimestamp) {
|
|
|
|
return o[i].Name < o[j].Name
|
|
|
|
}
|
|
|
|
return o[i].CreationTimestamp.Before(o[j].CreationTimestamp)
|
|
|
|
}
|