k3s/pkg/controller/replication_controller.go

345 lines
13 KiB
Go
Raw Normal View History

2014-06-06 23:40:48 +00:00
/*
Copyright 2014 Google Inc. All rights reserved.
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 controller
2014-06-06 23:40:48 +00:00
import (
2015-04-21 20:40:35 +00:00
"reflect"
"sort"
"sync"
2014-06-06 23:40:48 +00:00
"time"
2014-06-12 20:17:34 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
2014-06-06 23:40:48 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
2015-04-21 20:40:35 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/cache"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/record"
2015-04-21 20:40:35 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/controller/framework"
2015-03-15 21:51:41 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
2014-06-23 00:02:48 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
2015-04-21 20:40:35 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
2014-06-06 23:40:48 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
2015-04-21 20:40:35 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/workqueue"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
"github.com/golang/glog"
2014-06-06 23:40:48 +00:00
)
2015-04-21 20:40:35 +00:00
var (
rcKeyFunc = framework.DeletionHandlingMetaNamespaceKeyFunc
)
const (
// We'll attempt to recompute the required replicas of all replication controllers
// the have fulfilled their expectations at least this often.
FullControllerResyncPeriod = 30 * time.Second
// If a watch misdelivers info about a pod, it'll take this long
// to rectify the number of replicas.
PodRelistPeriod = 5 * time.Minute
// If a watch drops an (add, delete) event for a pod, it'll take this long
// before a dormant rc waiting for those packets is woken up anyway. This
// should typically be somewhere between the PodRelistPeriod and the
// FullControllerResyncPeriod. It is specifically targeted at the case
// where some problem prevents an update of expectations, without it the
// RC could stay asleep forever.
ExpectationsTimeout = 2 * time.Minute
)
// ReplicationManager is responsible for synchronizing ReplicationController objects stored
// in the system with actual running pods.
2014-06-06 23:40:48 +00:00
type ReplicationManager struct {
kubeClient client.Interface
2014-06-09 05:38:45 +00:00
podControl PodControlInterface
// To allow injection of syncReplicationController for testing.
2015-04-21 20:40:35 +00:00
syncHandler func(rcKey string) error
// A TTLCache of pod creates/deletes each rc expects to see
expectations *RCExpectations
// A store of controllers, populated by the rcController
controllerStore cache.StoreToControllerLister
// A store of pods, populated by the podController
podStore cache.StoreToPodLister
// Watches changes to all replication controllers
rcController *framework.Controller
// Watches changes to all pods
podController *framework.Controller
// Controllers that need to be updated
queue *workqueue.Type
2014-06-06 23:40:48 +00:00
}
// NewReplicationManager creates a new ReplicationManager.
func NewReplicationManager(kubeClient client.Interface) *ReplicationManager {
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartRecordingToSink(kubeClient.Events(""))
rm := &ReplicationManager{
2014-06-06 23:40:48 +00:00
kubeClient: kubeClient,
2014-06-09 05:38:45 +00:00
podControl: RealPodControl{
2014-06-06 23:40:48 +00:00
kubeClient: kubeClient,
recorder: eventBroadcaster.NewRecorder(api.EventSource{Component: "replication-controller"}),
2014-06-06 23:40:48 +00:00
},
2015-04-21 20:40:35 +00:00
expectations: NewRCExpectations(),
queue: workqueue.New(),
2014-06-06 23:40:48 +00:00
}
2015-04-21 20:40:35 +00:00
rm.controllerStore.Store, rm.rcController = framework.NewInformer(
&cache.ListWatch{
ListFunc: func() (runtime.Object, error) {
return rm.kubeClient.ReplicationControllers(api.NamespaceAll).List(labels.Everything())
},
WatchFunc: func(rv string) (watch.Interface, error) {
return rm.kubeClient.ReplicationControllers(api.NamespaceAll).Watch(labels.Everything(), fields.Everything(), rv)
},
},
&api.ReplicationController{},
FullControllerResyncPeriod,
framework.ResourceEventHandlerFuncs{
AddFunc: rm.enqueueController,
UpdateFunc: func(old, cur interface{}) {
// We only really need to do this when spec changes, but for correctness it is safer to
// periodically double check. It is overkill for 2 reasons:
// 1. Status.Replica updates will cause a sync
// 2. Every 30s we will get a full resync (this will happen anyway every 5 minutes when pods relist)
// However, it shouldn't be that bad as rcs that haven't met expectations won't sync, and all
// the listing is done using local stores.
oldRC := old.(*api.ReplicationController)
curRC := cur.(*api.ReplicationController)
if oldRC.Status.Replicas != curRC.Status.Replicas {
glog.V(4).Infof("Observed updated replica count for rc: %v, %d->%d", curRC.Name, oldRC.Status.Replicas, curRC.Status.Replicas)
}
rm.enqueueController(cur)
},
// This will enter the sync loop and no-op, becuase the controller has been deleted from the store.
// Note that deleting a controller immediately after resizing it to 0 will not work. The recommended
// way of achieving this is by performing a `stop` operation on the controller.
DeleteFunc: rm.enqueueController,
},
)
rm.podStore.Store, rm.podController = framework.NewInformer(
&cache.ListWatch{
ListFunc: func() (runtime.Object, error) {
return rm.kubeClient.Pods(api.NamespaceAll).List(labels.Everything(), fields.Everything())
},
WatchFunc: func(rv string) (watch.Interface, error) {
return rm.kubeClient.Pods(api.NamespaceAll).Watch(labels.Everything(), fields.Everything(), rv)
},
},
&api.Pod{},
PodRelistPeriod,
framework.ResourceEventHandlerFuncs{
AddFunc: rm.addPod,
// This invokes the rc for every pod change, eg: host assignment. Though this might seem like overkill
// the most frequent pod update is status, and the associated rc will only list from local storage, so
// it should be ok.
UpdateFunc: rm.updatePod,
DeleteFunc: rm.deletePod,
},
)
rm.syncHandler = rm.syncReplicationController
return rm
2014-06-06 23:40:48 +00:00
}
2014-07-10 11:47:10 +00:00
// Run begins watching and syncing.
2015-04-21 20:40:35 +00:00
func (rm *ReplicationManager) Run(workers int, stopCh <-chan struct{}) {
defer util.HandleCrash()
go rm.rcController.Run(stopCh)
go rm.podController.Run(stopCh)
for i := 0; i < workers; i++ {
go util.Until(rm.worker, time.Second, stopCh)
}
<-stopCh
rm.queue.ShutDown()
2014-06-17 23:42:29 +00:00
}
2015-04-21 20:40:35 +00:00
// getPodControllers returns the controller managing the given pod.
// TODO: Surface that we are ignoring multiple controllers for a single pod.
func (rm *ReplicationManager) getPodControllers(pod *api.Pod) *api.ReplicationController {
controllers, err := rm.controllerStore.GetPodControllers(pod)
if err != nil {
2015-04-21 20:40:35 +00:00
glog.V(4).Infof("No controllers found for pod %v, replication manager will avoid syncing", pod.Name)
return nil
}
return &controllers[0]
}
// When a pod is created, enqueue the controller that manages it and update it's expectations.
func (rm *ReplicationManager) addPod(obj interface{}) {
pod := obj.(*api.Pod)
if rc := rm.getPodControllers(pod); rc != nil {
rm.expectations.CreationObserved(rc)
rm.enqueueController(rc)
}
2015-04-21 20:40:35 +00:00
}
2014-06-14 01:11:32 +00:00
2015-04-21 20:40:35 +00:00
// When a pod is updated, figure out what controller/s manage it and wake them
// up. If the labels of the pod have changed we need to awaken both the old
// and new controller. old and cur must be *api.Pod types.
func (rm *ReplicationManager) updatePod(old, cur interface{}) {
if api.Semantic.DeepEqual(old, cur) {
// A periodic relist will send update events for all known pods.
return
}
// TODO: Write a unittest for this case
curPod := cur.(*api.Pod)
if rc := rm.getPodControllers(curPod); rc != nil {
rm.enqueueController(rc)
}
oldPod := old.(*api.Pod)
// Only need to get the old controller if the labels changed.
if !reflect.DeepEqual(curPod.Labels, oldPod.Labels) {
// If the old and new rc are the same, the first one that syncs
// will set expectations preventing any damage from the second.
if oldRC := rm.getPodControllers(oldPod); oldRC != nil {
rm.enqueueController(oldRC)
}
2014-06-06 23:40:48 +00:00
}
}
2015-04-21 20:40:35 +00:00
// When a pod is deleted, enqueue the controller that manages the pod and update its expectations.
// obj could be an *api.Pod, or a DeletionFinalStateUnknown marker item.
func (rm *ReplicationManager) deletePod(obj interface{}) {
if pod, ok := obj.(*api.Pod); ok {
if rc := rm.getPodControllers(pod); rc != nil {
rm.expectations.DeletionObserved(rc)
rm.enqueueController(rc)
2014-06-06 23:40:48 +00:00
}
2015-04-21 20:40:35 +00:00
return
}
podKey, err := framework.DeletionHandlingMetaNamespaceKeyFunc(obj)
if err != nil {
glog.Errorf("Couldn't get key for object %+v: %v", obj, err)
return
2014-06-06 23:40:48 +00:00
}
2015-04-21 20:40:35 +00:00
// A periodic relist might not have a pod that the store has, in such cases we are sent a tombstone key.
// We don't know which controllers to sync, so just let the controller relist handle this.
glog.Infof("Pod %q was deleted but we don't have a record of its final state so it could take up to %v before a controller recreates a replica.", podKey, ExpectationsTimeout)
2014-06-06 23:40:48 +00:00
}
2015-04-21 20:40:35 +00:00
// obj could be an *api.ReplicationController, or a DeletionFinalStateUnknown marker item.
func (rm *ReplicationManager) enqueueController(obj interface{}) {
key, err := rcKeyFunc(obj)
if err != nil {
glog.Errorf("Couldn't get key for object %+v: %v", obj, err)
return
}
2015-04-21 20:40:35 +00:00
rm.queue.Add(key)
}
2015-04-21 20:40:35 +00:00
// worker runs a worker thread that just dequeues items, processes them, and marks them done.
// It enforces that the syncHandler is never invoked concurrently with the same key.
func (rm *ReplicationManager) worker() {
for {
func() {
key, quit := rm.queue.Get()
if quit {
return
}
defer rm.queue.Done(key)
err := rm.syncHandler(key.(string))
if err != nil {
glog.Errorf("Error syncing replication controller: %v", err)
}
}()
}
}
2015-04-21 20:40:35 +00:00
// manageReplicas checks and updates replicas for the given replication controller.
func (rm *ReplicationManager) manageReplicas(filteredPods []*api.Pod, controller *api.ReplicationController) {
diff := len(filteredPods) - controller.Spec.Replicas
2014-06-06 23:40:48 +00:00
if diff < 0 {
diff *= -1
2015-04-21 20:40:35 +00:00
rm.expectations.ExpectCreations(controller, diff)
wait := sync.WaitGroup{}
wait.Add(diff)
glog.V(2).Infof("Too few %q replicas, creating %d", controller.Name, diff)
2014-06-06 23:40:48 +00:00
for i := 0; i < diff; i++ {
go func() {
defer wait.Done()
2015-04-21 20:40:35 +00:00
if err := rm.podControl.createReplica(controller.Namespace, controller); err != nil {
// Decrement the expected number of creates because the informer won't observe this pod
rm.expectations.CreationObserved(controller)
util.HandleError(err)
}
}()
2014-06-06 23:40:48 +00:00
}
wait.Wait()
2014-06-06 23:40:48 +00:00
} else if diff > 0 {
2015-04-21 20:40:35 +00:00
rm.expectations.ExpectDeletions(controller, diff)
glog.V(2).Infof("Too many %q replicas, deleting %d", controller.Name, diff)
// Sort the pods in the order such that not-ready < ready, unscheduled
// < scheduled, and pending < running. This ensures that we delete pods
// in the earlier stages whenever possible.
2015-04-21 20:40:35 +00:00
sort.Sort(activePods(filteredPods))
wait := sync.WaitGroup{}
wait.Add(diff)
2014-06-06 23:40:48 +00:00
for i := 0; i < diff; i++ {
go func(ix int) {
defer wait.Done()
2015-04-21 20:40:35 +00:00
if err := rm.podControl.deletePod(controller.Namespace, filteredPods[ix].Name); err != nil {
// Decrement the expected number of deletes because the informer won't observe this deletion
rm.expectations.DeletionObserved(controller)
}
}(i)
2014-06-06 23:40:48 +00:00
}
wait.Wait()
2014-06-06 23:40:48 +00:00
}
}
2015-04-21 20:40:35 +00:00
// syncReplicationController will sync the rc with the given key if it has had its expectations fulfilled, meaning
// it did not expect to see any more of its pods created or deleted. This function is not meant to be invoked
// concurrently with the same key.
func (rm *ReplicationManager) syncReplicationController(key string) error {
startTime := time.Now()
defer func() {
glog.V(4).Infof("Finished syncing controller %q (%v)", key, time.Now().Sub(startTime))
}()
obj, exists, err := rm.controllerStore.Store.GetByKey(key)
if !exists {
glog.Infof("Replication Controller has been deleted %v", key)
return nil
}
2014-06-17 23:42:29 +00:00
if err != nil {
2015-04-21 20:40:35 +00:00
glog.Infof("Unable to retrieve rc %v from store: %v", key, err)
rm.queue.Add(key)
return err
2014-06-17 23:42:29 +00:00
}
2015-04-21 20:40:35 +00:00
controller := *obj.(*api.ReplicationController)
podList, err := rm.podStore.Pods(controller.Namespace).List(labels.Set(controller.Spec.Selector).AsSelector())
if err != nil {
glog.Errorf("Error getting pods for rc %q: %v", key, err)
rm.queue.Add(key)
return err
}
// TODO: Do this in a single pass, or use an index.
filteredPods := filterActivePods(podList.Items)
if rm.expectations.SatisfiedExpectations(&controller) {
rm.manageReplicas(filteredPods, &controller)
2014-06-06 23:40:48 +00:00
}
2015-04-21 20:40:35 +00:00
// Always updates status as pods come up or die
if err := updateReplicaCount(rm.kubeClient.ReplicationControllers(controller.Namespace), &controller, len(filteredPods)); err != nil {
glog.V(2).Infof("Failed to update replica count for controller %v, will try on next sync", controller.Name)
}
return nil
2014-06-06 23:40:48 +00:00
}