Repository: kubernetes/kubernetes
File: test/e2e_node/podresources_test.go (Go)

Code before:
	podResourceList = []*kubeletpodresourcesv1.PodResources{resp.GetPodResources()}
	res = convertToMap(podResourceList)
	err = matchPodDescWithResources(expected, res)
	framework.ExpectNoError(err, "matchPodDescWithResources() failed err %v", err)
	tpd.deletePodsForTest(ctx, f)

	restartNever := v1.RestartPolicyNever
	tpd = newTestPodData()
	ginkgo.By("checking Get() returns an error for a terminated pod")

	completedDesc := podDesc{
		podName:        "pod-01",
		cntName:        "cnt-00",
		cpuRequest:     1000,
		restartPolicy:  &restartNever,
		mainCntCommand: []string{"sh", "-c", "/bin/true"},
	}

	completedPod := e2epod.NewPodClient(f).Create(ctx, makePodResourcesTestPod(completedDesc))
	framework.Logf("created pod %s", completedDesc.podName)
	tpd.PodMap[completedDesc.podName] = completedPod
	// Wait for the pod to complete to avoid races.
	err = e2epod.WaitForPodCondition(ctx, f.ClientSet, f.Namespace.Name, completedPod.Name, "Pod Succeeded", 2*time.Minute, testutils.PodSucceeded)
	framework.ExpectNoError(err, "pod did not succeed as expected")

	resp, err = cli.Get(ctx, &kubeletpodresourcesv1.GetPodResourcesRequest{PodName: completedDesc.podName, PodNamespace: f.Namespace.Name})
	gomega.Expect(err).To(gomega.HaveOccurred(), "expected Get() to return an error for a terminated pod")
	// Returned PodResources for a terminated pod must be empty.
	pr := resp.GetPodResources()
	if pr != nil {
		gomega.Expect(pr.GetContainers()).To(gomega.BeEmpty(),
			"expected no container resources in response for terminated pod; got: %#v", pr.GetContainers())
	}
	tpd.deletePodsForTest(ctx, f)

	if sidecarContainersEnabled {
		containerRestartPolicyAlways := v1.ContainerRestartPolicyAlways

		tpd = newTestPodData()
		ginkgo.By("checking the output when only pod with init containers require CPU")
		expected = []podDesc{
			{
				podName:    "pod-01",
				cntName:    "cnt-00",
				cpuRequest: 1000,
				initContainers: []initContainerDesc{
					{
						cntName:    "init-00",
						cpuRequest: 1000,
					},
					{

Review comment (suggestion):
Nit: This could flake, there's a window between the apiserver reflecting `PodSucceeded` and the kubelet's pod worker finishing cleanup, during which `isPodInactive` returns `false` (because `IsPodTerminationRequested` is still `true` and `IsPodKnownTerminated` is still `false`). During that window `Get()` would `succeed` instead of erroring.

The PR already uses `Eventually` for `Get()` elsewhere (line 1219) for the same reason, might be worth doing the same here to avoid flakes.

Code after:
	expected = []podDesc{
		{
			podName:    "pod-01",
			cntName:    "cnt-00",
			cpuRequest: 1000,
			cpuLimit:   1000,
			memRequest: 256,
			memLimit:   256,
			extraContainers: []containerDesc{
				{
					cntName:    "cnt-01",
					cpuRequest: 600,
				},
				{
					cntName:    "cnt-02",
					cpuRequest: 400,
				},
			},
			useFullSpec: true,
		},
	}
	tpd.createPodsForTest(ctx, f, expected)

	resp = podresourcesGetWithRetry(ctx, cli, f.Namespace.Name, "pod-01")
	podResourceList = []*kubeletpodresourcesv1.PodResources{resp.GetPodResources()}
	res = convertToMap(podResourceList)
	err = matchPodDescWithResources(expected, res)
	framework.ExpectNoError(err, "matchPodDescWithResources() failed err %v", err)
	tpd.deletePodsForTest(ctx, f)

	restartNever := v1.RestartPolicyNever
	tpd = newTestPodData()
	ginkgo.By("checking Get() returns an error for a terminated pod")

	completedDesc := podDesc{
		podName:        "pod-01",
		cntName:        "cnt-00",
		cpuRequest:     1000,
		restartPolicy:  &restartNever,
		mainCntCommand: []string{"sh", "-c", "/bin/true"},
	}

	completedPod := e2epod.NewPodClient(f).Create(ctx, makePodResourcesTestPod(completedDesc))
	framework.Logf("created pod %s", completedDesc.podName)
	tpd.PodMap[completedDesc.podName] = completedPod
	// Wait for the pod to complete to avoid races.
	err = e2epod.WaitForPodCondition(ctx, f.ClientSet, f.Namespace.Name, completedPod.Name, "Pod Succeeded", 2*time.Minute, testutils.PodSucceeded)
	framework.ExpectNoError(err, "pod did not succeed as expected")

	gomega.Eventually(func() error {
		resp, err = cli.Get(ctx, &kubeletpodresourcesv1.GetPodResourcesRequest{PodName: completedDesc.podName, PodNamespace: f.Namespace.Name})