kubernetes golang client: Get containers in a pod

3/27/2020

I have the following to exec into a pod to run a command

fmt.Println("Running the command:", command)
parameterCodec := runtime.NewParameterCodec(scheme)
req.VersionedParams(&corev1.PodExecOptions{
    Command:   strings.Fields(command),
    Stdin:     stdin != nil,
    Stdout:    true,
    Stderr:    true,
    TTY:       false,
}, parameterCodec)

I'm looking to run the same command but adding the Container option which is a string. I'm having a hard time figuring how how I can list all the containers in a pod.

Thanks

-- Mike
go
kubernetes

1 Answer

3/27/2020

got it

pod, err := clientset.CoreV1().Pods("default").Get(podname, metav1.GetOptions{})
if err != nil {
    return "", err
}

fmt.Println(pod.Spec.Containers[0])
-- Mike
Source: StackOverflow