Skip to content

Commit 8e7b2af

Browse files
committed
Use a better util
Signed-off-by: Laura Lorenz <[email protected]>
1 parent 285d433 commit 8e7b2af

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

staging/src/k8s.io/kubectl/pkg/util/podutils/podutils.go

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

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) {
194+
func maxContainerRestarts(pod *corev1.Pod) (regularRestarts, sidecarRestarts int) {
198195
for _, c := range pod.Status.ContainerStatuses {
199196
regularRestarts = max(regularRestarts, int(c.RestartCount))
200197
}
@@ -217,8 +214,8 @@ func MaxContainerRestarts(pod *corev1.Pod) (regularRestarts, sidecarRestarts int
217214
// false: pj has a higher container restart count.
218215
// nil: Both have the same container restart count.
219216
func compareMaxContainerRestarts(pi *corev1.Pod, pj *corev1.Pod) *bool {
220-
regularRestartsI, sidecarRestartsI := MaxContainerRestarts(pi)
221-
regularRestartsJ, sidecarRestartsJ := MaxContainerRestarts(pj)
217+
regularRestartsI, sidecarRestartsI := maxContainerRestarts(pi)
218+
regularRestartsJ, sidecarRestartsJ := maxContainerRestarts(pj)
222219
if regularRestartsI != regularRestartsJ {
223220
res := regularRestartsI > regularRestartsJ
224221
return &res

test/e2e/framework/pod/wait.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
apitypes "k8s.io/apimachinery/pkg/types"
3838
clientset "k8s.io/client-go/kubernetes"
3939
"k8s.io/kubectl/pkg/util/podutils"
40+
podv1util "k8s.io/kubernetes/pkg/api/v1/pod"
4041
"k8s.io/kubernetes/test/e2e/framework"
4142
testutils "k8s.io/kubernetes/test/utils"
4243
"k8s.io/kubernetes/test/utils/format"
@@ -869,11 +870,14 @@ func WaitForContainerTerminated(ctx context.Context, c clientset.Interface, name
869870
})
870871
}
871872

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 {
873+
// WaitForContainerRestartedNTimes waits for the given normal container in the Pod to have restarted N times
874+
func WaitForContainerRestartedNTimes(ctx context.Context, c clientset.Interface, namespace string, podName string, containerName string, timeout time.Duration, target int) error {
874875
conditionDesc := fmt.Sprintf("A container in pod %s restarted at least %d times", podName, target)
875876
return WaitForPodCondition(ctx, c, namespace, podName, conditionDesc, timeout, func(pod *v1.Pod) (bool, error) {
876-
r, _ := podutils.MaxContainerRestarts(pod)
877-
return r >= target, nil
877+
cs, found := podv1util.GetContainerStatus(pod.Status.ContainerStatuses, containerName)
878+
if !found {
879+
return false, fmt.Errorf("could not find container %s in pod %s", containerName, podName)
880+
}
881+
return cs.RestartCount >= int32(target), nil
878882
})
879883
}

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, 5*time.Minute, targetRestarts)
100+
podErr = e2epod.WaitForContainerRestartedNTimes(ctx, f.ClientSet, f.Namespace.Name, pod.Name, containerName, 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)

0 commit comments

Comments
 (0)