<p>I'm trying to exec a command into a running pod. I'm using go K8sclient to achieve this but facing a issue. I also don't know if solution is correct or not. Can anyone please check and provide correct solution?</p>
<p>This is my code.</p>
<pre><code>        namespace := getNamespace()
        podName := &quot;maxscale-0&quot;

        config, err := rest.InClusterConfig()
        if err != nil {
                log.Fatal(err)
        }

        clientset, err := kubernetes.NewForConfig(config)
        if err != nil {
                log.Fatal(err)
        }



        req := clientset.CoreV1().Pods(namespace).Exec(podName, &amp;corev1.PodExecOptions{
                Command: []string{&quot;sh&quot;, &quot;-c&quot;, &quot;grep -oP '\&quot;name\&quot;: \&quot;\\K[^\&quot;]*' /var/lib/maxscale/MariaDB-Monitor_journal.json&quot;},
        })

        // Set up a stream to capture the output
        execStream, err := req.Stream()
        if err != nil {
                fmt.Println(err)
                os.Exit(1)
        }

        // Print the output
        buf := new(bytes.Buffer)
        buf.ReadFrom(execStream)
        fmt.Println(buf.String())

</code></pre>
<p>The error I got is</p>
<pre><code>clientset.CoreV1().Pods(namespace).Exec undefined (type &quot;k8s.io/client-go/kubernetes/typed/core/v1&quot;.PodInterface has no field or method Exec)
</code></pre>

<p>As @David Maze shared, to use k8's go client to exec command in a pod follow the below code:</p>
<pre><code>import (
    &quot;io&quot;

    v1 &quot;k8s.io/api/core/v1&quot;
    &quot;k8s.io/client-go/kubernetes&quot;
    &quot;k8s.io/client-go/kubernetes/scheme&quot;
    restclient &quot;k8s.io/client-go/rest&quot;
    &quot;k8s.io/client-go/tools/remotecommand&quot;
)

// ExecCmd exec command on specific pod and wait the command's output.
func ExecCmdExample(client kubernetes.Interface, config *restclient.Config, podName string,
    command string, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
    cmd := []string{
        &quot;sh&quot;,
        &quot;-c&quot;,
        command,
    }
    req := client.CoreV1().RESTClient().Post().Resource(&quot;pods&quot;).Name(podName).
        Namespace(&quot;default&quot;).SubResource(&quot;exec&quot;)
    option := &amp;v1.PodExecOptions{
        Command: cmd,
        Stdin:   true,
        Stdout:  true,
        Stderr:  true,
        TTY:     true,
    }
    if stdin == nil {
        option.Stdin = false
    }
    req.VersionedParams(
        option,
        scheme.ParameterCodec,
    )
    exec, err := remotecommand.NewSPDYExecutor(config, &quot;POST&quot;, req.URL())
    if err != nil {
        return err
    }
    err = exec.Stream(remotecommand.StreamOptions{
        Stdin:  stdin,
        Stdout: stdout,
        Stderr: stderr,
    })
    if err != nil {
        return err
    }

    return nil
}
</code></pre>
<p>Also refer to this <a href="https://github.com/kubernetes/client-go/issues/912" rel="nofollow noreferrer">link</a> for more information</p>
