Skip to content

Commit 3f306ae

Browse files
authored
Merge pull request kubernetes#126343 from SergeyKanzhelev/succeededPodReadmitted
Terminated pod should not be re-admitted
2 parents b1559c6 + 300128d commit 3f306ae

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

pkg/kubelet/kubelet.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2567,7 +2567,8 @@ func (kl *Kubelet) HandlePodAdditions(pods []*v1.Pod) {
25672567
// shutting it down. If the pod hasn't started yet, we know that when
25682568
// the pod worker is invoked it will also avoid setting up the pod, so
25692569
// we simply avoid doing any work.
2570-
if !kl.podWorkers.IsPodTerminationRequested(pod.UID) {
2570+
// We also do not try to admit the pod that is already in terminated state.
2571+
if !kl.podWorkers.IsPodTerminationRequested(pod.UID) && !podutil.IsPodPhaseTerminal(pod.Status.Phase) {
25712572
// We failed pods that we rejected, so activePods include all admitted
25722573
// pods that are alive.
25732574
activePods := kl.filterOutInactivePods(existingPods)

test/e2e_node/device_plugin_test.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,50 @@ const (
9090

9191
// This is the sleep interval specified in the command executed in the pod so that container is restarted within the expected test run time
9292
sleepIntervalWithRestart string = "60s"
93+
94+
// This is the sleep interval specified in the command executed in the pod so that container is restarted within the expected test run time
95+
sleepIntervalToCompletion string = "5s"
9396
)
9497

9598
func testDevicePlugin(f *framework.Framework, pluginSockDir string) {
9699
pluginSockDir = filepath.Join(pluginSockDir) + "/"
100+
101+
type ResourceValue struct {
102+
Allocatable int
103+
Capacity int
104+
}
105+
106+
devicePluginGracefulTimeout := 5 * time.Minute // see endpointStopGracePeriod in pkg/kubelet/cm/devicemanager/types.go
107+
108+
var getNodeResourceValues = func(ctx context.Context, resourceName string) ResourceValue {
109+
ginkgo.GinkgoHelper()
110+
node := getLocalNode(ctx, f)
111+
112+
// -1 represents that the resource is not found
113+
result := ResourceValue{
114+
Allocatable: -1,
115+
Capacity: -1,
116+
}
117+
118+
for key, val := range node.Status.Capacity {
119+
resource := string(key)
120+
if resource == resourceName {
121+
result.Capacity = int(val.Value())
122+
break
123+
}
124+
}
125+
126+
for key, val := range node.Status.Allocatable {
127+
resource := string(key)
128+
if resource == resourceName {
129+
result.Allocatable = int(val.Value())
130+
break
131+
}
132+
}
133+
134+
return result
135+
}
136+
97137
f.Context("DevicePlugin", f.WithSerial(), f.WithDisruptive(), func() {
98138
var devicePluginPod, dptemplate *v1.Pod
99139
var v1alphaPodResources *kubeletpodresourcesv1alpha1.ListPodResourcesResponse
@@ -428,6 +468,55 @@ func testDevicePlugin(f *framework.Framework, pluginSockDir string) {
428468
framework.ExpectNoError(err, "inconsistent device assignment after pod restart")
429469
})
430470

471+
ginkgo.It("will not attempt to admit the succeeded pod after the kubelet restart and device plugin removed", func(ctx context.Context) {
472+
podRECMD := fmt.Sprintf("devs=$(ls /tmp/ | egrep '^Dev-[0-9]+$') && echo stub devices: $devs && sleep %s", sleepIntervalToCompletion)
473+
podSpec := makeBusyboxPod(SampleDeviceResourceName, podRECMD)
474+
podSpec.Spec.RestartPolicy = v1.RestartPolicyNever
475+
// Making sure the pod will not be garbage collected and will stay thru the kubelet restart after
476+
// it reached the terminated state. Using finalizers makes the test more reliable.
477+
podSpec.ObjectMeta.Finalizers = []string{testFinalizer}
478+
pod := e2epod.NewPodClient(f).CreateSync(ctx, podSpec)
479+
480+
deviceIDRE := "stub devices: (Dev-[0-9]+)"
481+
devID1, err := parseLog(ctx, f, pod.Name, pod.Name, deviceIDRE)
482+
framework.ExpectNoError(err, "getting logs for pod %q", pod.Name)
483+
484+
gomega.Expect(devID1).To(gomega.Not(gomega.Equal("")), "pod requested a device but started successfully without")
485+
486+
pod, err = e2epod.NewPodClient(f).Get(ctx, pod.Name, metav1.GetOptions{})
487+
framework.ExpectNoError(err)
488+
489+
ginkgo.By("Wait for node to be ready")
490+
gomega.Expect(e2enode.WaitForAllNodesSchedulable(ctx, f.ClientSet, 5*time.Minute)).To(gomega.Succeed())
491+
492+
ginkgo.By("Waiting for pod to succeed")
493+
gomega.Expect(e2epod.WaitForPodSuccessInNamespace(ctx, f.ClientSet, pod.Name, pod.Namespace)).To(gomega.Succeed())
494+
495+
ginkgo.By("Deleting the device plugin")
496+
e2epod.NewPodClient(f).DeleteSync(ctx, devicePluginPod.Name, metav1.DeleteOptions{}, time.Minute)
497+
waitForContainerRemoval(ctx, devicePluginPod.Spec.Containers[0].Name, devicePluginPod.Name, devicePluginPod.Namespace)
498+
499+
gomega.Eventually(getNodeResourceValues, devicePluginGracefulTimeout, f.Timeouts.Poll).WithContext(ctx).WithArguments(SampleDeviceResourceName).Should(gomega.Equal(ResourceValue{Allocatable: 0, Capacity: int(expectedSampleDevsAmount)}))
500+
501+
ginkgo.By("Restarting Kubelet")
502+
restartKubelet(true)
503+
504+
ginkgo.By("Wait for node to be ready again")
505+
gomega.Expect(e2enode.WaitForAllNodesSchedulable(ctx, f.ClientSet, 5*time.Minute)).To(gomega.Succeed())
506+
507+
ginkgo.By("Pod should still be in Succeed state")
508+
// This ensures that the pod was admitted successfully.
509+
// In the past we had and issue when kubelet will attempt to re-admit the terminated pod and will change it's phase to Failed.
510+
// There are no indication that the pod was re-admitted so we just wait for a minute after the node became ready.
511+
gomega.Consistently(func() v1.PodPhase {
512+
pod, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Get(ctx, pod.Name, metav1.GetOptions{})
513+
return pod.Status.Phase
514+
}, 1*time.Minute, f.Timeouts.Poll).Should(gomega.Equal(v1.PodSucceeded))
515+
516+
ginkgo.By("Removing the finalizer from the pod so it can be deleted now")
517+
e2epod.NewPodClient(f).RemoveFinalizer(context.TODO(), podSpec.Name, testFinalizer)
518+
})
519+
431520
// simulate device plugin re-registration, *but not* container and kubelet restart.
432521
// After the device plugin has re-registered, the list healthy devices is repopulated based on the devices discovered.
433522
// Once Pod2 is running we determine the device that was allocated it. As long as the device allocation succeeds the
@@ -826,6 +915,9 @@ func testDevicePluginNodeReboot(f *framework.Framework, pluginSockDir string) {
826915
continue
827916
}
828917

918+
ginkgo.By("Removing the finalizer from the pod in case it was used")
919+
e2epod.NewPodClient(f).RemoveFinalizer(context.TODO(), p.Name, testFinalizer)
920+
829921
framework.Logf("Deleting pod: %s", p.Name)
830922
e2epod.NewPodClient(f).DeleteSync(ctx, p.Name, metav1.DeleteOptions{}, 2*time.Minute)
831923
}

0 commit comments

Comments
 (0)