k3s/pkg/controller/replication/replication_controller_test.go

1560 lines
57 KiB
Go
Raw Normal View History

2014-06-06 23:40:48 +00:00
/*
Copyright 2014 The Kubernetes Authors.
2014-06-06 23:40:48 +00:00
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2016-01-17 22:26:25 +00:00
// If you make changes to this file, you should also make the corresponding change in ReplicaSet.
package replication
2014-06-06 23:40:48 +00:00
import (
"fmt"
"math/rand"
2014-06-06 23:40:48 +00:00
"net/http/httptest"
"reflect"
"strings"
2014-06-06 23:40:48 +00:00
"testing"
"time"
2014-06-06 23:40:48 +00:00
2017-01-11 14:09:48 +00:00
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/watch"
2017-01-19 18:27:59 +00:00
utiltesting "k8s.io/client-go/pkg/util/testing"
restclient "k8s.io/client-go/rest"
2016-11-19 23:32:10 +00:00
"k8s.io/kubernetes/pkg/api"
2015-08-05 22:03:47 +00:00
"k8s.io/kubernetes/pkg/api/testapi"
2016-11-18 20:50:17 +00:00
"k8s.io/kubernetes/pkg/api/v1"
"k8s.io/kubernetes/pkg/client/cache"
"k8s.io/kubernetes/pkg/client/clientset_generated/clientset"
2016-12-14 01:18:17 +00:00
"k8s.io/kubernetes/pkg/client/clientset_generated/clientset/fake"
fakeclientset "k8s.io/kubernetes/pkg/client/clientset_generated/clientset/fake"
"k8s.io/kubernetes/pkg/client/testing/core"
2015-08-05 22:03:47 +00:00
"k8s.io/kubernetes/pkg/controller"
2017-01-02 16:35:12 +00:00
"k8s.io/kubernetes/pkg/controller/informers"
2015-08-05 22:03:47 +00:00
"k8s.io/kubernetes/pkg/securitycontext"
2016-07-26 15:13:18 +00:00
"k8s.io/kubernetes/pkg/util/uuid"
2014-06-06 23:40:48 +00:00
)
var alwaysReady = func() bool { return true }
2016-11-18 20:50:17 +00:00
func getKey(rc *v1.ReplicationController, t *testing.T) string {
2015-07-28 01:21:37 +00:00
if key, err := controller.KeyFunc(rc); err != nil {
2015-04-21 20:40:35 +00:00
t.Errorf("Unexpected error getting key for rc %v: %v", rc.Name, err)
return ""
} else {
return key
}
}
2016-11-18 20:50:17 +00:00
func newReplicationController(replicas int) *v1.ReplicationController {
rc := &v1.ReplicationController{
2017-01-12 18:17:43 +00:00
TypeMeta: metav1.TypeMeta{APIVersion: api.Registry.GroupOrDie(v1.GroupName).GroupVersion.String()},
ObjectMeta: metav1.ObjectMeta{
2016-07-26 15:13:18 +00:00
UID: uuid.NewUUID(),
2015-04-21 20:40:35 +00:00
Name: "foobar",
2016-11-18 20:50:17 +00:00
Namespace: v1.NamespaceDefault,
2015-04-21 20:40:35 +00:00
ResourceVersion: "18",
},
2016-11-18 20:50:17 +00:00
Spec: v1.ReplicationControllerSpec{
Replicas: func() *int32 { i := int32(replicas); return &i }(),
2015-04-21 20:40:35 +00:00
Selector: map[string]string{"foo": "bar"},
2016-11-18 20:50:17 +00:00
Template: &v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"name": "foo",
"type": "production",
2014-06-06 23:40:48 +00:00
},
},
2016-11-18 20:50:17 +00:00
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Image: "foo/bar",
2016-11-18 20:50:17 +00:00
TerminationMessagePath: v1.TerminationMessagePathDefault,
ImagePullPolicy: v1.PullIfNotPresent,
SecurityContext: securitycontext.ValidSecurityContextWithContainerDefaults(),
},
},
2016-11-18 20:50:17 +00:00
RestartPolicy: v1.RestartPolicyAlways,
DNSPolicy: v1.DNSDefault,
NodeSelector: map[string]string{
"baz": "blah",
},
2014-06-06 23:40:48 +00:00
},
},
},
}
2015-04-21 20:40:35 +00:00
return rc
2014-06-06 23:40:48 +00:00
}
2016-06-10 23:28:42 +00:00
// create a pod with the given phase for the given rc (same selectors and namespace).
2016-12-03 18:57:26 +00:00
func newPod(name string, rc *v1.ReplicationController, status v1.PodPhase, lastTransitionTime *metav1.Time) *v1.Pod {
2016-11-18 20:50:17 +00:00
var conditions []v1.PodCondition
if status == v1.PodRunning {
condition := v1.PodCondition{Type: v1.PodReady, Status: v1.ConditionTrue}
if lastTransitionTime != nil {
condition.LastTransitionTime = *lastTransitionTime
}
conditions = append(conditions, condition)
2016-08-17 16:18:16 +00:00
}
2016-11-18 20:50:17 +00:00
return &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
2016-06-10 23:28:42 +00:00
Name: name,
Labels: rc.Spec.Selector,
Namespace: rc.Namespace,
},
2016-11-18 20:50:17 +00:00
Status: v1.PodStatus{Phase: status, Conditions: conditions},
2016-06-10 23:28:42 +00:00
}
}
2015-04-21 20:40:35 +00:00
// create count pods with the given phase for the given rc (same selectors and namespace), and add them to the store.
2016-11-18 20:50:17 +00:00
func newPodList(store cache.Store, count int, status v1.PodPhase, rc *v1.ReplicationController, name string) *v1.PodList {
pods := []v1.Pod{}
2016-06-10 23:28:42 +00:00
var trueVar = true
2016-12-09 18:16:33 +00:00
controllerReference := metav1.OwnerReference{UID: rc.UID, APIVersion: "v1", Kind: "ReplicationController", Name: rc.Name, Controller: &trueVar}
2014-06-06 23:40:48 +00:00
for i := 0; i < count; i++ {
pod := newPod(fmt.Sprintf("%s%d", name, i), rc, status, nil)
2016-12-09 18:16:33 +00:00
pod.OwnerReferences = []metav1.OwnerReference{controllerReference}
2015-04-21 20:40:35 +00:00
if store != nil {
2016-06-10 23:28:42 +00:00
store.Add(pod)
2015-04-21 20:40:35 +00:00
}
2016-06-10 23:28:42 +00:00
pods = append(pods, *pod)
2014-06-06 23:40:48 +00:00
}
2016-11-18 20:50:17 +00:00
return &v1.PodList{
2014-06-09 05:38:45 +00:00
Items: pods,
2014-06-06 23:40:48 +00:00
}
}
2016-06-10 23:28:42 +00:00
func validateSyncReplication(t *testing.T, fakePodControl *controller.FakePodControl, expectedCreates, expectedDeletes, expectedPatches int) {
if e, a := expectedCreates, len(fakePodControl.Templates); e != a {
t.Errorf("Unexpected number of creates. Expected %d, saw %d\n", e, a)
}
if e, a := expectedDeletes, len(fakePodControl.DeletePodName); e != a {
t.Errorf("Unexpected number of deletes. Expected %d, saw %d\n", e, a)
2014-06-06 23:40:48 +00:00
}
2016-06-10 23:28:42 +00:00
if e, a := expectedPatches, len(fakePodControl.Patches); e != a {
t.Errorf("Unexpected number of patches. Expected %d, saw %d\n", e, a)
2014-06-06 23:40:48 +00:00
}
}
func replicationControllerResourceName() string {
return "replicationcontrollers"
}
type serverResponse struct {
statusCode int
obj interface{}
}
2015-04-21 20:40:35 +00:00
func TestSyncReplicationControllerDoesNothing(t *testing.T) {
2017-01-12 18:17:43 +00:00
c := clientset.NewForConfigOrDie(&restclient.Config{Host: "", ContentConfig: restclient.ContentConfig{GroupVersion: &api.Registry.GroupOrDie(v1.GroupName).GroupVersion}})
fakePodControl := controller.FakePodControl{}
manager := NewReplicationManagerFromClient(c, controller.NoResyncPeriodFunc, BurstReplicas, 0)
2017-01-02 16:35:12 +00:00
manager.podListerSynced = alwaysReady
2014-06-06 23:40:48 +00:00
2015-04-21 20:40:35 +00:00
// 2 running pods, a controller with 2 replicas, sync is a no-op
controllerSpec := newReplicationController(2)
2017-01-02 16:35:12 +00:00
manager.rcLister.Indexer.Add(controllerSpec)
newPodList(manager.podLister.Indexer, 2, v1.PodRunning, controllerSpec, "pod")
2014-06-06 23:40:48 +00:00
2015-04-21 20:40:35 +00:00
manager.podControl = &fakePodControl
manager.syncReplicationController(getKey(controllerSpec, t))
2016-06-10 23:28:42 +00:00
validateSyncReplication(t, &fakePodControl, 0, 0, 0)
2014-06-06 23:40:48 +00:00
}
func TestSyncReplicationControllerDeletes(t *testing.T) {
2017-01-12 18:17:43 +00:00
c := clientset.NewForConfigOrDie(&restclient.Config{Host: "", ContentConfig: restclient.ContentConfig{GroupVersion: &api.Registry.GroupOrDie(v1.GroupName).GroupVersion}})
fakePodControl := controller.FakePodControl{}
manager := NewReplicationManagerFromClient(c, controller.NoResyncPeriodFunc, BurstReplicas, 0)
2017-01-02 16:35:12 +00:00
manager.podListerSynced = alwaysReady
2014-06-09 05:38:45 +00:00
manager.podControl = &fakePodControl
2014-06-06 23:40:48 +00:00
2015-04-21 20:40:35 +00:00
// 2 running pods and a controller with 1 replica, one pod delete expected
controllerSpec := newReplicationController(1)
2017-01-02 16:35:12 +00:00
manager.rcLister.Indexer.Add(controllerSpec)
newPodList(manager.podLister.Indexer, 2, v1.PodRunning, controllerSpec, "pod")
2014-06-06 23:40:48 +00:00
2015-04-21 20:40:35 +00:00
manager.syncReplicationController(getKey(controllerSpec, t))
2016-06-10 23:28:42 +00:00
validateSyncReplication(t, &fakePodControl, 0, 1, 0)
2014-06-06 23:40:48 +00:00
}
func TestDeleteFinalStateUnknown(t *testing.T) {
2017-01-12 18:17:43 +00:00
c := clientset.NewForConfigOrDie(&restclient.Config{Host: "", ContentConfig: restclient.ContentConfig{GroupVersion: &api.Registry.GroupOrDie(v1.GroupName).GroupVersion}})
fakePodControl := controller.FakePodControl{}
manager := NewReplicationManagerFromClient(c, controller.NoResyncPeriodFunc, BurstReplicas, 0)
2017-01-02 16:35:12 +00:00
manager.podListerSynced = alwaysReady
manager.podControl = &fakePodControl
received := make(chan string)
manager.syncHandler = func(key string) error {
received <- key
return nil
}
// The DeletedFinalStateUnknown object should cause the rc manager to insert
// the controller matching the selectors of the deleted pod into the work queue.
controllerSpec := newReplicationController(1)
2017-01-02 16:35:12 +00:00
manager.rcLister.Indexer.Add(controllerSpec)
2016-11-18 20:50:17 +00:00
pods := newPodList(nil, 1, v1.PodRunning, controllerSpec, "pod")
manager.deletePod(cache.DeletedFinalStateUnknown{Key: "foo", Obj: &pods.Items[0]})
go manager.worker()
expected := getKey(controllerSpec, t)
select {
case key := <-received:
if key != expected {
t.Errorf("Unexpected sync all for rc %v, expected %v", key, expected)
}
case <-time.After(wait.ForeverTestTimeout):
t.Errorf("Processing DeleteFinalStateUnknown took longer than expected")
}
}
2014-06-06 23:40:48 +00:00
func TestSyncReplicationControllerCreates(t *testing.T) {
2017-01-12 18:17:43 +00:00
c := clientset.NewForConfigOrDie(&restclient.Config{Host: "", ContentConfig: restclient.ContentConfig{GroupVersion: &api.Registry.GroupOrDie(v1.GroupName).GroupVersion}})
manager := NewReplicationManagerFromClient(c, controller.NoResyncPeriodFunc, BurstReplicas, 0)
2017-01-02 16:35:12 +00:00
manager.podListerSynced = alwaysReady
2015-04-21 20:40:35 +00:00
// A controller with 2 replicas and no pods in the store, 2 creates expected
rc := newReplicationController(2)
2017-01-02 16:35:12 +00:00
manager.rcLister.Indexer.Add(rc)
2014-06-06 23:40:48 +00:00
fakePodControl := controller.FakePodControl{}
2014-06-09 05:38:45 +00:00
manager.podControl = &fakePodControl
manager.syncReplicationController(getKey(rc, t))
2016-06-10 23:28:42 +00:00
validateSyncReplication(t, &fakePodControl, 2, 0, 0)
2014-06-06 23:40:48 +00:00
}
func TestStatusUpdatesWithoutReplicasChange(t *testing.T) {
2015-04-21 20:40:35 +00:00
// Setup a fake server to listen for requests, and run the rc manager in steady state
fakeHandler := utiltesting.FakeHandler{
2015-04-21 20:40:35 +00:00
StatusCode: 200,
ResponseBody: "",
}
testServer := httptest.NewServer(&fakeHandler)
defer testServer.Close()
2017-01-12 18:17:43 +00:00
c := clientset.NewForConfigOrDie(&restclient.Config{Host: testServer.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &api.Registry.GroupOrDie(v1.GroupName).GroupVersion}})
manager := NewReplicationManagerFromClient(c, controller.NoResyncPeriodFunc, BurstReplicas, 0)
2017-01-02 16:35:12 +00:00
manager.podListerSynced = alwaysReady
// Steady state for the replication controller, no Status.Replicas updates expected
activePods := 5
2015-04-21 20:40:35 +00:00
rc := newReplicationController(activePods)
2017-01-02 16:35:12 +00:00
manager.rcLister.Indexer.Add(rc)
2016-11-18 20:50:17 +00:00
rc.Status = v1.ReplicationControllerStatus{Replicas: int32(activePods), ReadyReplicas: int32(activePods), AvailableReplicas: int32(activePods)}
2017-01-02 16:35:12 +00:00
newPodList(manager.podLister.Indexer, activePods, v1.PodRunning, rc, "pod")
fakePodControl := controller.FakePodControl{}
manager.podControl = &fakePodControl
2015-04-21 20:40:35 +00:00
manager.syncReplicationController(getKey(rc, t))
2016-06-10 23:28:42 +00:00
validateSyncReplication(t, &fakePodControl, 0, 0, 0)
2015-04-21 20:40:35 +00:00
if fakeHandler.RequestReceived != nil {
t.Errorf("Unexpected update when pods and rcs are in a steady state")
}
// This response body is just so we don't err out decoding the http response, all
// we care about is the request body sent below.
2016-11-18 20:50:17 +00:00
response := runtime.EncodeOrDie(testapi.Default.Codec(), &v1.ReplicationController{})
fakeHandler.ResponseBody = response
rc.Generation = rc.Generation + 1
manager.syncReplicationController(getKey(rc, t))
rc.Status.ObservedGeneration = rc.Generation
updatedRc := runtime.EncodeOrDie(testapi.Default.Codec(), rc)
fakeHandler.ValidateRequest(t, testapi.Default.ResourcePath(replicationControllerResourceName(), rc.Namespace, rc.Name)+"/status", "PUT", &updatedRc)
}
func TestControllerUpdateReplicas(t *testing.T) {
2015-04-21 20:40:35 +00:00
// This is a happy server just to record the PUT request we expect for status.Replicas
fakeHandler := utiltesting.FakeHandler{
2015-04-21 20:40:35 +00:00
StatusCode: 200,
ResponseBody: "",
}
testServer := httptest.NewServer(&fakeHandler)
defer testServer.Close()
2017-01-12 18:17:43 +00:00
c := clientset.NewForConfigOrDie(&restclient.Config{Host: testServer.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &api.Registry.GroupOrDie(v1.GroupName).GroupVersion}})
manager := NewReplicationManagerFromClient(c, controller.NoResyncPeriodFunc, BurstReplicas, 0)
2017-01-02 16:35:12 +00:00
manager.podListerSynced = alwaysReady
2015-04-21 20:40:35 +00:00
// Insufficient number of pods in the system, and Status.Replicas is wrong;
// Status.Replica should update to match number of pods in system, 1 new pod should be created.
rc := newReplicationController(5)
2017-01-02 16:35:12 +00:00
manager.rcLister.Indexer.Add(rc)
2016-11-18 20:50:17 +00:00
rc.Status = v1.ReplicationControllerStatus{Replicas: 2, FullyLabeledReplicas: 6, ReadyReplicas: 2, AvailableReplicas: 2, ObservedGeneration: 0}
rc.Generation = 1
2017-01-02 16:35:12 +00:00
newPodList(manager.podLister.Indexer, 2, v1.PodRunning, rc, "pod")
rcCopy := *rc
extraLabelMap := map[string]string{"foo": "bar", "extraKey": "extraValue"}
rcCopy.Spec.Selector = extraLabelMap
2017-01-02 16:35:12 +00:00
newPodList(manager.podLister.Indexer, 2, v1.PodRunning, &rcCopy, "podWithExtraLabel")
// This response body is just so we don't err out decoding the http response
2016-11-18 20:50:17 +00:00
response := runtime.EncodeOrDie(testapi.Default.Codec(), &v1.ReplicationController{})
2015-04-21 20:40:35 +00:00
fakeHandler.ResponseBody = response
fakePodControl := controller.FakePodControl{}
manager.podControl = &fakePodControl
2015-04-21 20:40:35 +00:00
manager.syncReplicationController(getKey(rc, t))
// 1. Status.Replicas should go up from 2->4 even though we created 5-4=1 pod.
// 2. Status.FullyLabeledReplicas should equal to the number of pods that
// has the extra labels, i.e., 2.
// 3. Every update to the status should include the Generation of the spec.
2016-11-18 20:50:17 +00:00
rc.Status = v1.ReplicationControllerStatus{Replicas: 4, ReadyReplicas: 4, AvailableReplicas: 4, ObservedGeneration: 1}
decRc := runtime.EncodeOrDie(testapi.Default.Codec(), rc)
fakeHandler.ValidateRequest(t, testapi.Default.ResourcePath(replicationControllerResourceName(), rc.Namespace, rc.Name)+"/status", "PUT", &decRc)
2016-06-10 23:28:42 +00:00
validateSyncReplication(t, &fakePodControl, 1, 0, 0)
}
2015-04-21 20:40:35 +00:00
func TestSyncReplicationControllerDormancy(t *testing.T) {
// Setup a test server so we can lie about the current state of pods
fakeHandler := utiltesting.FakeHandler{
2015-04-21 20:40:35 +00:00
StatusCode: 200,
ResponseBody: "{}",
T: t,
2015-04-21 20:40:35 +00:00
}
testServer := httptest.NewServer(&fakeHandler)
defer testServer.Close()
2017-01-12 18:17:43 +00:00
c := clientset.NewForConfigOrDie(&restclient.Config{Host: testServer.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &api.Registry.GroupOrDie(v1.GroupName).GroupVersion}})
fakePodControl := controller.FakePodControl{}
manager := NewReplicationManagerFromClient(c, controller.NoResyncPeriodFunc, BurstReplicas, 0)
2017-01-02 16:35:12 +00:00
manager.podListerSynced = alwaysReady
2015-04-21 20:40:35 +00:00
manager.podControl = &fakePodControl
controllerSpec := newReplicationController(2)
2017-01-02 16:35:12 +00:00
manager.rcLister.Indexer.Add(controllerSpec)
newPodList(manager.podLister.Indexer, 1, v1.PodRunning, controllerSpec, "pod")
2015-04-21 20:40:35 +00:00
// Creates a replica and sets expectations
controllerSpec.Status.Replicas = 1
2016-08-17 16:18:16 +00:00
controllerSpec.Status.ReadyReplicas = 1
controllerSpec.Status.AvailableReplicas = 1
2015-04-21 20:40:35 +00:00
manager.syncReplicationController(getKey(controllerSpec, t))
2016-06-10 23:28:42 +00:00
validateSyncReplication(t, &fakePodControl, 1, 0, 0)
2015-04-21 20:40:35 +00:00
// Expectations prevents replicas but not an update on status
controllerSpec.Status.Replicas = 0
2016-08-17 16:18:16 +00:00
controllerSpec.Status.ReadyReplicas = 0
controllerSpec.Status.AvailableReplicas = 0
fakePodControl.Clear()
2015-04-21 20:40:35 +00:00
manager.syncReplicationController(getKey(controllerSpec, t))
2016-06-10 23:28:42 +00:00
validateSyncReplication(t, &fakePodControl, 0, 0, 0)
2015-04-21 20:40:35 +00:00
2015-07-28 01:21:37 +00:00
// Get the key for the controller
rcKey, err := controller.KeyFunc(controllerSpec)
if err != nil {
t.Errorf("Couldn't get key for object %#v: %v", controllerSpec, err)
2015-07-28 01:21:37 +00:00
}
2015-04-21 20:40:35 +00:00
// Lowering expectations should lead to a sync that creates a replica, however the
2016-06-10 23:28:42 +00:00
// fakePodControl error will prevent this, leaving expectations at 0, 0.
2015-07-28 01:21:37 +00:00
manager.expectations.CreationObserved(rcKey)
2015-04-21 20:40:35 +00:00
controllerSpec.Status.Replicas = 1
2016-08-17 16:18:16 +00:00
controllerSpec.Status.ReadyReplicas = 1
controllerSpec.Status.AvailableReplicas = 1
fakePodControl.Clear()
fakePodControl.Err = fmt.Errorf("Fake Error")
2015-04-21 20:40:35 +00:00
manager.syncReplicationController(getKey(controllerSpec, t))
2016-06-10 23:28:42 +00:00
validateSyncReplication(t, &fakePodControl, 1, 0, 0)
2015-04-21 20:40:35 +00:00
// This replica should not need a Lowering of expectations, since the previous create failed
2016-06-10 23:28:42 +00:00
fakePodControl.Clear()
fakePodControl.Err = nil
2015-04-21 20:40:35 +00:00
manager.syncReplicationController(getKey(controllerSpec, t))
2016-06-10 23:28:42 +00:00
validateSyncReplication(t, &fakePodControl, 1, 0, 0)
2015-04-21 20:40:35 +00:00
// 2 PUT for the rc status during dormancy window.
2015-04-21 20:40:35 +00:00
// Note that the pod creates go through pod control so they're not recorded.
fakeHandler.ValidateRequestCount(t, 2)
2015-04-21 20:40:35 +00:00
}
func TestPodControllerLookup(t *testing.T) {
2017-01-12 18:17:43 +00:00
manager := NewReplicationManagerFromClient(clientset.NewForConfigOrDie(&restclient.Config{Host: "", ContentConfig: restclient.ContentConfig{GroupVersion: &api.Registry.GroupOrDie(v1.GroupName).GroupVersion}}), controller.NoResyncPeriodFunc, BurstReplicas, 0)
2017-01-02 16:35:12 +00:00
manager.podListerSynced = alwaysReady
2015-04-21 20:40:35 +00:00
testCases := []struct {
2016-11-18 20:50:17 +00:00
inRCs []*v1.ReplicationController
pod *v1.Pod
2015-04-21 20:40:35 +00:00
outRCName string
}{
// pods without labels don't match any rcs
{
2016-11-18 20:50:17 +00:00
inRCs: []*v1.ReplicationController{
{ObjectMeta: metav1.ObjectMeta{Name: "basic"}}},
pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo1", Namespace: v1.NamespaceAll}},
2015-04-21 20:40:35 +00:00
outRCName: "",
},
// Matching labels, not namespace
{
2016-11-18 20:50:17 +00:00
inRCs: []*v1.ReplicationController{
2015-04-21 20:40:35 +00:00
{
ObjectMeta: metav1.ObjectMeta{Name: "foo"},
2016-11-18 20:50:17 +00:00
Spec: v1.ReplicationControllerSpec{
2015-04-21 20:40:35 +00:00
Selector: map[string]string{"foo": "bar"},
},
},
},
2016-11-18 20:50:17 +00:00
pod: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
2015-04-21 20:40:35 +00:00
Name: "foo2", Namespace: "ns", Labels: map[string]string{"foo": "bar"}}},
outRCName: "",
},
// Matching ns and labels returns the key to the rc, not the rc name
{
2016-11-18 20:50:17 +00:00
inRCs: []*v1.ReplicationController{
2015-04-21 20:40:35 +00:00
{
ObjectMeta: metav1.ObjectMeta{Name: "bar", Namespace: "ns"},
2016-11-18 20:50:17 +00:00
Spec: v1.ReplicationControllerSpec{
2015-04-21 20:40:35 +00:00
Selector: map[string]string{"foo": "bar"},
},
},
},
2016-11-18 20:50:17 +00:00
pod: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
2015-04-21 20:40:35 +00:00
Name: "foo3", Namespace: "ns", Labels: map[string]string{"foo": "bar"}}},
outRCName: "bar",
},
}
for _, c := range testCases {
for _, r := range c.inRCs {
2017-01-02 16:35:12 +00:00
manager.rcLister.Indexer.Add(r)
2015-04-21 20:40:35 +00:00
}
if rc := manager.getPodController(c.pod); rc != nil {
2015-04-21 20:40:35 +00:00
if c.outRCName != rc.Name {
t.Errorf("Got controller %+v expected %+v", rc.Name, c.outRCName)
}
} else if c.outRCName != "" {
t.Errorf("Expected a controller %v pod %v, found none", c.outRCName, c.pod.Name)
}
}
}
func TestWatchControllers(t *testing.T) {
fakeWatch := watch.NewFake()
c := &fake.Clientset{}
2017-01-02 16:35:12 +00:00
c.AddWatchReactor("replicationcontrollers", core.DefaultWatchReactor(fakeWatch, nil))
stopCh := make(chan struct{})
defer close(stopCh)
informers := informers.NewSharedInformerFactory(c, nil, controller.NoResyncPeriodFunc())
podInformer := informers.Pods().Informer()
rcInformer := informers.ReplicationControllers().Informer()
manager := NewReplicationManager(podInformer, rcInformer, c, BurstReplicas, 0, false)
informers.Start(stopCh)
manager.podListerSynced = alwaysReady
2015-04-21 20:40:35 +00:00
2016-11-18 20:50:17 +00:00
var testControllerSpec v1.ReplicationController
2015-04-21 20:40:35 +00:00
received := make(chan string)
// The update sent through the fakeWatcher should make its way into the workqueue,
// and eventually into the syncHandler. The handler validates the received controller
// and closes the received channel to indicate that the test can finish.
manager.syncHandler = func(key string) error {
2017-01-02 16:35:12 +00:00
obj, exists, err := manager.rcLister.Indexer.GetByKey(key)
2015-04-21 20:40:35 +00:00
if !exists || err != nil {
t.Errorf("Expected to find controller under key %v", key)
}
2016-11-18 20:50:17 +00:00
controllerSpec := *obj.(*v1.ReplicationController)
2016-11-19 23:32:10 +00:00
if !api.Semantic.DeepDerivative(controllerSpec, testControllerSpec) {
2015-04-21 20:40:35 +00:00
t.Errorf("Expected %#v, but got %#v", testControllerSpec, controllerSpec)
}
close(received)
return nil
}
2017-01-02 16:35:12 +00:00
2015-04-21 20:40:35 +00:00
// Start only the rc watcher and the workqueue, send a watch event,
// and make sure it hits the sync method.
go wait.Until(manager.worker, 10*time.Millisecond, stopCh)
2015-04-21 20:40:35 +00:00
testControllerSpec.Name = "foo"
fakeWatch.Add(&testControllerSpec)
select {
case <-received:
case <-time.After(wait.ForeverTestTimeout):
t.Errorf("unexpected timeout from result channel")
2015-04-21 20:40:35 +00:00
}
}
func TestWatchPods(t *testing.T) {
fakeWatch := watch.NewFake()
c := &fake.Clientset{}
c.AddWatchReactor("*", core.DefaultWatchReactor(fakeWatch, nil))
manager := NewReplicationManagerFromClient(c, controller.NoResyncPeriodFunc, BurstReplicas, 0)
2017-01-02 16:35:12 +00:00
manager.podListerSynced = alwaysReady
2015-04-21 20:40:35 +00:00
// Put one rc and one pod into the controller's stores
testControllerSpec := newReplicationController(1)
2017-01-02 16:35:12 +00:00
manager.rcLister.Indexer.Add(testControllerSpec)
2015-04-21 20:40:35 +00:00
received := make(chan string)
// The pod update sent through the fakeWatcher should figure out the managing rc and
// send it into the syncHandler.
manager.syncHandler = func(key string) error {
2017-01-02 16:35:12 +00:00
obj, exists, err := manager.rcLister.Indexer.GetByKey(key)
2015-04-21 20:40:35 +00:00
if !exists || err != nil {
t.Errorf("Expected to find controller under key %v", key)
}
2016-11-18 20:50:17 +00:00
controllerSpec := obj.(*v1.ReplicationController)
2016-11-19 23:32:10 +00:00
if !api.Semantic.DeepDerivative(controllerSpec, testControllerSpec) {
2015-04-21 20:40:35 +00:00
t.Errorf("\nExpected %#v,\nbut got %#v", testControllerSpec, controllerSpec)
}
close(received)
return nil
}
// Start only the pod watcher and the workqueue, send a watch event,
// and make sure it hits the sync method for the right rc.
stopCh := make(chan struct{})
defer close(stopCh)
go manager.internalPodInformer.Run(stopCh)
go wait.Until(manager.worker, 10*time.Millisecond, stopCh)
2015-04-21 20:40:35 +00:00
2016-11-18 20:50:17 +00:00
pods := newPodList(nil, 1, v1.PodRunning, testControllerSpec, "pod")
2015-04-21 20:40:35 +00:00
testPod := pods.Items[0]
2016-11-18 20:50:17 +00:00
testPod.Status.Phase = v1.PodFailed
2015-04-21 20:40:35 +00:00
fakeWatch.Add(&testPod)
select {
case <-received:
case <-time.After(wait.ForeverTestTimeout):
t.Errorf("unexpected timeout from result channel")
2015-04-21 20:40:35 +00:00
}
}
func TestUpdatePods(t *testing.T) {
manager := NewReplicationManagerFromClient(fake.NewSimpleClientset(), controller.NoResyncPeriodFunc, BurstReplicas, 0)
2017-01-02 16:35:12 +00:00
manager.podListerSynced = alwaysReady
2015-04-21 20:40:35 +00:00
received := make(chan string)
manager.syncHandler = func(key string) error {
2017-01-02 16:35:12 +00:00
obj, exists, err := manager.rcLister.Indexer.GetByKey(key)
2015-04-21 20:40:35 +00:00
if !exists || err != nil {
t.Errorf("Expected to find controller under key %v", key)
}
2016-11-18 20:50:17 +00:00
received <- obj.(*v1.ReplicationController).Name
2015-04-21 20:40:35 +00:00
return nil
}
stopCh := make(chan struct{})
defer close(stopCh)
go wait.Until(manager.worker, 10*time.Millisecond, stopCh)
2015-04-21 20:40:35 +00:00
// Put 2 rcs and one pod into the controller's stores
testControllerSpec1 := newReplicationController(1)
2017-01-02 16:35:12 +00:00
manager.rcLister.Indexer.Add(testControllerSpec1)
2015-04-21 20:40:35 +00:00
testControllerSpec2 := *testControllerSpec1
testControllerSpec2.Spec.Selector = map[string]string{"bar": "foo"}
testControllerSpec2.Name = "barfoo"
2017-01-02 16:35:12 +00:00
manager.rcLister.Indexer.Add(&testControllerSpec2)
2015-04-21 20:40:35 +00:00
2017-01-02 16:35:12 +00:00
// case 1: We put in the podLister a pod with labels matching
// testControllerSpec1, then update its labels to match testControllerSpec2.
// We expect to receive a sync request for both controllers.
2017-01-02 16:35:12 +00:00
pod1 := newPodList(manager.podLister.Indexer, 1, v1.PodRunning, testControllerSpec1, "pod").Items[0]
pod1.ResourceVersion = "1"
2015-04-21 20:40:35 +00:00
pod2 := pod1
pod2.Labels = testControllerSpec2.Spec.Selector
pod2.ResourceVersion = "2"
2015-04-21 20:40:35 +00:00
manager.updatePod(&pod1, &pod2)
expected := sets.NewString(testControllerSpec1.Name, testControllerSpec2.Name)
2015-04-21 20:40:35 +00:00
for _, name := range expected.List() {
t.Logf("Expecting update for %+v", name)
select {
case got := <-received:
if !expected.Has(got) {
t.Errorf("Expected keys %#v got %v", expected, got)
}
case <-time.After(wait.ForeverTestTimeout):
2015-04-21 20:40:35 +00:00
t.Errorf("Expected update notifications for controllers within 100ms each")
}
}
2017-01-02 16:35:12 +00:00
// case 2: pod1 in the podLister has labels matching testControllerSpec1.
// We update its labels to match no replication controller. We expect to
// receive a sync request for testControllerSpec1.
pod2.Labels = make(map[string]string)
pod2.ResourceVersion = "2"
manager.updatePod(&pod1, &pod2)
expected = sets.NewString(testControllerSpec1.Name)
for _, name := range expected.List() {
t.Logf("Expecting update for %+v", name)
select {
case got := <-received:
if !expected.Has(got) {
t.Errorf("Expected keys %#v got %v", expected, got)
}
case <-time.After(wait.ForeverTestTimeout):
t.Errorf("Expected update notifications for controllers within 100ms each")
}
}
2015-04-21 20:40:35 +00:00
}
func TestControllerUpdateRequeue(t *testing.T) {
// This server should force a requeue of the controller because it fails to update status.Replicas.
fakeHandler := utiltesting.FakeHandler{
StatusCode: 500,
ResponseBody: "",
}
testServer := httptest.NewServer(&fakeHandler)
defer testServer.Close()
2017-01-12 18:17:43 +00:00
c := clientset.NewForConfigOrDie(&restclient.Config{Host: testServer.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &api.Registry.GroupOrDie(v1.GroupName).GroupVersion}})
manager := NewReplicationManagerFromClient(c, controller.NoResyncPeriodFunc, BurstReplicas, 0)
2017-01-02 16:35:12 +00:00
manager.podListerSynced = alwaysReady
rc := newReplicationController(1)
2017-01-02 16:35:12 +00:00
manager.rcLister.Indexer.Add(rc)
2016-11-18 20:50:17 +00:00
rc.Status = v1.ReplicationControllerStatus{Replicas: 2}
2017-01-02 16:35:12 +00:00
newPodList(manager.podLister.Indexer, 1, v1.PodRunning, rc, "pod")
fakePodControl := controller.FakePodControl{}
manager.podControl = &fakePodControl
2016-08-15 14:36:46 +00:00
// an error from the sync function will be requeued, check to make sure we returned an error
if err := manager.syncReplicationController(getKey(rc, t)); err == nil {
t.Errorf("missing error for requeue")
}
2016-08-15 14:36:46 +00:00
// 1 Update and 1 GET, both of which fail
fakeHandler.ValidateRequestCount(t, 2)
}
func TestControllerUpdateStatusWithFailure(t *testing.T) {
rc := newReplicationController(1)
c := &fake.Clientset{}
c.AddReactor("get", "replicationcontrollers", func(action core.Action) (bool, runtime.Object, error) {
return true, rc, nil
})
c.AddReactor("*", "*", func(action core.Action) (bool, runtime.Object, error) {
2016-11-18 20:50:17 +00:00
return true, &v1.ReplicationController{}, fmt.Errorf("Fake error")
})
2016-02-03 21:21:05 +00:00
fakeRCClient := c.Core().ReplicationControllers("default")
numReplicas := int32(10)
2016-11-18 20:50:17 +00:00
status := v1.ReplicationControllerStatus{Replicas: numReplicas}
updateReplicationControllerStatus(fakeRCClient, *rc, status)
updates, gets := 0, 0
for _, a := range c.Actions() {
if a.GetResource().Resource != "replicationcontrollers" {
2015-08-03 13:21:11 +00:00
t.Errorf("Unexpected action %+v", a)
continue
}
switch action := a.(type) {
case core.GetAction:
gets++
// Make sure the get is for the right rc even though the update failed.
2015-08-03 13:21:11 +00:00
if action.GetName() != rc.Name {
t.Errorf("Expected get for rc %v, got %+v instead", rc.Name, action.GetName())
}
case core.UpdateAction:
updates++
// Confirm that the update has the right status.Replicas even though the Get
// returned an rc with replicas=1.
2016-11-18 20:50:17 +00:00
if c, ok := action.GetObject().(*v1.ReplicationController); !ok {
t.Errorf("Expected an rc as the argument to update, got %T", c)
} else if c.Status.Replicas != numReplicas {
t.Errorf("Expected update for rc to contain replicas %v, got %v instead",
numReplicas, c.Status.Replicas)
}
default:
t.Errorf("Unexpected action %+v", a)
break
}
}
if gets != 1 || updates != 2 {
t.Errorf("Expected 1 get and 2 updates, got %d gets %d updates", gets, updates)
}
}
2015-05-06 21:39:14 +00:00
// TODO: This test is too hairy for a unittest. It should be moved to an E2E suite.
2015-05-06 21:39:14 +00:00
func doTestControllerBurstReplicas(t *testing.T, burstReplicas, numReplicas int) {
2017-01-12 18:17:43 +00:00
c := clientset.NewForConfigOrDie(&restclient.Config{Host: "", ContentConfig: restclient.ContentConfig{GroupVersion: &api.Registry.GroupOrDie(v1.GroupName).GroupVersion}})
fakePodControl := controller.FakePodControl{}
manager := NewReplicationManagerFromClient(c, controller.NoResyncPeriodFunc, burstReplicas, 0)
2017-01-02 16:35:12 +00:00
manager.podListerSynced = alwaysReady
2015-05-06 21:39:14 +00:00
manager.podControl = &fakePodControl
controllerSpec := newReplicationController(numReplicas)
2017-01-02 16:35:12 +00:00
manager.rcLister.Indexer.Add(controllerSpec)
2015-05-06 21:39:14 +00:00
expectedPods := 0
2016-11-18 20:50:17 +00:00
pods := newPodList(nil, numReplicas, v1.PodPending, controllerSpec, "pod")
2015-05-06 21:39:14 +00:00
2015-07-28 01:21:37 +00:00
rcKey, err := controller.KeyFunc(controllerSpec)
if err != nil {
t.Errorf("Couldn't get key for object %#v: %v", controllerSpec, err)
2015-07-28 01:21:37 +00:00
}
2015-05-06 21:39:14 +00:00
// Size up the controller, then size it down, and confirm the expected create/delete pattern
for _, replicas := range []int{numReplicas, 0} {
2016-11-19 23:32:10 +00:00
*(controllerSpec.Spec.Replicas) = int32(replicas)
2017-01-02 16:35:12 +00:00
manager.rcLister.Indexer.Add(controllerSpec)
2015-05-06 21:39:14 +00:00
for i := 0; i < numReplicas; i += burstReplicas {
manager.syncReplicationController(getKey(controllerSpec, t))
// The store accrues active pods. It's also used by the rc to determine how many
// replicas to create.
2017-01-02 16:35:12 +00:00
activePods := len(manager.podLister.Indexer.List())
2015-05-06 21:39:14 +00:00
if replicas != 0 {
// This is the number of pods currently "in flight". They were created by the rc manager above,
// which then puts the rc to sleep till all of them have been observed.
expectedPods = replicas - activePods
if expectedPods > burstReplicas {
expectedPods = burstReplicas
}
// This validates the rc manager sync actually created pods
2016-06-10 23:28:42 +00:00
validateSyncReplication(t, &fakePodControl, expectedPods, 0, 0)
2015-05-06 21:39:14 +00:00
// This simulates the watch events for all but 1 of the expected pods.
// None of these should wake the controller because it has expectations==BurstReplicas.
for i := 0; i < expectedPods-1; i++ {
2017-01-02 16:35:12 +00:00
manager.podLister.Indexer.Add(&pods.Items[i])
manager.addPod(&pods.Items[i])
2015-05-06 21:39:14 +00:00
}
2015-07-28 01:21:37 +00:00
podExp, exists, err := manager.expectations.GetExpectations(rcKey)
2015-05-06 21:39:14 +00:00
if !exists || err != nil {
t.Fatalf("Did not find expectations for rc.")
}
2015-07-28 01:21:37 +00:00
if add, _ := podExp.GetExpectations(); add != 1 {
2015-05-06 21:39:14 +00:00
t.Fatalf("Expectations are wrong %v", podExp)
}
} else {
expectedPods = (replicas - activePods) * -1
if expectedPods > burstReplicas {
expectedPods = burstReplicas
}
2016-06-10 23:28:42 +00:00
validateSyncReplication(t, &fakePodControl, 0, expectedPods, 0)
// To accurately simulate a watch we must delete the exact pods
// the rc is waiting for.
expectedDels := manager.expectations.GetUIDs(getKey(controllerSpec, t))
2016-11-18 20:50:17 +00:00
podsToDelete := []*v1.Pod{}
for _, key := range expectedDels.List() {
nsName := strings.Split(key, "/")
2016-11-18 20:50:17 +00:00
podsToDelete = append(podsToDelete, &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: nsName[1],
Namespace: nsName[0],
Labels: controllerSpec.Spec.Selector,
},
})
}
// Don't delete all pods because we confirm that the last pod
// has exactly one expectation at the end, to verify that we
// don't double delete.
for i := range podsToDelete[1:] {
2017-01-02 16:35:12 +00:00
manager.podLister.Indexer.Delete(podsToDelete[i])
manager.deletePod(podsToDelete[i])
2015-05-06 21:39:14 +00:00
}
2015-07-28 01:21:37 +00:00
podExp, exists, err := manager.expectations.GetExpectations(rcKey)
2015-05-06 21:39:14 +00:00
if !exists || err != nil {
t.Fatalf("Did not find expectations for rc.")
}
2015-07-28 01:21:37 +00:00
if _, del := podExp.GetExpectations(); del != 1 {
2015-05-06 21:39:14 +00:00
t.Fatalf("Expectations are wrong %v", podExp)
}
}
// Check that the rc didn't take any action for all the above pods
fakePodControl.Clear()
2015-05-06 21:39:14 +00:00
manager.syncReplicationController(getKey(controllerSpec, t))
2016-06-10 23:28:42 +00:00
validateSyncReplication(t, &fakePodControl, 0, 0, 0)
2015-05-06 21:39:14 +00:00
// Create/Delete the last pod
// The last add pod will decrease the expectation of the rc to 0,
// which will cause it to create/delete the remaining replicas up to burstReplicas.
2015-05-06 21:39:14 +00:00
if replicas != 0 {
2017-01-02 16:35:12 +00:00
manager.podLister.Indexer.Add(&pods.Items[expectedPods-1])
2015-05-06 21:39:14 +00:00
manager.addPod(&pods.Items[expectedPods-1])
} else {
expectedDel := manager.expectations.GetUIDs(getKey(controllerSpec, t))
if expectedDel.Len() != 1 {
t.Fatalf("Waiting on unexpected number of deletes.")
}
nsName := strings.Split(expectedDel.List()[0], "/")
2016-11-18 20:50:17 +00:00
lastPod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: nsName[1],
Namespace: nsName[0],
Labels: controllerSpec.Spec.Selector,
},
}
2017-01-02 16:35:12 +00:00
manager.podLister.Indexer.Delete(lastPod)
manager.deletePod(lastPod)
2015-05-06 21:39:14 +00:00
}
pods.Items = pods.Items[expectedPods:]
}
// Confirm that we've created the right number of replicas
2017-01-02 16:35:12 +00:00
activePods := int32(len(manager.podLister.Indexer.List()))
2016-11-18 20:50:17 +00:00
if activePods != *(controllerSpec.Spec.Replicas) {
t.Fatalf("Unexpected number of active pods, expected %d, got %d", *(controllerSpec.Spec.Replicas), activePods)
2015-05-06 21:39:14 +00:00
}
// Replenish the pod list, since we cut it down sizing up
2016-11-18 20:50:17 +00:00
pods = newPodList(nil, replicas, v1.PodRunning, controllerSpec, "pod")
2015-05-06 21:39:14 +00:00
}
}
func TestControllerBurstReplicas(t *testing.T) {
doTestControllerBurstReplicas(t, 5, 30)
doTestControllerBurstReplicas(t, 5, 12)
doTestControllerBurstReplicas(t, 3, 2)
}
type FakeRCExpectations struct {
2015-07-28 01:21:37 +00:00
*controller.ControllerExpectations
satisfied bool
expSatisfied func()
}
2015-07-28 01:21:37 +00:00
func (fe FakeRCExpectations) SatisfiedExpectations(controllerKey string) bool {
fe.expSatisfied()
return fe.satisfied
}
// TestRCSyncExpectations tests that a pod cannot sneak in between counting active pods
// and checking expectations.
func TestRCSyncExpectations(t *testing.T) {
2017-01-12 18:17:43 +00:00
c := clientset.NewForConfigOrDie(&restclient.Config{Host: "", ContentConfig: restclient.ContentConfig{GroupVersion: &api.Registry.GroupOrDie(v1.GroupName).GroupVersion}})
fakePodControl := controller.FakePodControl{}
manager := NewReplicationManagerFromClient(c, controller.NoResyncPeriodFunc, 2, 0)
2017-01-02 16:35:12 +00:00
manager.podListerSynced = alwaysReady
manager.podControl = &fakePodControl
controllerSpec := newReplicationController(2)
2017-01-02 16:35:12 +00:00
manager.rcLister.Indexer.Add(controllerSpec)
2016-11-18 20:50:17 +00:00
pods := newPodList(nil, 2, v1.PodPending, controllerSpec, "pod")
2017-01-02 16:35:12 +00:00
manager.podLister.Indexer.Add(&pods.Items[0])
postExpectationsPod := pods.Items[1]
manager.expectations = controller.NewUIDTrackingControllerExpectations(FakeRCExpectations{
2015-07-28 01:21:37 +00:00
controller.NewControllerExpectations(), true, func() {
// If we check active pods before checking expectataions, the rc
// will create a new replica because it doesn't see this pod, but
// has fulfilled its expectations.
2017-01-02 16:35:12 +00:00
manager.podLister.Indexer.Add(&postExpectationsPod)
},
})
manager.syncReplicationController(getKey(controllerSpec, t))
2016-06-10 23:28:42 +00:00
validateSyncReplication(t, &fakePodControl, 0, 0, 0)
}
func TestDeleteControllerAndExpectations(t *testing.T) {
2017-01-12 18:17:43 +00:00
c := clientset.NewForConfigOrDie(&restclient.Config{Host: "", ContentConfig: restclient.ContentConfig{GroupVersion: &api.Registry.GroupOrDie(v1.GroupName).GroupVersion}})
manager := NewReplicationManagerFromClient(c, controller.NoResyncPeriodFunc, 10, 0)
2017-01-02 16:35:12 +00:00
manager.podListerSynced = alwaysReady
rc := newReplicationController(1)
2017-01-02 16:35:12 +00:00
manager.rcLister.Indexer.Add(rc)
fakePodControl := controller.FakePodControl{}
manager.podControl = &fakePodControl
// This should set expectations for the rc
manager.syncReplicationController(getKey(rc, t))
2016-06-10 23:28:42 +00:00
validateSyncReplication(t, &fakePodControl, 1, 0, 0)
fakePodControl.Clear()
2015-07-28 01:21:37 +00:00
// Get the RC key
rcKey, err := controller.KeyFunc(rc)
if err != nil {
t.Errorf("Couldn't get key for object %#v: %v", rc, err)
2015-07-28 01:21:37 +00:00
}
// This is to simulate a concurrent addPod, that has a handle on the expectations
// as the controller deletes it.
2015-07-28 01:21:37 +00:00
podExp, exists, err := manager.expectations.GetExpectations(rcKey)
if !exists || err != nil {
t.Errorf("No expectations found for rc")
}
2017-01-02 16:35:12 +00:00
manager.rcLister.Indexer.Delete(rc)
manager.syncReplicationController(getKey(rc, t))
2015-07-28 01:21:37 +00:00
if _, exists, err = manager.expectations.GetExpectations(rcKey); exists {
t.Errorf("Found expectaions, expected none since the rc has been deleted.")
}
// This should have no effect, since we've deleted the rc.
podExp.Add(-1, 0)
2017-01-02 16:35:12 +00:00
manager.podLister.Indexer.Replace(make([]interface{}, 0), "0")
manager.syncReplicationController(getKey(rc, t))
2016-06-10 23:28:42 +00:00
validateSyncReplication(t, &fakePodControl, 0, 0, 0)
}
2015-06-27 00:55:14 +00:00
// shuffle returns a new shuffled list of container controllers.
2016-11-18 20:50:17 +00:00
func shuffle(controllers []*v1.ReplicationController) []*v1.ReplicationController {
2015-06-27 00:55:14 +00:00
numControllers := len(controllers)
randIndexes := rand.Perm(numControllers)
2016-11-18 20:50:17 +00:00
shuffled := make([]*v1.ReplicationController, numControllers)
2015-06-27 00:55:14 +00:00
for i := 0; i < numControllers; i++ {
shuffled[i] = controllers[randIndexes[i]]
}
return shuffled
}
func TestOverlappingRCs(t *testing.T) {
2017-01-12 18:17:43 +00:00
c := clientset.NewForConfigOrDie(&restclient.Config{Host: "", ContentConfig: restclient.ContentConfig{GroupVersion: &api.Registry.GroupOrDie(v1.GroupName).GroupVersion}})
2015-06-27 00:55:14 +00:00
for i := 0; i < 5; i++ {
manager := NewReplicationManagerFromClient(c, controller.NoResyncPeriodFunc, 10, 0)
2017-01-02 16:35:12 +00:00
manager.podListerSynced = alwaysReady
2015-06-27 00:55:14 +00:00
// Create 10 rcs, shuffled them randomly and insert them into the rc manager's store
2016-11-18 20:50:17 +00:00
var controllers []*v1.ReplicationController
2015-06-27 00:55:14 +00:00
for j := 1; j < 10; j++ {
controllerSpec := newReplicationController(1)
2016-12-03 18:57:26 +00:00
controllerSpec.CreationTimestamp = metav1.Date(2014, time.December, j, 0, 0, 0, 0, time.Local)
2016-07-26 15:13:18 +00:00
controllerSpec.Name = string(uuid.NewUUID())
2015-06-27 00:55:14 +00:00
controllers = append(controllers, controllerSpec)
}
shuffledControllers := shuffle(controllers)
for j := range shuffledControllers {
2017-01-02 16:35:12 +00:00
manager.rcLister.Indexer.Add(shuffledControllers[j])
2015-06-27 00:55:14 +00:00
}
// Add a pod and make sure only the oldest rc is synced
2016-11-18 20:50:17 +00:00
pods := newPodList(nil, 1, v1.PodPending, controllers[0], "pod")
2015-06-27 00:55:14 +00:00
rcKey := getKey(controllers[0], t)
manager.addPod(&pods.Items[0])
queueRC, _ := manager.queue.Get()
if queueRC != rcKey {
t.Fatalf("Expected to find key %v in queue, found %v", rcKey, queueRC)
}
}
}
2016-02-28 08:23:47 +00:00
func TestDeletionTimestamp(t *testing.T) {
2017-01-12 18:17:43 +00:00
c := clientset.NewForConfigOrDie(&restclient.Config{Host: "", ContentConfig: restclient.ContentConfig{GroupVersion: &api.Registry.GroupOrDie(v1.GroupName).GroupVersion}})
manager := NewReplicationManagerFromClient(c, controller.NoResyncPeriodFunc, 10, 0)
2017-01-02 16:35:12 +00:00
manager.podListerSynced = alwaysReady
2016-02-28 08:23:47 +00:00
controllerSpec := newReplicationController(1)
2017-01-02 16:35:12 +00:00
manager.rcLister.Indexer.Add(controllerSpec)
2016-02-28 08:23:47 +00:00
rcKey, err := controller.KeyFunc(controllerSpec)
if err != nil {
t.Errorf("Couldn't get key for object %#v: %v", controllerSpec, err)
2016-02-28 08:23:47 +00:00
}
2016-11-18 20:50:17 +00:00
pod := newPodList(nil, 1, v1.PodPending, controllerSpec, "pod").Items[0]
2016-12-03 18:57:26 +00:00
pod.DeletionTimestamp = &metav1.Time{Time: time.Now()}
pod.ResourceVersion = "1"
manager.expectations.ExpectDeletions(rcKey, []string{controller.PodKey(&pod)})
2016-02-28 08:23:47 +00:00
// A pod added with a deletion timestamp should decrement deletions, not creations.
manager.addPod(&pod)
queueRC, _ := manager.queue.Get()
if queueRC != rcKey {
t.Fatalf("Expected to find key %v in queue, found %v", rcKey, queueRC)
}
manager.queue.Done(rcKey)
podExp, exists, err := manager.expectations.GetExpectations(rcKey)
if !exists || err != nil || !podExp.Fulfilled() {
t.Fatalf("Wrong expectations %#v", podExp)
2016-02-28 08:23:47 +00:00
}
// An update from no deletion timestamp to having one should be treated
// as a deletion.
2016-11-18 20:50:17 +00:00
oldPod := newPodList(nil, 1, v1.PodPending, controllerSpec, "pod").Items[0]
oldPod.ResourceVersion = "2"
manager.expectations.ExpectDeletions(rcKey, []string{controller.PodKey(&pod)})
2016-02-28 08:23:47 +00:00
manager.updatePod(&oldPod, &pod)
queueRC, _ = manager.queue.Get()
if queueRC != rcKey {
t.Fatalf("Expected to find key %v in queue, found %v", rcKey, queueRC)
}
manager.queue.Done(rcKey)
podExp, exists, err = manager.expectations.GetExpectations(rcKey)
if !exists || err != nil || !podExp.Fulfilled() {
t.Fatalf("Wrong expectations %#v", podExp)
2016-02-28 08:23:47 +00:00
}
// An update to the pod (including an update to the deletion timestamp)
// should not be counted as a second delete.
2016-11-18 20:50:17 +00:00
secondPod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Namespace: pod.Namespace,
Name: "secondPod",
Labels: pod.Labels,
},
}
manager.expectations.ExpectDeletions(rcKey, []string{controller.PodKey(secondPod)})
2016-12-03 18:57:26 +00:00
oldPod.DeletionTimestamp = &metav1.Time{Time: time.Now()}
oldPod.ResourceVersion = "2"
2016-02-28 08:23:47 +00:00
manager.updatePod(&oldPod, &pod)
podExp, exists, err = manager.expectations.GetExpectations(rcKey)
if !exists || err != nil || podExp.Fulfilled() {
t.Fatalf("Wrong expectations %#v", podExp)
2016-02-28 08:23:47 +00:00
}
// A pod with a non-nil deletion timestamp should also be ignored by the
// delete handler, because it's already been counted in the update.
manager.deletePod(&pod)
podExp, exists, err = manager.expectations.GetExpectations(rcKey)
if !exists || err != nil || podExp.Fulfilled() {
t.Fatalf("Wrong expectations %#v", podExp)
2016-02-28 08:23:47 +00:00
}
// Deleting the second pod should clear expectations.
manager.deletePod(secondPod)
2016-02-28 08:23:47 +00:00
queueRC, _ = manager.queue.Get()
if queueRC != rcKey {
t.Fatalf("Expected to find key %v in queue, found %v", rcKey, queueRC)
}
manager.queue.Done(rcKey)
podExp, exists, err = manager.expectations.GetExpectations(rcKey)
if !exists || err != nil || !podExp.Fulfilled() {
t.Fatalf("Wrong expectations %#v", podExp)
2016-02-28 08:23:47 +00:00
}
}
func BenchmarkGetPodControllerMultiNS(b *testing.B) {
2017-01-12 18:17:43 +00:00
client := clientset.NewForConfigOrDie(&restclient.Config{Host: "", ContentConfig: restclient.ContentConfig{GroupVersion: &api.Registry.GroupOrDie(v1.GroupName).GroupVersion}})
manager := NewReplicationManagerFromClient(client, controller.NoResyncPeriodFunc, BurstReplicas, 0)
const nsNum = 1000
2016-11-18 20:50:17 +00:00
pods := []v1.Pod{}
for i := 0; i < nsNum; i++ {
ns := fmt.Sprintf("ns-%d", i)
for j := 0; j < 10; j++ {
rcName := fmt.Sprintf("rc-%d", j)
for k := 0; k < 10; k++ {
podName := fmt.Sprintf("pod-%d-%d", j, k)
2016-11-18 20:50:17 +00:00
pods = append(pods, v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: podName,
Namespace: ns,
Labels: map[string]string{"rcName": rcName},
},
})
}
}
}
for i := 0; i < nsNum; i++ {
ns := fmt.Sprintf("ns-%d", i)
for j := 0; j < 10; j++ {
rcName := fmt.Sprintf("rc-%d", j)
2017-01-02 16:35:12 +00:00
manager.rcLister.Indexer.Add(&v1.ReplicationController{
ObjectMeta: metav1.ObjectMeta{Name: rcName, Namespace: ns},
2016-11-18 20:50:17 +00:00
Spec: v1.ReplicationControllerSpec{
Selector: map[string]string{"rcName": rcName},
},
})
}
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, pod := range pods {
manager.getPodController(&pod)
}
}
}
func BenchmarkGetPodControllerSingleNS(b *testing.B) {
2017-01-12 18:17:43 +00:00
client := clientset.NewForConfigOrDie(&restclient.Config{Host: "", ContentConfig: restclient.ContentConfig{GroupVersion: &api.Registry.GroupOrDie(v1.GroupName).GroupVersion}})
manager := NewReplicationManagerFromClient(client, controller.NoResyncPeriodFunc, BurstReplicas, 0)
const rcNum = 1000
const replicaNum = 3
2016-11-18 20:50:17 +00:00
pods := []v1.Pod{}
for i := 0; i < rcNum; i++ {
rcName := fmt.Sprintf("rc-%d", i)
for j := 0; j < replicaNum; j++ {
podName := fmt.Sprintf("pod-%d-%d", i, j)
2016-11-18 20:50:17 +00:00
pods = append(pods, v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: podName,
Namespace: "foo",
Labels: map[string]string{"rcName": rcName},
},
})
}
}
for i := 0; i < rcNum; i++ {
rcName := fmt.Sprintf("rc-%d", i)
2017-01-02 16:35:12 +00:00
manager.rcLister.Indexer.Add(&v1.ReplicationController{
ObjectMeta: metav1.ObjectMeta{Name: rcName, Namespace: "foo"},
2016-11-18 20:50:17 +00:00
Spec: v1.ReplicationControllerSpec{
Selector: map[string]string{"rcName": rcName},
},
})
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, pod := range pods {
manager.getPodController(&pod)
}
}
}
2016-06-10 23:28:42 +00:00
// setupManagerWithGCEnabled creates a RC manager with a fakePodControl and with garbageCollectorEnabled set to true
2016-08-15 14:36:46 +00:00
func setupManagerWithGCEnabled(objs ...runtime.Object) (manager *ReplicationManager, fakePodControl *controller.FakePodControl) {
c := fakeclientset.NewSimpleClientset(objs...)
2016-06-10 23:28:42 +00:00
fakePodControl = &controller.FakePodControl{}
manager = NewReplicationManagerFromClient(c, controller.NoResyncPeriodFunc, BurstReplicas, 0)
manager.garbageCollectorEnabled = true
2017-01-02 16:35:12 +00:00
manager.podListerSynced = alwaysReady
2016-06-10 23:28:42 +00:00
manager.podControl = fakePodControl
return manager, fakePodControl
}
func TestDoNotPatchPodWithOtherControlRef(t *testing.T) {
manager, fakePodControl := setupManagerWithGCEnabled()
rc := newReplicationController(2)
2017-01-02 16:35:12 +00:00
manager.rcLister.Indexer.Add(rc)
2016-06-10 23:28:42 +00:00
var trueVar = true
2016-12-09 18:16:33 +00:00
otherControllerReference := metav1.OwnerReference{UID: uuid.NewUUID(), APIVersion: "v1", Kind: "ReplicationController", Name: "AnotherRC", Controller: &trueVar}
2017-01-02 16:35:12 +00:00
// add to podLister a matching Pod controlled by another controller. Expect no patch.
2016-11-18 20:50:17 +00:00
pod := newPod("pod", rc, v1.PodRunning, nil)
2016-12-09 18:16:33 +00:00
pod.OwnerReferences = []metav1.OwnerReference{otherControllerReference}
2017-01-02 16:35:12 +00:00
manager.podLister.Indexer.Add(pod)
2016-06-10 23:28:42 +00:00
err := manager.syncReplicationController(getKey(rc, t))
if err != nil {
t.Fatal(err)
}
// because the matching pod already has a controller, so 2 pods should be created.
validateSyncReplication(t, fakePodControl, 2, 0, 0)
}
func TestPatchPodWithOtherOwnerRef(t *testing.T) {
rc := newReplicationController(2)
2016-08-15 14:36:46 +00:00
manager, fakePodControl := setupManagerWithGCEnabled(rc)
2017-01-02 16:35:12 +00:00
manager.rcLister.Indexer.Add(rc)
// add to podLister one more matching pod that doesn't have a controller
2016-06-10 23:28:42 +00:00
// ref, but has an owner ref pointing to other object. Expect a patch to
// take control of it.
2016-12-09 18:16:33 +00:00
unrelatedOwnerReference := metav1.OwnerReference{UID: uuid.NewUUID(), APIVersion: "batch/v1", Kind: "Job", Name: "Job"}
2016-11-18 20:50:17 +00:00
pod := newPod("pod", rc, v1.PodRunning, nil)
2016-12-09 18:16:33 +00:00
pod.OwnerReferences = []metav1.OwnerReference{unrelatedOwnerReference}
2017-01-02 16:35:12 +00:00
manager.podLister.Indexer.Add(pod)
2016-06-10 23:28:42 +00:00
err := manager.syncReplicationController(getKey(rc, t))
if err != nil {
t.Fatal(err)
}
// 1 patch to take control of pod, and 1 create of new pod.
validateSyncReplication(t, fakePodControl, 1, 0, 1)
}
func TestPatchPodWithCorrectOwnerRef(t *testing.T) {
rc := newReplicationController(2)
2016-08-15 14:36:46 +00:00
manager, fakePodControl := setupManagerWithGCEnabled(rc)
2017-01-02 16:35:12 +00:00
manager.rcLister.Indexer.Add(rc)
// add to podLister a matching pod that has an ownerRef pointing to the rc,
2016-06-10 23:28:42 +00:00
// but ownerRef.Controller is false. Expect a patch to take control it.
2016-12-09 18:16:33 +00:00
rcOwnerReference := metav1.OwnerReference{UID: rc.UID, APIVersion: "v1", Kind: "ReplicationController", Name: rc.Name}
2016-11-18 20:50:17 +00:00
pod := newPod("pod", rc, v1.PodRunning, nil)
2016-12-09 18:16:33 +00:00
pod.OwnerReferences = []metav1.OwnerReference{rcOwnerReference}
2017-01-02 16:35:12 +00:00
manager.podLister.Indexer.Add(pod)
2016-06-10 23:28:42 +00:00
err := manager.syncReplicationController(getKey(rc, t))
if err != nil {
t.Fatal(err)
}
// 1 patch to take control of pod, and 1 create of new pod.
validateSyncReplication(t, fakePodControl, 1, 0, 1)
}
func TestPatchPodFails(t *testing.T) {
rc := newReplicationController(2)
2016-08-15 14:36:46 +00:00
manager, fakePodControl := setupManagerWithGCEnabled(rc)
2017-01-02 16:35:12 +00:00
manager.rcLister.Indexer.Add(rc)
// add to podLister two matching pods. Expect two patches to take control
2016-06-10 23:28:42 +00:00
// them.
2017-01-02 16:35:12 +00:00
manager.podLister.Indexer.Add(newPod("pod1", rc, v1.PodRunning, nil))
manager.podLister.Indexer.Add(newPod("pod2", rc, v1.PodRunning, nil))
2016-06-10 23:28:42 +00:00
// let both patches fail. The rc manager will assume it fails to take
// control of the pods and create new ones.
fakePodControl.Err = fmt.Errorf("Fake Error")
err := manager.syncReplicationController(getKey(rc, t))
2016-08-15 14:36:46 +00:00
if err == nil || err.Error() != "Fake Error" {
t.Fatalf("expected Fake Error, got %v", err)
2016-06-10 23:28:42 +00:00
}
// 2 patches to take control of pod1 and pod2 (both fail), 2 creates.
validateSyncReplication(t, fakePodControl, 2, 0, 2)
}
func TestPatchExtraPodsThenDelete(t *testing.T) {
rc := newReplicationController(2)
2016-08-15 14:36:46 +00:00
manager, fakePodControl := setupManagerWithGCEnabled(rc)
2017-01-02 16:35:12 +00:00
manager.rcLister.Indexer.Add(rc)
// add to podLister three matching pods. Expect three patches to take control
2016-06-10 23:28:42 +00:00
// them, and later delete one of them.
2017-01-02 16:35:12 +00:00
manager.podLister.Indexer.Add(newPod("pod1", rc, v1.PodRunning, nil))
manager.podLister.Indexer.Add(newPod("pod2", rc, v1.PodRunning, nil))
manager.podLister.Indexer.Add(newPod("pod3", rc, v1.PodRunning, nil))
2016-06-10 23:28:42 +00:00
err := manager.syncReplicationController(getKey(rc, t))
if err != nil {
t.Fatal(err)
}
// 3 patches to take control of the pods, and 1 deletion because there is an extra pod.
validateSyncReplication(t, fakePodControl, 0, 1, 3)
}
func TestUpdateLabelsRemoveControllerRef(t *testing.T) {
manager, fakePodControl := setupManagerWithGCEnabled()
rc := newReplicationController(2)
2017-01-02 16:35:12 +00:00
manager.rcLister.Indexer.Add(rc)
// put one pod in the podLister
2016-11-18 20:50:17 +00:00
pod := newPod("pod", rc, v1.PodRunning, nil)
pod.ResourceVersion = "1"
2016-06-10 23:28:42 +00:00
var trueVar = true
2016-12-09 18:16:33 +00:00
rcOwnerReference := metav1.OwnerReference{UID: rc.UID, APIVersion: "v1", Kind: "ReplicationController", Name: rc.Name, Controller: &trueVar}
pod.OwnerReferences = []metav1.OwnerReference{rcOwnerReference}
2016-06-10 23:28:42 +00:00
updatedPod := *pod
// reset the labels
updatedPod.Labels = make(map[string]string)
updatedPod.ResourceVersion = "2"
2016-06-10 23:28:42 +00:00
// add the updatedPod to the store. This is consistent with the behavior of
// the Informer: Informer updates the store before call the handler
// (updatePod() in this case).
2017-01-02 16:35:12 +00:00
manager.podLister.Indexer.Add(&updatedPod)
2016-06-10 23:28:42 +00:00
// send a update of the same pod with modified labels
manager.updatePod(pod, &updatedPod)
// verifies that rc is added to the queue
rcKey := getKey(rc, t)
queueRC, _ := manager.queue.Get()
if queueRC != rcKey {
t.Fatalf("Expected to find key %v in queue, found %v", rcKey, queueRC)
}
manager.queue.Done(queueRC)
err := manager.syncReplicationController(rcKey)
if err != nil {
t.Fatal(err)
}
// expect 1 patch to be sent to remove the controllerRef for the pod.
2016-11-18 20:50:17 +00:00
// expect 2 creates because the *(rc.Spec.Replicas)=2 and there exists no
2016-06-10 23:28:42 +00:00
// matching pod.
validateSyncReplication(t, fakePodControl, 2, 0, 1)
fakePodControl.Clear()
}
func TestUpdateSelectorControllerRef(t *testing.T) {
manager, fakePodControl := setupManagerWithGCEnabled()
rc := newReplicationController(2)
2017-01-02 16:35:12 +00:00
// put 2 pods in the podLister
newPodList(manager.podLister.Indexer, 2, v1.PodRunning, rc, "pod")
2016-06-10 23:28:42 +00:00
// update the RC so that its selector no longer matches the pods
updatedRC := *rc
updatedRC.Spec.Selector = map[string]string{"foo": "baz"}
// put the updatedRC into the store. This is consistent with the behavior of
// the Informer: Informer updates the store before call the handler
// (updateRC() in this case).
2017-01-02 16:35:12 +00:00
manager.rcLister.Indexer.Add(&updatedRC)
2016-06-10 23:28:42 +00:00
manager.updateRC(rc, &updatedRC)
// verifies that the rc is added to the queue
rcKey := getKey(rc, t)
queueRC, _ := manager.queue.Get()
if queueRC != rcKey {
t.Fatalf("Expected to find key %v in queue, found %v", rcKey, queueRC)
}
manager.queue.Done(queueRC)
err := manager.syncReplicationController(rcKey)
if err != nil {
t.Fatal(err)
}
// expect 2 patches to be sent to remove the controllerRef for the pods.
2016-11-18 20:50:17 +00:00
// expect 2 creates because the *(rc.Spec.Replicas)=2 and there exists no
2016-06-10 23:28:42 +00:00
// matching pod.
validateSyncReplication(t, fakePodControl, 2, 0, 2)
fakePodControl.Clear()
}
// RC manager shouldn't adopt or create more pods if the rc is about to be
// deleted.
func TestDoNotAdoptOrCreateIfBeingDeleted(t *testing.T) {
manager, fakePodControl := setupManagerWithGCEnabled()
rc := newReplicationController(2)
2016-12-03 18:57:26 +00:00
now := metav1.Now()
2016-06-10 23:28:42 +00:00
rc.DeletionTimestamp = &now
2017-01-02 16:35:12 +00:00
manager.rcLister.Indexer.Add(rc)
2016-11-18 20:50:17 +00:00
pod1 := newPod("pod1", rc, v1.PodRunning, nil)
2017-01-02 16:35:12 +00:00
manager.podLister.Indexer.Add(pod1)
2016-06-10 23:28:42 +00:00
// no patch, no create
err := manager.syncReplicationController(getKey(rc, t))
if err != nil {
t.Fatal(err)
}
validateSyncReplication(t, fakePodControl, 0, 0, 0)
}
2016-08-17 16:18:16 +00:00
func TestReadyReplicas(t *testing.T) {
// This is a happy server just to record the PUT request we expect for status.Replicas
fakeHandler := utiltesting.FakeHandler{
StatusCode: 200,
ResponseBody: "{}",
}
testServer := httptest.NewServer(&fakeHandler)
defer testServer.Close()
2017-01-12 18:17:43 +00:00
c := clientset.NewForConfigOrDie(&restclient.Config{Host: testServer.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &api.Registry.GroupOrDie(v1.GroupName).GroupVersion}})
2016-08-17 16:18:16 +00:00
manager := NewReplicationManagerFromClient(c, controller.NoResyncPeriodFunc, BurstReplicas, 0)
2017-01-02 16:35:12 +00:00
manager.podListerSynced = alwaysReady
2016-08-17 16:18:16 +00:00
// Status.Replica should update to match number of pods in system, 1 new pod should be created.
rc := newReplicationController(2)
2016-11-18 20:50:17 +00:00
rc.Status = v1.ReplicationControllerStatus{Replicas: 2, ReadyReplicas: 0, AvailableReplicas: 0, ObservedGeneration: 1}
2016-08-17 16:18:16 +00:00
rc.Generation = 1
2017-01-02 16:35:12 +00:00
manager.rcLister.Indexer.Add(rc)
2016-08-17 16:18:16 +00:00
2017-01-02 16:35:12 +00:00
newPodList(manager.podLister.Indexer, 2, v1.PodPending, rc, "pod")
newPodList(manager.podLister.Indexer, 2, v1.PodRunning, rc, "pod")
2016-08-17 16:18:16 +00:00
// This response body is just so we don't err out decoding the http response
2016-11-18 20:50:17 +00:00
response := runtime.EncodeOrDie(testapi.Default.Codec(), &v1.ReplicationController{})
2016-08-17 16:18:16 +00:00
fakeHandler.ResponseBody = response
fakePodControl := controller.FakePodControl{}
manager.podControl = &fakePodControl
manager.syncReplicationController(getKey(rc, t))
// ReadyReplicas should go from 0 to 2.
2016-11-18 20:50:17 +00:00
rc.Status = v1.ReplicationControllerStatus{Replicas: 2, ReadyReplicas: 2, AvailableReplicas: 2, ObservedGeneration: 1}
decRc := runtime.EncodeOrDie(testapi.Default.Codec(), rc)
fakeHandler.ValidateRequest(t, testapi.Default.ResourcePath(replicationControllerResourceName(), rc.Namespace, rc.Name)+"/status", "PUT", &decRc)
validateSyncReplication(t, &fakePodControl, 0, 0, 0)
}
func TestAvailableReplicas(t *testing.T) {
// This is a happy server just to record the PUT request we expect for status.Replicas
fakeHandler := utiltesting.FakeHandler{
StatusCode: 200,
ResponseBody: "{}",
}
testServer := httptest.NewServer(&fakeHandler)
defer testServer.Close()
2017-01-12 18:17:43 +00:00
c := clientset.NewForConfigOrDie(&restclient.Config{Host: testServer.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &api.Registry.GroupOrDie(v1.GroupName).GroupVersion}})
manager := NewReplicationManagerFromClient(c, controller.NoResyncPeriodFunc, BurstReplicas, 0)
2017-01-02 16:35:12 +00:00
manager.podListerSynced = alwaysReady
// Status.Replica should update to match number of pods in system, 1 new pod should be created.
rc := newReplicationController(2)
2016-11-18 20:50:17 +00:00
rc.Status = v1.ReplicationControllerStatus{Replicas: 2, ReadyReplicas: 0, ObservedGeneration: 1}
rc.Generation = 1
// minReadySeconds set to 15s
rc.Spec.MinReadySeconds = 15
2017-01-02 16:35:12 +00:00
manager.rcLister.Indexer.Add(rc)
// First pod becomes ready 20s ago
2016-12-03 18:57:26 +00:00
moment := metav1.Time{Time: time.Now().Add(-2e10)}
2016-11-18 20:50:17 +00:00
pod := newPod("pod", rc, v1.PodRunning, &moment)
2017-01-02 16:35:12 +00:00
manager.podLister.Indexer.Add(pod)
// Second pod becomes ready now
2016-12-03 18:57:26 +00:00
otherMoment := metav1.Now()
2016-11-18 20:50:17 +00:00
otherPod := newPod("otherPod", rc, v1.PodRunning, &otherMoment)
2017-01-02 16:35:12 +00:00
manager.podLister.Indexer.Add(otherPod)
// This response body is just so we don't err out decoding the http response
2016-11-18 20:50:17 +00:00
response := runtime.EncodeOrDie(testapi.Default.Codec(), &v1.ReplicationController{})
fakeHandler.ResponseBody = response
fakePodControl := controller.FakePodControl{}
manager.podControl = &fakePodControl
// The controller should see only one available pod.
manager.syncReplicationController(getKey(rc, t))
2016-11-18 20:50:17 +00:00
rc.Status = v1.ReplicationControllerStatus{Replicas: 2, ReadyReplicas: 2, AvailableReplicas: 1, ObservedGeneration: 1}
2016-08-17 16:18:16 +00:00
decRc := runtime.EncodeOrDie(testapi.Default.Codec(), rc)
fakeHandler.ValidateRequest(t, testapi.Default.ResourcePath(replicationControllerResourceName(), rc.Namespace, rc.Name)+"/status", "PUT", &decRc)
validateSyncReplication(t, &fakePodControl, 0, 0, 0)
}
var (
2016-11-18 20:50:17 +00:00
imagePullBackOff v1.ReplicationControllerConditionType = "ImagePullBackOff"
2016-11-18 20:50:17 +00:00
condImagePullBackOff = func() v1.ReplicationControllerCondition {
return v1.ReplicationControllerCondition{
Type: imagePullBackOff,
2016-11-18 20:50:17 +00:00
Status: v1.ConditionTrue,
Reason: "NonExistentImage",
}
}
2016-11-18 20:50:17 +00:00
condReplicaFailure = func() v1.ReplicationControllerCondition {
return v1.ReplicationControllerCondition{
Type: v1.ReplicationControllerReplicaFailure,
Status: v1.ConditionTrue,
Reason: "OtherFailure",
}
}
2016-11-18 20:50:17 +00:00
condReplicaFailure2 = func() v1.ReplicationControllerCondition {
return v1.ReplicationControllerCondition{
Type: v1.ReplicationControllerReplicaFailure,
Status: v1.ConditionTrue,
Reason: "AnotherFailure",
}
}
2016-11-18 20:50:17 +00:00
status = func() *v1.ReplicationControllerStatus {
return &v1.ReplicationControllerStatus{
Conditions: []v1.ReplicationControllerCondition{condReplicaFailure()},
}
}
)
func TestGetCondition(t *testing.T) {
exampleStatus := status()
tests := []struct {
name string
2016-11-18 20:50:17 +00:00
status v1.ReplicationControllerStatus
condType v1.ReplicationControllerConditionType
condStatus v1.ConditionStatus
condReason string
expected bool
}{
{
name: "condition exists",
status: *exampleStatus,
2016-11-18 20:50:17 +00:00
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
2016-11-18 20:50:17 +00:00
status *v1.ReplicationControllerStatus
cond v1.ReplicationControllerCondition
2016-11-18 20:50:17 +00:00
expectedStatus *v1.ReplicationControllerStatus
}{
{
name: "set for the first time",
2016-11-18 20:50:17 +00:00
status: &v1.ReplicationControllerStatus{},
cond: condReplicaFailure(),
2016-11-18 20:50:17 +00:00
expectedStatus: &v1.ReplicationControllerStatus{Conditions: []v1.ReplicationControllerCondition{condReplicaFailure()}},
},
{
name: "simple set",
2016-11-18 20:50:17 +00:00
status: &v1.ReplicationControllerStatus{Conditions: []v1.ReplicationControllerCondition{condImagePullBackOff()}},
cond: condReplicaFailure(),
2016-11-18 20:50:17 +00:00
expectedStatus: &v1.ReplicationControllerStatus{Conditions: []v1.ReplicationControllerCondition{condImagePullBackOff(), condReplicaFailure()}},
},
{
name: "overwrite",
2016-11-18 20:50:17 +00:00
status: &v1.ReplicationControllerStatus{Conditions: []v1.ReplicationControllerCondition{condReplicaFailure()}},
cond: condReplicaFailure2(),
2016-11-18 20:50:17 +00:00
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
2016-11-18 20:50:17 +00:00
status *v1.ReplicationControllerStatus
condType v1.ReplicationControllerConditionType
2016-11-18 20:50:17 +00:00
expectedStatus *v1.ReplicationControllerStatus
}{
{
name: "remove from empty status",
2016-11-18 20:50:17 +00:00
status: &v1.ReplicationControllerStatus{},
condType: v1.ReplicationControllerReplicaFailure,
2016-11-18 20:50:17 +00:00
expectedStatus: &v1.ReplicationControllerStatus{},
},
{
name: "simple remove",
2016-11-18 20:50:17 +00:00
status: &v1.ReplicationControllerStatus{Conditions: []v1.ReplicationControllerCondition{condReplicaFailure()}},
condType: v1.ReplicationControllerReplicaFailure,
2016-11-18 20:50:17 +00:00
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)
}
}
}