Change:
<!-- Please use this template while reporting a bug and provide as much info as possible. Not doing so may result in your bug not being addressed in a timely manner. Thanks! If the matter is security related, please disclose it privately via --What happened: Right after calling , returns false even the pod is not killed yet. What you expected to happen: In kubelet, after is complete, the pod should be pending termination or be killed. How to reproduce it (as minimally and precisely as possible): Anything else we need to know?: Related to Environment: Kubernetes version (use ): Cloud provider or hardware configuration: OS (e.g: ): Kernel (e.g. ): Install tools: Network plugin and version (if this is a network-related bug): Others:
This issue is currently awaiting triage. If a SIG or subproject determines this is a relevant issue, they will accept it by applying the label and provide further guidance. The label can be by org members by writing in a comment. <detailsInstructions for interacting with me using PR comments are available . If you have questions or suggestions related to my behavior, please file an issue against the repository. </details
/sig node /assign

Discussion:
// PodKiller handles requests for killing pods type PodKiller interface { // KillPod receives pod speficier representing the pod to kill <del> KillPod(pair *kubecontainer.PodPair) </del> <ins> KillPod(podPair *kubecontainer.PodPair) </ins> // PerformPodKillingWork performs the actual pod killing work via calling CRI // It returns after its Close() func is called and all outstanding pod killing requests are served PerformPodKillingWork()

delete(pk.podTerminationMap, uid) } <del> // checkAndMarkPodPendingTerminationByPod checks to see if the pod is being // killed and returns true if it is, otherwise the pod is added to the map and // returns false func (pk *podKillerWithChannel) checkAndMarkPodPendingTerminationByPod(podPair *kubecontainer.PodPair) bool { </del> <ins> // KillPod sends pod killing request to the killer after marks the pod // unless the given pod has been marked to be killed func (pk *podKillerWithChannel) KillPod(podPair *kubecontainer.PodPair) { </ins> pk.podKillingLock.Lock() defer pk.podKillingLock.Unlock() var apiPodExists bool

} else { klog.V(4).Infof("running pod %q is pending termination", podPair.RunningPod.ID) } <del> return true </del> <ins> return </ins> } <del> return false </del> <ins> // Limit to one request per pod pk.podKillingCh <- podPair </ins> } // Close closes the channel through which requests are delivered

close(pk.podKillingCh) } <del> // KillPod sends pod killing request to the killer func (pk *podKillerWithChannel) KillPod(pair *kubecontainer.PodPair) { pk.podKillingCh <- pair } </del> // PerformPodKillingWork launches a goroutine to kill a pod received from the channel if // another goroutine isn't already in action. func (pk *podKillerWithChannel) PerformPodKillingWork() { for podPair := range pk.podKillingCh { <del> if pk.checkAndMarkPodPendingTerminationByPod(podPair) { // Pod is already being killed continue } </del> runningPod := podPair.RunningPod apiPod := podPair.APIPod

// assert that unwanted pods were killed fakeRuntime.AssertKilledPods([]string{"12345678"}) <ins> // simulate Runtime.KillPod fakeRuntime.PodList = nil </ins> kubelet.HandlePodCleanups() kubelet.HandlePodCleanups() kubelet.HandlePodCleanups()

fakeRuntime.AssertKilledPods([]string{"12345678"}) } <ins> func TestKillPodFollwedByIsPodPendingTermination(t *testing.T) { testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */) defer testKubelet.Cleanup() defer testKubelet.kubelet.podKiller.Close() go testKubelet.kubelet.podKiller.PerformPodKillingWork() 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}, } kl := testKubelet.kubelet kl.podKiller.KillPod(&kubecontainer.PodPair{ APIPod:     nil, RunningPod: pod, }) if !(kl.podKiller.IsPodPendingTerminationByUID(pod.ID) || fakeRuntime.AssertKilledPods([]string{"12345678"}) == nil) { t.Fatal("Race condition: When KillPod is complete, the pod should be pending termination or be killed") } } </ins> type testNodeLister struct { nodes []*v1.Node }