使用客户端go从exec到pod

使用客户端go从exec到pod,go,kubernetes,client-go,Go,Kubernetes,Client Go,我是新手,客户是:) 我看到了这个功能,我可以使用它向带有交互终端的pod发送命令或exec。在下面,我需要知道我应该提供哪些参数来从func main()调用它,因为我可以看到它需要“config*restclient.config”,我不理解。有什么例子或起点吗 另外,我知道如何使用kubeconfig创建客户端集进行身份验证。只需要知道这需要什么参数,以及如何从主函数调用它 package main import ( "io" v1 "k8

我是新手,客户是:)

我看到了这个功能,我可以使用它向带有交互终端的pod发送命令或exec。在下面,我需要知道我应该提供哪些参数来从func main()调用它,因为我可以看到它需要“config*restclient.config”,我不理解。有什么例子或起点吗

另外,我知道如何使用kubeconfig创建客户端集进行身份验证。只需要知道这需要什么参数,以及如何从主函数调用它

package main

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"
)

//ExecCmdExample 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
}

我能够解决这个问题。这个问题可以结束了

编辑:

您可以在此处查看代码:


请编辑您的答案,以帮助有相同问题的人。您知道是否有办法获取远程执行命令的退出代码吗?