Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c0a8981

Browse files
committed
Delete expectations of a deleted rc instead of letting them expire
1 parent 520f84a commit c0a8981

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

pkg/controller/controller_utils.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ var expKeyFunc = func(obj interface{}) (string, error) {
6363
type RCExpectationsManager interface {
6464
GetExpectations(rc *api.ReplicationController) (*PodExpectations, bool, error)
6565
SatisfiedExpectations(rc *api.ReplicationController) bool
66+
DeleteExpectations(rcKey string)
6667
ExpectCreations(rc *api.ReplicationController, adds int) error
6768
ExpectDeletions(rc *api.ReplicationController, dels int) error
6869
CreationObserved(rc *api.ReplicationController)
@@ -87,6 +88,15 @@ func (r *RCExpectations) GetExpectations(rc *api.ReplicationController) (*PodExp
8788
}
8889
}
8990

91+
// DeleteExpectations deletes the expectations of the given RC from the TTLStore.
92+
func (r *RCExpectations) DeleteExpectations(rcKey string) {
93+
if podExp, exists, err := r.GetByKey(rcKey); err == nil && exists {
94+
if err := r.Delete(podExp); err != nil {
95+
glog.V(2).Infof("Error deleting expectations for rc %v: %v", rcKey, err)
96+
}
97+
}
98+
}
99+
90100
// SatisfiedExpectations returns true if the replication manager has observed the required adds/dels
91101
// for the given rc. Add/del counts are established by the rc at sync time, and updated as pods
92102
// are observed by the replication manager's podController.

pkg/controller/replication_controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ func (rm *ReplicationManager) syncReplicationController(key string) error {
351351
obj, exists, err := rm.controllerStore.Store.GetByKey(key)
352352
if !exists {
353353
glog.Infof("Replication Controller has been deleted %v", key)
354+
rm.expectations.DeleteExpectations(key)
354355
return nil
355356
}
356357
if err != nil {

pkg/controller/replication_controller_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,3 +990,38 @@ func TestRCSyncExpectations(t *testing.T) {
990990
manager.syncReplicationController(getKey(controllerSpec, t))
991991
validateSyncReplication(t, &fakePodControl, 0, 0)
992992
}
993+
994+
func TestDeleteControllerAndExpectations(t *testing.T) {
995+
client := client.NewOrDie(&client.Config{Host: "", Version: testapi.Version()})
996+
manager := NewReplicationManager(client, 10)
997+
998+
rc := newReplicationController(1)
999+
manager.controllerStore.Store.Add(rc)
1000+
1001+
fakePodControl := FakePodControl{}
1002+
manager.podControl = &fakePodControl
1003+
1004+
// This should set expectations for the rc
1005+
manager.syncReplicationController(getKey(rc, t))
1006+
validateSyncReplication(t, &fakePodControl, 1, 0)
1007+
fakePodControl.clear()
1008+
1009+
// This is to simulate a concurrent addPod, that has a handle on the expectations
1010+
// as the controller deletes it.
1011+
podExp, exists, err := manager.expectations.GetExpectations(rc)
1012+
if !exists || err != nil {
1013+
t.Errorf("No expectations found for rc")
1014+
}
1015+
manager.controllerStore.Delete(rc)
1016+
manager.syncReplicationController(getKey(rc, t))
1017+
1018+
if _, exists, err = manager.expectations.GetExpectations(rc); exists {
1019+
t.Errorf("Found expectaions, expected none since the rc has been deleted.")
1020+
}
1021+
1022+
// This should have no effect, since we've deleted the rc.
1023+
podExp.Seen(1, 0)
1024+
manager.podStore.Store.Replace(make([]interface{}, 0))
1025+
manager.syncReplicationController(getKey(rc, t))
1026+
validateSyncReplication(t, &fakePodControl, 0, 0)
1027+
}

0 commit comments

Comments
 (0)