mirror of
https://github.com/ceph/ceph-csi.git
synced 2025-06-13 18:43:34 +00:00
vendor files
This commit is contained in:
91
vendor/k8s.io/kubernetes/pkg/controller/replicaset/BUILD
generated
vendored
Normal file
91
vendor/k8s.io/kubernetes/pkg/controller/replicaset/BUILD
generated
vendored
Normal file
@ -0,0 +1,91 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
"go_test",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"doc.go",
|
||||
"replica_set.go",
|
||||
"replica_set_utils.go",
|
||||
],
|
||||
importpath = "k8s.io/kubernetes/pkg/controller/replicaset",
|
||||
deps = [
|
||||
"//pkg/api/v1/pod:go_default_library",
|
||||
"//pkg/controller:go_default_library",
|
||||
"//pkg/util/metrics:go_default_library",
|
||||
"//vendor/github.com/golang/glog:go_default_library",
|
||||
"//vendor/k8s.io/api/core/v1:go_default_library",
|
||||
"//vendor/k8s.io/api/extensions/v1beta1: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/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/wait:go_default_library",
|
||||
"//vendor/k8s.io/client-go/informers/core/v1:go_default_library",
|
||||
"//vendor/k8s.io/client-go/informers/extensions/v1beta1: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/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/extensions/v1beta1: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/util/integer:go_default_library",
|
||||
"//vendor/k8s.io/client-go/util/workqueue:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = [
|
||||
"replica_set_test.go",
|
||||
"replica_set_utils_test.go",
|
||||
],
|
||||
importpath = "k8s.io/kubernetes/pkg/controller/replicaset",
|
||||
library = ":go_default_library",
|
||||
deps = [
|
||||
"//pkg/api/legacyscheme: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/api/extensions/v1beta1: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/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(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [
|
||||
":package-srcs",
|
||||
"//pkg/controller/replicaset/options:all-srcs",
|
||||
],
|
||||
tags = ["automanaged"],
|
||||
)
|
9
vendor/k8s.io/kubernetes/pkg/controller/replicaset/OWNERS
generated
vendored
Executable file
9
vendor/k8s.io/kubernetes/pkg/controller/replicaset/OWNERS
generated
vendored
Executable file
@ -0,0 +1,9 @@
|
||||
approvers:
|
||||
- caesarxuchao
|
||||
- lavalamp
|
||||
- enisoc
|
||||
reviewers:
|
||||
- caesarxuchao
|
||||
- lavalamp
|
||||
- tnozicka
|
||||
- enisoc
|
19
vendor/k8s.io/kubernetes/pkg/controller/replicaset/doc.go
generated
vendored
Normal file
19
vendor/k8s.io/kubernetes/pkg/controller/replicaset/doc.go
generated
vendored
Normal file
@ -0,0 +1,19 @@
|
||||
/*
|
||||
Copyright 2016 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 replicaset contains logic for watching and synchronizing
|
||||
// ReplicaSets.
|
||||
package replicaset // import "k8s.io/kubernetes/pkg/controller/replicaset"
|
26
vendor/k8s.io/kubernetes/pkg/controller/replicaset/options/BUILD
generated
vendored
Normal file
26
vendor/k8s.io/kubernetes/pkg/controller/replicaset/options/BUILD
generated
vendored
Normal file
@ -0,0 +1,26 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["options.go"],
|
||||
importpath = "k8s.io/kubernetes/pkg/controller/replicaset/options",
|
||||
deps = ["//vendor/github.com/spf13/pflag:go_default_library"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
)
|
35
vendor/k8s.io/kubernetes/pkg/controller/replicaset/options/options.go
generated
vendored
Normal file
35
vendor/k8s.io/kubernetes/pkg/controller/replicaset/options/options.go
generated
vendored
Normal file
@ -0,0 +1,35 @@
|
||||
/*
|
||||
Copyright 2016 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 options
|
||||
|
||||
import (
|
||||
"github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
type ReplicasetControllerOptions struct {
|
||||
ConcurrentRSSyncs int
|
||||
}
|
||||
|
||||
func NewReplicasetControllerOptions() ReplicasetControllerOptions {
|
||||
return ReplicasetControllerOptions{
|
||||
ConcurrentRSSyncs: 5,
|
||||
}
|
||||
}
|
||||
|
||||
func (o *ReplicasetControllerOptions) AddFlags(fs *pflag.FlagSet) {
|
||||
fs.IntVar(&o.ConcurrentRSSyncs, "concurrent-replicaset-syncs", o.ConcurrentRSSyncs, "The number of replicasets that are allowed to sync concurrently. Larger number = more reponsive replica management, but more CPU (and network) load")
|
||||
}
|
717
vendor/k8s.io/kubernetes/pkg/controller/replicaset/replica_set.go
generated
vendored
Normal file
717
vendor/k8s.io/kubernetes/pkg/controller/replicaset/replica_set.go
generated
vendored
Normal file
@ -0,0 +1,717 @@
|
||||
/*
|
||||
Copyright 2016 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.
|
||||
*/
|
||||
|
||||
// ### 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
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/golang/glog"
|
||||
"k8s.io/api/core/v1"
|
||||
"k8s.io/api/extensions/v1beta1"
|
||||
extensions "k8s.io/api/extensions/v1beta1"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||
"k8s.io/apimachinery/pkg/util/wait"
|
||||
coreinformers "k8s.io/client-go/informers/core/v1"
|
||||
extensionsinformers "k8s.io/client-go/informers/extensions/v1beta1"
|
||||
clientset "k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/kubernetes/scheme"
|
||||
v1core "k8s.io/client-go/kubernetes/typed/core/v1"
|
||||
corelisters "k8s.io/client-go/listers/core/v1"
|
||||
extensionslisters "k8s.io/client-go/listers/extensions/v1beta1"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
"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/util/metrics"
|
||||
)
|
||||
|
||||
const (
|
||||
// Realistic value of the burstReplica field for the replica set manager based off
|
||||
// performance requirements for kubernetes 1.0.
|
||||
BurstReplicas = 500
|
||||
|
||||
// The number of times we retry updating a ReplicaSet's status.
|
||||
statusUpdateRetries = 1
|
||||
)
|
||||
|
||||
// ReplicaSetController is responsible for synchronizing ReplicaSet objects stored
|
||||
// in the system with actual running pods.
|
||||
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
|
||||
podControl controller.PodControlInterface
|
||||
|
||||
// A ReplicaSet 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 syncReplicaSet for testing.
|
||||
syncHandler func(rsKey string) error
|
||||
|
||||
// A TTLCache of pod creates/deletes each rc expects to see.
|
||||
expectations *controller.UIDTrackingControllerExpectations
|
||||
|
||||
// A store of ReplicaSets, populated by the shared informer passed to NewReplicaSetController
|
||||
rsLister extensionslisters.ReplicaSetLister
|
||||
// rsListerSynced returns true if the pod store has been synced at least once.
|
||||
// Added as a member to the struct to allow injection for testing.
|
||||
rsListerSynced cache.InformerSynced
|
||||
|
||||
// A store of pods, populated by the shared informer passed to NewReplicaSetController
|
||||
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
|
||||
}
|
||||
|
||||
// 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 {
|
||||
eventBroadcaster := record.NewBroadcaster()
|
||||
eventBroadcaster.StartLogging(glog.Infof)
|
||||
eventBroadcaster.StartRecordingToSink(&v1core.EventSinkImpl{Interface: v1core.New(kubeClient.CoreV1().RESTClient()).Events("")})
|
||||
return NewBaseController(rsInformer, podInformer, kubeClient, burstReplicas,
|
||||
v1beta1.SchemeGroupVersion.WithKind("ReplicaSet"),
|
||||
"replicaset_controller",
|
||||
"replicaset",
|
||||
controller.RealPodControl{
|
||||
KubeClient: kubeClient,
|
||||
Recorder: eventBroadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: "replicaset-controller"}),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// 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{
|
||||
AddFunc: rsc.enqueueReplicaSet,
|
||||
UpdateFunc: rsc.updateRS,
|
||||
// This will enter the sync loop and no-op, because the replica set has been deleted from the store.
|
||||
// Note that deleting a replica set immediately after scaling it to 0 will not work. The recommended
|
||||
// way of achieving this is by performing a `stop` operation on the replica set.
|
||||
DeleteFunc: rsc.enqueueReplicaSet,
|
||||
})
|
||||
rsc.rsLister = rsInformer.Lister()
|
||||
rsc.rsListerSynced = rsInformer.Informer().HasSynced
|
||||
|
||||
podInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
|
||||
AddFunc: rsc.addPod,
|
||||
// This invokes the ReplicaSet for every pod change, eg: host assignment. Though this might seem like
|
||||
// overkill the most frequent pod update is status, and the associated ReplicaSet will only list from
|
||||
// local storage, so it should be ok.
|
||||
UpdateFunc: rsc.updatePod,
|
||||
DeleteFunc: rsc.deletePod,
|
||||
})
|
||||
rsc.podLister = podInformer.Lister()
|
||||
rsc.podListerSynced = podInformer.Informer().HasSynced
|
||||
|
||||
rsc.syncHandler = rsc.syncReplicaSet
|
||||
|
||||
return rsc
|
||||
}
|
||||
|
||||
// SetEventRecorder replaces the event recorder used by the ReplicaSetController
|
||||
// with the given recorder. Only used for testing.
|
||||
func (rsc *ReplicaSetController) SetEventRecorder(recorder record.EventRecorder) {
|
||||
// TODO: Hack. We can't cleanly shutdown the event recorder, so benchmarks
|
||||
// need to pass in a fake.
|
||||
rsc.podControl = controller.RealPodControl{KubeClient: rsc.kubeClient, Recorder: recorder}
|
||||
}
|
||||
|
||||
// Run begins watching and syncing.
|
||||
func (rsc *ReplicaSetController) Run(workers int, stopCh <-chan struct{}) {
|
||||
defer utilruntime.HandleCrash()
|
||||
defer rsc.queue.ShutDown()
|
||||
|
||||
controllerName := strings.ToLower(rsc.Kind)
|
||||
glog.Infof("Starting %v controller", controllerName)
|
||||
defer glog.Infof("Shutting down %v controller", controllerName)
|
||||
|
||||
if !controller.WaitForCacheSync(rsc.Kind, stopCh, rsc.podListerSynced, rsc.rsListerSynced) {
|
||||
return
|
||||
}
|
||||
|
||||
for i := 0; i < workers; i++ {
|
||||
go wait.Until(rsc.worker, time.Second, stopCh)
|
||||
}
|
||||
|
||||
<-stopCh
|
||||
}
|
||||
|
||||
// getPodReplicaSets returns a list of ReplicaSets matching the given pod.
|
||||
func (rsc *ReplicaSetController) getPodReplicaSets(pod *v1.Pod) []*extensions.ReplicaSet {
|
||||
rss, err := rsc.rsLister.GetPodReplicaSets(pod)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
if len(rss) > 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 %v is selecting pods with labels: %+v", rsc.Kind, pod.Labels))
|
||||
}
|
||||
return rss
|
||||
}
|
||||
|
||||
// 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 (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.
|
||||
// Don't even try to look up by Name if it's the wrong Kind.
|
||||
if controllerRef.Kind != rsc.Kind {
|
||||
return nil
|
||||
}
|
||||
rs, err := rsc.rsLister.ReplicaSets(namespace).Get(controllerRef.Name)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
if rs.UID != controllerRef.UID {
|
||||
// The controller we found with this Name is not the same one that the
|
||||
// ControllerRef points to.
|
||||
return nil
|
||||
}
|
||||
return rs
|
||||
}
|
||||
|
||||
// callback when RS is updated
|
||||
func (rsc *ReplicaSetController) updateRS(old, cur interface{}) {
|
||||
oldRS := old.(*extensions.ReplicaSet)
|
||||
curRS := cur.(*extensions.ReplicaSet)
|
||||
|
||||
// You might imagine that we only really need to enqueue the
|
||||
// replica set when Spec changes, but it is safer to sync any
|
||||
// time this function is triggered. That way a full informer
|
||||
// resync can requeue any replica set 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
|
||||
// replica sets 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 ReplicaSets that haven't met expectations yet won't
|
||||
// sync, and all the listing is done using local stores.
|
||||
if *(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)
|
||||
}
|
||||
|
||||
// When a pod is created, enqueue the replica set that manages it and update its expectations.
|
||||
func (rsc *ReplicaSetController) 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.
|
||||
rsc.deletePod(pod)
|
||||
return
|
||||
}
|
||||
|
||||
// If it has a ControllerRef, that's all that matters.
|
||||
if controllerRef := metav1.GetControllerOf(pod); controllerRef != nil {
|
||||
rs := rsc.resolveControllerRef(pod.Namespace, controllerRef)
|
||||
if rs == nil {
|
||||
return
|
||||
}
|
||||
rsKey, err := controller.KeyFunc(rs)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
glog.V(4).Infof("Pod %s created: %#v.", pod.Name, pod)
|
||||
rsc.expectations.CreationObserved(rsKey)
|
||||
rsc.enqueueReplicaSet(rs)
|
||||
return
|
||||
}
|
||||
|
||||
// Otherwise, it's an orphan. Get a list of all matching ReplicaSets and sync
|
||||
// them to see if anyone wants to adopt it.
|
||||
// DO NOT observe creation because no controller should be waiting for an
|
||||
// orphan.
|
||||
rss := rsc.getPodReplicaSets(pod)
|
||||
if len(rss) == 0 {
|
||||
return
|
||||
}
|
||||
glog.V(4).Infof("Orphan Pod %s created: %#v.", pod.Name, pod)
|
||||
for _, rs := range rss {
|
||||
rsc.enqueueReplicaSet(rs)
|
||||
}
|
||||
}
|
||||
|
||||
// When a pod is updated, figure out what replica set/s manage it and wake them
|
||||
// up. If the labels of the pod have changed we need to awaken both the old
|
||||
// and new replica set. old and cur must be *v1.Pod types.
|
||||
func (rsc *ReplicaSetController) 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 rs 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 rs never initiates a phase change, and so is never asleep waiting for the same.
|
||||
rsc.deletePod(curPod)
|
||||
if labelChanged {
|
||||
// we don't need to check the oldPod.DeletionTimestamp because DeletionTimestamp cannot be unset.
|
||||
rsc.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 rs := rsc.resolveControllerRef(oldPod.Namespace, oldControllerRef); rs != nil {
|
||||
rsc.enqueueReplicaSet(rs)
|
||||
}
|
||||
}
|
||||
|
||||
// If it has a ControllerRef, that's all that matters.
|
||||
if curControllerRef != nil {
|
||||
rs := rsc.resolveControllerRef(curPod.Namespace, curControllerRef)
|
||||
if rs == nil {
|
||||
return
|
||||
}
|
||||
glog.V(4).Infof("Pod %s updated, objectMeta %+v -> %+v.", curPod.Name, oldPod.ObjectMeta, curPod.ObjectMeta)
|
||||
rsc.enqueueReplicaSet(rs)
|
||||
// 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 replica set 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 replica set controller).
|
||||
if !podutil.IsPodReady(oldPod) && podutil.IsPodReady(curPod) && rs.Spec.MinReadySeconds > 0 {
|
||||
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.
|
||||
// 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)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Otherwise, it's an orphan. If anything changed, sync matching controllers
|
||||
// to see if anyone wants to adopt it now.
|
||||
if labelChanged || controllerRefChanged {
|
||||
rss := rsc.getPodReplicaSets(curPod)
|
||||
if len(rss) == 0 {
|
||||
return
|
||||
}
|
||||
glog.V(4).Infof("Orphan Pod %s updated, objectMeta %+v -> %+v.", curPod.Name, oldPod.ObjectMeta, curPod.ObjectMeta)
|
||||
for _, rs := range rss {
|
||||
rsc.enqueueReplicaSet(rs)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// When a pod is deleted, enqueue the replica set that manages the pod and update its expectations.
|
||||
// obj could be an *v1.Pod, or a DeletionFinalStateUnknown marker item.
|
||||
func (rsc *ReplicaSetController) 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 ReplicaSet 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
|
||||
}
|
||||
rs := rsc.resolveControllerRef(pod.Namespace, controllerRef)
|
||||
if rs == nil {
|
||||
return
|
||||
}
|
||||
rsKey, err := controller.KeyFunc(rs)
|
||||
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)
|
||||
rsc.expectations.DeletionObserved(rsKey, controller.PodKey(pod))
|
||||
rsc.enqueueReplicaSet(rs)
|
||||
}
|
||||
|
||||
// obj could be an *extensions.ReplicaSet, or a DeletionFinalStateUnknown marker item.
|
||||
func (rsc *ReplicaSetController) enqueueReplicaSet(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
|
||||
}
|
||||
rsc.queue.Add(key)
|
||||
}
|
||||
|
||||
// obj could be an *extensions.ReplicaSet, or a DeletionFinalStateUnknown marker item.
|
||||
func (rsc *ReplicaSetController) enqueueReplicaSetAfter(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
|
||||
}
|
||||
rsc.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 (rsc *ReplicaSetController) worker() {
|
||||
for rsc.processNextWorkItem() {
|
||||
}
|
||||
}
|
||||
|
||||
func (rsc *ReplicaSetController) processNextWorkItem() bool {
|
||||
key, quit := rsc.queue.Get()
|
||||
if quit {
|
||||
return false
|
||||
}
|
||||
defer rsc.queue.Done(key)
|
||||
|
||||
err := rsc.syncHandler(key.(string))
|
||||
if err == nil {
|
||||
rsc.queue.Forget(key)
|
||||
return true
|
||||
}
|
||||
|
||||
utilruntime.HandleError(fmt.Errorf("Sync %q failed with %v", key, err))
|
||||
rsc.queue.AddRateLimited(key)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// manageReplicas checks and updates replicas for the given ReplicaSet.
|
||||
// Does NOT modify <filteredPods>.
|
||||
// It will requeue the replica set in case of an error while creating/deleting pods.
|
||||
func (rsc *ReplicaSetController) manageReplicas(filteredPods []*v1.Pod, rs *extensions.ReplicaSet) error {
|
||||
diff := len(filteredPods) - int(*(rs.Spec.Replicas))
|
||||
rsKey, err := controller.KeyFunc(rs)
|
||||
if err != nil {
|
||||
utilruntime.HandleError(fmt.Errorf("Couldn't get key for %v %#v: %v", rsc.Kind, rs, err))
|
||||
return nil
|
||||
}
|
||||
if diff < 0 {
|
||||
diff *= -1
|
||||
if diff > rsc.burstReplicas {
|
||||
diff = rsc.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.
|
||||
rsc.expectations.ExpectCreations(rsKey, 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
|
||||
// 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.
|
||||
successfulCreations, err := slowStartBatch(diff, controller.SlowStartInitialBatchSize, func() error {
|
||||
boolPtr := func(b bool) *bool { return &b }
|
||||
controllerRef := &metav1.OwnerReference{
|
||||
APIVersion: rsc.GroupVersion().String(),
|
||||
Kind: rsc.Kind,
|
||||
Name: rs.Name,
|
||||
UID: rs.UID,
|
||||
BlockOwnerDeletion: boolPtr(true),
|
||||
Controller: boolPtr(true),
|
||||
}
|
||||
err := rsc.podControl.CreatePodsWithControllerRef(rs.Namespace, &rs.Spec.Template, rs, 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 nil
|
||||
}
|
||||
return err
|
||||
})
|
||||
|
||||
// Any skipped pods that we never attempted to start shouldn't be expected.
|
||||
// The skipped pods will be retried later. The next controller resync will
|
||||
// retry the slow start process.
|
||||
if skippedPods := diff - successfulCreations; skippedPods > 0 {
|
||||
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++ {
|
||||
// Decrement the expected number of creates because the informer won't observe this pod
|
||||
rsc.expectations.CreationObserved(rsKey)
|
||||
}
|
||||
}
|
||||
return err
|
||||
} else if diff > 0 {
|
||||
if diff > rsc.burstReplicas {
|
||||
diff = rsc.burstReplicas
|
||||
}
|
||||
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.
|
||||
podsToDelete := getPodsToDelete(filteredPods, diff)
|
||||
|
||||
// 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/rs 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.
|
||||
rsc.expectations.ExpectDeletions(rsKey, getPodKeys(podsToDelete))
|
||||
|
||||
errCh := make(chan error, diff)
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(diff)
|
||||
for _, pod := range podsToDelete {
|
||||
go func(targetPod *v1.Pod) {
|
||||
defer wg.Done()
|
||||
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
|
||||
podKey := controller.PodKey(targetPod)
|
||||
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)
|
||||
errCh <- err
|
||||
}
|
||||
}(pod)
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
// syncReplicaSet will sync the ReplicaSet 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 (rsc *ReplicaSetController) syncReplicaSet(key string) error {
|
||||
|
||||
startTime := time.Now()
|
||||
defer func() {
|
||||
glog.V(4).Infof("Finished syncing %v %q (%v)", rsc.Kind, key, time.Since(startTime))
|
||||
}()
|
||||
|
||||
namespace, name, err := cache.SplitMetaNamespaceKey(key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rs, err := rsc.rsLister.ReplicaSets(namespace).Get(name)
|
||||
if errors.IsNotFound(err) {
|
||||
glog.V(4).Infof("%v %v has been deleted", rsc.Kind, key)
|
||||
rsc.expectations.DeleteExpectations(key)
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rsNeedsSync := rsc.expectations.SatisfiedExpectations(key)
|
||||
selector, err := metav1.LabelSelectorAsSelector(rs.Spec.Selector)
|
||||
if err != nil {
|
||||
utilruntime.HandleError(fmt.Errorf("Error converting pod selector to selector: %v", err))
|
||||
return nil
|
||||
}
|
||||
|
||||
// list all pods to include the pods that don't match the rs`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 := rsc.podLister.Pods(rs.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)
|
||||
}
|
||||
}
|
||||
|
||||
// NOTE: filteredPods are pointing to objects from cache - if you need to
|
||||
// modify them, you need to copy it first.
|
||||
filteredPods, err = rsc.claimPods(rs, selector, filteredPods)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var manageReplicasErr error
|
||||
if rsNeedsSync && rs.DeletionTimestamp == nil {
|
||||
manageReplicasErr = rsc.manageReplicas(filteredPods, rs)
|
||||
}
|
||||
rs = rs.DeepCopy()
|
||||
newStatus := calculateStatus(rs, filteredPods, manageReplicasErr)
|
||||
|
||||
// Always updates status as pods come up or die.
|
||||
updatedRS, err := updateReplicaSetStatus(rsc.kubeClient.ExtensionsV1beta1().ReplicaSets(rs.Namespace), rs, newStatus)
|
||||
if err != nil {
|
||||
// Multiple things could lead to this update failing. Requeuing the replica set ensures
|
||||
// Returning an error causes a requeue without forcing a hotloop
|
||||
return err
|
||||
}
|
||||
// Resync the ReplicaSet after MinReadySeconds as a last line of defense to guard against clock-skew.
|
||||
if manageReplicasErr == nil && updatedRS.Spec.MinReadySeconds > 0 &&
|
||||
updatedRS.Status.ReadyReplicas == *(updatedRS.Spec.Replicas) &&
|
||||
updatedRS.Status.AvailableReplicas != *(updatedRS.Spec.Replicas) {
|
||||
rsc.enqueueReplicaSetAfter(updatedRS, time.Duration(updatedRS.Spec.MinReadySeconds)*time.Second)
|
||||
}
|
||||
return manageReplicasErr
|
||||
}
|
||||
|
||||
func (rsc *ReplicaSetController) claimPods(rs *extensions.ReplicaSet, selector labels.Selector, filteredPods []*v1.Pod) ([]*v1.Pod, error) {
|
||||
// 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 := rsc.kubeClient.ExtensionsV1beta1().ReplicaSets(rs.Namespace).Get(rs.Name, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if 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
|
||||
})
|
||||
cm := controller.NewPodControllerRefManager(rsc.podControl, rs, selector, rsc.GroupVersionKind, canAdoptFunc)
|
||||
return cm.ClaimPods(filteredPods)
|
||||
}
|
||||
|
||||
// slowStartBatch tries to call the provided function a total of 'count' times,
|
||||
// starting slow to check for errors, then speeding up if calls succeed.
|
||||
//
|
||||
// It groups the calls into batches, starting with a group of initialBatchSize.
|
||||
// Within each batch, it may call the function multiple times concurrently.
|
||||
//
|
||||
// If a whole batch succeeds, the next batch may get exponentially larger.
|
||||
// If there are any failures in a batch, all remaining batches are skipped
|
||||
// after waiting for the current batch to complete.
|
||||
//
|
||||
// It returns the number of successful calls to the function.
|
||||
func slowStartBatch(count int, initialBatchSize int, fn func() error) (int, error) {
|
||||
remaining := count
|
||||
successes := 0
|
||||
for batchSize := integer.IntMin(remaining, initialBatchSize); batchSize > 0; batchSize = integer.IntMin(2*batchSize, remaining) {
|
||||
errCh := make(chan error, batchSize)
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(batchSize)
|
||||
for i := 0; i < batchSize; i++ {
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
if err := fn(); err != nil {
|
||||
errCh <- err
|
||||
}
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
curSuccesses := batchSize - len(errCh)
|
||||
successes += curSuccesses
|
||||
if len(errCh) > 0 {
|
||||
return successes, <-errCh
|
||||
}
|
||||
remaining -= batchSize
|
||||
}
|
||||
return successes, nil
|
||||
}
|
||||
|
||||
func getPodsToDelete(filteredPods []*v1.Pod, diff int) []*v1.Pod {
|
||||
// No need to sort pods if we are about to delete all of them.
|
||||
// diff will always be <= len(filteredPods), so not need to handle > case.
|
||||
if diff < len(filteredPods) {
|
||||
// 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))
|
||||
}
|
||||
return filteredPods[:diff]
|
||||
}
|
||||
|
||||
func getPodKeys(pods []*v1.Pod) []string {
|
||||
podKeys := make([]string, 0, len(pods))
|
||||
for _, pod := range pods {
|
||||
podKeys = append(podKeys, controller.PodKey(pod))
|
||||
}
|
||||
return podKeys
|
||||
}
|
1625
vendor/k8s.io/kubernetes/pkg/controller/replicaset/replica_set_test.go
generated
vendored
Normal file
1625
vendor/k8s.io/kubernetes/pkg/controller/replicaset/replica_set_test.go
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
176
vendor/k8s.io/kubernetes/pkg/controller/replicaset/replica_set_utils.go
generated
vendored
Normal file
176
vendor/k8s.io/kubernetes/pkg/controller/replicaset/replica_set_utils.go
generated
vendored
Normal file
@ -0,0 +1,176 @@
|
||||
/*
|
||||
Copyright 2016 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.
|
||||
*/
|
||||
|
||||
// If you make changes to this file, you should also make the corresponding change in ReplicationController.
|
||||
|
||||
package replicaset
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"github.com/golang/glog"
|
||||
|
||||
"k8s.io/api/core/v1"
|
||||
extensions "k8s.io/api/extensions/v1beta1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
unversionedextensions "k8s.io/client-go/kubernetes/typed/extensions/v1beta1"
|
||||
podutil "k8s.io/kubernetes/pkg/api/v1/pod"
|
||||
)
|
||||
|
||||
// updateReplicaSetStatus attempts to update the Status.Replicas of the given ReplicaSet, with a single GET/PUT retry.
|
||||
func updateReplicaSetStatus(c unversionedextensions.ReplicaSetInterface, rs *extensions.ReplicaSet, newStatus extensions.ReplicaSetStatus) (*extensions.ReplicaSet, error) {
|
||||
// This is the steady state. It happens when the ReplicaSet 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 rs.Status.Replicas == newStatus.Replicas &&
|
||||
rs.Status.FullyLabeledReplicas == newStatus.FullyLabeledReplicas &&
|
||||
rs.Status.ReadyReplicas == newStatus.ReadyReplicas &&
|
||||
rs.Status.AvailableReplicas == newStatus.AvailableReplicas &&
|
||||
rs.Generation == rs.Status.ObservedGeneration &&
|
||||
reflect.DeepEqual(rs.Status.Conditions, newStatus.Conditions) {
|
||||
return rs, 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 = rs.Generation
|
||||
|
||||
var getErr, updateErr error
|
||||
var updatedRS *extensions.ReplicaSet
|
||||
for i, rs := 0, rs; ; i++ {
|
||||
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("fullyLabeledReplicas %d->%d, ", rs.Status.FullyLabeledReplicas, newStatus.FullyLabeledReplicas) +
|
||||
fmt.Sprintf("readyReplicas %d->%d, ", rs.Status.ReadyReplicas, newStatus.ReadyReplicas) +
|
||||
fmt.Sprintf("availableReplicas %d->%d, ", rs.Status.AvailableReplicas, newStatus.AvailableReplicas) +
|
||||
fmt.Sprintf("sequence No: %v->%v", rs.Status.ObservedGeneration, newStatus.ObservedGeneration))
|
||||
|
||||
rs.Status = newStatus
|
||||
updatedRS, updateErr = c.UpdateStatus(rs)
|
||||
if updateErr == nil {
|
||||
return updatedRS, nil
|
||||
}
|
||||
// Stop retrying if we exceed statusUpdateRetries - the replicaSet will be requeued with a rate limit.
|
||||
if i >= statusUpdateRetries {
|
||||
break
|
||||
}
|
||||
// Update the ReplicaSet with the latest resource version for the next poll
|
||||
if rs, getErr = c.Get(rs.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
|
||||
}
|
||||
|
||||
func calculateStatus(rs *extensions.ReplicaSet, filteredPods []*v1.Pod, manageReplicasErr error) extensions.ReplicaSetStatus {
|
||||
newStatus := rs.Status
|
||||
// Count the number of pods that have labels matching the labels of the pod
|
||||
// template of the replica set, 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 replica set, so the possible
|
||||
// matching pods must be part of the filteredPods.
|
||||
fullyLabeledReplicasCount := 0
|
||||
readyReplicasCount := 0
|
||||
availableReplicasCount := 0
|
||||
templateLabel := labels.Set(rs.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, rs.Spec.MinReadySeconds, metav1.Now()) {
|
||||
availableReplicasCount++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
failureCond := GetCondition(rs.Status, extensions.ReplicaSetReplicaFailure)
|
||||
if manageReplicasErr != nil && failureCond == nil {
|
||||
var reason string
|
||||
if diff := len(filteredPods) - int(*(rs.Spec.Replicas)); diff < 0 {
|
||||
reason = "FailedCreate"
|
||||
} else if diff > 0 {
|
||||
reason = "FailedDelete"
|
||||
}
|
||||
cond := NewReplicaSetCondition(extensions.ReplicaSetReplicaFailure, v1.ConditionTrue, reason, manageReplicasErr.Error())
|
||||
SetCondition(&newStatus, cond)
|
||||
} else if manageReplicasErr == nil && failureCond != nil {
|
||||
RemoveCondition(&newStatus, extensions.ReplicaSetReplicaFailure)
|
||||
}
|
||||
|
||||
newStatus.Replicas = int32(len(filteredPods))
|
||||
newStatus.FullyLabeledReplicas = int32(fullyLabeledReplicasCount)
|
||||
newStatus.ReadyReplicas = int32(readyReplicasCount)
|
||||
newStatus.AvailableReplicas = int32(availableReplicasCount)
|
||||
return newStatus
|
||||
}
|
||||
|
||||
// NewReplicaSetCondition creates a new replicaset condition.
|
||||
func NewReplicaSetCondition(condType extensions.ReplicaSetConditionType, status v1.ConditionStatus, reason, msg string) extensions.ReplicaSetCondition {
|
||||
return extensions.ReplicaSetCondition{
|
||||
Type: condType,
|
||||
Status: status,
|
||||
LastTransitionTime: metav1.Now(),
|
||||
Reason: reason,
|
||||
Message: msg,
|
||||
}
|
||||
}
|
||||
|
||||
// GetCondition returns a replicaset condition with the provided type if it exists.
|
||||
func GetCondition(status extensions.ReplicaSetStatus, condType extensions.ReplicaSetConditionType) *extensions.ReplicaSetCondition {
|
||||
for _, c := range status.Conditions {
|
||||
if c.Type == condType {
|
||||
return &c
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetCondition adds/replaces the given condition in the replicaset status. If the condition that we
|
||||
// are about to add already exists and has the same status and reason then we are not going to update.
|
||||
func SetCondition(status *extensions.ReplicaSetStatus, condition extensions.ReplicaSetCondition) {
|
||||
currentCond := GetCondition(*status, condition.Type)
|
||||
if currentCond != nil && currentCond.Status == condition.Status && currentCond.Reason == condition.Reason {
|
||||
return
|
||||
}
|
||||
newConditions := filterOutCondition(status.Conditions, condition.Type)
|
||||
status.Conditions = append(newConditions, condition)
|
||||
}
|
||||
|
||||
// RemoveCondition removes the condition with the provided type from the replicaset status.
|
||||
func RemoveCondition(status *extensions.ReplicaSetStatus, condType extensions.ReplicaSetConditionType) {
|
||||
status.Conditions = filterOutCondition(status.Conditions, condType)
|
||||
}
|
||||
|
||||
// filterOutCondition returns a new slice of replicaset conditions without conditions with the provided type.
|
||||
func filterOutCondition(conditions []extensions.ReplicaSetCondition, condType extensions.ReplicaSetConditionType) []extensions.ReplicaSetCondition {
|
||||
var newConditions []extensions.ReplicaSetCondition
|
||||
for _, c := range conditions {
|
||||
if c.Type == condType {
|
||||
continue
|
||||
}
|
||||
newConditions = append(newConditions, c)
|
||||
}
|
||||
return newConditions
|
||||
}
|
246
vendor/k8s.io/kubernetes/pkg/controller/replicaset/replica_set_utils_test.go
generated
vendored
Normal file
246
vendor/k8s.io/kubernetes/pkg/controller/replicaset/replica_set_utils_test.go
generated
vendored
Normal file
@ -0,0 +1,246 @@
|
||||
/*
|
||||
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.
|
||||
*/
|
||||
|
||||
// If you make changes to this file, you should also make the corresponding change in ReplicationController.
|
||||
|
||||
package replicaset
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"k8s.io/api/core/v1"
|
||||
extensions "k8s.io/api/extensions/v1beta1"
|
||||
)
|
||||
|
||||
func TestCalculateStatus(t *testing.T) {
|
||||
labelMap := map[string]string{"name": "foo"}
|
||||
fullLabelMap := map[string]string{"name": "foo", "type": "production"}
|
||||
notFullyLabelledRS := newReplicaSet(1, labelMap)
|
||||
// Set replica num to 2 for status condition testing (diff < 0, diff > 0)
|
||||
fullyLabelledRS := newReplicaSet(2, fullLabelMap)
|
||||
longMinReadySecondsRS := newReplicaSet(1, fullLabelMap)
|
||||
longMinReadySecondsRS.Spec.MinReadySeconds = 3600
|
||||
|
||||
rsStatusTests := []struct {
|
||||
name string
|
||||
replicaset *extensions.ReplicaSet
|
||||
filteredPods []*v1.Pod
|
||||
expectedReplicaSetStatus extensions.ReplicaSetStatus
|
||||
}{
|
||||
{
|
||||
"1 fully labelled pod",
|
||||
fullyLabelledRS,
|
||||
[]*v1.Pod{
|
||||
newPod("pod1", fullyLabelledRS, v1.PodRunning, nil, true),
|
||||
},
|
||||
extensions.ReplicaSetStatus{
|
||||
Replicas: 1,
|
||||
FullyLabeledReplicas: 1,
|
||||
ReadyReplicas: 1,
|
||||
AvailableReplicas: 1,
|
||||
},
|
||||
},
|
||||
{
|
||||
"1 not fully labelled pod",
|
||||
notFullyLabelledRS,
|
||||
[]*v1.Pod{
|
||||
newPod("pod1", notFullyLabelledRS, v1.PodRunning, nil, true),
|
||||
},
|
||||
extensions.ReplicaSetStatus{
|
||||
Replicas: 1,
|
||||
FullyLabeledReplicas: 0,
|
||||
ReadyReplicas: 1,
|
||||
AvailableReplicas: 1,
|
||||
},
|
||||
},
|
||||
{
|
||||
"2 fully labelled pods",
|
||||
fullyLabelledRS,
|
||||
[]*v1.Pod{
|
||||
newPod("pod1", fullyLabelledRS, v1.PodRunning, nil, true),
|
||||
newPod("pod2", fullyLabelledRS, v1.PodRunning, nil, true),
|
||||
},
|
||||
extensions.ReplicaSetStatus{
|
||||
Replicas: 2,
|
||||
FullyLabeledReplicas: 2,
|
||||
ReadyReplicas: 2,
|
||||
AvailableReplicas: 2,
|
||||
},
|
||||
},
|
||||
{
|
||||
"2 not fully labelled pods",
|
||||
notFullyLabelledRS,
|
||||
[]*v1.Pod{
|
||||
newPod("pod1", notFullyLabelledRS, v1.PodRunning, nil, true),
|
||||
newPod("pod2", notFullyLabelledRS, v1.PodRunning, nil, true),
|
||||
},
|
||||
extensions.ReplicaSetStatus{
|
||||
Replicas: 2,
|
||||
FullyLabeledReplicas: 0,
|
||||
ReadyReplicas: 2,
|
||||
AvailableReplicas: 2,
|
||||
},
|
||||
},
|
||||
{
|
||||
"1 fully labelled pod, 1 not fully labelled pod",
|
||||
notFullyLabelledRS,
|
||||
[]*v1.Pod{
|
||||
newPod("pod1", notFullyLabelledRS, v1.PodRunning, nil, true),
|
||||
newPod("pod2", fullyLabelledRS, v1.PodRunning, nil, true),
|
||||
},
|
||||
extensions.ReplicaSetStatus{
|
||||
Replicas: 2,
|
||||
FullyLabeledReplicas: 1,
|
||||
ReadyReplicas: 2,
|
||||
AvailableReplicas: 2,
|
||||
},
|
||||
},
|
||||
{
|
||||
"1 non-ready pod",
|
||||
fullyLabelledRS,
|
||||
[]*v1.Pod{
|
||||
newPod("pod1", fullyLabelledRS, v1.PodPending, nil, true),
|
||||
},
|
||||
extensions.ReplicaSetStatus{
|
||||
Replicas: 1,
|
||||
FullyLabeledReplicas: 1,
|
||||
ReadyReplicas: 0,
|
||||
AvailableReplicas: 0,
|
||||
},
|
||||
},
|
||||
{
|
||||
"1 ready but non-available pod",
|
||||
longMinReadySecondsRS,
|
||||
[]*v1.Pod{
|
||||
newPod("pod1", longMinReadySecondsRS, v1.PodRunning, nil, true),
|
||||
},
|
||||
extensions.ReplicaSetStatus{
|
||||
Replicas: 1,
|
||||
FullyLabeledReplicas: 1,
|
||||
ReadyReplicas: 1,
|
||||
AvailableReplicas: 0,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range rsStatusTests {
|
||||
replicaSetStatus := calculateStatus(test.replicaset, test.filteredPods, nil)
|
||||
if !reflect.DeepEqual(replicaSetStatus, test.expectedReplicaSetStatus) {
|
||||
t.Errorf("%s: unexpected replicaset status: expected %v, got %v", test.name, test.expectedReplicaSetStatus, replicaSetStatus)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalculateStatusConditions(t *testing.T) {
|
||||
labelMap := map[string]string{"name": "foo"}
|
||||
rs := newReplicaSet(2, labelMap)
|
||||
replicaFailureRS := newReplicaSet(10, labelMap)
|
||||
replicaFailureRS.Status.Conditions = []extensions.ReplicaSetCondition{
|
||||
{
|
||||
Type: extensions.ReplicaSetReplicaFailure,
|
||||
Status: v1.ConditionTrue,
|
||||
},
|
||||
}
|
||||
|
||||
rsStatusConditionTests := []struct {
|
||||
name string
|
||||
replicaset *extensions.ReplicaSet
|
||||
filteredPods []*v1.Pod
|
||||
manageReplicasErr error
|
||||
expectedReplicaSetConditions []extensions.ReplicaSetCondition
|
||||
}{
|
||||
|
||||
{
|
||||
"manageReplicasErr != nil && failureCond == nil, diff < 0",
|
||||
rs,
|
||||
[]*v1.Pod{
|
||||
newPod("pod1", rs, v1.PodRunning, nil, true),
|
||||
},
|
||||
fmt.Errorf("fake manageReplicasErr"),
|
||||
[]extensions.ReplicaSetCondition{
|
||||
{
|
||||
Type: extensions.ReplicaSetReplicaFailure,
|
||||
Status: v1.ConditionTrue,
|
||||
Reason: "FailedCreate",
|
||||
Message: "fake manageReplicasErr",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"manageReplicasErr != nil && failureCond == nil, diff > 0",
|
||||
rs,
|
||||
[]*v1.Pod{
|
||||
newPod("pod1", rs, v1.PodRunning, nil, true),
|
||||
newPod("pod2", rs, v1.PodRunning, nil, true),
|
||||
newPod("pod3", rs, v1.PodRunning, nil, true),
|
||||
},
|
||||
fmt.Errorf("fake manageReplicasErr"),
|
||||
[]extensions.ReplicaSetCondition{
|
||||
{
|
||||
Type: extensions.ReplicaSetReplicaFailure,
|
||||
Status: v1.ConditionTrue,
|
||||
Reason: "FailedDelete",
|
||||
Message: "fake manageReplicasErr",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"manageReplicasErr == nil && failureCond != nil",
|
||||
replicaFailureRS,
|
||||
[]*v1.Pod{
|
||||
newPod("pod1", replicaFailureRS, v1.PodRunning, nil, true),
|
||||
},
|
||||
nil,
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"manageReplicasErr != nil && failureCond != nil",
|
||||
replicaFailureRS,
|
||||
[]*v1.Pod{
|
||||
newPod("pod1", replicaFailureRS, v1.PodRunning, nil, true),
|
||||
},
|
||||
fmt.Errorf("fake manageReplicasErr"),
|
||||
[]extensions.ReplicaSetCondition{
|
||||
{
|
||||
Type: extensions.ReplicaSetReplicaFailure,
|
||||
Status: v1.ConditionTrue,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"manageReplicasErr == nil && failureCond == nil",
|
||||
rs,
|
||||
[]*v1.Pod{
|
||||
newPod("pod1", rs, v1.PodRunning, nil, true),
|
||||
},
|
||||
nil,
|
||||
nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range rsStatusConditionTests {
|
||||
replicaSetStatus := calculateStatus(test.replicaset, test.filteredPods, test.manageReplicasErr)
|
||||
// all test cases have at most 1 status condition
|
||||
if len(replicaSetStatus.Conditions) > 0 {
|
||||
test.expectedReplicaSetConditions[0].LastTransitionTime = replicaSetStatus.Conditions[0].LastTransitionTime
|
||||
}
|
||||
if !reflect.DeepEqual(replicaSetStatus.Conditions, test.expectedReplicaSetConditions) {
|
||||
t.Errorf("%s: unexpected replicaset status: expected %v, got %v", test.name, test.expectedReplicaSetConditions, replicaSetStatus.Conditions)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user