register all pending pod deletions and check for kill

do not delete the cgroup from a pod when it is being killed
This commit is contained in:
Ryan Phillips
2021-01-26 16:22:08 -06:00
parent 35bc0fbad5
commit f918e11e3a
6 changed files with 473 additions and 67 deletions

View File

@@ -106,13 +106,14 @@ func (f *fakeImageGCManager) GetImageList() ([]kubecontainer.Image, error) {
}
type TestKubelet struct {
kubelet *Kubelet
fakeRuntime *containertest.FakeRuntime
fakeKubeClient *fake.Clientset
fakeMirrorClient *podtest.FakeMirrorClient
fakeClock *clock.FakeClock
mounter mount.Interface
volumePlugin *volumetest.FakeVolumePlugin
kubelet *Kubelet
fakeRuntime *containertest.FakeRuntime
fakeContainerManager *cm.FakeContainerManager
fakeKubeClient *fake.Clientset
fakeMirrorClient *podtest.FakeMirrorClient
fakeClock *clock.FakeClock
mounter mount.Interface
volumePlugin *volumetest.FakeVolumePlugin
}
func (tk *TestKubelet) Cleanup() {
@@ -240,7 +241,8 @@ func newTestKubeletWithImageList(
kubelet.livenessManager = proberesults.NewManager()
kubelet.startupManager = proberesults.NewManager()
kubelet.containerManager = cm.NewStubContainerManager()
fakeContainerManager := cm.NewFakeContainerManager()
kubelet.containerManager = fakeContainerManager
fakeNodeRef := &v1.ObjectReference{
Kind: "Node",
Name: testKubeletHostname,
@@ -349,7 +351,7 @@ func newTestKubeletWithImageList(
kubelet.AddPodSyncLoopHandler(activeDeadlineHandler)
kubelet.AddPodSyncHandler(activeDeadlineHandler)
return &TestKubelet{kubelet, fakeRuntime, fakeKubeClient, fakeMirrorClient, fakeClock, nil, plug}
return &TestKubelet{kubelet, fakeRuntime, fakeContainerManager, fakeKubeClient, fakeMirrorClient, fakeClock, nil, plug}
}
func newTestPods(count int) []*v1.Pod {
@@ -405,6 +407,69 @@ func TestSyncPodsStartPod(t *testing.T) {
fakeRuntime.AssertStartedPods([]string{string(pods[0].UID)})
}
func TestSyncPodsDeletesWhenSourcesAreReadyPerQOS(t *testing.T) {
ready := false // sources will not be ready initially, enabled later
testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
go testKubelet.kubelet.podKiller.PerformPodKillingWork()
defer testKubelet.Cleanup()
defer testKubelet.kubelet.podKiller.Close()
pod := &kubecontainer.Pod{
ID: "12345678",
Name: "foo",
Namespace: "new",
Containers: []*kubecontainer.Container{
{Name: "bar"},
},
}
fakeRuntime := testKubelet.fakeRuntime
fakeContainerManager := testKubelet.fakeContainerManager
fakeContainerManager.PodContainerManager.AddPodFromCgroups(pod) // add pod to mock cgroup
fakeRuntime.PodList = []*containertest.FakePod{
{Pod: pod},
}
kubelet := testKubelet.kubelet
kubelet.cgroupsPerQOS = true // enable cgroupsPerQOS to turn on the cgroups cleanup
kubelet.sourcesReady = config.NewSourcesReady(func(_ sets.String) bool { return ready })
// HandlePodCleanups gets called every 2 seconds within the Kubelet's
// housekeeping routine. This test registers the pod, removes the unwanted pod, then calls into
// HandlePodCleanups a few more times. We should only see one Destroy() event. podKiller runs
// within a goroutine so a two second delay should be enough time to
// mark the pod as killed (within this test case).
kubelet.HandlePodCleanups()
time.Sleep(2 * time.Second)
fakeRuntime.AssertKilledPods([]string{}) // Sources are not ready yet. Don't remove any pods.
ready = true // mark sources as ready
kubelet.HandlePodCleanups()
time.Sleep(2 * time.Second)
// assert that unwanted pods were killed
fakeRuntime.AssertKilledPods([]string{"12345678"})
kubelet.HandlePodCleanups()
kubelet.HandlePodCleanups()
kubelet.HandlePodCleanups()
time.Sleep(2 * time.Second)
fakeContainerManager.PodContainerManager.Lock()
defer fakeContainerManager.PodContainerManager.Unlock()
calledFunctionCount := len(fakeContainerManager.PodContainerManager.CalledFunctions)
destroyCount := 0
for _, functionName := range fakeContainerManager.PodContainerManager.CalledFunctions {
if functionName == "Destroy" {
destroyCount = destroyCount + 1
}
}
assert.Equal(t, 1, destroyCount, "Expect only 1 destroy")
assert.True(t, calledFunctionCount > 2, "expect more than two PodContainerManager calls")
}
func TestSyncPodsDeletesWhenSourcesAreReady(t *testing.T) {
ready := false