mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-26 05:03:09 +00:00
Deduplicate RC/RS controller code.
The code was already 99% similar between RC and RS. This is a wild idea to try to deduplicate the two controllers in a type-safe manner without adding tons of boilerplate, and without using code generation. They are still separate resources. This is a refactor that isn't intended to change any behavior.
This commit is contained in:
parent
18402f6c51
commit
2c7ef5ad4f
@ -24,6 +24,7 @@ go_library(
|
|||||||
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/labels:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/labels:go_default_library",
|
||||||
|
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/util/wait:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/util/wait:go_default_library",
|
||||||
"//vendor/k8s.io/client-go/informers/core/v1:go_default_library",
|
"//vendor/k8s.io/client-go/informers/core/v1:go_default_library",
|
||||||
|
@ -14,7 +14,16 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// If you make changes to this file, you should also make the corresponding change in ReplicationController.
|
// ### ATTENTION ###
|
||||||
|
//
|
||||||
|
// This code implements both ReplicaSet and ReplicationController.
|
||||||
|
//
|
||||||
|
// For RC, the objects are converted on the way in and out (see ../replication/),
|
||||||
|
// as if ReplicationController were just an older API version of ReplicaSet.
|
||||||
|
// However, RC and RS still have separate storage and separate instantiations
|
||||||
|
// of the ReplicaSetController object.
|
||||||
|
//
|
||||||
|
// Use rsc.Kind in log messages rather than hard-coding "ReplicaSet".
|
||||||
|
|
||||||
package replicaset
|
package replicaset
|
||||||
|
|
||||||
@ -22,6 +31,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -32,6 +42,7 @@ import (
|
|||||||
"k8s.io/apimachinery/pkg/api/errors"
|
"k8s.io/apimachinery/pkg/api/errors"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/labels"
|
"k8s.io/apimachinery/pkg/labels"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||||
"k8s.io/apimachinery/pkg/util/wait"
|
"k8s.io/apimachinery/pkg/util/wait"
|
||||||
coreinformers "k8s.io/client-go/informers/core/v1"
|
coreinformers "k8s.io/client-go/informers/core/v1"
|
||||||
@ -59,12 +70,14 @@ const (
|
|||||||
statusUpdateRetries = 1
|
statusUpdateRetries = 1
|
||||||
)
|
)
|
||||||
|
|
||||||
// controllerKind contains the schema.GroupVersionKind for this controller type.
|
|
||||||
var controllerKind = v1beta1.SchemeGroupVersion.WithKind("ReplicaSet")
|
|
||||||
|
|
||||||
// ReplicaSetController is responsible for synchronizing ReplicaSet objects stored
|
// ReplicaSetController is responsible for synchronizing ReplicaSet objects stored
|
||||||
// in the system with actual running pods.
|
// in the system with actual running pods.
|
||||||
type ReplicaSetController struct {
|
type ReplicaSetController struct {
|
||||||
|
// GroupVersionKind indicates the controller type.
|
||||||
|
// Different instances of this struct may handle different GVKs.
|
||||||
|
// For example, this struct can be used (with adapters) to handle ReplicationController.
|
||||||
|
schema.GroupVersionKind
|
||||||
|
|
||||||
kubeClient clientset.Interface
|
kubeClient clientset.Interface
|
||||||
podControl controller.PodControlInterface
|
podControl controller.PodControlInterface
|
||||||
|
|
||||||
@ -95,22 +108,35 @@ type ReplicaSetController struct {
|
|||||||
|
|
||||||
// NewReplicaSetController configures a replica set controller with the specified event recorder
|
// NewReplicaSetController configures a replica set controller with the specified event recorder
|
||||||
func NewReplicaSetController(rsInformer extensionsinformers.ReplicaSetInformer, podInformer coreinformers.PodInformer, kubeClient clientset.Interface, burstReplicas int) *ReplicaSetController {
|
func NewReplicaSetController(rsInformer extensionsinformers.ReplicaSetInformer, podInformer coreinformers.PodInformer, kubeClient clientset.Interface, burstReplicas int) *ReplicaSetController {
|
||||||
if kubeClient != nil && kubeClient.CoreV1().RESTClient().GetRateLimiter() != nil {
|
|
||||||
metrics.RegisterMetricAndTrackRateLimiterUsage("replicaset_controller", kubeClient.CoreV1().RESTClient().GetRateLimiter())
|
|
||||||
}
|
|
||||||
eventBroadcaster := record.NewBroadcaster()
|
eventBroadcaster := record.NewBroadcaster()
|
||||||
eventBroadcaster.StartLogging(glog.Infof)
|
eventBroadcaster.StartLogging(glog.Infof)
|
||||||
eventBroadcaster.StartRecordingToSink(&v1core.EventSinkImpl{Interface: v1core.New(kubeClient.CoreV1().RESTClient()).Events("")})
|
eventBroadcaster.StartRecordingToSink(&v1core.EventSinkImpl{Interface: v1core.New(kubeClient.CoreV1().RESTClient()).Events("")})
|
||||||
|
return NewBaseController(rsInformer, podInformer, kubeClient, burstReplicas,
|
||||||
rsc := &ReplicaSetController{
|
v1beta1.SchemeGroupVersion.WithKind("ReplicaSet"),
|
||||||
kubeClient: kubeClient,
|
"replicaset_controller",
|
||||||
podControl: controller.RealPodControl{
|
"replicaset",
|
||||||
|
controller.RealPodControl{
|
||||||
KubeClient: kubeClient,
|
KubeClient: kubeClient,
|
||||||
Recorder: eventBroadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: "replicaset-controller"}),
|
Recorder: eventBroadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: "replicaset-controller"}),
|
||||||
},
|
},
|
||||||
burstReplicas: burstReplicas,
|
)
|
||||||
expectations: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectations()),
|
}
|
||||||
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "replicaset"),
|
|
||||||
|
// NewBaseController is the implementation of NewReplicaSetController with additional injected
|
||||||
|
// parameters so that it can also serve as the implementation of NewReplicationController.
|
||||||
|
func NewBaseController(rsInformer extensionsinformers.ReplicaSetInformer, podInformer coreinformers.PodInformer, kubeClient clientset.Interface, burstReplicas int,
|
||||||
|
gvk schema.GroupVersionKind, metricOwnerName, queueName string, podControl controller.PodControlInterface) *ReplicaSetController {
|
||||||
|
if kubeClient != nil && kubeClient.CoreV1().RESTClient().GetRateLimiter() != nil {
|
||||||
|
metrics.RegisterMetricAndTrackRateLimiterUsage(metricOwnerName, kubeClient.CoreV1().RESTClient().GetRateLimiter())
|
||||||
|
}
|
||||||
|
|
||||||
|
rsc := &ReplicaSetController{
|
||||||
|
GroupVersionKind: gvk,
|
||||||
|
kubeClient: kubeClient,
|
||||||
|
podControl: podControl,
|
||||||
|
burstReplicas: burstReplicas,
|
||||||
|
expectations: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectations()),
|
||||||
|
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), queueName),
|
||||||
}
|
}
|
||||||
|
|
||||||
rsInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
|
rsInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
|
||||||
@ -153,10 +179,11 @@ func (rsc *ReplicaSetController) Run(workers int, stopCh <-chan struct{}) {
|
|||||||
defer utilruntime.HandleCrash()
|
defer utilruntime.HandleCrash()
|
||||||
defer rsc.queue.ShutDown()
|
defer rsc.queue.ShutDown()
|
||||||
|
|
||||||
glog.Infof("Starting replica set controller")
|
controllerName := strings.ToLower(rsc.Kind)
|
||||||
defer glog.Infof("Shutting down replica set Controller")
|
glog.Infof("Starting %v controller", controllerName)
|
||||||
|
defer glog.Infof("Shutting down %v controller", controllerName)
|
||||||
|
|
||||||
if !controller.WaitForCacheSync("replica set", stopCh, rsc.podListerSynced, rsc.rsListerSynced) {
|
if !controller.WaitForCacheSync(rsc.Kind, stopCh, rsc.podListerSynced, rsc.rsListerSynced) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -176,7 +203,7 @@ func (rsc *ReplicaSetController) getPodReplicaSets(pod *v1.Pod) []*extensions.Re
|
|||||||
if len(rss) > 1 {
|
if len(rss) > 1 {
|
||||||
// ControllerRef will ensure we don't do anything crazy, but more than one
|
// ControllerRef will ensure we don't do anything crazy, but more than one
|
||||||
// item in this list nevertheless constitutes user error.
|
// item in this list nevertheless constitutes user error.
|
||||||
utilruntime.HandleError(fmt.Errorf("user error! more than one ReplicaSet is selecting pods with labels: %+v", pod.Labels))
|
utilruntime.HandleError(fmt.Errorf("user error! more than one %v is selecting pods with labels: %+v", rsc.Kind, pod.Labels))
|
||||||
}
|
}
|
||||||
return rss
|
return rss
|
||||||
}
|
}
|
||||||
@ -187,7 +214,7 @@ func (rsc *ReplicaSetController) getPodReplicaSets(pod *v1.Pod) []*extensions.Re
|
|||||||
func (rsc *ReplicaSetController) resolveControllerRef(namespace string, controllerRef *metav1.OwnerReference) *extensions.ReplicaSet {
|
func (rsc *ReplicaSetController) resolveControllerRef(namespace string, controllerRef *metav1.OwnerReference) *extensions.ReplicaSet {
|
||||||
// We can't look up by UID, so look up by Name and then verify UID.
|
// 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.
|
// Don't even try to look up by Name if it's the wrong Kind.
|
||||||
if controllerRef.Kind != controllerKind.Kind {
|
if controllerRef.Kind != rsc.Kind {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
rs, err := rsc.rsLister.ReplicaSets(namespace).Get(controllerRef.Name)
|
rs, err := rsc.rsLister.ReplicaSets(namespace).Get(controllerRef.Name)
|
||||||
@ -220,7 +247,7 @@ func (rsc *ReplicaSetController) updateRS(old, cur interface{}) {
|
|||||||
// that bad as ReplicaSets that haven't met expectations yet won't
|
// that bad as ReplicaSets that haven't met expectations yet won't
|
||||||
// sync, and all the listing is done using local stores.
|
// sync, and all the listing is done using local stores.
|
||||||
if *(oldRS.Spec.Replicas) != *(curRS.Spec.Replicas) {
|
if *(oldRS.Spec.Replicas) != *(curRS.Spec.Replicas) {
|
||||||
glog.V(4).Infof("Replica set %v updated. Desired pod count change: %d->%d", curRS.Name, *(oldRS.Spec.Replicas), *(curRS.Spec.Replicas))
|
glog.V(4).Infof("%v %v updated. Desired pod count change: %d->%d", rsc.Kind, curRS.Name, *(oldRS.Spec.Replicas), *(curRS.Spec.Replicas))
|
||||||
}
|
}
|
||||||
rsc.enqueueReplicaSet(cur)
|
rsc.enqueueReplicaSet(cur)
|
||||||
}
|
}
|
||||||
@ -319,7 +346,7 @@ func (rsc *ReplicaSetController) updatePod(old, cur interface{}) {
|
|||||||
// Note that this still suffers from #29229, we are just moving the problem one level
|
// Note that this still suffers from #29229, we are just moving the problem one level
|
||||||
// "closer" to kubelet (from the deployment to the replica set controller).
|
// "closer" to kubelet (from the deployment to the replica set controller).
|
||||||
if !podutil.IsPodReady(oldPod) && podutil.IsPodReady(curPod) && rs.Spec.MinReadySeconds > 0 {
|
if !podutil.IsPodReady(oldPod) && podutil.IsPodReady(curPod) && rs.Spec.MinReadySeconds > 0 {
|
||||||
glog.V(2).Infof("ReplicaSet %q will be enqueued after %ds for availability check", rs.Name, rs.Spec.MinReadySeconds)
|
glog.V(2).Infof("%v %q will be enqueued after %ds for availability check", rsc.Kind, rs.Name, rs.Spec.MinReadySeconds)
|
||||||
// Add a second to avoid milliseconds skew in AddAfter.
|
// Add a second to avoid milliseconds skew in AddAfter.
|
||||||
// See https://github.com/kubernetes/kubernetes/issues/39785#issuecomment-279959133 for more info.
|
// See https://github.com/kubernetes/kubernetes/issues/39785#issuecomment-279959133 for more info.
|
||||||
rsc.enqueueReplicaSetAfter(rs, (time.Duration(rs.Spec.MinReadySeconds)*time.Second)+time.Second)
|
rsc.enqueueReplicaSetAfter(rs, (time.Duration(rs.Spec.MinReadySeconds)*time.Second)+time.Second)
|
||||||
@ -434,7 +461,7 @@ func (rsc *ReplicaSetController) manageReplicas(filteredPods []*v1.Pod, rs *exte
|
|||||||
diff := len(filteredPods) - int(*(rs.Spec.Replicas))
|
diff := len(filteredPods) - int(*(rs.Spec.Replicas))
|
||||||
rsKey, err := controller.KeyFunc(rs)
|
rsKey, err := controller.KeyFunc(rs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utilruntime.HandleError(fmt.Errorf("Couldn't get key for ReplicaSet %#v: %v", rs, err))
|
utilruntime.HandleError(fmt.Errorf("Couldn't get key for %v %#v: %v", rsc.Kind, rs, err))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if diff < 0 {
|
if diff < 0 {
|
||||||
@ -448,7 +475,7 @@ func (rsc *ReplicaSetController) manageReplicas(filteredPods []*v1.Pod, rs *exte
|
|||||||
// into a performance bottleneck. We should generate a UID for the pod
|
// into a performance bottleneck. We should generate a UID for the pod
|
||||||
// beforehand and store it via ExpectCreations.
|
// beforehand and store it via ExpectCreations.
|
||||||
rsc.expectations.ExpectCreations(rsKey, diff)
|
rsc.expectations.ExpectCreations(rsKey, diff)
|
||||||
glog.V(2).Infof("Too few %q/%q replicas, need %d, creating %d", rs.Namespace, rs.Name, *(rs.Spec.Replicas), diff)
|
glog.V(2).Infof("Too few replicas for %v %s/%s, need %d, creating %d", rsc.Kind, rs.Namespace, rs.Name, *(rs.Spec.Replicas), diff)
|
||||||
// Batch the pod creates. Batch sizes start at SlowStartInitialBatchSize
|
// Batch the pod creates. Batch sizes start at SlowStartInitialBatchSize
|
||||||
// and double with each successful iteration in a kind of "slow start".
|
// and double with each successful iteration in a kind of "slow start".
|
||||||
// This handles attempts to start large numbers of pods that would
|
// This handles attempts to start large numbers of pods that would
|
||||||
@ -460,8 +487,8 @@ func (rsc *ReplicaSetController) manageReplicas(filteredPods []*v1.Pod, rs *exte
|
|||||||
successfulCreations, err := slowStartBatch(diff, controller.SlowStartInitialBatchSize, func() error {
|
successfulCreations, err := slowStartBatch(diff, controller.SlowStartInitialBatchSize, func() error {
|
||||||
boolPtr := func(b bool) *bool { return &b }
|
boolPtr := func(b bool) *bool { return &b }
|
||||||
controllerRef := &metav1.OwnerReference{
|
controllerRef := &metav1.OwnerReference{
|
||||||
APIVersion: controllerKind.GroupVersion().String(),
|
APIVersion: rsc.GroupVersion().String(),
|
||||||
Kind: controllerKind.Kind,
|
Kind: rsc.Kind,
|
||||||
Name: rs.Name,
|
Name: rs.Name,
|
||||||
UID: rs.UID,
|
UID: rs.UID,
|
||||||
BlockOwnerDeletion: boolPtr(true),
|
BlockOwnerDeletion: boolPtr(true),
|
||||||
@ -485,7 +512,7 @@ func (rsc *ReplicaSetController) manageReplicas(filteredPods []*v1.Pod, rs *exte
|
|||||||
// The skipped pods will be retried later. The next controller resync will
|
// The skipped pods will be retried later. The next controller resync will
|
||||||
// retry the slow start process.
|
// retry the slow start process.
|
||||||
if skippedPods := diff - successfulCreations; skippedPods > 0 {
|
if skippedPods := diff - successfulCreations; skippedPods > 0 {
|
||||||
glog.V(2).Infof("Slow-start failure. Skipping creation of %d pods, decrementing expectations for replica set %v/%v", skippedPods, rs.Namespace, rs.Name)
|
glog.V(2).Infof("Slow-start failure. Skipping creation of %d pods, decrementing expectations for %v %v/%v", skippedPods, rsc.Kind, rs.Namespace, rs.Name)
|
||||||
for i := 0; i < skippedPods; i++ {
|
for i := 0; i < skippedPods; i++ {
|
||||||
// Decrement the expected number of creates because the informer won't observe this pod
|
// Decrement the expected number of creates because the informer won't observe this pod
|
||||||
rsc.expectations.CreationObserved(rsKey)
|
rsc.expectations.CreationObserved(rsKey)
|
||||||
@ -496,7 +523,7 @@ func (rsc *ReplicaSetController) manageReplicas(filteredPods []*v1.Pod, rs *exte
|
|||||||
if diff > rsc.burstReplicas {
|
if diff > rsc.burstReplicas {
|
||||||
diff = rsc.burstReplicas
|
diff = rsc.burstReplicas
|
||||||
}
|
}
|
||||||
glog.V(2).Infof("Too many %q/%q replicas, need %d, deleting %d", rs.Namespace, rs.Name, *(rs.Spec.Replicas), diff)
|
glog.V(2).Infof("Too many replicas for %v %s/%s, need %d, deleting %d", rsc.Kind, rs.Namespace, rs.Name, *(rs.Spec.Replicas), diff)
|
||||||
|
|
||||||
// Choose which Pods to delete, preferring those in earlier phases of startup.
|
// Choose which Pods to delete, preferring those in earlier phases of startup.
|
||||||
podsToDelete := getPodsToDelete(filteredPods, diff)
|
podsToDelete := getPodsToDelete(filteredPods, diff)
|
||||||
@ -518,7 +545,7 @@ func (rsc *ReplicaSetController) manageReplicas(filteredPods []*v1.Pod, rs *exte
|
|||||||
if err := rsc.podControl.DeletePod(rs.Namespace, targetPod.Name, rs); err != nil {
|
if err := rsc.podControl.DeletePod(rs.Namespace, targetPod.Name, rs); err != nil {
|
||||||
// Decrement the expected number of deletes because the informer won't observe this deletion
|
// Decrement the expected number of deletes because the informer won't observe this deletion
|
||||||
podKey := controller.PodKey(targetPod)
|
podKey := controller.PodKey(targetPod)
|
||||||
glog.V(2).Infof("Failed to delete %v, decrementing expectations for controller %q/%q", podKey, rs.Namespace, rs.Name)
|
glog.V(2).Infof("Failed to delete %v, decrementing expectations for %v %s/%s", podKey, rsc.Kind, rs.Namespace, rs.Name)
|
||||||
rsc.expectations.DeletionObserved(rsKey, podKey)
|
rsc.expectations.DeletionObserved(rsKey, podKey)
|
||||||
errCh <- err
|
errCh <- err
|
||||||
}
|
}
|
||||||
@ -543,9 +570,10 @@ func (rsc *ReplicaSetController) manageReplicas(filteredPods []*v1.Pod, rs *exte
|
|||||||
// meaning it did not expect to see any more of its pods created or deleted. This function is not meant to be
|
// 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.
|
// invoked concurrently with the same key.
|
||||||
func (rsc *ReplicaSetController) syncReplicaSet(key string) error {
|
func (rsc *ReplicaSetController) syncReplicaSet(key string) error {
|
||||||
|
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
defer func() {
|
defer func() {
|
||||||
glog.V(4).Infof("Finished syncing replica set %q (%v)", key, time.Now().Sub(startTime))
|
glog.V(4).Infof("Finished syncing %v %q (%v)", rsc.Kind, key, time.Since(startTime))
|
||||||
}()
|
}()
|
||||||
|
|
||||||
namespace, name, err := cache.SplitMetaNamespaceKey(key)
|
namespace, name, err := cache.SplitMetaNamespaceKey(key)
|
||||||
@ -554,7 +582,7 @@ func (rsc *ReplicaSetController) syncReplicaSet(key string) error {
|
|||||||
}
|
}
|
||||||
rs, err := rsc.rsLister.ReplicaSets(namespace).Get(name)
|
rs, err := rsc.rsLister.ReplicaSets(namespace).Get(name)
|
||||||
if errors.IsNotFound(err) {
|
if errors.IsNotFound(err) {
|
||||||
glog.V(4).Infof("ReplicaSet has been deleted %v", key)
|
glog.V(4).Infof("%v %v has been deleted", rsc.Kind, key)
|
||||||
rsc.expectations.DeleteExpectations(key)
|
rsc.expectations.DeleteExpectations(key)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -623,11 +651,11 @@ func (rsc *ReplicaSetController) claimPods(rs *extensions.ReplicaSet, selector l
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if fresh.UID != rs.UID {
|
if fresh.UID != rs.UID {
|
||||||
return nil, fmt.Errorf("original ReplicaSet %v/%v is gone: got uid %v, wanted %v", rs.Namespace, rs.Name, fresh.UID, rs.UID)
|
return nil, fmt.Errorf("original %v %v/%v is gone: got uid %v, wanted %v", rsc.Kind, rs.Namespace, rs.Name, fresh.UID, rs.UID)
|
||||||
}
|
}
|
||||||
return fresh, nil
|
return fresh, nil
|
||||||
})
|
})
|
||||||
cm := controller.NewPodControllerRefManager(rsc.podControl, rs, selector, controllerKind, canAdoptFunc)
|
cm := controller.NewPodControllerRefManager(rsc.podControl, rs, selector, rsc.GroupVersionKind, canAdoptFunc)
|
||||||
return cm.ClaimPods(filteredPods)
|
return cm.ClaimPods(filteredPods)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,8 +14,6 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// If you make changes to this file, you should also make the corresponding change in ReplicationController.
|
|
||||||
|
|
||||||
package replicaset
|
package replicaset
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -55,7 +55,7 @@ func updateReplicaSetStatus(c unversionedextensions.ReplicaSetInterface, rs *ext
|
|||||||
var getErr, updateErr error
|
var getErr, updateErr error
|
||||||
var updatedRS *extensions.ReplicaSet
|
var updatedRS *extensions.ReplicaSet
|
||||||
for i, rs := 0, rs; ; i++ {
|
for i, rs := 0, rs; ; i++ {
|
||||||
glog.V(4).Infof(fmt.Sprintf("Updating status for ReplicaSet: %s/%s, ", rs.Namespace, rs.Name) +
|
glog.V(4).Infof(fmt.Sprintf("Updating status for %v: %s/%s, ", rs.Kind, rs.Namespace, rs.Name) +
|
||||||
fmt.Sprintf("replicas %d->%d (need %d), ", rs.Status.Replicas, newStatus.Replicas, *(rs.Spec.Replicas)) +
|
fmt.Sprintf("replicas %d->%d (need %d), ", rs.Status.Replicas, newStatus.Replicas, *(rs.Spec.Replicas)) +
|
||||||
fmt.Sprintf("fullyLabeledReplicas %d->%d, ", rs.Status.FullyLabeledReplicas, newStatus.FullyLabeledReplicas) +
|
fmt.Sprintf("fullyLabeledReplicas %d->%d, ", rs.Status.FullyLabeledReplicas, newStatus.FullyLabeledReplicas) +
|
||||||
fmt.Sprintf("readyReplicas %d->%d, ", rs.Status.ReadyReplicas, newStatus.ReadyReplicas) +
|
fmt.Sprintf("readyReplicas %d->%d, ", rs.Status.ReadyReplicas, newStatus.ReadyReplicas) +
|
||||||
|
@ -9,63 +9,45 @@ load(
|
|||||||
go_library(
|
go_library(
|
||||||
name = "go_default_library",
|
name = "go_default_library",
|
||||||
srcs = [
|
srcs = [
|
||||||
|
"conversion.go",
|
||||||
"doc.go",
|
"doc.go",
|
||||||
"replication_controller.go",
|
"replication_controller.go",
|
||||||
"replication_controller_utils.go",
|
"replication_controller_utils.go",
|
||||||
],
|
],
|
||||||
importpath = "k8s.io/kubernetes/pkg/controller/replication",
|
importpath = "k8s.io/kubernetes/pkg/controller/replication",
|
||||||
deps = [
|
deps = [
|
||||||
"//pkg/api/v1/pod:go_default_library",
|
"//pkg/api/v1:go_default_library",
|
||||||
|
"//pkg/apis/extensions:go_default_library",
|
||||||
|
"//pkg/apis/extensions/v1beta1:go_default_library",
|
||||||
"//pkg/controller:go_default_library",
|
"//pkg/controller:go_default_library",
|
||||||
"//pkg/util/metrics:go_default_library",
|
"//pkg/controller/replicaset:go_default_library",
|
||||||
"//vendor/github.com/golang/glog:go_default_library",
|
"//vendor/github.com/golang/glog:go_default_library",
|
||||||
"//vendor/k8s.io/api/core/v1:go_default_library",
|
"//vendor/k8s.io/api/core/v1:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
|
"//vendor/k8s.io/api/extensions/v1beta1:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/labels:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/labels:go_default_library",
|
||||||
|
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
||||||
|
"//vendor/k8s.io/apimachinery/pkg/types:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/util/wait:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/watch:go_default_library",
|
||||||
"//vendor/k8s.io/apiserver/pkg/util/trace:go_default_library",
|
|
||||||
"//vendor/k8s.io/client-go/informers/core/v1:go_default_library",
|
"//vendor/k8s.io/client-go/informers/core/v1:go_default_library",
|
||||||
"//vendor/k8s.io/client-go/kubernetes:go_default_library",
|
"//vendor/k8s.io/client-go/kubernetes:go_default_library",
|
||||||
"//vendor/k8s.io/client-go/kubernetes/scheme:go_default_library",
|
"//vendor/k8s.io/client-go/kubernetes/scheme:go_default_library",
|
||||||
"//vendor/k8s.io/client-go/kubernetes/typed/core/v1:go_default_library",
|
"//vendor/k8s.io/client-go/kubernetes/typed/core/v1:go_default_library",
|
||||||
|
"//vendor/k8s.io/client-go/kubernetes/typed/extensions/v1beta1:go_default_library",
|
||||||
"//vendor/k8s.io/client-go/listers/core/v1:go_default_library",
|
"//vendor/k8s.io/client-go/listers/core/v1:go_default_library",
|
||||||
|
"//vendor/k8s.io/client-go/listers/extensions/v1beta1:go_default_library",
|
||||||
"//vendor/k8s.io/client-go/tools/cache:go_default_library",
|
"//vendor/k8s.io/client-go/tools/cache:go_default_library",
|
||||||
"//vendor/k8s.io/client-go/tools/record:go_default_library",
|
"//vendor/k8s.io/client-go/tools/record:go_default_library",
|
||||||
"//vendor/k8s.io/client-go/util/integer:go_default_library",
|
|
||||||
"//vendor/k8s.io/client-go/util/workqueue:go_default_library",
|
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
go_test(
|
go_test(
|
||||||
name = "go_default_test",
|
name = "go_default_test",
|
||||||
srcs = ["replication_controller_test.go"],
|
srcs = ["replication_controller_utils_test.go"],
|
||||||
importpath = "k8s.io/kubernetes/pkg/controller/replication",
|
importpath = "k8s.io/kubernetes/pkg/controller/replication",
|
||||||
library = ":go_default_library",
|
library = ":go_default_library",
|
||||||
deps = [
|
deps = ["//vendor/k8s.io/api/core/v1:go_default_library"],
|
||||||
"//pkg/api/legacyscheme:go_default_library",
|
|
||||||
"//pkg/api/testapi:go_default_library",
|
|
||||||
"//pkg/controller:go_default_library",
|
|
||||||
"//pkg/securitycontext:go_default_library",
|
|
||||||
"//vendor/k8s.io/api/core/v1:go_default_library",
|
|
||||||
"//vendor/k8s.io/apimachinery/pkg/api/equality:go_default_library",
|
|
||||||
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
|
||||||
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
|
||||||
"//vendor/k8s.io/apimachinery/pkg/util/sets:go_default_library",
|
|
||||||
"//vendor/k8s.io/apimachinery/pkg/util/uuid:go_default_library",
|
|
||||||
"//vendor/k8s.io/apimachinery/pkg/util/wait:go_default_library",
|
|
||||||
"//vendor/k8s.io/apimachinery/pkg/watch:go_default_library",
|
|
||||||
"//vendor/k8s.io/client-go/informers:go_default_library",
|
|
||||||
"//vendor/k8s.io/client-go/informers/core/v1:go_default_library",
|
|
||||||
"//vendor/k8s.io/client-go/kubernetes:go_default_library",
|
|
||||||
"//vendor/k8s.io/client-go/kubernetes/fake:go_default_library",
|
|
||||||
"//vendor/k8s.io/client-go/rest:go_default_library",
|
|
||||||
"//vendor/k8s.io/client-go/testing:go_default_library",
|
|
||||||
"//vendor/k8s.io/client-go/tools/cache:go_default_library",
|
|
||||||
"//vendor/k8s.io/client-go/util/testing:go_default_library",
|
|
||||||
"//vendor/k8s.io/client-go/util/workqueue:go_default_library",
|
|
||||||
],
|
|
||||||
)
|
)
|
||||||
|
|
||||||
filegroup(
|
filegroup(
|
||||||
|
332
pkg/controller/replication/conversion.go
Normal file
332
pkg/controller/replication/conversion.go
Normal file
@ -0,0 +1,332 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2017 The Kubernetes Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// This file contains adapters that convert between RC and RS,
|
||||||
|
// as if ReplicationController were an older API version of ReplicaSet.
|
||||||
|
// It allows ReplicaSetController to directly replace the old ReplicationManager,
|
||||||
|
// which was previously a manually-maintained copy-paste of RSC.
|
||||||
|
|
||||||
|
package replication
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"k8s.io/api/core/v1"
|
||||||
|
extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/labels"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
|
"k8s.io/apimachinery/pkg/types"
|
||||||
|
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||||
|
"k8s.io/apimachinery/pkg/watch"
|
||||||
|
coreinformers "k8s.io/client-go/informers/core/v1"
|
||||||
|
clientset "k8s.io/client-go/kubernetes"
|
||||||
|
v1client "k8s.io/client-go/kubernetes/typed/core/v1"
|
||||||
|
extensionsv1beta1client "k8s.io/client-go/kubernetes/typed/extensions/v1beta1"
|
||||||
|
v1listers "k8s.io/client-go/listers/core/v1"
|
||||||
|
extensionslisters "k8s.io/client-go/listers/extensions/v1beta1"
|
||||||
|
"k8s.io/client-go/tools/cache"
|
||||||
|
apiv1 "k8s.io/kubernetes/pkg/api/v1"
|
||||||
|
"k8s.io/kubernetes/pkg/apis/extensions"
|
||||||
|
extensionsinternalv1beta1 "k8s.io/kubernetes/pkg/apis/extensions/v1beta1"
|
||||||
|
"k8s.io/kubernetes/pkg/controller"
|
||||||
|
)
|
||||||
|
|
||||||
|
// informerAdapter implements ReplicaSetInformer by wrapping ReplicationControllerInformer
|
||||||
|
// and converting objects.
|
||||||
|
type informerAdapter struct {
|
||||||
|
rcInformer coreinformers.ReplicationControllerInformer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i informerAdapter) Informer() cache.SharedIndexInformer {
|
||||||
|
return conversionInformer{i.rcInformer.Informer()}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i informerAdapter) Lister() extensionslisters.ReplicaSetLister {
|
||||||
|
return conversionLister{i.rcInformer.Lister()}
|
||||||
|
}
|
||||||
|
|
||||||
|
type conversionInformer struct {
|
||||||
|
cache.SharedIndexInformer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i conversionInformer) AddEventHandler(handler cache.ResourceEventHandler) {
|
||||||
|
i.SharedIndexInformer.AddEventHandler(conversionEventHandler{handler})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i conversionInformer) AddEventHandlerWithResyncPeriod(handler cache.ResourceEventHandler, resyncPeriod time.Duration) {
|
||||||
|
i.SharedIndexInformer.AddEventHandlerWithResyncPeriod(conversionEventHandler{handler}, resyncPeriod)
|
||||||
|
}
|
||||||
|
|
||||||
|
type conversionLister struct {
|
||||||
|
rcLister v1listers.ReplicationControllerLister
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l conversionLister) List(selector labels.Selector) ([]*extensionsv1beta1.ReplicaSet, error) {
|
||||||
|
rcList, err := l.rcLister.List(selector)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return convertSlice(rcList)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l conversionLister) ReplicaSets(namespace string) extensionslisters.ReplicaSetNamespaceLister {
|
||||||
|
return conversionNamespaceLister{l.rcLister.ReplicationControllers(namespace)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l conversionLister) GetPodReplicaSets(pod *v1.Pod) ([]*extensionsv1beta1.ReplicaSet, error) {
|
||||||
|
rcList, err := l.rcLister.GetPodControllers(pod)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return convertSlice(rcList)
|
||||||
|
}
|
||||||
|
|
||||||
|
type conversionNamespaceLister struct {
|
||||||
|
rcLister v1listers.ReplicationControllerNamespaceLister
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l conversionNamespaceLister) List(selector labels.Selector) ([]*extensionsv1beta1.ReplicaSet, error) {
|
||||||
|
rcList, err := l.rcLister.List(selector)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return convertSlice(rcList)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l conversionNamespaceLister) Get(name string) (*extensionsv1beta1.ReplicaSet, error) {
|
||||||
|
rc, err := l.rcLister.Get(name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return convertRCtoRS(rc, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
type conversionEventHandler struct {
|
||||||
|
handler cache.ResourceEventHandler
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h conversionEventHandler) OnAdd(obj interface{}) {
|
||||||
|
rs, err := convertRCtoRS(obj.(*v1.ReplicationController), nil)
|
||||||
|
if err != nil {
|
||||||
|
utilruntime.HandleError(fmt.Errorf("dropping RC OnAdd event: can't convert object %#v to RS: %v", obj, err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
h.handler.OnAdd(rs)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h conversionEventHandler) OnUpdate(oldObj, newObj interface{}) {
|
||||||
|
oldRS, err := convertRCtoRS(oldObj.(*v1.ReplicationController), nil)
|
||||||
|
if err != nil {
|
||||||
|
utilruntime.HandleError(fmt.Errorf("dropping RC OnUpdate event: can't convert old object %#v to RS: %v", oldObj, err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
newRS, err := convertRCtoRS(newObj.(*v1.ReplicationController), nil)
|
||||||
|
if err != nil {
|
||||||
|
utilruntime.HandleError(fmt.Errorf("dropping RC OnUpdate event: can't convert new object %#v to RS: %v", newObj, err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
h.handler.OnUpdate(oldRS, newRS)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h conversionEventHandler) OnDelete(obj interface{}) {
|
||||||
|
rc, ok := obj.(*v1.ReplicationController)
|
||||||
|
if !ok {
|
||||||
|
// Convert the Obj inside DeletedFinalStateUnknown.
|
||||||
|
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
|
||||||
|
if !ok {
|
||||||
|
utilruntime.HandleError(fmt.Errorf("dropping RC OnDelete event: couldn't get object from tombstone %+v", obj))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
rc, ok = tombstone.Obj.(*v1.ReplicationController)
|
||||||
|
if !ok {
|
||||||
|
utilruntime.HandleError(fmt.Errorf("dropping RC OnDelete event: tombstone contained object that is not a RC %#v", obj))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
rs, err := convertRCtoRS(rc, nil)
|
||||||
|
if err != nil {
|
||||||
|
utilruntime.HandleError(fmt.Errorf("dropping RC OnDelete event: can't convert object %#v to RS: %v", obj, err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
h.handler.OnDelete(cache.DeletedFinalStateUnknown{Key: tombstone.Key, Obj: rs})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// It's a regular RC object.
|
||||||
|
rs, err := convertRCtoRS(rc, nil)
|
||||||
|
if err != nil {
|
||||||
|
utilruntime.HandleError(fmt.Errorf("dropping RC OnDelete event: can't convert object %#v to RS: %v", obj, err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
h.handler.OnDelete(rs)
|
||||||
|
}
|
||||||
|
|
||||||
|
type clientsetAdapter struct {
|
||||||
|
clientset.Interface
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c clientsetAdapter) ExtensionsV1beta1() extensionsv1beta1client.ExtensionsV1beta1Interface {
|
||||||
|
return conversionExtensionsClient{c.Interface, c.Interface.ExtensionsV1beta1()}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c clientsetAdapter) Extensions() extensionsv1beta1client.ExtensionsV1beta1Interface {
|
||||||
|
return conversionExtensionsClient{c.Interface, c.Interface.Extensions()}
|
||||||
|
}
|
||||||
|
|
||||||
|
type conversionExtensionsClient struct {
|
||||||
|
clientset clientset.Interface
|
||||||
|
extensionsv1beta1client.ExtensionsV1beta1Interface
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c conversionExtensionsClient) ReplicaSets(namespace string) extensionsv1beta1client.ReplicaSetInterface {
|
||||||
|
return conversionClient{c.clientset.CoreV1().ReplicationControllers(namespace)}
|
||||||
|
}
|
||||||
|
|
||||||
|
type conversionClient struct {
|
||||||
|
v1client.ReplicationControllerInterface
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c conversionClient) Create(rs *extensionsv1beta1.ReplicaSet) (*extensionsv1beta1.ReplicaSet, error) {
|
||||||
|
return convertCall(c.ReplicationControllerInterface.Create, rs)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c conversionClient) Update(rs *extensionsv1beta1.ReplicaSet) (*extensionsv1beta1.ReplicaSet, error) {
|
||||||
|
return convertCall(c.ReplicationControllerInterface.Update, rs)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c conversionClient) UpdateStatus(rs *extensionsv1beta1.ReplicaSet) (*extensionsv1beta1.ReplicaSet, error) {
|
||||||
|
return convertCall(c.ReplicationControllerInterface.UpdateStatus, rs)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c conversionClient) Get(name string, options metav1.GetOptions) (*extensionsv1beta1.ReplicaSet, error) {
|
||||||
|
rc, err := c.ReplicationControllerInterface.Get(name, options)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return convertRCtoRS(rc, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c conversionClient) List(opts metav1.ListOptions) (*extensionsv1beta1.ReplicaSetList, error) {
|
||||||
|
rcList, err := c.ReplicationControllerInterface.List(opts)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return convertList(rcList)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c conversionClient) Watch(opts metav1.ListOptions) (watch.Interface, error) {
|
||||||
|
// This is not used by RSC because we wrap the shared informer instead.
|
||||||
|
return nil, errors.New("Watch() is not implemented for conversionClient")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c conversionClient) Patch(name string, pt types.PatchType, data []byte, subresources ...string) (result *extensionsv1beta1.ReplicaSet, err error) {
|
||||||
|
// This is not used by RSC.
|
||||||
|
return nil, errors.New("Patch() is not implemented for conversionClient")
|
||||||
|
}
|
||||||
|
|
||||||
|
func convertSlice(rcList []*v1.ReplicationController) ([]*extensionsv1beta1.ReplicaSet, error) {
|
||||||
|
rsList := make([]*extensionsv1beta1.ReplicaSet, 0, len(rcList))
|
||||||
|
for _, rc := range rcList {
|
||||||
|
rs, err := convertRCtoRS(rc, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
rsList = append(rsList, rs)
|
||||||
|
}
|
||||||
|
return rsList, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func convertList(rcList *v1.ReplicationControllerList) (*extensionsv1beta1.ReplicaSetList, error) {
|
||||||
|
rsList := &extensionsv1beta1.ReplicaSetList{Items: make([]extensionsv1beta1.ReplicaSet, len(rcList.Items))}
|
||||||
|
for i := range rcList.Items {
|
||||||
|
rc := &rcList.Items[i]
|
||||||
|
_, err := convertRCtoRS(rc, &rsList.Items[i])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return rsList, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func convertCall(fn func(*v1.ReplicationController) (*v1.ReplicationController, error), rs *extensionsv1beta1.ReplicaSet) (*extensionsv1beta1.ReplicaSet, error) {
|
||||||
|
rc, err := convertRStoRC(rs)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
result, err := fn(rc)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return convertRCtoRS(result, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func convertRCtoRS(rc *v1.ReplicationController, out *extensionsv1beta1.ReplicaSet) (*extensionsv1beta1.ReplicaSet, error) {
|
||||||
|
var rsInternal extensions.ReplicaSet
|
||||||
|
if err := apiv1.Convert_v1_ReplicationController_to_extensions_ReplicaSet(rc, &rsInternal, nil); err != nil {
|
||||||
|
return nil, fmt.Errorf("can't convert ReplicationController %v/%v to ReplicaSet: %v", rc.Namespace, rc.Name, err)
|
||||||
|
}
|
||||||
|
if out == nil {
|
||||||
|
out = new(extensionsv1beta1.ReplicaSet)
|
||||||
|
}
|
||||||
|
if err := extensionsinternalv1beta1.Convert_extensions_ReplicaSet_To_v1beta1_ReplicaSet(&rsInternal, out, nil); err != nil {
|
||||||
|
return nil, fmt.Errorf("can't convert ReplicaSet (converted from ReplicationController %v/%v) from internal to extensions/v1beta1: %v", rc.Namespace, rc.Name, err)
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func convertRStoRC(rs *extensionsv1beta1.ReplicaSet) (*v1.ReplicationController, error) {
|
||||||
|
var rsInternal extensions.ReplicaSet
|
||||||
|
if err := extensionsinternalv1beta1.Convert_v1beta1_ReplicaSet_To_extensions_ReplicaSet(rs, &rsInternal, nil); err != nil {
|
||||||
|
return nil, fmt.Errorf("can't convert ReplicaSet (converting to ReplicationController %v/%v) from extensions/v1beta1 to internal: %v", rs.Namespace, rs.Name, err)
|
||||||
|
}
|
||||||
|
var rc v1.ReplicationController
|
||||||
|
if err := apiv1.Convert_extensions_ReplicaSet_to_v1_ReplicationController(&rsInternal, &rc, nil); err != nil {
|
||||||
|
return nil, fmt.Errorf("can't convert ReplicaSet to ReplicationController %v/%v: %v", rs.Namespace, rs.Name, err)
|
||||||
|
}
|
||||||
|
return &rc, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type podControlAdapter struct {
|
||||||
|
controller.PodControlInterface
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pc podControlAdapter) CreatePods(namespace string, template *v1.PodTemplateSpec, object runtime.Object) error {
|
||||||
|
// This is not used by RSC.
|
||||||
|
return errors.New("CreatePods() is not implemented for podControlAdapter")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pc podControlAdapter) CreatePodsOnNode(nodeName, namespace string, template *v1.PodTemplateSpec, object runtime.Object, controllerRef *metav1.OwnerReference) error {
|
||||||
|
// This is not used by RSC.
|
||||||
|
return errors.New("CreatePodsOnNode() is not implemented for podControlAdapter")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pc podControlAdapter) CreatePodsWithControllerRef(namespace string, template *v1.PodTemplateSpec, object runtime.Object, controllerRef *metav1.OwnerReference) error {
|
||||||
|
rc, err := convertRStoRC(object.(*extensionsv1beta1.ReplicaSet))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return pc.PodControlInterface.CreatePodsWithControllerRef(namespace, template, rc, controllerRef)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pc podControlAdapter) DeletePod(namespace string, podID string, object runtime.Object) error {
|
||||||
|
rc, err := convertRStoRC(object.(*extensionsv1beta1.ReplicaSet))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return pc.PodControlInterface.DeletePod(namespace, podID, rc)
|
||||||
|
}
|
@ -14,653 +14,54 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// If you make changes to this file, you should also make the corresponding change in ReplicaSet.
|
// ### ATTENTION ###
|
||||||
|
//
|
||||||
|
// ReplicationManager is now just a wrapper around ReplicaSetController,
|
||||||
|
// with a conversion layer that effectively treats ReplicationController
|
||||||
|
// as if it were an older API version of ReplicaSet.
|
||||||
|
//
|
||||||
|
// However, RC and RS still have separate storage and separate instantiations
|
||||||
|
// of the ReplicaSetController object.
|
||||||
|
|
||||||
package replication
|
package replication
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"reflect"
|
|
||||||
"sort"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
"k8s.io/api/core/v1"
|
"k8s.io/api/core/v1"
|
||||||
"k8s.io/apimachinery/pkg/api/errors"
|
|
||||||
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"
|
|
||||||
utiltrace "k8s.io/apiserver/pkg/util/trace"
|
|
||||||
coreinformers "k8s.io/client-go/informers/core/v1"
|
coreinformers "k8s.io/client-go/informers/core/v1"
|
||||||
clientset "k8s.io/client-go/kubernetes"
|
clientset "k8s.io/client-go/kubernetes"
|
||||||
"k8s.io/client-go/kubernetes/scheme"
|
"k8s.io/client-go/kubernetes/scheme"
|
||||||
v1core "k8s.io/client-go/kubernetes/typed/core/v1"
|
v1core "k8s.io/client-go/kubernetes/typed/core/v1"
|
||||||
corelisters "k8s.io/client-go/listers/core/v1"
|
|
||||||
"k8s.io/client-go/tools/cache"
|
|
||||||
"k8s.io/client-go/tools/record"
|
"k8s.io/client-go/tools/record"
|
||||||
"k8s.io/client-go/util/integer"
|
|
||||||
"k8s.io/client-go/util/workqueue"
|
|
||||||
podutil "k8s.io/kubernetes/pkg/api/v1/pod"
|
|
||||||
"k8s.io/kubernetes/pkg/controller"
|
"k8s.io/kubernetes/pkg/controller"
|
||||||
"k8s.io/kubernetes/pkg/util/metrics"
|
"k8s.io/kubernetes/pkg/controller/replicaset"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// Realistic value of the burstReplica field for the replication manager based off
|
BurstReplicas = replicaset.BurstReplicas
|
||||||
// performance requirements for kubernetes 1.0.
|
|
||||||
BurstReplicas = 500
|
|
||||||
|
|
||||||
// The number of times we retry updating a replication controller's status.
|
|
||||||
statusUpdateRetries = 1
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// controllerKind contains the schema.GroupVersionKind for this controller type.
|
|
||||||
var controllerKind = v1.SchemeGroupVersion.WithKind("ReplicationController")
|
|
||||||
|
|
||||||
// ReplicationManager is responsible for synchronizing ReplicationController objects stored
|
// ReplicationManager is responsible for synchronizing ReplicationController objects stored
|
||||||
// in the system with actual running pods.
|
// in the system with actual running pods.
|
||||||
// NOTE: using this name to distinguish this type from API object "ReplicationController"; will
|
// It is actually just a wrapper around ReplicaSetController.
|
||||||
// not fix it right now. Refer to #41459 for more detail.
|
|
||||||
type ReplicationManager struct {
|
type ReplicationManager struct {
|
||||||
kubeClient clientset.Interface
|
replicaset.ReplicaSetController
|
||||||
podControl controller.PodControlInterface
|
|
||||||
|
|
||||||
// An rc is temporarily suspended after creating/deleting these many replicas.
|
|
||||||
// It resumes normal action after observing the watch events for them.
|
|
||||||
burstReplicas int
|
|
||||||
// To allow injection of syncReplicationController for testing.
|
|
||||||
syncHandler func(rcKey string) error
|
|
||||||
|
|
||||||
// A TTLCache of pod creates/deletes each rc expects to see.
|
|
||||||
expectations *controller.UIDTrackingControllerExpectations
|
|
||||||
|
|
||||||
rcLister corelisters.ReplicationControllerLister
|
|
||||||
rcListerSynced cache.InformerSynced
|
|
||||||
|
|
||||||
podLister corelisters.PodLister
|
|
||||||
// podListerSynced returns true if the pod store has been synced at least once.
|
|
||||||
// Added as a member to the struct to allow injection for testing.
|
|
||||||
podListerSynced cache.InformerSynced
|
|
||||||
|
|
||||||
// Controllers that need to be synced
|
|
||||||
queue workqueue.RateLimitingInterface
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewReplicationManager configures a replication manager with the specified event recorder
|
// NewReplicationManager configures a replication manager with the specified event recorder
|
||||||
func NewReplicationManager(podInformer coreinformers.PodInformer, rcInformer coreinformers.ReplicationControllerInformer, kubeClient clientset.Interface, burstReplicas int) *ReplicationManager {
|
func NewReplicationManager(podInformer coreinformers.PodInformer, rcInformer coreinformers.ReplicationControllerInformer, kubeClient clientset.Interface, burstReplicas int) *ReplicationManager {
|
||||||
if kubeClient != nil && kubeClient.CoreV1().RESTClient().GetRateLimiter() != nil {
|
|
||||||
metrics.RegisterMetricAndTrackRateLimiterUsage("replication_controller", kubeClient.CoreV1().RESTClient().GetRateLimiter())
|
|
||||||
}
|
|
||||||
|
|
||||||
eventBroadcaster := record.NewBroadcaster()
|
eventBroadcaster := record.NewBroadcaster()
|
||||||
eventBroadcaster.StartLogging(glog.Infof)
|
eventBroadcaster.StartLogging(glog.Infof)
|
||||||
eventBroadcaster.StartRecordingToSink(&v1core.EventSinkImpl{Interface: v1core.New(kubeClient.CoreV1().RESTClient()).Events("")})
|
eventBroadcaster.StartRecordingToSink(&v1core.EventSinkImpl{Interface: v1core.New(kubeClient.CoreV1().RESTClient()).Events("")})
|
||||||
|
return &ReplicationManager{
|
||||||
rm := &ReplicationManager{
|
*replicaset.NewBaseController(informerAdapter{rcInformer}, podInformer, clientsetAdapter{kubeClient}, burstReplicas,
|
||||||
kubeClient: kubeClient,
|
v1.SchemeGroupVersion.WithKind("ReplicationController"),
|
||||||
podControl: controller.RealPodControl{
|
"replication_controller",
|
||||||
KubeClient: kubeClient,
|
"replicationmanager",
|
||||||
Recorder: eventBroadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: "replication-controller"}),
|
podControlAdapter{controller.RealPodControl{
|
||||||
},
|
KubeClient: kubeClient,
|
||||||
burstReplicas: burstReplicas,
|
Recorder: eventBroadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: "replication-controller"}),
|
||||||
expectations: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectations()),
|
}},
|
||||||
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "replicationmanager"),
|
),
|
||||||
}
|
|
||||||
|
|
||||||
rcInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
|
|
||||||
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,
|
|
||||||
})
|
|
||||||
rm.rcLister = rcInformer.Lister()
|
|
||||||
rm.rcListerSynced = rcInformer.Informer().HasSynced
|
|
||||||
|
|
||||||
podInformer.Informer().AddEventHandler(cache.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.podLister = podInformer.Lister()
|
|
||||||
rm.podListerSynced = podInformer.Informer().HasSynced
|
|
||||||
|
|
||||||
rm.syncHandler = rm.syncReplicationController
|
|
||||||
return rm
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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.
|
|
||||||
rm.podControl = controller.RealPodControl{KubeClient: rm.kubeClient, Recorder: recorder}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run begins watching and syncing.
|
|
||||||
func (rm *ReplicationManager) Run(workers int, stopCh <-chan struct{}) {
|
|
||||||
defer utilruntime.HandleCrash()
|
|
||||||
defer rm.queue.ShutDown()
|
|
||||||
|
|
||||||
glog.Infof("Starting RC controller")
|
|
||||||
defer glog.Infof("Shutting down RC controller")
|
|
||||||
|
|
||||||
if !controller.WaitForCacheSync("RC", stopCh, rm.podListerSynced, rm.rcListerSynced) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < workers; i++ {
|
|
||||||
go wait.Until(rm.worker, time.Second, stopCh)
|
|
||||||
}
|
|
||||||
|
|
||||||
<-stopCh
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
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))
|
|
||||||
}
|
|
||||||
return rcs
|
|
||||||
}
|
|
||||||
|
|
||||||
// resolveControllerRef returns the controller referenced by a ControllerRef,
|
|
||||||
// or nil if the ControllerRef could not be resolved to a matching controller
|
|
||||||
// of the correct Kind.
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// callback when RC is updated
|
|
||||||
func (rm *ReplicationManager) updateRC(old, cur interface{}) {
|
|
||||||
oldRC := old.(*v1.ReplicationController)
|
|
||||||
curRC := cur.(*v1.ReplicationController)
|
|
||||||
|
|
||||||
// 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.
|
|
||||||
if *(oldRC.Spec.Replicas) != *(curRC.Spec.Replicas) {
|
|
||||||
glog.V(4).Infof("Replication controller %v updated. Desired pod count change: %d->%d", curRC.Name, *(oldRC.Spec.Replicas), *(curRC.Spec.Replicas))
|
|
||||||
}
|
|
||||||
rm.enqueueController(cur)
|
|
||||||
}
|
|
||||||
|
|
||||||
// When a pod is created, enqueue the ReplicationController that manages it and update its expectations.
|
|
||||||
func (rm *ReplicationManager) addPod(obj interface{}) {
|
|
||||||
pod := obj.(*v1.Pod)
|
|
||||||
|
|
||||||
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.
|
|
||||||
rm.deletePod(pod)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// If it has a ControllerRef, that's all that matters.
|
|
||||||
if controllerRef := metav1.GetControllerOf(pod); controllerRef != nil {
|
|
||||||
rc := rm.resolveControllerRef(pod.Namespace, controllerRef)
|
|
||||||
if rc == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
rsKey, err := controller.KeyFunc(rc)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
glog.V(4).Infof("Pod %s created: %#v.", pod.Name, pod)
|
|
||||||
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.
|
|
||||||
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 {
|
|
||||||
rm.enqueueController(rc)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// When a pod is updated, figure out what ReplicationController/s manage it and wake them
|
|
||||||
// up. If the labels of the pod have changed we need to awaken both the old
|
|
||||||
// and new ReplicationController. old and cur must be *v1.Pod types.
|
|
||||||
func (rm *ReplicationManager) updatePod(old, cur interface{}) {
|
|
||||||
curPod := cur.(*v1.Pod)
|
|
||||||
oldPod := old.(*v1.Pod)
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
labelChanged := !reflect.DeepEqual(curPod.Labels, oldPod.Labels)
|
|
||||||
if curPod.DeletionTimestamp != nil {
|
|
||||||
// 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.
|
|
||||||
rm.deletePod(curPod)
|
|
||||||
if labelChanged {
|
|
||||||
// we don't need to check the oldPod.DeletionTimestamp because DeletionTimestamp cannot be unset.
|
|
||||||
rm.deletePod(oldPod)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
curControllerRef := metav1.GetControllerOf(curPod)
|
|
||||||
oldControllerRef := metav1.GetControllerOf(oldPod)
|
|
||||||
controllerRefChanged := !reflect.DeepEqual(curControllerRef, oldControllerRef)
|
|
||||||
if controllerRefChanged && oldControllerRef != nil {
|
|
||||||
// The ControllerRef was changed. Sync the old controller, if any.
|
|
||||||
if rc := rm.resolveControllerRef(oldPod.Namespace, oldControllerRef); rc != nil {
|
|
||||||
rm.enqueueController(rc)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If it has a ControllerRef, that's all that matters.
|
|
||||||
if curControllerRef != nil {
|
|
||||||
rc := rm.resolveControllerRef(curPod.Namespace, curControllerRef)
|
|
||||||
if rc == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
glog.V(4).Infof("Pod %s updated, objectMeta %+v -> %+v.", curPod.Name, oldPod.ObjectMeta, curPod.ObjectMeta)
|
|
||||||
rm.enqueueController(rc)
|
|
||||||
// TODO: MinReadySeconds in the Pod will generate an Available condition to be added in
|
|
||||||
// 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.
|
|
||||||
// Note that this still suffers from #29229, we are just moving the problem one level
|
|
||||||
// "closer" to kubelet (from the deployment to the ReplicationController controller).
|
|
||||||
if !podutil.IsPodReady(oldPod) && podutil.IsPodReady(curPod) && rc.Spec.MinReadySeconds > 0 {
|
|
||||||
glog.V(2).Infof("ReplicationController %q will be enqueued after %ds for availability check", rc.Name, rc.Spec.MinReadySeconds)
|
|
||||||
// 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)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise, it's an orphan. If anything changed, sync matching controllers
|
|
||||||
// to see if anyone wants to adopt it now.
|
|
||||||
if labelChanged || controllerRefChanged {
|
|
||||||
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 {
|
|
||||||
rm.enqueueController(rc)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// When a pod is deleted, enqueue the ReplicationController that manages the pod and update its expectations.
|
|
||||||
// obj could be an *v1.Pod, or a DeletionFinalStateUnknown marker item.
|
|
||||||
func (rm *ReplicationManager) deletePod(obj interface{}) {
|
|
||||||
pod, ok := obj.(*v1.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
|
|
||||||
// changed labels the new ReplicationController will not be woken up till the periodic resync.
|
|
||||||
if !ok {
|
|
||||||
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
|
|
||||||
if !ok {
|
|
||||||
utilruntime.HandleError(fmt.Errorf("couldn't get object from tombstone %+v", obj))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
pod, ok = tombstone.Obj.(*v1.Pod)
|
|
||||||
if !ok {
|
|
||||||
utilruntime.HandleError(fmt.Errorf("tombstone contained object that is not a pod %#v", obj))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
controllerRef := metav1.GetControllerOf(pod)
|
|
||||||
if controllerRef == nil {
|
|
||||||
// No controller should care about orphans being deleted.
|
|
||||||
return
|
|
||||||
}
|
|
||||||
rc := rm.resolveControllerRef(pod.Namespace, controllerRef)
|
|
||||||
if rc == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
rsKey, err := controller.KeyFunc(rc)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
glog.V(4).Infof("Pod %s/%s deleted through %v, timestamp %+v: %#v.", pod.Namespace, pod.Name, utilruntime.GetCaller(), pod.DeletionTimestamp, pod)
|
|
||||||
rm.expectations.DeletionObserved(rsKey, controller.PodKey(pod))
|
|
||||||
rm.enqueueController(rc)
|
|
||||||
}
|
|
||||||
|
|
||||||
// obj could be an *v1.ReplicationController, or a DeletionFinalStateUnknown marker item.
|
|
||||||
func (rm *ReplicationManager) enqueueController(obj interface{}) {
|
|
||||||
key, err := controller.KeyFunc(obj)
|
|
||||||
if err != nil {
|
|
||||||
utilruntime.HandleError(fmt.Errorf("couldn't get key for object %+v: %v", obj, err))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
rm.queue.Add(key)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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 {
|
|
||||||
utilruntime.HandleError(fmt.Errorf("couldn't get key for object %+v: %v", obj, err))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
rm.queue.AddAfter(key, after)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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 rm.processNextWorkItem() {
|
|
||||||
}
|
|
||||||
glog.Infof("replication controller worker shutting down")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rm *ReplicationManager) processNextWorkItem() bool {
|
|
||||||
key, quit := rm.queue.Get()
|
|
||||||
if quit {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
defer rm.queue.Done(key)
|
|
||||||
|
|
||||||
err := rm.syncHandler(key.(string))
|
|
||||||
if err == nil {
|
|
||||||
rm.queue.Forget(key)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
rm.queue.AddRateLimited(key)
|
|
||||||
utilruntime.HandleError(err)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// manageReplicas checks and updates replicas for the given replication controller.
|
|
||||||
// Does NOT modify <filteredPods>.
|
|
||||||
func (rm *ReplicationManager) manageReplicas(filteredPods []*v1.Pod, rc *v1.ReplicationController) error {
|
|
||||||
diff := len(filteredPods) - int(*(rc.Spec.Replicas))
|
|
||||||
rcKey, err := controller.KeyFunc(rc)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if diff == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if diff < 0 {
|
|
||||||
diff *= -1
|
|
||||||
if diff > rm.burstReplicas {
|
|
||||||
diff = rm.burstReplicas
|
|
||||||
}
|
|
||||||
// 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.
|
|
||||||
errCh := make(chan error, diff)
|
|
||||||
rm.expectations.ExpectCreations(rcKey, diff)
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
glog.V(2).Infof("Too few %q/%q replicas, need %d, creating %d", rc.Namespace, rc.Name, *(rc.Spec.Replicas), diff)
|
|
||||||
// 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++ {
|
|
||||||
// Decrement the expected number of creates because the informer won't observe this pod
|
|
||||||
rm.expectations.CreationObserved(rcKey)
|
|
||||||
}
|
|
||||||
// The skipped pods will be retried later. The next controller resync will
|
|
||||||
// retry the slow start process.
|
|
||||||
break
|
|
||||||
}
|
|
||||||
diff -= batchSize
|
|
||||||
}
|
|
||||||
|
|
||||||
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:
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if diff > rm.burstReplicas {
|
|
||||||
diff = rm.burstReplicas
|
|
||||||
}
|
|
||||||
glog.V(2).Infof("Too many %q/%q replicas, need %d, deleting %d", rc.Namespace, rc.Name, *(rc.Spec.Replicas), diff)
|
|
||||||
// No need to sort pods if we are about to delete all of them
|
|
||||||
if *(rc.Spec.Replicas) != 0 {
|
|
||||||
// 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
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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 {
|
|
||||||
trace := utiltrace.New("syncReplicationController: " + key)
|
|
||||||
defer trace.LogIfLong(250 * time.Millisecond)
|
|
||||||
|
|
||||||
startTime := time.Now()
|
|
||||||
defer func() {
|
|
||||||
glog.V(4).Infof("Finished syncing controller %q (%v)", key, time.Now().Sub(startTime))
|
|
||||||
}()
|
|
||||||
|
|
||||||
namespace, name, err := cache.SplitMetaNamespaceKey(key)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
rc, err := rm.rcLister.ReplicationControllers(namespace).Get(name)
|
|
||||||
if errors.IsNotFound(err) {
|
|
||||||
glog.Infof("Replication Controller has been deleted %v", key)
|
|
||||||
rm.expectations.DeleteExpectations(key)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
trace.Step("ReplicationController restored")
|
|
||||||
rcNeedsSync := rm.expectations.SatisfiedExpectations(key)
|
|
||||||
trace.Step("Expectations restored")
|
|
||||||
|
|
||||||
// list all pods to include the pods that don't match the rc's selector
|
|
||||||
// anymore but has the stale controller ref.
|
|
||||||
// TODO: Do the List and Filter in a single pass, or use an index.
|
|
||||||
allPods, err := rm.podLister.Pods(rc.Namespace).List(labels.Everything())
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// Ignore inactive pods.
|
|
||||||
var filteredPods []*v1.Pod
|
|
||||||
for _, pod := range allPods {
|
|
||||||
if controller.IsPodActive(pod) {
|
|
||||||
filteredPods = append(filteredPods, pod)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 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)
|
|
||||||
// NOTE: filteredPods are pointing to objects from cache - if you need to
|
|
||||||
// modify them, you need to copy it first.
|
|
||||||
filteredPods, err = cm.ClaimPods(filteredPods)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
var manageReplicasErr error
|
|
||||||
if rcNeedsSync && rc.DeletionTimestamp == nil {
|
|
||||||
manageReplicasErr = rm.manageReplicas(filteredPods, rc)
|
|
||||||
}
|
|
||||||
trace.Step("manageReplicas done")
|
|
||||||
|
|
||||||
rc = rc.DeepCopy()
|
|
||||||
|
|
||||||
newStatus := calculateStatus(rc, filteredPods, manageReplicasErr)
|
|
||||||
|
|
||||||
// Always updates status as pods come up or die.
|
|
||||||
updatedRC, err := updateReplicationControllerStatus(rm.kubeClient.CoreV1().ReplicationControllers(rc.Namespace), *rc, newStatus)
|
|
||||||
if err != nil {
|
|
||||||
// Multiple things could lead to this update failing. Returning an error causes a requeue without forcing a hotloop
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// 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)
|
|
||||||
}
|
|
||||||
return manageReplicasErr
|
|
||||||
}
|
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -19,123 +19,10 @@ limitations under the License.
|
|||||||
package replication
|
package replication
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"reflect"
|
|
||||||
|
|
||||||
"github.com/golang/glog"
|
|
||||||
"k8s.io/api/core/v1"
|
"k8s.io/api/core/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/labels"
|
|
||||||
v1core "k8s.io/client-go/kubernetes/typed/core/v1"
|
|
||||||
podutil "k8s.io/kubernetes/pkg/api/v1/pod"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// updateReplicationControllerStatus attempts to update the Status.Replicas of the given controller, with a single GET/PUT retry.
|
|
||||||
func updateReplicationControllerStatus(c v1core.ReplicationControllerInterface, rc v1.ReplicationController, newStatus v1.ReplicationControllerStatus) (*v1.ReplicationController, error) {
|
|
||||||
// This is the steady state. It happens when the rc doesn't have any expectations, since
|
|
||||||
// we do a periodic relist every 30s. If the generations differ but the replicas are
|
|
||||||
// the same, a caller might've resized to the same replica count.
|
|
||||||
if rc.Status.Replicas == newStatus.Replicas &&
|
|
||||||
rc.Status.FullyLabeledReplicas == newStatus.FullyLabeledReplicas &&
|
|
||||||
rc.Status.ReadyReplicas == newStatus.ReadyReplicas &&
|
|
||||||
rc.Status.AvailableReplicas == newStatus.AvailableReplicas &&
|
|
||||||
rc.Generation == rc.Status.ObservedGeneration &&
|
|
||||||
reflect.DeepEqual(rc.Status.Conditions, newStatus.Conditions) {
|
|
||||||
return &rc, nil
|
|
||||||
}
|
|
||||||
// Save the generation number we acted on, otherwise we might wrongfully indicate
|
|
||||||
// that we've seen a spec update when we retry.
|
|
||||||
// TODO: This can clobber an update if we allow multiple agents to write to the
|
|
||||||
// same status.
|
|
||||||
newStatus.ObservedGeneration = rc.Generation
|
|
||||||
|
|
||||||
var getErr, updateErr error
|
|
||||||
var updatedRC *v1.ReplicationController
|
|
||||||
for i, rc := 0, &rc; ; i++ {
|
|
||||||
glog.V(4).Infof(fmt.Sprintf("Updating status for rc: %s/%s, ", rc.Namespace, rc.Name) +
|
|
||||||
fmt.Sprintf("replicas %d->%d (need %d), ", rc.Status.Replicas, newStatus.Replicas, *(rc.Spec.Replicas)) +
|
|
||||||
fmt.Sprintf("fullyLabeledReplicas %d->%d, ", rc.Status.FullyLabeledReplicas, newStatus.FullyLabeledReplicas) +
|
|
||||||
fmt.Sprintf("readyReplicas %d->%d, ", rc.Status.ReadyReplicas, newStatus.ReadyReplicas) +
|
|
||||||
fmt.Sprintf("availableReplicas %d->%d, ", rc.Status.AvailableReplicas, newStatus.AvailableReplicas) +
|
|
||||||
fmt.Sprintf("sequence No: %v->%v", rc.Status.ObservedGeneration, newStatus.ObservedGeneration))
|
|
||||||
|
|
||||||
rc.Status = newStatus
|
|
||||||
updatedRC, updateErr = c.UpdateStatus(rc)
|
|
||||||
if updateErr == nil {
|
|
||||||
return updatedRC, nil
|
|
||||||
}
|
|
||||||
// Stop retrying if we exceed statusUpdateRetries - the replicationController will be requeued with a rate limit.
|
|
||||||
if i >= statusUpdateRetries {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
// Update the controller with the latest resource version for the next poll
|
|
||||||
if rc, getErr = c.Get(rc.Name, metav1.GetOptions{}); 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 nil, getErr
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, updateErr
|
|
||||||
}
|
|
||||||
|
|
||||||
// OverlappingControllers sorts a list of controllers by creation timestamp, using their names as a tie breaker.
|
|
||||||
type OverlappingControllers []*v1.ReplicationController
|
|
||||||
|
|
||||||
func (o OverlappingControllers) Len() int { return len(o) }
|
|
||||||
func (o OverlappingControllers) Swap(i, j int) { o[i], o[j] = o[j], o[i] }
|
|
||||||
|
|
||||||
func (o OverlappingControllers) 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)
|
|
||||||
}
|
|
||||||
|
|
||||||
func calculateStatus(rc *v1.ReplicationController, filteredPods []*v1.Pod, manageReplicasErr error) v1.ReplicationControllerStatus {
|
|
||||||
newStatus := rc.Status
|
|
||||||
// Count the number of pods that have labels matching the labels of the pod
|
|
||||||
// template of the replication controller, the matching pods may have more
|
|
||||||
// labels than are in the template. Because the label of podTemplateSpec is
|
|
||||||
// a superset of the selector of the replication controller, so the possible
|
|
||||||
// matching pods must be part of the filteredPods.
|
|
||||||
fullyLabeledReplicasCount := 0
|
|
||||||
readyReplicasCount := 0
|
|
||||||
availableReplicasCount := 0
|
|
||||||
templateLabel := labels.Set(rc.Spec.Template.Labels).AsSelectorPreValidated()
|
|
||||||
for _, pod := range filteredPods {
|
|
||||||
if templateLabel.Matches(labels.Set(pod.Labels)) {
|
|
||||||
fullyLabeledReplicasCount++
|
|
||||||
}
|
|
||||||
if podutil.IsPodReady(pod) {
|
|
||||||
readyReplicasCount++
|
|
||||||
if podutil.IsPodAvailable(pod, rc.Spec.MinReadySeconds, metav1.Now()) {
|
|
||||||
availableReplicasCount++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
failureCond := GetCondition(rc.Status, v1.ReplicationControllerReplicaFailure)
|
|
||||||
if manageReplicasErr != nil && failureCond == nil {
|
|
||||||
var reason string
|
|
||||||
if diff := len(filteredPods) - int(*(rc.Spec.Replicas)); diff < 0 {
|
|
||||||
reason = "FailedCreate"
|
|
||||||
} else if diff > 0 {
|
|
||||||
reason = "FailedDelete"
|
|
||||||
}
|
|
||||||
cond := NewReplicationControllerCondition(v1.ReplicationControllerReplicaFailure, v1.ConditionTrue, reason, manageReplicasErr.Error())
|
|
||||||
SetCondition(&newStatus, cond)
|
|
||||||
} else if manageReplicasErr == nil && failureCond != nil {
|
|
||||||
RemoveCondition(&newStatus, v1.ReplicationControllerReplicaFailure)
|
|
||||||
}
|
|
||||||
|
|
||||||
newStatus.Replicas = int32(len(filteredPods))
|
|
||||||
newStatus.FullyLabeledReplicas = int32(fullyLabeledReplicasCount)
|
|
||||||
newStatus.ReadyReplicas = int32(readyReplicasCount)
|
|
||||||
newStatus.AvailableReplicas = int32(availableReplicasCount)
|
|
||||||
return newStatus
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewReplicationControllerCondition creates a new replication controller condition.
|
// NewReplicationControllerCondition creates a new replication controller condition.
|
||||||
func NewReplicationControllerCondition(condType v1.ReplicationControllerConditionType, status v1.ConditionStatus, reason, msg string) v1.ReplicationControllerCondition {
|
func NewReplicationControllerCondition(condType v1.ReplicationControllerConditionType, status v1.ConditionStatus, reason, msg string) v1.ReplicationControllerCondition {
|
||||||
return v1.ReplicationControllerCondition{
|
return v1.ReplicationControllerCondition{
|
||||||
|
184
pkg/controller/replication/replication_controller_utils_test.go
Normal file
184
pkg/controller/replication/replication_controller_utils_test.go
Normal file
@ -0,0 +1,184 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2017 The Kubernetes Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package replication
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"k8s.io/api/core/v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
imagePullBackOff v1.ReplicationControllerConditionType = "ImagePullBackOff"
|
||||||
|
|
||||||
|
condImagePullBackOff = func() v1.ReplicationControllerCondition {
|
||||||
|
return v1.ReplicationControllerCondition{
|
||||||
|
Type: imagePullBackOff,
|
||||||
|
Status: v1.ConditionTrue,
|
||||||
|
Reason: "NonExistentImage",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
condReplicaFailure = func() v1.ReplicationControllerCondition {
|
||||||
|
return v1.ReplicationControllerCondition{
|
||||||
|
Type: v1.ReplicationControllerReplicaFailure,
|
||||||
|
Status: v1.ConditionTrue,
|
||||||
|
Reason: "OtherFailure",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
condReplicaFailure2 = func() v1.ReplicationControllerCondition {
|
||||||
|
return v1.ReplicationControllerCondition{
|
||||||
|
Type: v1.ReplicationControllerReplicaFailure,
|
||||||
|
Status: v1.ConditionTrue,
|
||||||
|
Reason: "AnotherFailure",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
status = func() *v1.ReplicationControllerStatus {
|
||||||
|
return &v1.ReplicationControllerStatus{
|
||||||
|
Conditions: []v1.ReplicationControllerCondition{condReplicaFailure()},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetCondition(t *testing.T) {
|
||||||
|
exampleStatus := status()
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
|
||||||
|
status v1.ReplicationControllerStatus
|
||||||
|
condType v1.ReplicationControllerConditionType
|
||||||
|
condStatus v1.ConditionStatus
|
||||||
|
condReason string
|
||||||
|
|
||||||
|
expected bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "condition exists",
|
||||||
|
|
||||||
|
status: *exampleStatus,
|
||||||
|
condType: v1.ReplicationControllerReplicaFailure,
|
||||||
|
|
||||||
|
expected: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "condition does not exist",
|
||||||
|
|
||||||
|
status: *exampleStatus,
|
||||||
|
condType: imagePullBackOff,
|
||||||
|
|
||||||
|
expected: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
cond := GetCondition(test.status, test.condType)
|
||||||
|
exists := cond != nil
|
||||||
|
if exists != test.expected {
|
||||||
|
t.Errorf("%s: expected condition to exist: %t, got: %t", test.name, test.expected, exists)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSetCondition(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
|
||||||
|
status *v1.ReplicationControllerStatus
|
||||||
|
cond v1.ReplicationControllerCondition
|
||||||
|
|
||||||
|
expectedStatus *v1.ReplicationControllerStatus
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "set for the first time",
|
||||||
|
|
||||||
|
status: &v1.ReplicationControllerStatus{},
|
||||||
|
cond: condReplicaFailure(),
|
||||||
|
|
||||||
|
expectedStatus: &v1.ReplicationControllerStatus{Conditions: []v1.ReplicationControllerCondition{condReplicaFailure()}},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "simple set",
|
||||||
|
|
||||||
|
status: &v1.ReplicationControllerStatus{Conditions: []v1.ReplicationControllerCondition{condImagePullBackOff()}},
|
||||||
|
cond: condReplicaFailure(),
|
||||||
|
|
||||||
|
expectedStatus: &v1.ReplicationControllerStatus{Conditions: []v1.ReplicationControllerCondition{condImagePullBackOff(), condReplicaFailure()}},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "overwrite",
|
||||||
|
|
||||||
|
status: &v1.ReplicationControllerStatus{Conditions: []v1.ReplicationControllerCondition{condReplicaFailure()}},
|
||||||
|
cond: condReplicaFailure2(),
|
||||||
|
|
||||||
|
expectedStatus: &v1.ReplicationControllerStatus{Conditions: []v1.ReplicationControllerCondition{condReplicaFailure2()}},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
SetCondition(test.status, test.cond)
|
||||||
|
if !reflect.DeepEqual(test.status, test.expectedStatus) {
|
||||||
|
t.Errorf("%s: expected status: %v, got: %v", test.name, test.expectedStatus, test.status)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRemoveCondition(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
|
||||||
|
status *v1.ReplicationControllerStatus
|
||||||
|
condType v1.ReplicationControllerConditionType
|
||||||
|
|
||||||
|
expectedStatus *v1.ReplicationControllerStatus
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "remove from empty status",
|
||||||
|
|
||||||
|
status: &v1.ReplicationControllerStatus{},
|
||||||
|
condType: v1.ReplicationControllerReplicaFailure,
|
||||||
|
|
||||||
|
expectedStatus: &v1.ReplicationControllerStatus{},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "simple remove",
|
||||||
|
|
||||||
|
status: &v1.ReplicationControllerStatus{Conditions: []v1.ReplicationControllerCondition{condReplicaFailure()}},
|
||||||
|
condType: v1.ReplicationControllerReplicaFailure,
|
||||||
|
|
||||||
|
expectedStatus: &v1.ReplicationControllerStatus{},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "doesn't remove anything",
|
||||||
|
|
||||||
|
status: status(),
|
||||||
|
condType: imagePullBackOff,
|
||||||
|
|
||||||
|
expectedStatus: status(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
RemoveCondition(test.status, test.condType)
|
||||||
|
if !reflect.DeepEqual(test.status, test.expectedStatus) {
|
||||||
|
t.Errorf("%s: expected status: %v, got: %v", test.name, test.expectedStatus, test.status)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user