| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2016-06-03 08:25:58 +08:00
										 |  |  | Copyright 2015 The Kubernetes Authors. | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  | you may not use this file except in compliance with the License. | 
					
						
							|  |  |  | You may obtain a copy of the License at | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  | distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  | See the License for the specific language governing permissions and | 
					
						
							|  |  |  | limitations under the License. | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package controller | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2016-11-19 04:50:17 +08:00
										 |  |  | 	"encoding/json" | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2017-05-18 07:53:46 +08:00
										 |  |  | 	"math" | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 	"math/rand" | 
					
						
							|  |  |  | 	"net/http/httptest" | 
					
						
							|  |  |  | 	"reflect" | 
					
						
							|  |  |  | 	"sort" | 
					
						
							|  |  |  | 	"sync" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-23 02:24:23 +08:00
										 |  |  | 	"k8s.io/api/core/v1" | 
					
						
							|  |  |  | 	extensions "k8s.io/api/extensions/v1beta1" | 
					
						
							| 
									
										
										
										
											2017-01-25 21:39:54 +08:00
										 |  |  | 	apiequality "k8s.io/apimachinery/pkg/api/equality" | 
					
						
							| 
									
										
										
										
											2017-01-11 22:09:48 +08:00
										 |  |  | 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | 
					
						
							|  |  |  | 	"k8s.io/apimachinery/pkg/runtime" | 
					
						
							| 
									
										
										
										
											2017-05-20 01:57:39 +08:00
										 |  |  | 	"k8s.io/apimachinery/pkg/util/clock" | 
					
						
							| 
									
										
										
										
											2017-01-11 22:09:48 +08:00
										 |  |  | 	"k8s.io/apimachinery/pkg/util/sets" | 
					
						
							| 
									
										
										
										
											2017-01-24 22:35:22 +08:00
										 |  |  | 	"k8s.io/apimachinery/pkg/util/uuid" | 
					
						
							| 
									
										
										
										
											2017-01-20 02:27:59 +08:00
										 |  |  | 	restclient "k8s.io/client-go/rest" | 
					
						
							| 
									
										
										
										
											2017-01-24 22:11:51 +08:00
										 |  |  | 	"k8s.io/client-go/tools/cache" | 
					
						
							| 
									
										
										
										
											2017-01-31 02:39:54 +08:00
										 |  |  | 	"k8s.io/client-go/tools/record" | 
					
						
							| 
									
										
										
										
											2017-01-24 02:37:22 +08:00
										 |  |  | 	utiltesting "k8s.io/client-go/util/testing" | 
					
						
							| 
									
										
										
										
											2016-11-20 07:32:10 +08:00
										 |  |  | 	"k8s.io/kubernetes/pkg/api" | 
					
						
							| 
									
										
										
										
											2015-08-06 06:03:47 +08:00
										 |  |  | 	"k8s.io/kubernetes/pkg/api/testapi" | 
					
						
							| 
									
										
										
										
											2017-01-06 14:34:29 +08:00
										 |  |  | 	"k8s.io/kubernetes/pkg/client/clientset_generated/clientset" | 
					
						
							| 
									
										
										
										
											2015-08-06 06:03:47 +08:00
										 |  |  | 	"k8s.io/kubernetes/pkg/securitycontext" | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // NewFakeControllerExpectationsLookup creates a fake store for PodExpectations.
 | 
					
						
							| 
									
										
										
										
											2016-05-26 11:08:56 +08:00
										 |  |  | func NewFakeControllerExpectationsLookup(ttl time.Duration) (*ControllerExpectations, *clock.FakeClock) { | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 	fakeTime := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC) | 
					
						
							| 
									
										
										
										
											2016-05-26 11:08:56 +08:00
										 |  |  | 	fakeClock := clock.NewFakeClock(fakeTime) | 
					
						
							| 
									
										
										
										
											2015-08-08 09:52:23 +08:00
										 |  |  | 	ttlPolicy := &cache.TTLPolicy{Ttl: ttl, Clock: fakeClock} | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 	ttlStore := cache.NewFakeExpirationStore( | 
					
						
							|  |  |  | 		ExpKeyFunc, nil, ttlPolicy, fakeClock) | 
					
						
							|  |  |  | 	return &ControllerExpectations{ttlStore}, fakeClock | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-19 04:50:17 +08:00
										 |  |  | func newReplicationController(replicas int) *v1.ReplicationController { | 
					
						
							|  |  |  | 	rc := &v1.ReplicationController{ | 
					
						
							| 
									
										
										
										
											2017-01-13 02:17:43 +08:00
										 |  |  | 		TypeMeta: metav1.TypeMeta{APIVersion: api.Registry.GroupOrDie(v1.GroupName).GroupVersion.String()}, | 
					
						
							| 
									
										
										
										
											2017-01-17 11:38:19 +08:00
										 |  |  | 		ObjectMeta: metav1.ObjectMeta{ | 
					
						
							| 
									
										
										
										
											2016-07-26 23:13:18 +08:00
										 |  |  | 			UID:             uuid.NewUUID(), | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 			Name:            "foobar", | 
					
						
							| 
									
										
										
										
											2017-01-22 11:36:02 +08:00
										 |  |  | 			Namespace:       metav1.NamespaceDefault, | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 			ResourceVersion: "18", | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-11-19 04:50:17 +08:00
										 |  |  | 		Spec: v1.ReplicationControllerSpec{ | 
					
						
							|  |  |  | 			Replicas: func() *int32 { i := int32(replicas); return &i }(), | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 			Selector: map[string]string{"foo": "bar"}, | 
					
						
							| 
									
										
										
										
											2016-11-19 04:50:17 +08:00
										 |  |  | 			Template: &v1.PodTemplateSpec{ | 
					
						
							| 
									
										
										
										
											2017-01-17 11:38:19 +08:00
										 |  |  | 				ObjectMeta: metav1.ObjectMeta{ | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 					Labels: map[string]string{ | 
					
						
							|  |  |  | 						"name": "foo", | 
					
						
							|  |  |  | 						"type": "production", | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-11-19 04:50:17 +08:00
										 |  |  | 				Spec: v1.PodSpec{ | 
					
						
							|  |  |  | 					Containers: []v1.Container{ | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 						{ | 
					
						
							|  |  |  | 							Image: "foo/bar", | 
					
						
							| 
									
										
										
										
											2016-11-19 04:50:17 +08:00
										 |  |  | 							TerminationMessagePath: v1.TerminationMessagePathDefault, | 
					
						
							|  |  |  | 							ImagePullPolicy:        v1.PullIfNotPresent, | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 							SecurityContext:        securitycontext.ValidSecurityContextWithContainerDefaults(), | 
					
						
							|  |  |  | 						}, | 
					
						
							|  |  |  | 					}, | 
					
						
							| 
									
										
										
										
											2016-11-19 04:50:17 +08:00
										 |  |  | 					RestartPolicy: v1.RestartPolicyAlways, | 
					
						
							|  |  |  | 					DNSPolicy:     v1.DNSDefault, | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 					NodeSelector: map[string]string{ | 
					
						
							|  |  |  | 						"baz": "blah", | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return rc | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // create count pods with the given phase for the given rc (same selectors and namespace), and add them to the store.
 | 
					
						
							| 
									
										
										
										
											2016-11-19 04:50:17 +08:00
										 |  |  | func newPodList(store cache.Store, count int, status v1.PodPhase, rc *v1.ReplicationController) *v1.PodList { | 
					
						
							|  |  |  | 	pods := []v1.Pod{} | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 	for i := 0; i < count; i++ { | 
					
						
							| 
									
										
										
										
											2016-11-19 04:50:17 +08:00
										 |  |  | 		newPod := v1.Pod{ | 
					
						
							| 
									
										
										
										
											2017-01-17 11:38:19 +08:00
										 |  |  | 			ObjectMeta: metav1.ObjectMeta{ | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 				Name:      fmt.Sprintf("pod%d", i), | 
					
						
							|  |  |  | 				Labels:    rc.Spec.Selector, | 
					
						
							|  |  |  | 				Namespace: rc.Namespace, | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-11-19 04:50:17 +08:00
										 |  |  | 			Status: v1.PodStatus{Phase: status}, | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if store != nil { | 
					
						
							|  |  |  | 			store.Add(&newPod) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		pods = append(pods, newPod) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-11-19 04:50:17 +08:00
										 |  |  | 	return &v1.PodList{ | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 		Items: pods, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-09 06:18:13 +08:00
										 |  |  | func newReplicaSet(name string, replicas int) *extensions.ReplicaSet { | 
					
						
							|  |  |  | 	return &extensions.ReplicaSet{ | 
					
						
							|  |  |  | 		TypeMeta: metav1.TypeMeta{APIVersion: api.Registry.GroupOrDie(v1.GroupName).GroupVersion.String()}, | 
					
						
							|  |  |  | 		ObjectMeta: metav1.ObjectMeta{ | 
					
						
							|  |  |  | 			UID:             uuid.NewUUID(), | 
					
						
							|  |  |  | 			Name:            name, | 
					
						
							|  |  |  | 			Namespace:       metav1.NamespaceDefault, | 
					
						
							|  |  |  | 			ResourceVersion: "18", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		Spec: extensions.ReplicaSetSpec{ | 
					
						
							|  |  |  | 			Replicas: func() *int32 { i := int32(replicas); return &i }(), | 
					
						
							|  |  |  | 			Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}}, | 
					
						
							|  |  |  | 			Template: v1.PodTemplateSpec{ | 
					
						
							|  |  |  | 				ObjectMeta: metav1.ObjectMeta{ | 
					
						
							|  |  |  | 					Labels: map[string]string{ | 
					
						
							|  |  |  | 						"name": "foo", | 
					
						
							|  |  |  | 						"type": "production", | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				Spec: v1.PodSpec{ | 
					
						
							|  |  |  | 					Containers: []v1.Container{ | 
					
						
							|  |  |  | 						{ | 
					
						
							|  |  |  | 							Image: "foo/bar", | 
					
						
							|  |  |  | 							TerminationMessagePath: v1.TerminationMessagePathDefault, | 
					
						
							|  |  |  | 							ImagePullPolicy:        v1.PullIfNotPresent, | 
					
						
							|  |  |  | 							SecurityContext:        securitycontext.ValidSecurityContextWithContainerDefaults(), | 
					
						
							|  |  |  | 						}, | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					RestartPolicy: v1.RestartPolicyAlways, | 
					
						
							|  |  |  | 					DNSPolicy:     v1.DNSDefault, | 
					
						
							|  |  |  | 					NodeSelector: map[string]string{ | 
					
						
							|  |  |  | 						"baz": "blah", | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | func TestControllerExpectations(t *testing.T) { | 
					
						
							|  |  |  | 	ttl := 30 * time.Second | 
					
						
							|  |  |  | 	e, fakeClock := NewFakeControllerExpectationsLookup(ttl) | 
					
						
							|  |  |  | 	// In practice we can't really have add and delete expectations since we only either create or
 | 
					
						
							|  |  |  | 	// delete replicas in one rc pass, and the rc goes to sleep soon after until the expectations are
 | 
					
						
							|  |  |  | 	// either fulfilled or timeout.
 | 
					
						
							|  |  |  | 	adds, dels := 10, 30 | 
					
						
							|  |  |  | 	rc := newReplicationController(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// RC fires off adds and deletes at apiserver, then sets expectations
 | 
					
						
							|  |  |  | 	rcKey, err := KeyFunc(rc) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-06-14 20:04:38 +08:00
										 |  |  | 		t.Errorf("Couldn't get key for object %#v: %v", rc, err) | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	e.SetExpectations(rcKey, adds, dels) | 
					
						
							|  |  |  | 	var wg sync.WaitGroup | 
					
						
							|  |  |  | 	for i := 0; i < adds+1; i++ { | 
					
						
							|  |  |  | 		wg.Add(1) | 
					
						
							|  |  |  | 		go func() { | 
					
						
							|  |  |  | 			// In prod this can happen either because of a failed create by the rc
 | 
					
						
							|  |  |  | 			// or after having observed a create via informer
 | 
					
						
							|  |  |  | 			e.CreationObserved(rcKey) | 
					
						
							|  |  |  | 			wg.Done() | 
					
						
							|  |  |  | 		}() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	wg.Wait() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// There are still delete expectations
 | 
					
						
							|  |  |  | 	if e.SatisfiedExpectations(rcKey) { | 
					
						
							|  |  |  | 		t.Errorf("Rc will sync before expectations are met") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for i := 0; i < dels+1; i++ { | 
					
						
							|  |  |  | 		wg.Add(1) | 
					
						
							|  |  |  | 		go func() { | 
					
						
							|  |  |  | 			e.DeletionObserved(rcKey) | 
					
						
							|  |  |  | 			wg.Done() | 
					
						
							|  |  |  | 		}() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	wg.Wait() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Expectations have been surpassed
 | 
					
						
							|  |  |  | 	if podExp, exists, err := e.GetExpectations(rcKey); err == nil && exists { | 
					
						
							|  |  |  | 		add, del := podExp.GetExpectations() | 
					
						
							|  |  |  | 		if add != -1 || del != -1 { | 
					
						
							|  |  |  | 			t.Errorf("Unexpected pod expectations %#v", podExp) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		t.Errorf("Could not get expectations for rc, exists %v and err %v", exists, err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if !e.SatisfiedExpectations(rcKey) { | 
					
						
							|  |  |  | 		t.Errorf("Expectations are met but the rc will not sync") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Next round of rc sync, old expectations are cleared
 | 
					
						
							|  |  |  | 	e.SetExpectations(rcKey, 1, 2) | 
					
						
							|  |  |  | 	if podExp, exists, err := e.GetExpectations(rcKey); err == nil && exists { | 
					
						
							|  |  |  | 		add, del := podExp.GetExpectations() | 
					
						
							|  |  |  | 		if add != 1 || del != 2 { | 
					
						
							|  |  |  | 			t.Errorf("Unexpected pod expectations %#v", podExp) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		t.Errorf("Could not get expectations for rc, exists %v and err %v", exists, err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Expectations have expired because of ttl
 | 
					
						
							| 
									
										
										
										
											2016-02-02 02:50:05 +08:00
										 |  |  | 	fakeClock.Step(ttl + 1) | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 	if !e.SatisfiedExpectations(rcKey) { | 
					
						
							|  |  |  | 		t.Errorf("Expectations should have expired but didn't") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-05 08:51:01 +08:00
										 |  |  | func TestUIDExpectations(t *testing.T) { | 
					
						
							|  |  |  | 	uidExp := NewUIDTrackingControllerExpectations(NewControllerExpectations()) | 
					
						
							| 
									
										
										
										
											2016-11-19 04:50:17 +08:00
										 |  |  | 	rcList := []*v1.ReplicationController{ | 
					
						
							| 
									
										
										
										
											2016-03-05 08:51:01 +08:00
										 |  |  | 		newReplicationController(2), | 
					
						
							|  |  |  | 		newReplicationController(1), | 
					
						
							|  |  |  | 		newReplicationController(0), | 
					
						
							|  |  |  | 		newReplicationController(5), | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	rcToPods := map[string][]string{} | 
					
						
							|  |  |  | 	rcKeys := []string{} | 
					
						
							|  |  |  | 	for i := range rcList { | 
					
						
							|  |  |  | 		rc := rcList[i] | 
					
						
							|  |  |  | 		rcName := fmt.Sprintf("rc-%v", i) | 
					
						
							|  |  |  | 		rc.Name = rcName | 
					
						
							|  |  |  | 		rc.Spec.Selector[rcName] = rcName | 
					
						
							| 
									
										
										
										
											2016-11-19 04:50:17 +08:00
										 |  |  | 		podList := newPodList(nil, 5, v1.PodRunning, rc) | 
					
						
							| 
									
										
										
										
											2016-03-05 08:51:01 +08:00
										 |  |  | 		rcKey, err := KeyFunc(rc) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2016-06-14 20:04:38 +08:00
										 |  |  | 			t.Fatalf("Couldn't get key for object %#v: %v", rc, err) | 
					
						
							| 
									
										
										
										
											2016-03-05 08:51:01 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		rcKeys = append(rcKeys, rcKey) | 
					
						
							|  |  |  | 		rcPodNames := []string{} | 
					
						
							|  |  |  | 		for i := range podList.Items { | 
					
						
							|  |  |  | 			p := &podList.Items[i] | 
					
						
							|  |  |  | 			p.Name = fmt.Sprintf("%v-%v", p.Name, rc.Name) | 
					
						
							|  |  |  | 			rcPodNames = append(rcPodNames, PodKey(p)) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		rcToPods[rcKey] = rcPodNames | 
					
						
							|  |  |  | 		uidExp.ExpectDeletions(rcKey, rcPodNames) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for i := range rcKeys { | 
					
						
							|  |  |  | 		j := rand.Intn(i + 1) | 
					
						
							|  |  |  | 		rcKeys[i], rcKeys[j] = rcKeys[j], rcKeys[i] | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for _, rcKey := range rcKeys { | 
					
						
							|  |  |  | 		if uidExp.SatisfiedExpectations(rcKey) { | 
					
						
							|  |  |  | 			t.Errorf("Controller %v satisfied expectations before deletion", rcKey) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		for _, p := range rcToPods[rcKey] { | 
					
						
							|  |  |  | 			uidExp.DeletionObserved(rcKey, p) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if !uidExp.SatisfiedExpectations(rcKey) { | 
					
						
							|  |  |  | 			t.Errorf("Controller %v didn't satisfy expectations after deletion", rcKey) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		uidExp.DeleteExpectations(rcKey) | 
					
						
							|  |  |  | 		if uidExp.GetUIDs(rcKey) != nil { | 
					
						
							|  |  |  | 			t.Errorf("Failed to delete uid expectations for %v", rcKey) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-27 20:19:35 +08:00
										 |  |  | func TestCreatePods(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2017-01-22 11:36:02 +08:00
										 |  |  | 	ns := metav1.NamespaceDefault | 
					
						
							| 
									
										
										
										
											2017-01-17 11:38:19 +08:00
										 |  |  | 	body := runtime.EncodeOrDie(testapi.Default.Codec(), &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "empty_pod"}}) | 
					
						
							| 
									
										
										
										
											2016-01-15 14:33:50 +08:00
										 |  |  | 	fakeHandler := utiltesting.FakeHandler{ | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 		StatusCode:   200, | 
					
						
							|  |  |  | 		ResponseBody: string(body), | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	testServer := httptest.NewServer(&fakeHandler) | 
					
						
							| 
									
										
										
										
											2016-04-21 19:50:55 +08:00
										 |  |  | 	defer testServer.Close() | 
					
						
							| 
									
										
										
										
											2017-01-13 02:17:43 +08:00
										 |  |  | 	clientset := clientset.NewForConfigOrDie(&restclient.Config{Host: testServer.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &api.Registry.GroupOrDie(v1.GroupName).GroupVersion}}) | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	podControl := RealPodControl{ | 
					
						
							| 
									
										
										
										
											2016-01-15 13:00:58 +08:00
										 |  |  | 		KubeClient: clientset, | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 		Recorder:   &record.FakeRecorder{}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	controllerSpec := newReplicationController(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Make sure createReplica sends a POST to the apiserver with a pod from the controllers pod template
 | 
					
						
							| 
									
										
										
										
											2016-06-11 07:28:42 +08:00
										 |  |  | 	if err := podControl.CreatePods(ns, controllerSpec.Spec.Template, controllerSpec); err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("unexpected error: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-19 04:50:17 +08:00
										 |  |  | 	expectedPod := v1.Pod{ | 
					
						
							| 
									
										
										
										
											2017-01-17 11:38:19 +08:00
										 |  |  | 		ObjectMeta: metav1.ObjectMeta{ | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 			Labels:       controllerSpec.Spec.Template.Labels, | 
					
						
							|  |  |  | 			GenerateName: fmt.Sprintf("%s-", controllerSpec.Name), | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		Spec: controllerSpec.Spec.Template.Spec, | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-22 11:36:02 +08:00
										 |  |  | 	fakeHandler.ValidateRequest(t, testapi.Default.ResourcePath("pods", metav1.NamespaceDefault, ""), "POST", nil) | 
					
						
							| 
									
										
										
										
											2016-11-19 04:50:17 +08:00
										 |  |  | 	var actualPod = &v1.Pod{} | 
					
						
							|  |  |  | 	err := json.Unmarshal([]byte(fakeHandler.RequestBody), actualPod) | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-06-11 07:28:42 +08:00
										 |  |  | 		t.Fatalf("Unexpected error: %v", err) | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-25 21:39:54 +08:00
										 |  |  | 	if !apiequality.Semantic.DeepDerivative(&expectedPod, actualPod) { | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 		t.Logf("Body: %s", fakeHandler.RequestBody) | 
					
						
							|  |  |  | 		t.Errorf("Unexpected mismatch.  Expected\n %#v,\n Got:\n %#v", &expectedPod, actualPod) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestActivePodFiltering(t *testing.T) { | 
					
						
							|  |  |  | 	// This rc is not needed by the test, only the newPodList to give the pods labels/a namespace.
 | 
					
						
							|  |  |  | 	rc := newReplicationController(0) | 
					
						
							| 
									
										
										
										
											2016-11-19 04:50:17 +08:00
										 |  |  | 	podList := newPodList(nil, 5, v1.PodRunning, rc) | 
					
						
							|  |  |  | 	podList.Items[0].Status.Phase = v1.PodSucceeded | 
					
						
							|  |  |  | 	podList.Items[1].Status.Phase = v1.PodFailed | 
					
						
							| 
									
										
										
										
											2015-09-10 01:45:01 +08:00
										 |  |  | 	expectedNames := sets.NewString() | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 	for _, pod := range podList.Items[2:] { | 
					
						
							|  |  |  | 		expectedNames.Insert(pod.Name) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-19 04:50:17 +08:00
										 |  |  | 	var podPointers []*v1.Pod | 
					
						
							| 
									
										
										
										
											2016-08-18 04:05:37 +08:00
										 |  |  | 	for i := range podList.Items { | 
					
						
							|  |  |  | 		podPointers = append(podPointers, &podList.Items[i]) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	got := FilterActivePods(podPointers) | 
					
						
							| 
									
										
										
										
											2015-09-10 01:45:01 +08:00
										 |  |  | 	gotNames := sets.NewString() | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 	for _, pod := range got { | 
					
						
							|  |  |  | 		gotNames.Insert(pod.Name) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if expectedNames.Difference(gotNames).Len() != 0 || gotNames.Difference(expectedNames).Len() != 0 { | 
					
						
							|  |  |  | 		t.Errorf("expected %v, got %v", expectedNames.List(), gotNames.List()) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestSortingActivePods(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-02-27 07:32:49 +08:00
										 |  |  | 	numPods := 9 | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 	// This rc is not needed by the test, only the newPodList to give the pods labels/a namespace.
 | 
					
						
							|  |  |  | 	rc := newReplicationController(0) | 
					
						
							| 
									
										
										
										
											2016-11-19 04:50:17 +08:00
										 |  |  | 	podList := newPodList(nil, numPods, v1.PodRunning, rc) | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-19 04:50:17 +08:00
										 |  |  | 	pods := make([]*v1.Pod, len(podList.Items)) | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 	for i := range podList.Items { | 
					
						
							|  |  |  | 		pods[i] = &podList.Items[i] | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// pods[0] is not scheduled yet.
 | 
					
						
							|  |  |  | 	pods[0].Spec.NodeName = "" | 
					
						
							| 
									
										
										
										
											2016-11-19 04:50:17 +08:00
										 |  |  | 	pods[0].Status.Phase = v1.PodPending | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 	// pods[1] is scheduled but pending.
 | 
					
						
							|  |  |  | 	pods[1].Spec.NodeName = "bar" | 
					
						
							| 
									
										
										
										
											2016-11-19 04:50:17 +08:00
										 |  |  | 	pods[1].Status.Phase = v1.PodPending | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 	// pods[2] is unknown.
 | 
					
						
							|  |  |  | 	pods[2].Spec.NodeName = "foo" | 
					
						
							| 
									
										
										
										
											2016-11-19 04:50:17 +08:00
										 |  |  | 	pods[2].Status.Phase = v1.PodUnknown | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 	// pods[3] is running but not ready.
 | 
					
						
							|  |  |  | 	pods[3].Spec.NodeName = "foo" | 
					
						
							| 
									
										
										
										
											2016-11-19 04:50:17 +08:00
										 |  |  | 	pods[3].Status.Phase = v1.PodRunning | 
					
						
							| 
									
										
										
										
											2016-02-27 07:32:49 +08:00
										 |  |  | 	// pods[4] is running and ready but without LastTransitionTime.
 | 
					
						
							| 
									
										
										
										
											2016-12-04 02:57:26 +08:00
										 |  |  | 	now := metav1.Now() | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 	pods[4].Spec.NodeName = "foo" | 
					
						
							| 
									
										
										
										
											2016-11-19 04:50:17 +08:00
										 |  |  | 	pods[4].Status.Phase = v1.PodRunning | 
					
						
							|  |  |  | 	pods[4].Status.Conditions = []v1.PodCondition{{Type: v1.PodReady, Status: v1.ConditionTrue}} | 
					
						
							|  |  |  | 	pods[4].Status.ContainerStatuses = []v1.ContainerStatus{{RestartCount: 3}, {RestartCount: 0}} | 
					
						
							| 
									
										
										
										
											2016-02-27 07:32:49 +08:00
										 |  |  | 	// pods[5] is running and ready and with LastTransitionTime.
 | 
					
						
							| 
									
										
										
										
											2016-02-27 04:02:05 +08:00
										 |  |  | 	pods[5].Spec.NodeName = "foo" | 
					
						
							| 
									
										
										
										
											2016-11-19 04:50:17 +08:00
										 |  |  | 	pods[5].Status.Phase = v1.PodRunning | 
					
						
							|  |  |  | 	pods[5].Status.Conditions = []v1.PodCondition{{Type: v1.PodReady, Status: v1.ConditionTrue, LastTransitionTime: now}} | 
					
						
							|  |  |  | 	pods[5].Status.ContainerStatuses = []v1.ContainerStatus{{RestartCount: 3}, {RestartCount: 0}} | 
					
						
							| 
									
										
										
										
											2016-02-27 07:32:49 +08:00
										 |  |  | 	// pods[6] is running ready for a longer time than pods[5].
 | 
					
						
							| 
									
										
										
										
											2016-12-04 02:57:26 +08:00
										 |  |  | 	then := metav1.Time{Time: now.AddDate(0, -1, 0)} | 
					
						
							| 
									
										
										
										
											2016-02-27 04:02:05 +08:00
										 |  |  | 	pods[6].Spec.NodeName = "foo" | 
					
						
							| 
									
										
										
										
											2016-11-19 04:50:17 +08:00
										 |  |  | 	pods[6].Status.Phase = v1.PodRunning | 
					
						
							|  |  |  | 	pods[6].Status.Conditions = []v1.PodCondition{{Type: v1.PodReady, Status: v1.ConditionTrue, LastTransitionTime: then}} | 
					
						
							|  |  |  | 	pods[6].Status.ContainerStatuses = []v1.ContainerStatus{{RestartCount: 3}, {RestartCount: 0}} | 
					
						
							| 
									
										
										
										
											2016-02-27 07:32:49 +08:00
										 |  |  | 	// pods[7] has lower container restart count than pods[6].
 | 
					
						
							| 
									
										
										
										
											2016-02-27 04:02:05 +08:00
										 |  |  | 	pods[7].Spec.NodeName = "foo" | 
					
						
							| 
									
										
										
										
											2016-11-19 04:50:17 +08:00
										 |  |  | 	pods[7].Status.Phase = v1.PodRunning | 
					
						
							|  |  |  | 	pods[7].Status.Conditions = []v1.PodCondition{{Type: v1.PodReady, Status: v1.ConditionTrue, LastTransitionTime: then}} | 
					
						
							|  |  |  | 	pods[7].Status.ContainerStatuses = []v1.ContainerStatus{{RestartCount: 2}, {RestartCount: 1}} | 
					
						
							| 
									
										
										
										
											2016-02-27 07:32:49 +08:00
										 |  |  | 	pods[7].CreationTimestamp = now | 
					
						
							|  |  |  | 	// pods[8] is older than pods[7].
 | 
					
						
							|  |  |  | 	pods[8].Spec.NodeName = "foo" | 
					
						
							| 
									
										
										
										
											2016-11-19 04:50:17 +08:00
										 |  |  | 	pods[8].Status.Phase = v1.PodRunning | 
					
						
							|  |  |  | 	pods[8].Status.Conditions = []v1.PodCondition{{Type: v1.PodReady, Status: v1.ConditionTrue, LastTransitionTime: then}} | 
					
						
							|  |  |  | 	pods[8].Status.ContainerStatuses = []v1.ContainerStatus{{RestartCount: 2}, {RestartCount: 1}} | 
					
						
							| 
									
										
										
										
											2016-02-27 07:32:49 +08:00
										 |  |  | 	pods[8].CreationTimestamp = then | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-19 04:50:17 +08:00
										 |  |  | 	getOrder := func(pods []*v1.Pod) []string { | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 		names := make([]string, len(pods)) | 
					
						
							|  |  |  | 		for i := range pods { | 
					
						
							|  |  |  | 			names[i] = pods[i].Name | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return names | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	expected := getOrder(pods) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i := 0; i < 20; i++ { | 
					
						
							|  |  |  | 		idx := rand.Perm(numPods) | 
					
						
							| 
									
										
										
										
											2016-11-19 04:50:17 +08:00
										 |  |  | 		randomizedPods := make([]*v1.Pod, numPods) | 
					
						
							| 
									
										
										
										
											2015-07-28 09:21:37 +08:00
										 |  |  | 		for j := 0; j < numPods; j++ { | 
					
						
							|  |  |  | 			randomizedPods[j] = pods[idx[j]] | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		sort.Sort(ActivePods(randomizedPods)) | 
					
						
							|  |  |  | 		actual := getOrder(randomizedPods) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if !reflect.DeepEqual(actual, expected) { | 
					
						
							|  |  |  | 			t.Errorf("expected %v, got %v", expected, actual) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-02-09 06:18:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestActiveReplicaSetsFiltering(t *testing.T) { | 
					
						
							|  |  |  | 	var replicaSets []*extensions.ReplicaSet | 
					
						
							|  |  |  | 	replicaSets = append(replicaSets, newReplicaSet("zero", 0)) | 
					
						
							|  |  |  | 	replicaSets = append(replicaSets, nil) | 
					
						
							|  |  |  | 	replicaSets = append(replicaSets, newReplicaSet("foo", 1)) | 
					
						
							|  |  |  | 	replicaSets = append(replicaSets, newReplicaSet("bar", 2)) | 
					
						
							|  |  |  | 	expectedNames := sets.NewString() | 
					
						
							|  |  |  | 	for _, rs := range replicaSets[2:] { | 
					
						
							|  |  |  | 		expectedNames.Insert(rs.Name) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	got := FilterActiveReplicaSets(replicaSets) | 
					
						
							|  |  |  | 	gotNames := sets.NewString() | 
					
						
							|  |  |  | 	for _, rs := range got { | 
					
						
							|  |  |  | 		gotNames.Insert(rs.Name) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if expectedNames.Difference(gotNames).Len() != 0 || gotNames.Difference(expectedNames).Len() != 0 { | 
					
						
							|  |  |  | 		t.Errorf("expected %v, got %v", expectedNames.List(), gotNames.List()) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-05-18 07:53:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func int64P(num int64) *int64 { | 
					
						
							|  |  |  | 	return &num | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestComputeHash(t *testing.T) { | 
					
						
							|  |  |  | 	tests := []struct { | 
					
						
							|  |  |  | 		name                string | 
					
						
							|  |  |  | 		template            *v1.PodTemplateSpec | 
					
						
							|  |  |  | 		collisionCount      *int64 | 
					
						
							|  |  |  | 		otherCollisionCount *int64 | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			name:                "simple", | 
					
						
							|  |  |  | 			template:            &v1.PodTemplateSpec{}, | 
					
						
							|  |  |  | 			collisionCount:      int64P(1), | 
					
						
							|  |  |  | 			otherCollisionCount: int64P(2), | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			name:                "using math.MaxInt64", | 
					
						
							|  |  |  | 			template:            &v1.PodTemplateSpec{}, | 
					
						
							|  |  |  | 			collisionCount:      nil, | 
					
						
							|  |  |  | 			otherCollisionCount: int64P(int64(math.MaxInt64)), | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, test := range tests { | 
					
						
							|  |  |  | 		hash := ComputeHash(test.template, test.collisionCount) | 
					
						
							|  |  |  | 		otherHash := ComputeHash(test.template, test.otherCollisionCount) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if hash == otherHash { | 
					
						
							|  |  |  | 			t.Errorf("expected different hashes but got the same: %d", hash) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |