Skip to content

Commit 285d433

Browse files
committed
Clearer image pull test and utils
Signed-off-by: Laura Lorenz <[email protected]>
1 parent e03d0f6 commit 285d433

File tree

4 files changed

+14
-17
lines changed

4 files changed

+14
-17
lines changed

Diff for: staging/src/k8s.io/kubectl/pkg/util/podutils/podutils.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,10 @@ func podReadyTime(pod *corev1.Pod) *metav1.Time {
191191
return &metav1.Time{}
192192
}
193193

194-
func maxContainerRestarts(pod *corev1.Pod) (regularRestarts, sidecarRestarts int) {
194+
// MaxContainerRestarts iterates through all the normal containers and sidecar
195+
// containers in a Pod object and reports the highest restart count observed per
196+
// category.
197+
func MaxContainerRestarts(pod *corev1.Pod) (regularRestarts, sidecarRestarts int) {
195198
for _, c := range pod.Status.ContainerStatuses {
196199
regularRestarts = max(regularRestarts, int(c.RestartCount))
197200
}
@@ -214,8 +217,8 @@ func maxContainerRestarts(pod *corev1.Pod) (regularRestarts, sidecarRestarts int
214217
// false: pj has a higher container restart count.
215218
// nil: Both have the same container restart count.
216219
func compareMaxContainerRestarts(pi *corev1.Pod, pj *corev1.Pod) *bool {
217-
regularRestartsI, sidecarRestartsI := maxContainerRestarts(pi)
218-
regularRestartsJ, sidecarRestartsJ := maxContainerRestarts(pj)
220+
regularRestartsI, sidecarRestartsI := MaxContainerRestarts(pi)
221+
regularRestartsJ, sidecarRestartsJ := MaxContainerRestarts(pj)
219222
if regularRestartsI != regularRestartsJ {
220223
res := regularRestartsI > regularRestartsJ
221224
return &res

Diff for: test/e2e/framework/pod/wait.go

+5-11
Original file line numberDiff line numberDiff line change
@@ -869,17 +869,11 @@ func WaitForContainerTerminated(ctx context.Context, c clientset.Interface, name
869869
})
870870
}
871871

872-
// WaitForContainerRestartedNTimes waits for the given Pod container to have restarted N times
873-
func WaitForContainerRestartedNTimes(ctx context.Context, c clientset.Interface, namespace, podName, containerName string, timeout time.Duration, target int) error {
874-
conditionDesc := fmt.Sprintf("container %s restarted at least %d times", containerName, target)
872+
// WaitForContainerRestartedNTimes waits for a container in the Pod to have restarted N times
873+
func WaitForContainerRestartedNTimes(ctx context.Context, c clientset.Interface, namespace string, podName string, timeout time.Duration, target int) error {
874+
conditionDesc := fmt.Sprintf("A container in pod %s restarted at least %d times", podName, target)
875875
return WaitForPodCondition(ctx, c, namespace, podName, conditionDesc, timeout, func(pod *v1.Pod) (bool, error) {
876-
for _, statuses := range [][]v1.ContainerStatus{pod.Status.ContainerStatuses, pod.Status.InitContainerStatuses, pod.Status.EphemeralContainerStatuses} {
877-
for _, cs := range statuses {
878-
if cs.Name == containerName {
879-
return cs.RestartCount >= int32(target), nil
880-
}
881-
}
882-
}
883-
return false, nil
876+
r, _ := podutils.MaxContainerRestarts(pod)
877+
return r >= target, nil
884878
})
885879
}

Diff for: test/e2e_node/container_restart_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func doTest(ctx context.Context, f *framework.Framework, targetRestarts int, con
9797

9898
// Hard wait 30 seconds for targetRestarts in the best case; longer timeout later will handle if infra was slow.
9999
time.Sleep(30 * time.Second)
100-
podErr = e2epod.WaitForContainerRestartedNTimes(ctx, f.ClientSet, f.Namespace.Name, pod.Name, containerName, 5*time.Minute, targetRestarts)
100+
podErr = e2epod.WaitForContainerRestartedNTimes(ctx, f.ClientSet, f.Namespace.Name, pod.Name, 5*time.Minute, targetRestarts)
101101
gomega.Expect(podErr).ShouldNot(gomega.HaveOccurred(), "Expected container to repeatedly back off container failures")
102102

103103
r, err := extractObservedBackoff(ctx, f, pod.Name, containerName)

Diff for: test/e2e_node/image_pull_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ var _ = SIGDescribe("Pull Image", feature.CriProxy, framework.WithSerial(), func
257257
isExpectedErrMsg := strings.Contains(eventMsg, expectedErr.Error())
258258
gomega.Expect(isExpectedErrMsg).To(gomega.BeTrueBecause("we injected an exception into the PullImage interface of the cri proxy"))
259259

260-
podErr = e2epod.WaitForPodContainerStarted(ctx, f.ClientSet, f.Namespace.Name, pod.Name, 0, 30*time.Second)
261-
gomega.Expect(podErr).To(gomega.HaveOccurred(), "Expected container not to start from repeatedly backing off image pulls")
260+
// Hard wait 30 seconds for image pulls to repeatedly back off.
261+
time.Sleep(30 * time.Second)
262262

263263
e, err := getImagePullAttempts(ctx, f, pod.Name)
264264
framework.ExpectNoError(err)

0 commit comments

Comments
 (0)