2014-06-06 23:40:48 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors.
|
2014-06-06 23:40:48 +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.
|
|
|
|
*/
|
2014-06-17 21:49:44 +00:00
|
|
|
|
2016-01-17 22:26:25 +00:00
|
|
|
// If you make changes to this file, you should also make the corresponding change in ReplicaSet.
|
|
|
|
|
2015-10-10 03:58:57 +00:00
|
|
|
package replication
|
2014-06-06 23:40:48 +00:00
|
|
|
|
|
|
|
import (
|
2017-02-06 18:35:50 +00:00
|
|
|
"fmt"
|
2015-04-21 20:40:35 +00:00
|
|
|
"reflect"
|
2015-04-17 00:37:57 +00:00
|
|
|
"sort"
|
2014-07-25 04:55:56 +00:00
|
|
|
"sync"
|
2014-06-06 23:40:48 +00:00
|
|
|
"time"
|
|
|
|
|
2015-08-05 22:05:17 +00:00
|
|
|
"github.com/golang/glog"
|
2017-06-22 18:24:23 +00:00
|
|
|
"k8s.io/api/core/v1"
|
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"
|
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
|
|
|
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
|
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
2017-01-24 15:15:11 +00:00
|
|
|
utiltrace "k8s.io/apiserver/pkg/util/trace"
|
2017-06-23 20:56:37 +00:00
|
|
|
coreinformers "k8s.io/client-go/informers/core/v1"
|
|
|
|
clientset "k8s.io/client-go/kubernetes"
|
2017-07-10 17:54:48 +00:00
|
|
|
"k8s.io/client-go/kubernetes/scheme"
|
2017-01-30 18:39:54 +00:00
|
|
|
v1core "k8s.io/client-go/kubernetes/typed/core/v1"
|
2017-06-23 20:56:37 +00:00
|
|
|
corelisters "k8s.io/client-go/listers/core/v1"
|
2017-01-24 14:11:51 +00:00
|
|
|
"k8s.io/client-go/tools/cache"
|
2017-01-30 18:39:54 +00:00
|
|
|
"k8s.io/client-go/tools/record"
|
2017-07-11 04:04:35 +00:00
|
|
|
"k8s.io/client-go/util/integer"
|
2017-01-27 15:20:40 +00:00
|
|
|
"k8s.io/client-go/util/workqueue"
|
2017-04-17 17:56:40 +00:00
|
|
|
podutil "k8s.io/kubernetes/pkg/api/v1/pod"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/controller"
|
2016-04-13 18:38:32 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/metrics"
|
2014-06-06 23:40:48 +00:00
|
|
|
)
|
|
|
|
|
2015-04-21 20:40:35 +00:00
|
|
|
const (
|
2015-05-06 21:39:14 +00:00
|
|
|
// Realistic value of the burstReplica field for the replication manager based off
|
|
|
|
// performance requirements for kubernetes 1.0.
|
|
|
|
BurstReplicas = 500
|
2015-06-19 20:35:19 +00:00
|
|
|
|
2015-07-28 01:21:37 +00:00
|
|
|
// The number of times we retry updating a replication controller's status.
|
|
|
|
statusUpdateRetries = 1
|
2015-04-21 20:40:35 +00:00
|
|
|
)
|
|
|
|
|
2017-02-23 16:58:28 +00:00
|
|
|
// controllerKind contains the schema.GroupVersionKind for this controller type.
|
|
|
|
var controllerKind = v1.SchemeGroupVersion.WithKind("ReplicationController")
|
2016-06-10 23:28:42 +00:00
|
|
|
|
2014-08-04 03:27:38 +00:00
|
|
|
// ReplicationManager is responsible for synchronizing ReplicationController objects stored
|
|
|
|
// in the system with actual running pods.
|
2017-04-30 11:33:08 +00:00
|
|
|
// NOTE: using this name to distinguish this type from API object "ReplicationController"; will
|
|
|
|
// not fix it right now. Refer to #41459 for more detail.
|
2014-06-06 23:40:48 +00:00
|
|
|
type ReplicationManager struct {
|
2016-01-15 05:00:58 +00:00
|
|
|
kubeClient clientset.Interface
|
2015-07-28 01:21:37 +00:00
|
|
|
podControl controller.PodControlInterface
|
2014-06-18 20:10:19 +00:00
|
|
|
|
2015-05-06 21:39:14 +00:00
|
|
|
// An rc is temporarily suspended after creating/deleting these many replicas.
|
|
|
|
// It resumes normal action after observing the watch events for them.
|
|
|
|
burstReplicas int
|
2014-06-18 20:10:19 +00:00
|
|
|
// To allow injection of syncReplicationController for testing.
|
2015-04-21 20:40:35 +00:00
|
|
|
syncHandler func(rcKey string) error
|
2015-06-19 20:35:19 +00:00
|
|
|
|
2016-03-05 00:51:01 +00:00
|
|
|
// A TTLCache of pod creates/deletes each rc expects to see.
|
|
|
|
expectations *controller.UIDTrackingControllerExpectations
|
2015-07-28 01:21:37 +00:00
|
|
|
|
2017-02-06 18:35:50 +00:00
|
|
|
rcLister corelisters.ReplicationControllerLister
|
|
|
|
rcListerSynced cache.InformerSynced
|
|
|
|
|
|
|
|
podLister corelisters.PodLister
|
2017-01-02 16:35:12 +00:00
|
|
|
// podListerSynced returns true if the pod store has been synced at least once.
|
2015-09-19 01:52:50 +00:00
|
|
|
// Added as a member to the struct to allow injection for testing.
|
2017-02-06 18:35:50 +00:00
|
|
|
podListerSynced cache.InformerSynced
|
2015-09-19 01:52:50 +00:00
|
|
|
|
|
|
|
// Controllers that need to be synced
|
2016-08-15 14:36:46 +00:00
|
|
|
queue workqueue.RateLimitingInterface
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2017-01-02 16:35:12 +00:00
|
|
|
// NewReplicationManager configures a replication manager with the specified event recorder
|
2017-02-23 16:58:28 +00:00
|
|
|
func NewReplicationManager(podInformer coreinformers.PodInformer, rcInformer coreinformers.ReplicationControllerInformer, kubeClient clientset.Interface, burstReplicas int) *ReplicationManager {
|
2017-10-25 15:54:32 +00:00
|
|
|
if kubeClient != nil && kubeClient.CoreV1().RESTClient().GetRateLimiter() != nil {
|
|
|
|
metrics.RegisterMetricAndTrackRateLimiterUsage("replication_controller", kubeClient.CoreV1().RESTClient().GetRateLimiter())
|
2016-04-13 18:38:32 +00:00
|
|
|
}
|
|
|
|
|
2017-01-02 16:35:12 +00:00
|
|
|
eventBroadcaster := record.NewBroadcaster()
|
|
|
|
eventBroadcaster.StartLogging(glog.Infof)
|
2017-10-25 15:54:32 +00:00
|
|
|
eventBroadcaster.StartRecordingToSink(&v1core.EventSinkImpl{Interface: v1core.New(kubeClient.CoreV1().RESTClient()).Events("")})
|
2017-01-02 16:35:12 +00:00
|
|
|
|
2014-06-18 20:10:19 +00:00
|
|
|
rm := &ReplicationManager{
|
2014-06-06 23:40:48 +00:00
|
|
|
kubeClient: kubeClient,
|
2015-07-28 01:21:37 +00:00
|
|
|
podControl: controller.RealPodControl{
|
|
|
|
KubeClient: kubeClient,
|
2017-07-15 05:25:54 +00:00
|
|
|
Recorder: eventBroadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: "replication-controller"}),
|
2014-06-06 23:40:48 +00:00
|
|
|
},
|
2015-05-06 21:39:14 +00:00
|
|
|
burstReplicas: burstReplicas,
|
2016-03-05 00:51:01 +00:00
|
|
|
expectations: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectations()),
|
2016-08-22 18:15:45 +00:00
|
|
|
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "replicationmanager"),
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2015-04-14 19:42:49 +00:00
|
|
|
|
2017-02-06 18:35:50 +00:00
|
|
|
rcInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
|
2017-01-02 16:35:12 +00:00
|
|
|
AddFunc: rm.enqueueController,
|
|
|
|
UpdateFunc: rm.updateRC,
|
|
|
|
// This will enter the sync loop and no-op, because the controller has been deleted from the store.
|
|
|
|
// Note that deleting a controller immediately after scaling it to 0 will not work. The recommended
|
|
|
|
// way of achieving this is by performing a `stop` operation on the controller.
|
|
|
|
DeleteFunc: rm.enqueueController,
|
|
|
|
})
|
2017-02-06 18:35:50 +00:00
|
|
|
rm.rcLister = rcInformer.Lister()
|
|
|
|
rm.rcListerSynced = rcInformer.Informer().HasSynced
|
|
|
|
|
|
|
|
podInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
|
2016-04-14 18:00:52 +00:00
|
|
|
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,
|
|
|
|
})
|
2017-02-06 18:35:50 +00:00
|
|
|
rm.podLister = podInformer.Lister()
|
|
|
|
rm.podListerSynced = podInformer.Informer().HasSynced
|
2015-04-21 20:40:35 +00:00
|
|
|
|
2014-07-20 19:00:52 +00:00
|
|
|
rm.syncHandler = rm.syncReplicationController
|
2014-06-18 20:10:19 +00:00
|
|
|
return rm
|
2016-05-10 19:58:49 +00:00
|
|
|
}
|
2016-04-14 18:00:52 +00:00
|
|
|
|
2015-04-09 21:50:27 +00:00
|
|
|
// SetEventRecorder replaces the event recorder used by the replication manager
|
|
|
|
// with the given recorder. Only used for testing.
|
|
|
|
func (rm *ReplicationManager) SetEventRecorder(recorder record.EventRecorder) {
|
|
|
|
// TODO: Hack. We can't cleanly shutdown the event recorder, so benchmarks
|
|
|
|
// need to pass in a fake.
|
2015-08-08 01:52:23 +00:00
|
|
|
rm.podControl = controller.RealPodControl{KubeClient: rm.kubeClient, Recorder: recorder}
|
2015-04-09 21:50:27 +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{}) {
|
2016-01-15 07:32:10 +00:00
|
|
|
defer utilruntime.HandleCrash()
|
2017-01-02 16:35:12 +00:00
|
|
|
defer rm.queue.ShutDown()
|
2016-04-14 18:00:52 +00:00
|
|
|
|
2017-04-12 19:49:17 +00:00
|
|
|
glog.Infof("Starting RC controller")
|
|
|
|
defer glog.Infof("Shutting down RC controller")
|
2016-04-14 18:00:52 +00:00
|
|
|
|
2017-04-12 19:49:17 +00:00
|
|
|
if !controller.WaitForCacheSync("RC", stopCh, rm.podListerSynced, rm.rcListerSynced) {
|
2017-01-02 16:35:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < workers; i++ {
|
|
|
|
go wait.Until(rm.worker, time.Second, stopCh)
|
|
|
|
}
|
|
|
|
|
2015-04-21 20:40:35 +00:00
|
|
|
<-stopCh
|
2014-06-17 23:42:29 +00:00
|
|
|
}
|
|
|
|
|
2017-02-23 16:58:28 +00:00
|
|
|
// getPodControllers returns a list of ReplicationControllers matching the given pod.
|
|
|
|
func (rm *ReplicationManager) getPodControllers(pod *v1.Pod) []*v1.ReplicationController {
|
|
|
|
rcs, err := rm.rcLister.GetPodControllers(pod)
|
2014-07-20 19:00:52 +00:00
|
|
|
if err != nil {
|
2015-04-21 20:40:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-02-23 16:58:28 +00:00
|
|
|
if len(rcs) > 1 {
|
|
|
|
// ControllerRef will ensure we don't do anything crazy, but more than one
|
|
|
|
// item in this list nevertheless constitutes user error.
|
|
|
|
utilruntime.HandleError(fmt.Errorf("user error! more than one ReplicationController is selecting pods with labels: %+v", pod.Labels))
|
2015-10-06 01:01:05 +00:00
|
|
|
}
|
2017-02-23 16:58:28 +00:00
|
|
|
return rcs
|
2016-02-23 15:17:27 +00:00
|
|
|
}
|
|
|
|
|
2017-03-07 18:40:20 +00:00
|
|
|
// resolveControllerRef returns the controller referenced by a ControllerRef,
|
|
|
|
// or nil if the ControllerRef could not be resolved to a matching controller
|
2017-07-21 06:09:18 +00:00
|
|
|
// of the correct Kind.
|
2017-03-07 18:40:20 +00:00
|
|
|
func (rm *ReplicationManager) resolveControllerRef(namespace string, controllerRef *metav1.OwnerReference) *v1.ReplicationController {
|
|
|
|
// We can't look up by UID, so look up by Name and then verify UID.
|
|
|
|
// Don't even try to look up by Name if it's the wrong Kind.
|
|
|
|
if controllerRef.Kind != controllerKind.Kind {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
rc, err := rm.rcLister.ReplicationControllers(namespace).Get(controllerRef.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if rc.UID != controllerRef.UID {
|
|
|
|
// The controller we found with this Name is not the same one that the
|
|
|
|
// ControllerRef points to.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return rc
|
|
|
|
}
|
|
|
|
|
2016-06-10 23:28:42 +00:00
|
|
|
// callback when RC is updated
|
|
|
|
func (rm *ReplicationManager) updateRC(old, cur interface{}) {
|
2016-11-18 20:50:17 +00:00
|
|
|
oldRC := old.(*v1.ReplicationController)
|
|
|
|
curRC := cur.(*v1.ReplicationController)
|
2016-06-10 23:28:42 +00:00
|
|
|
|
|
|
|
// You might imagine that we only really need to enqueue the
|
|
|
|
// controller when Spec changes, but it is safer to sync any
|
|
|
|
// time this function is triggered. That way a full informer
|
|
|
|
// resync can requeue any controllers that don't yet have pods
|
|
|
|
// but whose last attempts at creating a pod have failed (since
|
|
|
|
// we don't block on creation of pods) instead of those
|
|
|
|
// controllers stalling indefinitely. Enqueueing every time
|
|
|
|
// does result in some spurious syncs (like when Status.Replica
|
|
|
|
// is updated and the watch notification from it retriggers
|
|
|
|
// this function), but in general extra resyncs shouldn't be
|
|
|
|
// that bad as rcs that haven't met expectations yet won't
|
|
|
|
// sync, and all the listing is done using local stores.
|
2017-04-21 11:42:50 +00:00
|
|
|
if *(oldRC.Spec.Replicas) != *(curRC.Spec.Replicas) {
|
2017-04-12 13:52:58 +00:00
|
|
|
glog.V(4).Infof("Replication controller %v updated. Desired pod count change: %d->%d", curRC.Name, *(oldRC.Spec.Replicas), *(curRC.Spec.Replicas))
|
2016-06-10 23:28:42 +00:00
|
|
|
}
|
|
|
|
rm.enqueueController(cur)
|
|
|
|
}
|
|
|
|
|
2017-02-23 16:58:28 +00:00
|
|
|
// When a pod is created, enqueue the ReplicationController that manages it and update its expectations.
|
2015-04-21 20:40:35 +00:00
|
|
|
func (rm *ReplicationManager) addPod(obj interface{}) {
|
2016-11-18 20:50:17 +00:00
|
|
|
pod := obj.(*v1.Pod)
|
2016-02-28 08:23:47 +00:00
|
|
|
|
2015-08-20 01:52:34 +00:00
|
|
|
if pod.DeletionTimestamp != nil {
|
|
|
|
// on a restart of the controller manager, it's possible a new pod shows up in a state that
|
|
|
|
// is already pending deletion. Prevent the pod from being a creation observation.
|
2016-03-05 00:51:01 +00:00
|
|
|
rm.deletePod(pod)
|
|
|
|
return
|
2014-07-20 19:00:52 +00:00
|
|
|
}
|
2017-02-23 16:58:28 +00:00
|
|
|
|
|
|
|
// If it has a ControllerRef, that's all that matters.
|
2017-08-02 09:41:33 +00:00
|
|
|
if controllerRef := metav1.GetControllerOf(pod); controllerRef != nil {
|
2017-03-07 18:40:20 +00:00
|
|
|
rc := rm.resolveControllerRef(pod.Namespace, controllerRef)
|
|
|
|
if rc == nil {
|
2017-02-23 16:58:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
rsKey, err := controller.KeyFunc(rc)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-03-07 18:40:20 +00:00
|
|
|
glog.V(4).Infof("Pod %s created: %#v.", pod.Name, pod)
|
2017-02-23 16:58:28 +00:00
|
|
|
rm.expectations.CreationObserved(rsKey)
|
|
|
|
rm.enqueueController(rc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, it's an orphan. Get a list of all matching ReplicationControllers and sync
|
|
|
|
// them to see if anyone wants to adopt it.
|
|
|
|
// DO NOT observe creation because no controller should be waiting for an
|
|
|
|
// orphan.
|
2017-03-01 22:59:25 +00:00
|
|
|
rcs := rm.getPodControllers(pod)
|
|
|
|
if len(rcs) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
glog.V(4).Infof("Orphan Pod %s created: %#v.", pod.Name, pod)
|
|
|
|
for _, rc := range rcs {
|
2017-02-23 16:58:28 +00:00
|
|
|
rm.enqueueController(rc)
|
|
|
|
}
|
2015-04-21 20:40:35 +00:00
|
|
|
}
|
2014-06-14 01:11:32 +00:00
|
|
|
|
2017-02-23 16:58:28 +00:00
|
|
|
// When a pod is updated, figure out what ReplicationController/s manage it and wake them
|
2015-04-21 20:40:35 +00:00
|
|
|
// up. If the labels of the pod have changed we need to awaken both the old
|
2017-02-23 16:58:28 +00:00
|
|
|
// and new ReplicationController. old and cur must be *v1.Pod types.
|
2015-04-21 20:40:35 +00:00
|
|
|
func (rm *ReplicationManager) updatePod(old, cur interface{}) {
|
2016-11-18 20:50:17 +00:00
|
|
|
curPod := cur.(*v1.Pod)
|
|
|
|
oldPod := old.(*v1.Pod)
|
2016-08-09 13:57:21 +00:00
|
|
|
if curPod.ResourceVersion == oldPod.ResourceVersion {
|
|
|
|
// Periodic resync will send update events for all known pods.
|
|
|
|
// Two different versions of the same pod will always have different RVs.
|
|
|
|
return
|
|
|
|
}
|
2017-02-23 16:58:28 +00:00
|
|
|
|
2016-06-15 04:26:47 +00:00
|
|
|
labelChanged := !reflect.DeepEqual(curPod.Labels, oldPod.Labels)
|
2016-03-05 00:51:01 +00:00
|
|
|
if curPod.DeletionTimestamp != nil {
|
2015-08-20 01:52:34 +00:00
|
|
|
// when a pod is deleted gracefully it's deletion timestamp is first modified to reflect a grace period,
|
|
|
|
// and after such time has passed, the kubelet actually deletes it from the store. We receive an update
|
|
|
|
// for modification of the deletion timestamp and expect an rc to create more replicas asap, not wait
|
|
|
|
// until the kubelet actually deletes the pod. This is different from the Phase of a pod changing, because
|
|
|
|
// an rc never initiates a phase change, and so is never asleep waiting for the same.
|
2016-03-05 00:51:01 +00:00
|
|
|
rm.deletePod(curPod)
|
2016-06-15 04:26:47 +00:00
|
|
|
if labelChanged {
|
|
|
|
// we don't need to check the oldPod.DeletionTimestamp because DeletionTimestamp cannot be unset.
|
|
|
|
rm.deletePod(oldPod)
|
|
|
|
}
|
2016-03-05 00:51:01 +00:00
|
|
|
return
|
2015-04-21 20:40:35 +00:00
|
|
|
}
|
2016-06-15 04:26:47 +00:00
|
|
|
|
2017-08-02 09:41:33 +00:00
|
|
|
curControllerRef := metav1.GetControllerOf(curPod)
|
|
|
|
oldControllerRef := metav1.GetControllerOf(oldPod)
|
2017-02-23 16:58:28 +00:00
|
|
|
controllerRefChanged := !reflect.DeepEqual(curControllerRef, oldControllerRef)
|
2017-03-07 18:40:20 +00:00
|
|
|
if controllerRefChanged && oldControllerRef != nil {
|
2017-02-23 16:58:28 +00:00
|
|
|
// The ControllerRef was changed. Sync the old controller, if any.
|
2017-03-07 18:40:20 +00:00
|
|
|
if rc := rm.resolveControllerRef(oldPod.Namespace, oldControllerRef); rc != nil {
|
2017-02-23 16:58:28 +00:00
|
|
|
rm.enqueueController(rc)
|
2014-07-12 06:29:51 +00:00
|
|
|
}
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2016-06-10 23:28:42 +00:00
|
|
|
|
2017-02-23 16:58:28 +00:00
|
|
|
// If it has a ControllerRef, that's all that matters.
|
|
|
|
if curControllerRef != nil {
|
2017-03-07 18:40:20 +00:00
|
|
|
rc := rm.resolveControllerRef(curPod.Namespace, curControllerRef)
|
|
|
|
if rc == nil {
|
2017-02-23 16:58:28 +00:00
|
|
|
return
|
|
|
|
}
|
2017-03-01 22:59:25 +00:00
|
|
|
glog.V(4).Infof("Pod %s updated, objectMeta %+v -> %+v.", curPod.Name, oldPod.ObjectMeta, curPod.ObjectMeta)
|
2017-02-23 16:58:28 +00:00
|
|
|
rm.enqueueController(rc)
|
2016-12-05 01:14:46 +00:00
|
|
|
// TODO: MinReadySeconds in the Pod will generate an Available condition to be added in
|
2017-02-23 16:58:28 +00:00
|
|
|
// the Pod status which in turn will trigger a requeue of the owning ReplicationController thus
|
|
|
|
// having its status updated with the newly available replica. For now, we can fake the
|
|
|
|
// update by resyncing the controller MinReadySeconds after the it is requeued because
|
|
|
|
// a Pod transitioned to Ready.
|
2016-12-05 01:14:46 +00:00
|
|
|
// Note that this still suffers from #29229, we are just moving the problem one level
|
2017-02-23 16:58:28 +00:00
|
|
|
// "closer" to kubelet (from the deployment to the ReplicationController controller).
|
2017-04-17 17:56:40 +00:00
|
|
|
if !podutil.IsPodReady(oldPod) && podutil.IsPodReady(curPod) && rc.Spec.MinReadySeconds > 0 {
|
2017-02-23 16:58:28 +00:00
|
|
|
glog.V(2).Infof("ReplicationController %q will be enqueued after %ds for availability check", rc.Name, rc.Spec.MinReadySeconds)
|
2017-03-19 03:44:43 +00:00
|
|
|
// Add a second to avoid milliseconds skew in AddAfter.
|
|
|
|
// See https://github.com/kubernetes/kubernetes/issues/39785#issuecomment-279959133 for more info.
|
|
|
|
rm.enqueueControllerAfter(rc, (time.Duration(rc.Spec.MinReadySeconds)*time.Second)+time.Second)
|
2017-02-23 16:58:28 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, it's an orphan. If anything changed, sync matching controllers
|
|
|
|
// to see if anyone wants to adopt it now.
|
|
|
|
if labelChanged || controllerRefChanged {
|
2017-03-01 22:59:25 +00:00
|
|
|
rcs := rm.getPodControllers(curPod)
|
|
|
|
if len(rcs) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
glog.V(4).Infof("Orphan Pod %s updated, objectMeta %+v -> %+v.", curPod.Name, oldPod.ObjectMeta, curPod.ObjectMeta)
|
|
|
|
for _, rc := range rcs {
|
2017-02-23 16:58:28 +00:00
|
|
|
rm.enqueueController(rc)
|
2016-12-05 01:14:46 +00:00
|
|
|
}
|
2016-06-10 23:28:42 +00:00
|
|
|
}
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2017-02-23 16:58:28 +00:00
|
|
|
// When a pod is deleted, enqueue the ReplicationController that manages the pod and update its expectations.
|
2016-11-18 20:50:17 +00:00
|
|
|
// obj could be an *v1.Pod, or a DeletionFinalStateUnknown marker item.
|
2015-04-21 20:40:35 +00:00
|
|
|
func (rm *ReplicationManager) deletePod(obj interface{}) {
|
2016-11-18 20:50:17 +00:00
|
|
|
pod, ok := obj.(*v1.Pod)
|
2015-05-29 16:24:39 +00:00
|
|
|
|
|
|
|
// 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
|
2017-02-23 16:58:28 +00:00
|
|
|
// changed labels the new ReplicationController will not be woken up till the periodic resync.
|
2015-05-29 16:24:39 +00:00
|
|
|
if !ok {
|
|
|
|
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
|
|
|
|
if !ok {
|
2017-02-23 16:58:28 +00:00
|
|
|
utilruntime.HandleError(fmt.Errorf("couldn't get object from tombstone %+v", obj))
|
2015-05-29 16:24:39 +00:00
|
|
|
return
|
|
|
|
}
|
2016-11-18 20:50:17 +00:00
|
|
|
pod, ok = tombstone.Obj.(*v1.Pod)
|
2015-05-29 16:24:39 +00:00
|
|
|
if !ok {
|
2017-02-23 16:58:28 +00:00
|
|
|
utilruntime.HandleError(fmt.Errorf("tombstone contained object that is not a pod %#v", obj))
|
2015-05-29 16:24:39 +00:00
|
|
|
return
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2015-04-21 20:40:35 +00:00
|
|
|
}
|
2017-02-23 16:58:28 +00:00
|
|
|
|
2017-08-02 09:41:33 +00:00
|
|
|
controllerRef := metav1.GetControllerOf(pod)
|
2017-02-23 16:58:28 +00:00
|
|
|
if controllerRef == nil {
|
|
|
|
// No controller should care about orphans being deleted.
|
|
|
|
return
|
|
|
|
}
|
2017-03-07 18:40:20 +00:00
|
|
|
rc := rm.resolveControllerRef(pod.Namespace, controllerRef)
|
|
|
|
if rc == nil {
|
2017-02-23 16:58:28 +00:00
|
|
|
return
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2017-02-23 16:58:28 +00:00
|
|
|
rsKey, err := controller.KeyFunc(rc)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-03-07 18:40:20 +00:00
|
|
|
glog.V(4).Infof("Pod %s/%s deleted through %v, timestamp %+v: %#v.", pod.Namespace, pod.Name, utilruntime.GetCaller(), pod.DeletionTimestamp, pod)
|
2017-02-23 16:58:28 +00:00
|
|
|
rm.expectations.DeletionObserved(rsKey, controller.PodKey(pod))
|
|
|
|
rm.enqueueController(rc)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 20:50:17 +00:00
|
|
|
// obj could be an *v1.ReplicationController, or a DeletionFinalStateUnknown marker item.
|
2015-04-21 20:40:35 +00:00
|
|
|
func (rm *ReplicationManager) enqueueController(obj interface{}) {
|
2015-07-28 01:21:37 +00:00
|
|
|
key, err := controller.KeyFunc(obj)
|
2015-04-21 20:40:35 +00:00
|
|
|
if err != nil {
|
2017-02-23 16:58:28 +00:00
|
|
|
utilruntime.HandleError(fmt.Errorf("couldn't get key for object %+v: %v", obj, err))
|
2015-04-21 20:40:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
rm.queue.Add(key)
|
|
|
|
}
|
2015-04-17 00:37:57 +00:00
|
|
|
|
2016-12-05 01:14:46 +00:00
|
|
|
// obj could be an *v1.ReplicationController, or a DeletionFinalStateUnknown marker item.
|
|
|
|
func (rm *ReplicationManager) enqueueControllerAfter(obj interface{}, after time.Duration) {
|
|
|
|
key, err := controller.KeyFunc(obj)
|
|
|
|
if err != nil {
|
2017-02-23 16:58:28 +00:00
|
|
|
utilruntime.HandleError(fmt.Errorf("couldn't get key for object %+v: %v", obj, err))
|
2016-12-05 01:14:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
rm.queue.AddAfter(key, after)
|
|
|
|
}
|
|
|
|
|
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() {
|
2017-02-25 21:09:15 +00:00
|
|
|
for rm.processNextWorkItem() {
|
|
|
|
}
|
|
|
|
glog.Infof("replication controller worker shutting down")
|
|
|
|
}
|
2016-08-15 14:36:46 +00:00
|
|
|
|
2017-02-25 21:09:15 +00:00
|
|
|
func (rm *ReplicationManager) processNextWorkItem() bool {
|
|
|
|
key, quit := rm.queue.Get()
|
|
|
|
if quit {
|
2016-05-10 19:58:49 +00:00
|
|
|
return false
|
|
|
|
}
|
2017-02-25 21:09:15 +00:00
|
|
|
defer rm.queue.Done(key)
|
|
|
|
|
|
|
|
err := rm.syncHandler(key.(string))
|
|
|
|
if err == nil {
|
|
|
|
rm.queue.Forget(key)
|
|
|
|
return true
|
2015-04-17 00:37:57 +00:00
|
|
|
}
|
2017-02-25 21:09:15 +00:00
|
|
|
|
|
|
|
rm.queue.AddRateLimited(key)
|
|
|
|
utilruntime.HandleError(err)
|
|
|
|
return true
|
2015-04-17 00:37:57 +00:00
|
|
|
}
|
|
|
|
|
2015-04-21 20:40:35 +00:00
|
|
|
// manageReplicas checks and updates replicas for the given replication controller.
|
2016-08-17 14:16:01 +00:00
|
|
|
// Does NOT modify <filteredPods>.
|
2016-11-18 20:50:17 +00:00
|
|
|
func (rm *ReplicationManager) manageReplicas(filteredPods []*v1.Pod, rc *v1.ReplicationController) error {
|
|
|
|
diff := len(filteredPods) - int(*(rc.Spec.Replicas))
|
2015-07-28 01:21:37 +00:00
|
|
|
rcKey, err := controller.KeyFunc(rc)
|
|
|
|
if err != nil {
|
2016-08-15 14:36:46 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if diff == 0 {
|
|
|
|
return nil
|
2015-07-28 01:21:37 +00:00
|
|
|
}
|
2016-08-15 14:36:46 +00:00
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
if diff < 0 {
|
|
|
|
diff *= -1
|
2015-05-06 21:39:14 +00:00
|
|
|
if diff > rm.burstReplicas {
|
|
|
|
diff = rm.burstReplicas
|
|
|
|
}
|
2016-03-05 00:51:01 +00:00
|
|
|
// TODO: Track UIDs of creates just like deletes. The problem currently
|
|
|
|
// is we'd need to wait on the result of a create to record the pod's
|
|
|
|
// UID, which would require locking *across* the create, which will turn
|
|
|
|
// into a performance bottleneck. We should generate a UID for the pod
|
|
|
|
// beforehand and store it via ExpectCreations.
|
2016-08-15 14:36:46 +00:00
|
|
|
errCh := make(chan error, diff)
|
2015-07-28 01:21:37 +00:00
|
|
|
rm.expectations.ExpectCreations(rcKey, diff)
|
2016-06-17 19:16:15 +00:00
|
|
|
var wg sync.WaitGroup
|
2016-11-18 20:50:17 +00:00
|
|
|
glog.V(2).Infof("Too few %q/%q replicas, need %d, creating %d", rc.Namespace, rc.Name, *(rc.Spec.Replicas), diff)
|
2017-07-11 04:04:35 +00:00
|
|
|
// Batch the pod creates. Batch sizes start at SlowStartInitialBatchSize
|
|
|
|
// and double with each successful iteration in a kind of "slow start".
|
|
|
|
// This handles attempts to start large numbers of pods that would
|
|
|
|
// likely all fail with the same error. For example a project with a
|
|
|
|
// low quota that attempts to create a large number of pods will be
|
|
|
|
// prevented from spamming the API service with the pod create requests
|
|
|
|
// after one of its pods fails. Conveniently, this also prevents the
|
|
|
|
// event spam that those failures would generate.
|
|
|
|
for batchSize := integer.IntMin(diff, controller.SlowStartInitialBatchSize); diff > 0; batchSize = integer.IntMin(2*batchSize, diff) {
|
|
|
|
errorCount := len(errCh)
|
|
|
|
wg.Add(batchSize)
|
|
|
|
for i := 0; i < batchSize; i++ {
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
var err error
|
|
|
|
boolPtr := func(b bool) *bool { return &b }
|
|
|
|
controllerRef := &metav1.OwnerReference{
|
|
|
|
APIVersion: controllerKind.GroupVersion().String(),
|
|
|
|
Kind: controllerKind.Kind,
|
|
|
|
Name: rc.Name,
|
|
|
|
UID: rc.UID,
|
|
|
|
BlockOwnerDeletion: boolPtr(true),
|
|
|
|
Controller: boolPtr(true),
|
|
|
|
}
|
|
|
|
err = rm.podControl.CreatePodsWithControllerRef(rc.Namespace, rc.Spec.Template, rc, controllerRef)
|
|
|
|
if err != nil && errors.IsTimeout(err) {
|
|
|
|
// Pod is created but its initialization has timed out.
|
|
|
|
// If the initialization is successful eventually, the
|
|
|
|
// controller will observe the creation via the informer.
|
|
|
|
// If the initialization fails, or if the pod keeps
|
|
|
|
// uninitialized for a long time, the informer will not
|
|
|
|
// receive any update, and the controller will create a new
|
|
|
|
// pod when the expectation expires.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
// Decrement the expected number of creates because the informer won't observe this pod
|
|
|
|
glog.V(2).Infof("Failed creation, decrementing expectations for controller %q/%q", rc.Namespace, rc.Name)
|
|
|
|
rm.expectations.CreationObserved(rcKey)
|
|
|
|
errCh <- err
|
|
|
|
utilruntime.HandleError(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
// any skipped pods that we never attempted to start shouldn't be expected.
|
|
|
|
skippedPods := diff - batchSize
|
|
|
|
if errorCount < len(errCh) && skippedPods > 0 {
|
|
|
|
glog.V(2).Infof("Slow-start failure. Skipping creation of %d pods, decrementing expectations for controller %q/%q", skippedPods, rc.Namespace, rc.Name)
|
|
|
|
for i := 0; i < skippedPods; i++ {
|
2015-04-21 20:40:35 +00:00
|
|
|
// Decrement the expected number of creates because the informer won't observe this pod
|
2015-07-28 01:21:37 +00:00
|
|
|
rm.expectations.CreationObserved(rcKey)
|
2015-04-21 20:40:35 +00:00
|
|
|
}
|
2017-07-11 04:04:35 +00:00
|
|
|
// The skipped pods will be retried later. The next controller resync will
|
|
|
|
// retry the slow start process.
|
|
|
|
break
|
|
|
|
}
|
|
|
|
diff -= batchSize
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2016-08-15 14:36:46 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
|
|
|
// all errors have been reported before and they're likely to be the same, so we'll only return the first one we hit.
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
default:
|
2016-03-05 00:51:01 +00:00
|
|
|
}
|
2016-08-15 14:36:46 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff > rm.burstReplicas {
|
|
|
|
diff = rm.burstReplicas
|
|
|
|
}
|
2016-11-18 20:50:17 +00:00
|
|
|
glog.V(2).Infof("Too many %q/%q replicas, need %d, deleting %d", rc.Namespace, rc.Name, *(rc.Spec.Replicas), diff)
|
2016-08-15 14:36:46 +00:00
|
|
|
// No need to sort pods if we are about to delete all of them
|
2016-11-18 20:50:17 +00:00
|
|
|
if *(rc.Spec.Replicas) != 0 {
|
2016-08-15 14:36:46 +00:00
|
|
|
// 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.
|
|
|
|
sort.Sort(controller.ActivePods(filteredPods))
|
|
|
|
}
|
|
|
|
// Snapshot the UIDs (ns/name) of the pods we're expecting to see
|
|
|
|
// deleted, so we know to record their expectations exactly once either
|
|
|
|
// when we see it as an update of the deletion timestamp, or as a delete.
|
|
|
|
// Note that if the labels on a pod/rc change in a way that the pod gets
|
|
|
|
// orphaned, the rs will only wake up after the expectations have
|
|
|
|
// expired even if other pods are deleted.
|
|
|
|
deletedPodKeys := []string{}
|
|
|
|
for i := 0; i < diff; i++ {
|
|
|
|
deletedPodKeys = append(deletedPodKeys, controller.PodKey(filteredPods[i]))
|
|
|
|
}
|
|
|
|
// We use pod namespace/name as a UID to wait for deletions, so if the
|
|
|
|
// labels on a pod/rc change in a way that the pod gets orphaned, the
|
|
|
|
// rc will only wake up after the expectation has expired.
|
|
|
|
errCh := make(chan error, diff)
|
|
|
|
rm.expectations.ExpectDeletions(rcKey, deletedPodKeys)
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(diff)
|
|
|
|
for i := 0; i < diff; i++ {
|
|
|
|
go func(ix int) {
|
|
|
|
defer wg.Done()
|
|
|
|
if err := rm.podControl.DeletePod(rc.Namespace, filteredPods[ix].Name, rc); err != nil {
|
|
|
|
// Decrement the expected number of deletes because the informer won't observe this deletion
|
|
|
|
podKey := controller.PodKey(filteredPods[ix])
|
|
|
|
glog.V(2).Infof("Failed to delete %v due to %v, decrementing expectations for controller %q/%q", podKey, err, rc.Namespace, rc.Name)
|
|
|
|
rm.expectations.DeletionObserved(rcKey, podKey)
|
|
|
|
errCh <- err
|
|
|
|
utilruntime.HandleError(err)
|
|
|
|
}
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
|
|
|
// all errors have been reported before and they're likely to be the same, so we'll only return the first one we hit.
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2016-08-15 14:36:46 +00:00
|
|
|
default:
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2016-08-15 14:36:46 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
|
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 {
|
2017-01-24 15:15:11 +00:00
|
|
|
trace := utiltrace.New("syncReplicationController: " + key)
|
2016-05-13 08:30:45 +00:00
|
|
|
defer trace.LogIfLong(250 * time.Millisecond)
|
|
|
|
|
2015-04-21 20:40:35 +00:00
|
|
|
startTime := time.Now()
|
|
|
|
defer func() {
|
|
|
|
glog.V(4).Infof("Finished syncing controller %q (%v)", key, time.Now().Sub(startTime))
|
|
|
|
}()
|
|
|
|
|
2017-02-06 18:35:50 +00:00
|
|
|
namespace, name, err := cache.SplitMetaNamespaceKey(key)
|
2017-01-02 16:35:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-02-13 03:47:33 +00:00
|
|
|
}
|
2017-02-06 18:35:50 +00:00
|
|
|
rc, err := rm.rcLister.ReplicationControllers(namespace).Get(name)
|
|
|
|
if errors.IsNotFound(err) {
|
2015-04-21 20:40:35 +00:00
|
|
|
glog.Infof("Replication Controller has been deleted %v", key)
|
2015-05-08 21:16:58 +00:00
|
|
|
rm.expectations.DeleteExpectations(key)
|
2015-04-21 20:40:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-02-06 18:35:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-21 20:40:35 +00:00
|
|
|
|
2016-05-13 08:30:45 +00:00
|
|
|
trace.Step("ReplicationController restored")
|
2016-12-12 13:42:18 +00:00
|
|
|
rcNeedsSync := rm.expectations.SatisfiedExpectations(key)
|
2016-05-13 08:30:45 +00:00
|
|
|
trace.Step("Expectations restored")
|
2015-05-12 21:39:23 +00:00
|
|
|
|
2017-02-10 00:02:23 +00:00
|
|
|
// list all pods to include the pods that don't match the rc's selector
|
|
|
|
// anymore but has the stale controller ref.
|
2017-02-25 20:39:49 +00:00
|
|
|
// TODO: Do the List and Filter in a single pass, or use an index.
|
2017-03-06 22:14:40 +00:00
|
|
|
allPods, err := rm.podLister.Pods(rc.Namespace).List(labels.Everything())
|
2017-02-10 00:02:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-25 20:39:49 +00:00
|
|
|
// Ignore inactive pods.
|
|
|
|
var filteredPods []*v1.Pod
|
2017-03-06 22:14:40 +00:00
|
|
|
for _, pod := range allPods {
|
2017-02-25 20:39:49 +00:00
|
|
|
if controller.IsPodActive(pod) {
|
|
|
|
filteredPods = append(filteredPods, pod)
|
|
|
|
}
|
|
|
|
}
|
2017-03-11 01:13:51 +00:00
|
|
|
// If any adoptions are attempted, we should first recheck for deletion with
|
|
|
|
// an uncached quorum read sometime after listing Pods (see #42639).
|
|
|
|
canAdoptFunc := controller.RecheckDeletionTimestamp(func() (metav1.Object, error) {
|
|
|
|
fresh, err := rm.kubeClient.CoreV1().ReplicationControllers(rc.Namespace).Get(rc.Name, metav1.GetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if fresh.UID != rc.UID {
|
|
|
|
return nil, fmt.Errorf("original ReplicationController %v/%v is gone: got uid %v, wanted %v", rc.Namespace, rc.Name, fresh.UID, rc.UID)
|
|
|
|
}
|
|
|
|
return fresh, nil
|
|
|
|
})
|
|
|
|
cm := controller.NewPodControllerRefManager(rm.podControl, rc, labels.Set(rc.Spec.Selector).AsSelectorPreValidated(), controllerKind, canAdoptFunc)
|
2017-02-25 20:39:49 +00:00
|
|
|
// NOTE: filteredPods are pointing to objects from cache - if you need to
|
|
|
|
// modify them, you need to copy it first.
|
2017-03-06 22:14:40 +00:00
|
|
|
filteredPods, err = cm.ClaimPods(filteredPods)
|
2017-02-10 00:02:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-06-10 23:28:42 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 14:36:46 +00:00
|
|
|
var manageReplicasErr error
|
2016-06-10 23:28:42 +00:00
|
|
|
if rcNeedsSync && rc.DeletionTimestamp == nil {
|
2017-02-06 18:35:50 +00:00
|
|
|
manageReplicasErr = rm.manageReplicas(filteredPods, rc)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2016-05-13 08:30:45 +00:00
|
|
|
trace.Step("manageReplicas done")
|
2015-04-21 20:40:35 +00:00
|
|
|
|
2017-08-15 12:14:21 +00:00
|
|
|
rc = rc.DeepCopy()
|
2017-02-06 18:35:50 +00:00
|
|
|
|
2016-10-17 15:19:26 +00:00
|
|
|
newStatus := calculateStatus(rc, filteredPods, manageReplicasErr)
|
2016-03-11 18:34:13 +00:00
|
|
|
|
2015-05-01 15:49:06 +00:00
|
|
|
// Always updates status as pods come up or die.
|
2017-10-25 15:54:32 +00:00
|
|
|
updatedRC, err := updateReplicationControllerStatus(rm.kubeClient.CoreV1().ReplicationControllers(rc.Namespace), *rc, newStatus)
|
2017-02-25 10:51:20 +00:00
|
|
|
if err != nil {
|
2016-08-15 14:36:46 +00:00
|
|
|
// Multiple things could lead to this update failing. Returning an error causes a requeue without forcing a hotloop
|
|
|
|
return err
|
2015-04-21 20:40:35 +00:00
|
|
|
}
|
2017-02-25 10:51:20 +00:00
|
|
|
// Resync the ReplicationController after MinReadySeconds as a last line of defense to guard against clock-skew.
|
|
|
|
if manageReplicasErr == nil && updatedRC.Spec.MinReadySeconds > 0 &&
|
|
|
|
updatedRC.Status.ReadyReplicas == *(updatedRC.Spec.Replicas) &&
|
|
|
|
updatedRC.Status.AvailableReplicas != *(updatedRC.Spec.Replicas) {
|
|
|
|
rm.enqueueControllerAfter(updatedRC, time.Duration(updatedRC.Spec.MinReadySeconds)*time.Second)
|
|
|
|
}
|
2016-08-15 14:36:46 +00:00
|
|
|
return manageReplicasErr
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|