1

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?

This is my code.

        namespace := getNamespace()
        podName := "maxscale-0"

        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, &corev1.PodExecOptions{
                Command: []string{"sh", "-c", "grep -oP '\"name\": \"\\K[^\"]*' /var/lib/maxscale/MariaDB-Monitor_journal.json"},
        })

        // 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())

The error I got is

clientset.CoreV1().Pods(namespace).Exec undefined (type "k8s.io/client-go/kubernetes/typed/core/v1".PodInterface has no field or method Exec)
Amin Rashidbeigi
  • 670
  • 6
  • 11
Dhanu
  • 31
  • 3
  • try this https://stackoverflow.com/questions/26411594/executing-docker-command-using-golang-exec-fails – error404 Jan 14 '23 at 07:31
  • @error404 Thanks for suggestion. In this solution they are running the command directly. What I need is go inside a Kubernetes pod then run my command and to go inside a pod I am not allowed to use kubectl command. I can only use K8s go-client. If you know any solution please tell me. Thank you. – Dhanu Jan 14 '23 at 08:56
  • 2
    Trying to exec a command like this probably isn't a correct solution. What's your higher-level goal? [`PodInterface`](https://pkg.go.dev/k8s.io/client-go@v0.26.0/kubernetes/typed/core/v1#PodInterface) in fact does not have an `Exec()` method. Are you looking for [example of exec in k8s's pod by using go client](https://stackoverflow.com/questions/43314689/example-of-exec-in-k8ss-pod-by-using-go-client)? – David Maze Jan 14 '23 at 11:52

1 Answers1

1

As @David Maze shared, to use k8's go client to exec command in a pod follow the below code:

import (
    "io"

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

// 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{
        "sh",
        "-c",
        command,
    }
    req := client.CoreV1().RESTClient().Post().Resource("pods").Name(podName).
        Namespace("default").SubResource("exec")
    option := &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, "POST", 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
}

Also refer to this link for more information

Fariya Rahmat
  • 2,123
  • 3
  • 11