Move orphaned Pod deletion logic to PodGC

This commit is contained in:
gmarek
2016-09-12 16:47:17 +02:00
parent dc06ceb87d
commit cb0a13c1e5
7 changed files with 213 additions and 201 deletions

View File

@@ -23,12 +23,12 @@ import (
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/client/cache"
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
"k8s.io/kubernetes/pkg/controller"
"k8s.io/kubernetes/pkg/util/sets"
)
func TestGC(t *testing.T) {
func TestGCTerminated(t *testing.T) {
type nameToPhase struct {
name string
phase api.PodPhase
@@ -44,9 +44,28 @@ func TestGC(t *testing.T) {
{name: "a", phase: api.PodFailed},
{name: "b", phase: api.PodSucceeded},
},
threshold: 0,
threshold: 0,
// threshold = 0 disables terminated pod deletion
deletedPodNames: sets.NewString(),
},
{
pods: []nameToPhase{
{name: "a", phase: api.PodFailed},
{name: "b", phase: api.PodSucceeded},
{name: "c", phase: api.PodFailed},
},
threshold: 1,
deletedPodNames: sets.NewString("a", "b"),
},
{
pods: []nameToPhase{
{name: "a", phase: api.PodRunning},
{name: "b", phase: api.PodSucceeded},
{name: "c", phase: api.PodFailed},
},
threshold: 1,
deletedPodNames: sets.NewString("b"),
},
{
pods: []nameToPhase{
{name: "a", phase: api.PodFailed},
@@ -67,7 +86,7 @@ func TestGC(t *testing.T) {
for i, test := range testCases {
client := fake.NewSimpleClientset()
gcc := New(client, controller.NoResyncPeriodFunc, test.threshold)
gcc := NewFromClient(client, test.threshold)
deletedPodNames := make([]string, 0)
var lock sync.Mutex
gcc.deletePod = func(_, name string) error {
@@ -83,9 +102,86 @@ func TestGC(t *testing.T) {
gcc.podStore.Indexer.Add(&api.Pod{
ObjectMeta: api.ObjectMeta{Name: pod.name, CreationTimestamp: unversioned.Time{Time: creationTime}},
Status: api.PodStatus{Phase: pod.phase},
Spec: api.PodSpec{NodeName: "node"},
})
}
store := cache.NewStore(cache.MetaNamespaceKeyFunc)
store.Add(&api.Node{
ObjectMeta: api.ObjectMeta{Name: "node"},
})
gcc.nodeStore = cache.StoreToNodeLister{Store: store}
gcc.gc()
pass := true
for _, pod := range deletedPodNames {
if !test.deletedPodNames.Has(pod) {
pass = false
}
}
if len(deletedPodNames) != len(test.deletedPodNames) {
pass = false
}
if !pass {
t.Errorf("[%v]pod's deleted expected and actual did not match.\n\texpected: %v\n\tactual: %v", i, test.deletedPodNames, deletedPodNames)
}
}
}
func TestGCOrphaned(t *testing.T) {
type nameToPhase struct {
name string
phase api.PodPhase
}
testCases := []struct {
pods []nameToPhase
threshold int
deletedPodNames sets.String
}{
{
pods: []nameToPhase{
{name: "a", phase: api.PodFailed},
{name: "b", phase: api.PodSucceeded},
},
threshold: 0,
deletedPodNames: sets.NewString("a", "b"),
},
{
pods: []nameToPhase{
{name: "a", phase: api.PodRunning},
},
threshold: 1,
deletedPodNames: sets.NewString("a"),
},
}
for i, test := range testCases {
client := fake.NewSimpleClientset()
gcc := NewFromClient(client, test.threshold)
deletedPodNames := make([]string, 0)
var lock sync.Mutex
gcc.deletePod = func(_, name string) error {
lock.Lock()
defer lock.Unlock()
deletedPodNames = append(deletedPodNames, name)
return nil
}
creationTime := time.Unix(0, 0)
for _, pod := range test.pods {
creationTime = creationTime.Add(1 * time.Hour)
gcc.podStore.Indexer.Add(&api.Pod{
ObjectMeta: api.ObjectMeta{Name: pod.name, CreationTimestamp: unversioned.Time{Time: creationTime}},
Status: api.PodStatus{Phase: pod.phase},
Spec: api.PodSpec{NodeName: "node"},
})
}
store := cache.NewStore(cache.MetaNamespaceKeyFunc)
gcc.nodeStore = cache.StoreToNodeLister{Store: store}
gcc.gc()
pass := true