From 915437b314d87dbe84ea81f6a8222e5e073dfc30 Mon Sep 17 00:00:00 2001 From: Rakshith R Date: Mon, 22 Mar 2021 10:01:25 +0530 Subject: [PATCH] e2e: add listPods() listPods returns slice of pods matching given ListOptions and namespace. Signed-off-by: Rakshith R --- e2e/pod.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/e2e/pod.go b/e2e/pod.go index cfcd4e931..61cd5e19a 100644 --- a/e2e/pod.go +++ b/e2e/pod.go @@ -107,19 +107,15 @@ func waitForDeploymentComplete(name, ns string, c kubernetes.Interface, t int) e func getCommandInPodOpts(f *framework.Framework, c, ns string, opt *metav1.ListOptions) (framework.ExecOptions, error) { cmd := []string{"/bin/sh", "-c", c} - podList, err := f.PodClientNS(ns).List(context.TODO(), *opt) - framework.ExpectNoError(err) - if len(podList.Items) == 0 { - return framework.ExecOptions{}, errors.New("podlist is empty") - } + pods, err := listPods(f, ns, opt) if err != nil { return framework.ExecOptions{}, err } return framework.ExecOptions{ Command: cmd, - PodName: podList.Items[0].Name, + PodName: pods[0].Name, Namespace: ns, - ContainerName: podList.Items[0].Spec.Containers[0].Name, + ContainerName: pods[0].Spec.Containers[0].Name, Stdin: nil, CaptureStdout: true, CaptureStderr: true, @@ -127,6 +123,15 @@ func getCommandInPodOpts(f *framework.Framework, c, ns string, opt *metav1.ListO }, nil } +// listPods returns slice of pods matching given ListOptions and namespace. +func listPods(f *framework.Framework, ns string, opt *metav1.ListOptions) ([]v1.Pod, error) { + podList, err := f.PodClientNS(ns).List(context.TODO(), *opt) + if len(podList.Items) == 0 { + return podList.Items, fmt.Errorf("podlist for label '%s' in namespace %s is empty", opt.LabelSelector, ns) + } + return podList.Items, err +} + func execCommandInPod(f *framework.Framework, c, ns string, opt *metav1.ListOptions) (string, string, error) { podOpt, err := getCommandInPodOpts(f, c, ns, opt) if err != nil {