k3s/pkg/controller/controller_utils.go

519 lines
19 KiB
Go
Raw Normal View History

2015-04-21 20:40:35 +00:00
/*
Copyright 2014 The Kubernetes Authors All rights reserved.
2015-04-21 20:40:35 +00:00
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controller
import (
"fmt"
"sync"
2015-08-27 17:18:01 +00:00
"sync/atomic"
"time"
2015-08-27 17:18:01 +00:00
2015-08-05 22:05:17 +00:00
"github.com/golang/glog"
2015-08-05 22:03:47 +00:00
"k8s.io/kubernetes/pkg/api"
2016-01-22 05:11:30 +00:00
"k8s.io/kubernetes/pkg/api/unversioned"
2015-08-05 22:03:47 +00:00
"k8s.io/kubernetes/pkg/api/validation"
2016-02-06 02:43:02 +00:00
"k8s.io/kubernetes/pkg/apis/extensions"
"k8s.io/kubernetes/pkg/client/cache"
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
"k8s.io/kubernetes/pkg/client/record"
2015-08-05 22:03:47 +00:00
"k8s.io/kubernetes/pkg/controller/framework"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util"
2016-02-26 20:02:05 +00:00
"k8s.io/kubernetes/pkg/util/integer"
2015-04-21 20:40:35 +00:00
)
2016-02-24 22:01:48 +00:00
const CreatedByAnnotation = "kubernetes.io/created-by"
2015-07-28 01:21:37 +00:00
var (
KeyFunc = framework.DeletionHandlingMetaNamespaceKeyFunc
)
2015-04-21 20:40:35 +00:00
type ResyncPeriodFunc func() time.Duration
// Returns 0 for resyncPeriod in case resyncing is not needed.
func NoResyncPeriodFunc() time.Duration {
return 0
}
2015-11-11 21:19:39 +00:00
// StaticResyncPeriodFunc returns the resync period specified
func StaticResyncPeriodFunc(resyncPeriod time.Duration) ResyncPeriodFunc {
return func() time.Duration {
return resyncPeriod
}
}
2015-07-28 01:21:37 +00:00
// Expectations are a way for controllers to tell the controller manager what they expect. eg:
// ControllerExpectations: {
// controller1: expects 2 adds in 2 minutes
// controller2: expects 2 dels in 2 minutes
// controller3: expects -1 adds in 2 minutes => controller3's expectations have already been met
2015-04-21 20:40:35 +00:00
// }
//
// Implementation:
// ControlleeExpectation = pair of atomic counters to track controllee's creation/deletion
// ControllerExpectationsStore = TTLStore + a ControlleeExpectation per controller
2015-04-21 20:40:35 +00:00
//
// * Once set expectations can only be lowered
2015-07-28 01:21:37 +00:00
// * A controller isn't synced till its expectations are either fulfilled, or expire
// * Controllers that don't set expectations will get woken up for every matching controllee
2015-04-21 20:40:35 +00:00
// ExpKeyFunc to parse out the key from a ControlleeExpectation
2015-07-28 01:21:37 +00:00
var ExpKeyFunc = func(obj interface{}) (string, error) {
if e, ok := obj.(*ControlleeExpectations); ok {
2015-04-21 20:40:35 +00:00
return e.key, nil
}
return "", fmt.Errorf("Could not find key for obj %#v", obj)
}
2015-07-28 01:21:37 +00:00
// ControllerExpectationsInterface is an interface that allows users to set and wait on expectations.
// Only abstracted out for testing.
2015-07-28 01:21:37 +00:00
// Warning: if using KeyFunc it is not safe to use a single ControllerExpectationsInterface with different
// types of controllers, because the keys might conflict across types.
type ControllerExpectationsInterface interface {
GetExpectations(controllerKey string) (*ControlleeExpectations, bool, error)
2015-07-28 01:21:37 +00:00
SatisfiedExpectations(controllerKey string) bool
DeleteExpectations(controllerKey string)
SetExpectations(controllerKey string, add, del int) error
ExpectCreations(controllerKey string, adds int) error
ExpectDeletions(controllerKey string, dels int) error
CreationObserved(controllerKey string)
DeletionObserved(controllerKey string)
RaiseExpectations(controllerKey string, add, del int)
LowerExpectations(controllerKey string, add, del int)
}
// ControllerExpectations is a cache mapping controllers to what they expect to see before being woken up for a sync.
2015-07-28 01:21:37 +00:00
type ControllerExpectations struct {
2015-04-21 20:40:35 +00:00
cache.Store
}
// GetExpectations returns the ControlleeExpectations of the given controller.
func (r *ControllerExpectations) GetExpectations(controllerKey string) (*ControlleeExpectations, bool, error) {
if exp, exists, err := r.GetByKey(controllerKey); err == nil && exists {
return exp.(*ControlleeExpectations), true, nil
2015-04-21 20:40:35 +00:00
} else {
return nil, false, err
}
}
2015-07-28 01:21:37 +00:00
// DeleteExpectations deletes the expectations of the given controller from the TTLStore.
func (r *ControllerExpectations) DeleteExpectations(controllerKey string) {
if exp, exists, err := r.GetByKey(controllerKey); err == nil && exists {
if err := r.Delete(exp); err != nil {
2015-07-28 01:21:37 +00:00
glog.V(2).Infof("Error deleting expectations for controller %v: %v", controllerKey, err)
}
}
}
2015-07-28 01:21:37 +00:00
// SatisfiedExpectations returns true if the required adds/dels for the given controller have been observed.
// Add/del counts are established by the controller at sync time, and updated as controllees are observed by the controller
2015-07-28 01:21:37 +00:00
// manager.
func (r *ControllerExpectations) SatisfiedExpectations(controllerKey string) bool {
if exp, exists, err := r.GetExpectations(controllerKey); exists {
if exp.Fulfilled() {
2015-04-21 20:40:35 +00:00
return true
} else if exp.isExpired() {
glog.V(4).Infof("Controller expectations expired %#v", exp)
return true
2015-04-21 20:40:35 +00:00
} else {
glog.V(4).Infof("Controller still waiting on expectations %#v", exp)
2015-04-21 20:40:35 +00:00
return false
}
} else if err != nil {
glog.V(2).Infof("Error encountered while checking expectations %#v, forcing sync", err)
} else {
2015-07-28 01:21:37 +00:00
// When a new controller is created, it doesn't have expectations.
2015-04-21 20:40:35 +00:00
// When it doesn't see expected watch events for > TTL, the expectations expire.
// - In this case it wakes up, creates/deletes controllees, and sets expectations again.
// When it has satisfied expectations and no controllees need to be created/destroyed > TTL, the expectations expire.
// - In this case it continues without setting expectations till it needs to create/delete controllees.
2015-07-28 01:21:37 +00:00
glog.V(4).Infof("Controller %v either never recorded expectations, or the ttl expired.", controllerKey)
2015-04-21 20:40:35 +00:00
}
// Trigger a sync if we either encountered and error (which shouldn't happen since we're
2015-07-28 01:21:37 +00:00
// getting from local store) or this controller hasn't established expectations.
2015-04-21 20:40:35 +00:00
return true
}
// TODO: Extend ExpirationCache to support explicit expiration.
// TODO: Make this possible to disable in tests.
// TODO: Parameterize timeout.
// TODO: Support injection of clock.
func (exp *ControlleeExpectations) isExpired() bool {
return util.RealClock{}.Since(exp.timestamp) > 10*time.Second
}
2015-07-28 01:21:37 +00:00
// SetExpectations registers new expectations for the given controller. Forgets existing expectations.
func (r *ControllerExpectations) SetExpectations(controllerKey string, add, del int) error {
exp := &ControlleeExpectations{add: int64(add), del: int64(del), key: controllerKey, timestamp: util.RealClock{}.Now()}
glog.V(4).Infof("Setting expectations %+v", exp)
return r.Add(exp)
2015-04-21 20:40:35 +00:00
}
2015-07-28 01:21:37 +00:00
func (r *ControllerExpectations) ExpectCreations(controllerKey string, adds int) error {
return r.SetExpectations(controllerKey, adds, 0)
2015-04-21 20:40:35 +00:00
}
2015-07-28 01:21:37 +00:00
func (r *ControllerExpectations) ExpectDeletions(controllerKey string, dels int) error {
return r.SetExpectations(controllerKey, 0, dels)
2015-04-21 20:40:35 +00:00
}
2015-07-28 01:21:37 +00:00
// Decrements the expectation counts of the given controller.
func (r *ControllerExpectations) LowerExpectations(controllerKey string, add, del int) {
if exp, exists, err := r.GetExpectations(controllerKey); err == nil && exists {
exp.Add(int64(-add), int64(-del))
// The expectations might've been modified since the update on the previous line.
glog.V(4).Infof("Lowered expectations %+v", exp)
}
}
// Increments the expectation counts of the given controller.
func (r *ControllerExpectations) RaiseExpectations(controllerKey string, add, del int) {
if exp, exists, err := r.GetExpectations(controllerKey); err == nil && exists {
exp.Add(int64(add), int64(del))
// The expectations might've been modified since the update on the previous line.
glog.V(4).Infof("Raised expectations %+v", exp)
2015-04-21 20:40:35 +00:00
}
}
2015-07-28 01:21:37 +00:00
// CreationObserved atomically decrements the `add` expecation count of the given controller.
func (r *ControllerExpectations) CreationObserved(controllerKey string) {
r.LowerExpectations(controllerKey, 1, 0)
2015-04-21 20:40:35 +00:00
}
2015-07-28 01:21:37 +00:00
// DeletionObserved atomically decrements the `del` expectation count of the given controller.
func (r *ControllerExpectations) DeletionObserved(controllerKey string) {
r.LowerExpectations(controllerKey, 0, 1)
2015-04-21 20:40:35 +00:00
}
// Expectations are either fulfilled, or expire naturally.
type Expectations interface {
Fulfilled() bool
}
// ControlleeExpectations track controllee creates/deletes.
type ControlleeExpectations struct {
add int64
del int64
key string
timestamp time.Time
2015-04-21 20:40:35 +00:00
}
// Add increments the add and del counters.
func (e *ControlleeExpectations) Add(add, del int64) {
atomic.AddInt64(&e.add, add)
atomic.AddInt64(&e.del, del)
2015-04-21 20:40:35 +00:00
}
// Fulfilled returns true if this expectation has been fulfilled.
func (e *ControlleeExpectations) Fulfilled() bool {
2015-04-21 20:40:35 +00:00
// TODO: think about why this line being atomic doesn't matter
return atomic.LoadInt64(&e.add) <= 0 && atomic.LoadInt64(&e.del) <= 0
}
// GetExpectations returns the add and del expectations of the controllee.
func (e *ControlleeExpectations) GetExpectations() (int64, int64) {
return atomic.LoadInt64(&e.add), atomic.LoadInt64(&e.del)
}
// NewControllerExpectations returns a store for ControlleeExpectations.
2015-07-28 01:21:37 +00:00
func NewControllerExpectations() *ControllerExpectations {
2016-02-24 22:01:48 +00:00
return &ControllerExpectations{cache.NewStore(ExpKeyFunc)}
2015-04-21 20:40:35 +00:00
}
// PodControlInterface is an interface that knows how to add or delete pods
// created as an interface to allow testing.
type PodControlInterface interface {
2015-08-27 12:19:35 +00:00
// CreatePods creates new pods according to the spec.
CreatePods(namespace string, template *api.PodTemplateSpec, object runtime.Object) error
// CreatePodsOnNode creates a new pod accorting to the spec on the specified node.
CreatePodsOnNode(nodeName, namespace string, template *api.PodTemplateSpec, object runtime.Object) error
2015-07-28 01:21:37 +00:00
// DeletePod deletes the pod identified by podID.
2015-11-27 16:36:39 +00:00
DeletePod(namespace string, podID string, object runtime.Object) error
2015-04-21 20:40:35 +00:00
}
// RealPodControl is the default implementation of PodControlInterface.
2015-04-21 20:40:35 +00:00
type RealPodControl struct {
KubeClient clientset.Interface
2015-07-28 01:21:37 +00:00
Recorder record.EventRecorder
2015-04-21 20:40:35 +00:00
}
var _ PodControlInterface = &RealPodControl{}
2015-08-27 12:19:35 +00:00
func getPodsLabelSet(template *api.PodTemplateSpec) labels.Set {
2015-04-21 20:40:35 +00:00
desiredLabels := make(labels.Set)
2015-07-28 01:21:37 +00:00
for k, v := range template.Labels {
2015-04-21 20:40:35 +00:00
desiredLabels[k] = v
}
2015-07-28 01:21:37 +00:00
return desiredLabels
}
2015-08-27 12:19:35 +00:00
func getPodsAnnotationSet(template *api.PodTemplateSpec, object runtime.Object) (labels.Set, error) {
2015-04-21 20:40:35 +00:00
desiredAnnotations := make(labels.Set)
2015-07-28 01:21:37 +00:00
for k, v := range template.Annotations {
2015-04-21 20:40:35 +00:00
desiredAnnotations[k] = v
}
2015-07-28 01:21:37 +00:00
createdByRef, err := api.GetReference(object)
2015-04-21 20:40:35 +00:00
if err != nil {
2015-07-28 01:21:37 +00:00
return desiredAnnotations, fmt.Errorf("unable to get controller reference: %v", err)
2015-04-21 20:40:35 +00:00
}
2016-01-22 05:11:30 +00:00
// TODO: this code was not safe previously - as soon as new code came along that switched to v2, old clients
// would be broken upon reading it. This is explicitly hardcoded to v1 to guarantee predictable deployment.
// We need to consistently handle this case of annotation versioning.
codec := api.Codecs.LegacyCodec(unversioned.GroupVersion{Group: api.GroupName, Version: "v1"})
createdByRefJson, err := runtime.Encode(codec, &api.SerializedReference{
Reference: *createdByRef,
})
2015-04-21 20:40:35 +00:00
if err != nil {
2015-07-28 01:21:37 +00:00
return desiredAnnotations, fmt.Errorf("unable to serialize controller reference: %v", err)
2015-04-21 20:40:35 +00:00
}
desiredAnnotations[CreatedByAnnotation] = string(createdByRefJson)
2015-07-28 01:21:37 +00:00
return desiredAnnotations, nil
}
2015-04-21 20:40:35 +00:00
2015-08-27 12:19:35 +00:00
func getPodsPrefix(controllerName string) string {
2015-04-21 20:40:35 +00:00
// use the dash (if the name isn't too long) to make the pod name a bit prettier
2015-07-28 01:21:37 +00:00
prefix := fmt.Sprintf("%s-", controllerName)
2015-04-21 20:40:35 +00:00
if ok, _ := validation.ValidatePodName(prefix, true); !ok {
2015-07-28 01:21:37 +00:00
prefix = controllerName
}
return prefix
}
2015-08-27 12:19:35 +00:00
func (r RealPodControl) CreatePods(namespace string, template *api.PodTemplateSpec, object runtime.Object) error {
return r.createPods("", namespace, template, object)
}
2015-04-21 20:40:35 +00:00
2015-08-27 12:19:35 +00:00
func (r RealPodControl) CreatePodsOnNode(nodeName, namespace string, template *api.PodTemplateSpec, object runtime.Object) error {
return r.createPods(nodeName, namespace, template, object)
2015-04-21 20:40:35 +00:00
}
2015-08-27 12:19:35 +00:00
func (r RealPodControl) createPods(nodeName, namespace string, template *api.PodTemplateSpec, object runtime.Object) error {
desiredLabels := getPodsLabelSet(template)
desiredAnnotations, err := getPodsAnnotationSet(template, object)
2015-08-27 17:18:01 +00:00
if err != nil {
return err
}
2015-08-27 12:19:35 +00:00
meta, err := api.ObjectMetaFor(object)
if err != nil {
return fmt.Errorf("object does not have ObjectMeta, %v", err)
}
prefix := getPodsPrefix(meta.Name)
2015-08-27 17:18:01 +00:00
pod := &api.Pod{
ObjectMeta: api.ObjectMeta{
Labels: desiredLabels,
Annotations: desiredAnnotations,
GenerateName: prefix,
},
}
2015-08-27 12:19:35 +00:00
if err := api.Scheme.Convert(&template.Spec, &pod.Spec); err != nil {
2015-08-27 17:18:01 +00:00
return fmt.Errorf("unable to convert pod template: %v", err)
}
if len(nodeName) != 0 {
pod.Spec.NodeName = nodeName
}
2015-08-27 17:18:01 +00:00
if labels.Set(pod.Labels).AsSelector().Empty() {
2015-08-27 12:19:35 +00:00
return fmt.Errorf("unable to create pods, no labels")
2015-08-27 17:18:01 +00:00
}
2016-02-03 21:21:05 +00:00
if newPod, err := r.KubeClient.Core().Pods(namespace).Create(pod); err != nil {
r.Recorder.Eventf(object, api.EventTypeWarning, "FailedCreate", "Error creating: %v", err)
2015-08-27 12:19:35 +00:00
return fmt.Errorf("unable to create pods: %v", err)
2015-08-27 17:18:01 +00:00
} else {
2015-08-27 12:19:35 +00:00
glog.V(4).Infof("Controller %v created pod %v", meta.Name, newPod.Name)
r.Recorder.Eventf(object, api.EventTypeNormal, "SuccessfulCreate", "Created pod: %v", newPod.Name)
2015-08-27 17:18:01 +00:00
}
return nil
}
2015-11-27 16:36:39 +00:00
func (r RealPodControl) DeletePod(namespace string, podID string, object runtime.Object) error {
meta, err := api.ObjectMetaFor(object)
if err != nil {
return fmt.Errorf("object does not have ObjectMeta, %v", err)
}
2016-02-03 21:21:05 +00:00
if err := r.KubeClient.Core().Pods(namespace).Delete(podID, nil); err != nil {
2015-11-27 16:36:39 +00:00
r.Recorder.Eventf(object, api.EventTypeWarning, "FailedDelete", "Error deleting: %v", err)
return fmt.Errorf("unable to delete pods: %v", err)
} else {
glog.V(4).Infof("Controller %v deleted pod %v", meta.Name, podID)
r.Recorder.Eventf(object, api.EventTypeNormal, "SuccessfulDelete", "Deleted pod: %v", podID)
}
return nil
2015-04-21 20:40:35 +00:00
}
type FakePodControl struct {
sync.Mutex
Templates []api.PodTemplateSpec
DeletePodName []string
Err error
}
var _ PodControlInterface = &FakePodControl{}
func (f *FakePodControl) CreatePods(namespace string, spec *api.PodTemplateSpec, object runtime.Object) error {
f.Lock()
defer f.Unlock()
if f.Err != nil {
return f.Err
}
f.Templates = append(f.Templates, *spec)
return nil
}
func (f *FakePodControl) CreatePodsOnNode(nodeName, namespace string, template *api.PodTemplateSpec, object runtime.Object) error {
f.Lock()
defer f.Unlock()
if f.Err != nil {
return f.Err
}
f.Templates = append(f.Templates, *template)
return nil
}
2015-11-27 16:36:39 +00:00
func (f *FakePodControl) DeletePod(namespace string, podID string, object runtime.Object) error {
f.Lock()
defer f.Unlock()
if f.Err != nil {
return f.Err
}
2015-11-27 16:36:39 +00:00
f.DeletePodName = append(f.DeletePodName, podID)
return nil
}
func (f *FakePodControl) Clear() {
f.Lock()
defer f.Unlock()
f.DeletePodName = []string{}
f.Templates = []api.PodTemplateSpec{}
}
2015-07-28 01:21:37 +00:00
// ActivePods type allows custom sorting of pods so a controller can pick the best ones to delete.
type ActivePods []*api.Pod
2015-04-21 20:40:35 +00:00
2015-07-28 01:21:37 +00:00
func (s ActivePods) Len() int { return len(s) }
func (s ActivePods) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
2015-04-21 20:40:35 +00:00
2015-07-28 01:21:37 +00:00
func (s ActivePods) Less(i, j int) bool {
2016-02-26 20:02:05 +00:00
// 1. Unassigned < assigned
// If only one of the pods is unassigned, the unassigned one is smaller
if s[i].Spec.NodeName != s[j].Spec.NodeName && (len(s[i].Spec.NodeName) == 0 || len(s[j].Spec.NodeName) == 0) {
return len(s[i].Spec.NodeName) == 0
2015-04-21 20:40:35 +00:00
}
2016-02-26 20:02:05 +00:00
// 2. PodPending < PodUnknown < PodRunning
2015-04-21 20:40:35 +00:00
m := map[api.PodPhase]int{api.PodPending: 0, api.PodUnknown: 1, api.PodRunning: 2}
if m[s[i].Status.Phase] != m[s[j].Status.Phase] {
return m[s[i].Status.Phase] < m[s[j].Status.Phase]
}
2016-02-26 20:02:05 +00:00
// 3. Not ready < ready
// If only one of the pods is not ready, the not ready one is smaller
if api.IsPodReady(s[i]) != api.IsPodReady(s[j]) {
return !api.IsPodReady(s[i])
}
// TODO: take availability into account when we push minReadySeconds information from deployment into pods,
// see https://github.com/kubernetes/kubernetes/issues/22065
// 4. Been ready for less time < more time
// If both pods are ready, the latest ready one is smaller
if api.IsPodReady(s[i]) && api.IsPodReady(s[j]) && !podReadyTime(s[i]).Equal(podReadyTime(s[j])) {
return podReadyTime(s[i]).After(podReadyTime(s[j]).Time)
}
// 5. Pods with containers with higher restart counts < lower restart counts
if maxContainerRestarts(s[i]) != maxContainerRestarts(s[j]) {
return maxContainerRestarts(s[i]) > maxContainerRestarts(s[j])
}
// 6. Newer pods < older pods
if !s[i].CreationTimestamp.Equal(s[j].CreationTimestamp) {
return s[i].CreationTimestamp.After(s[j].CreationTimestamp.Time)
2015-04-21 20:40:35 +00:00
}
return false
}
2016-02-26 20:02:05 +00:00
func podReadyTime(pod *api.Pod) unversioned.Time {
if api.IsPodReady(pod) {
for _, c := range pod.Status.Conditions {
// we only care about pod ready conditions
if c.Type == api.PodReady && c.Status == api.ConditionTrue {
return c.LastTransitionTime
}
}
}
return unversioned.Time{}
}
func maxContainerRestarts(pod *api.Pod) int {
maxRestarts := 0
for _, c := range pod.Status.ContainerStatuses {
maxRestarts = integer.IntMax(maxRestarts, c.RestartCount)
}
return maxRestarts
}
2015-07-28 01:21:37 +00:00
// FilterActivePods returns pods that have not terminated.
func FilterActivePods(pods []api.Pod) []*api.Pod {
2015-04-21 20:40:35 +00:00
var result []*api.Pod
for i := range pods {
p := pods[i]
if api.PodSucceeded != p.Status.Phase &&
api.PodFailed != p.Status.Phase &&
p.DeletionTimestamp == nil {
result = append(result, &p)
} else {
glog.V(4).Infof("Ignoring inactive pod %v/%v in state %v, deletion time %v",
p.Namespace, p.Name, p.Status.Phase, p.DeletionTimestamp)
2015-04-21 20:40:35 +00:00
}
}
return result
}
// FilterActiveReplicaSets returns replica sets that have (or at least ought to have) pods.
func FilterActiveReplicaSets(replicaSets []*extensions.ReplicaSet) []*extensions.ReplicaSet {
active := []*extensions.ReplicaSet{}
for i := range replicaSets {
if replicaSets[i].Spec.Replicas > 0 {
active = append(active, replicaSets[i])
}
}
return active
}
2016-01-28 06:13:07 +00:00
// ControllersByCreationTimestamp sorts a list of ReplicationControllers by creation timestamp, using their names as a tie breaker.
type ControllersByCreationTimestamp []*api.ReplicationController
func (o ControllersByCreationTimestamp) Len() int { return len(o) }
func (o ControllersByCreationTimestamp) Swap(i, j int) { o[i], o[j] = o[j], o[i] }
func (o ControllersByCreationTimestamp) 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)
}
2016-02-06 02:43:02 +00:00
// ReplicaSetsByCreationTimestamp sorts a list of ReplicationSets by creation timestamp, using their names as a tie breaker.
type ReplicaSetsByCreationTimestamp []*extensions.ReplicaSet
func (o ReplicaSetsByCreationTimestamp) Len() int { return len(o) }
func (o ReplicaSetsByCreationTimestamp) Swap(i, j int) { o[i], o[j] = o[j], o[i] }
func (o ReplicaSetsByCreationTimestamp) 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)
}