Avoid computing super-expensive DeepEqual on every pod update

This commit is contained in:
Wojciech Tyczynski
2016-08-09 15:57:21 +02:00
parent 1c9e623045
commit acfd8c787f
9 changed files with 58 additions and 31 deletions

View File

@@ -367,12 +367,13 @@ func (rm *ReplicationManager) addPod(obj interface{}) {
// up. If the labels of the pod have changed we need to awaken both the old
// and new controller. old and cur must be *api.Pod types.
func (rm *ReplicationManager) updatePod(old, cur interface{}) {
if api.Semantic.DeepEqual(old, cur) {
// A periodic relist will send update events for all known pods.
return
}
curPod := cur.(*api.Pod)
oldPod := old.(*api.Pod)
if curPod.ResourceVersion == oldPod.ResourceVersion {
// Periodic resync will send update events for all known pods.
// Two different versions of the same pod will always have different RVs.
return
}
glog.V(4).Infof("Pod %s updated, objectMeta %+v -> %+v.", curPod.Name, oldPod.ObjectMeta, curPod.ObjectMeta)
labelChanged := !reflect.DeepEqual(curPod.Labels, oldPod.Labels)
if curPod.DeletionTimestamp != nil {

View File

@@ -547,8 +547,10 @@ func TestUpdatePods(t *testing.T) {
// testControllerSpec1, then update its labels to match testControllerSpec2.
// We expect to receive a sync request for both controllers.
pod1 := newPodList(manager.podStore.Indexer, 1, api.PodRunning, testControllerSpec1, "pod").Items[0]
pod1.ResourceVersion = "1"
pod2 := pod1
pod2.Labels = testControllerSpec2.Spec.Selector
pod2.ResourceVersion = "2"
manager.updatePod(&pod1, &pod2)
expected := sets.NewString(testControllerSpec1.Name, testControllerSpec2.Name)
for _, name := range expected.List() {
@@ -567,6 +569,7 @@ func TestUpdatePods(t *testing.T) {
// 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() {
@@ -969,6 +972,7 @@ func TestDeletionTimestamp(t *testing.T) {
}
pod := newPodList(nil, 1, api.PodPending, controllerSpec, "pod").Items[0]
pod.DeletionTimestamp = &unversioned.Time{Time: time.Now()}
pod.ResourceVersion = "1"
manager.expectations.ExpectDeletions(rcKey, []string{controller.PodKey(&pod)})
// A pod added with a deletion timestamp should decrement deletions, not creations.
@@ -988,6 +992,7 @@ func TestDeletionTimestamp(t *testing.T) {
// An update from no deletion timestamp to having one should be treated
// as a deletion.
oldPod := newPodList(nil, 1, api.PodPending, controllerSpec, "pod").Items[0]
oldPod.ResourceVersion = "2"
manager.expectations.ExpectDeletions(rcKey, []string{controller.PodKey(&pod)})
manager.updatePod(&oldPod, &pod)
@@ -1013,6 +1018,7 @@ func TestDeletionTimestamp(t *testing.T) {
}
manager.expectations.ExpectDeletions(rcKey, []string{controller.PodKey(secondPod)})
oldPod.DeletionTimestamp = &unversioned.Time{Time: time.Now()}
oldPod.ResourceVersion = "2"
manager.updatePod(&oldPod, &pod)
podExp, exists, err = manager.expectations.GetExpectations(rcKey)
@@ -1239,12 +1245,14 @@ func TestUpdateLabelsRemoveControllerRef(t *testing.T) {
manager.rcStore.Indexer.Add(rc)
// put one pod in the podStore
pod := newPod("pod", rc, api.PodRunning)
pod.ResourceVersion = "1"
var trueVar = true
rcOwnerReference := api.OwnerReference{UID: rc.UID, APIVersion: "v1", Kind: "ReplicationController", Name: rc.Name, Controller: &trueVar}
pod.OwnerReferences = []api.OwnerReference{rcOwnerReference}
updatedPod := *pod
// reset the labels
updatedPod.Labels = make(map[string]string)
updatedPod.ResourceVersion = "2"
// 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).