From 285d433dea79e6838cdd0648c36a2a82fd398179 Mon Sep 17 00:00:00 2001 From: Laura Lorenz Date: Tue, 12 Nov 2024 22:48:17 +0000 Subject: [PATCH] Clearer image pull test and utils Signed-off-by: Laura Lorenz --- .../k8s.io/kubectl/pkg/util/podutils/podutils.go | 9 ++++++--- test/e2e/framework/pod/wait.go | 16 +++++----------- test/e2e_node/container_restart_test.go | 2 +- test/e2e_node/image_pull_test.go | 4 ++-- 4 files changed, 14 insertions(+), 17 deletions(-) diff --git a/staging/src/k8s.io/kubectl/pkg/util/podutils/podutils.go b/staging/src/k8s.io/kubectl/pkg/util/podutils/podutils.go index dfefdef4567..9e457f406d3 100644 --- a/staging/src/k8s.io/kubectl/pkg/util/podutils/podutils.go +++ b/staging/src/k8s.io/kubectl/pkg/util/podutils/podutils.go @@ -191,7 +191,10 @@ func podReadyTime(pod *corev1.Pod) *metav1.Time { return &metav1.Time{} } -func maxContainerRestarts(pod *corev1.Pod) (regularRestarts, sidecarRestarts int) { +// MaxContainerRestarts iterates through all the normal containers and sidecar +// containers in a Pod object and reports the highest restart count observed per +// category. +func MaxContainerRestarts(pod *corev1.Pod) (regularRestarts, sidecarRestarts int) { for _, c := range pod.Status.ContainerStatuses { regularRestarts = max(regularRestarts, int(c.RestartCount)) } @@ -214,8 +217,8 @@ func maxContainerRestarts(pod *corev1.Pod) (regularRestarts, sidecarRestarts int // false: pj has a higher container restart count. // nil: Both have the same container restart count. func compareMaxContainerRestarts(pi *corev1.Pod, pj *corev1.Pod) *bool { - regularRestartsI, sidecarRestartsI := maxContainerRestarts(pi) - regularRestartsJ, sidecarRestartsJ := maxContainerRestarts(pj) + regularRestartsI, sidecarRestartsI := MaxContainerRestarts(pi) + regularRestartsJ, sidecarRestartsJ := MaxContainerRestarts(pj) if regularRestartsI != regularRestartsJ { res := regularRestartsI > regularRestartsJ return &res diff --git a/test/e2e/framework/pod/wait.go b/test/e2e/framework/pod/wait.go index 6e69f8ec487..7a416c475c7 100644 --- a/test/e2e/framework/pod/wait.go +++ b/test/e2e/framework/pod/wait.go @@ -869,17 +869,11 @@ func WaitForContainerTerminated(ctx context.Context, c clientset.Interface, name }) } -// WaitForContainerRestartedNTimes waits for the given Pod container to have restarted N times -func WaitForContainerRestartedNTimes(ctx context.Context, c clientset.Interface, namespace, podName, containerName string, timeout time.Duration, target int) error { - conditionDesc := fmt.Sprintf("container %s restarted at least %d times", containerName, target) +// WaitForContainerRestartedNTimes waits for a container in the Pod to have restarted N times +func WaitForContainerRestartedNTimes(ctx context.Context, c clientset.Interface, namespace string, podName string, timeout time.Duration, target int) error { + conditionDesc := fmt.Sprintf("A container in pod %s restarted at least %d times", podName, target) return WaitForPodCondition(ctx, c, namespace, podName, conditionDesc, timeout, func(pod *v1.Pod) (bool, error) { - for _, statuses := range [][]v1.ContainerStatus{pod.Status.ContainerStatuses, pod.Status.InitContainerStatuses, pod.Status.EphemeralContainerStatuses} { - for _, cs := range statuses { - if cs.Name == containerName { - return cs.RestartCount >= int32(target), nil - } - } - } - return false, nil + r, _ := podutils.MaxContainerRestarts(pod) + return r >= target, nil }) } diff --git a/test/e2e_node/container_restart_test.go b/test/e2e_node/container_restart_test.go index 5a2d530f3f2..5fdbae30a11 100644 --- a/test/e2e_node/container_restart_test.go +++ b/test/e2e_node/container_restart_test.go @@ -97,7 +97,7 @@ func doTest(ctx context.Context, f *framework.Framework, targetRestarts int, con // Hard wait 30 seconds for targetRestarts in the best case; longer timeout later will handle if infra was slow. time.Sleep(30 * time.Second) - podErr = e2epod.WaitForContainerRestartedNTimes(ctx, f.ClientSet, f.Namespace.Name, pod.Name, containerName, 5*time.Minute, targetRestarts) + podErr = e2epod.WaitForContainerRestartedNTimes(ctx, f.ClientSet, f.Namespace.Name, pod.Name, 5*time.Minute, targetRestarts) gomega.Expect(podErr).ShouldNot(gomega.HaveOccurred(), "Expected container to repeatedly back off container failures") r, err := extractObservedBackoff(ctx, f, pod.Name, containerName) diff --git a/test/e2e_node/image_pull_test.go b/test/e2e_node/image_pull_test.go index fce685e5264..28c393f2a96 100644 --- a/test/e2e_node/image_pull_test.go +++ b/test/e2e_node/image_pull_test.go @@ -257,8 +257,8 @@ var _ = SIGDescribe("Pull Image", feature.CriProxy, framework.WithSerial(), func isExpectedErrMsg := strings.Contains(eventMsg, expectedErr.Error()) gomega.Expect(isExpectedErrMsg).To(gomega.BeTrueBecause("we injected an exception into the PullImage interface of the cri proxy")) - podErr = e2epod.WaitForPodContainerStarted(ctx, f.ClientSet, f.Namespace.Name, pod.Name, 0, 30*time.Second) - gomega.Expect(podErr).To(gomega.HaveOccurred(), "Expected container not to start from repeatedly backing off image pulls") + // Hard wait 30 seconds for image pulls to repeatedly back off. + time.Sleep(30 * time.Second) e, err := getImagePullAttempts(ctx, f, pod.Name) framework.ExpectNoError(err)