From e2dc8439e488cfe59e528f726010815c1407f325 Mon Sep 17 00:00:00 2001 From: Rakshith R Date: Mon, 22 Mar 2021 10:25:58 +0530 Subject: [PATCH] e2e: add execCommandInDaemonsetPod() execCommandInDaemonsetPod() executes commands inside given container of a daemonset pod on a particular node. Signed-off-by: Rakshith R --- e2e/pod.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/e2e/pod.go b/e2e/pod.go index 61cd5e19a..400002233 100644 --- a/e2e/pod.go +++ b/e2e/pod.go @@ -123,6 +123,43 @@ func getCommandInPodOpts(f *framework.Framework, c, ns string, opt *metav1.ListO }, nil } +// execCommandInDaemonsetPod executes commands inside given container of a daemonset pod on a particular node. +func execCommandInDaemonsetPod(f *framework.Framework, c, daemonsetName, nodeName, containerName, ns string) (string, string, error) { + selector, err := getDaemonSetLabelSelector(f, ns, daemonsetName) + if err != nil { + return "", "", err + } + + opt := &metav1.ListOptions{ + LabelSelector: selector, + } + pods, err := listPods(f, ns, opt) + if err != nil { + return "", "", err + } + + podName := "" + for i := range pods { + if pods[i].Spec.NodeName == nodeName { + podName = pods[i].Name + } + } + if podName == "" { + return "", "", fmt.Errorf("%s daemonset pod on node %s in namespace %s not found", daemonsetName, nodeName, ns) + } + + cmd := []string{"/bin/sh", "-c", c} + podOpt := framework.ExecOptions{ + Command: cmd, + Namespace: ns, + PodName: podName, + ContainerName: containerName, + CaptureStdout: true, + CaptureStderr: true, + } + return f.ExecWithOptions(podOpt) +} + // 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)