example of exec in k8s's pod by using go client

4/10/2017

I want to use k8s go client to exec command in a pod. However I cannot find any example about this. So I read kubectl exec source code, and write code as below. And err = exec.Stream(sopt) always get an error without any message. Can anyone tell me how to debug this problem, or give me a correct example.

config := &restclient.Config{
 Host: "http://192.168.8.175:8080",
Insecure: true,
}

config.ContentConfig.GroupVersion = &api.Unversioned
config.ContentConfig.NegotiatedSerializer = api.Codecs

restClient, err := restclient.RESTClientFor(config)
if err != nil {
  panic(err.Error())
}

req := restClient.Post().Resource("pods").Name("wordpress-mysql-213049546-29s7d").Namespace("default").SubResource("exec").Param("container", "mysql")
req.VersionedParams(&api.PodExecOptions{
Container: "mysql",
Command:   []string{"ls"},
Stdin:     true,
Stdout:    true,
}, api.ParameterCodec)

 exec, err := remotecommand.NewExecutor(config, "POST", req.URL())
 if err != nil {
   panic(err.Error())
}
sopt := remotecommand.StreamOptions{
SupportedProtocols: remotecommandserver.SupportedStreamingProtocols,
Stdin:              os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
Tty:    false,
}

err = exec.Stream(sopt)
if err != nil {
 panic(err.Error())
}
-- JJBoooom
kubernetes

5 Answers

10/9/2019
package k8s

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
}

It works to me.

-- noanswer
Source: StackOverflow

4/15/2017

It may be interesting for you, to have a look at e2e/framework/exec_util.go

-- Adam Otto
Source: StackOverflow

11/18/2019

you miss .CoreV1() when create request!

-- zhongfox
Source: StackOverflow

7/2/2018

I faced this problem in execing into the Pod using Kubernetes' Client-go for long. But finally I found a way to make it work. I have written a simple code to perform this task here in my repo. I request you to check it. I believe it will be helpful.

-- Abhishek Kashyap
Source: StackOverflow

1/22/2019

it worked for me when the command is an array of strings starting with /bin/sh:

[]string{"/bin/sh", "-c", "ls", "-ll", "."}
-- Yasha
Source: StackOverflow