首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >exec到pod使用client-go

exec到pod使用client-go
EN

Stack Overflow用户
提问于 2020-11-23 01:08:06
回答 1查看 510关注 0票数 0

我是新来的go和client-go :)。

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

PS。我知道如何创建客户端集来使用kubeconfig进行身份验证。只需要知道这将需要哪些参数,以及如何从main函数调用它。

代码语言:javascript
复制
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
}
EN

回答 1

Stack Overflow用户

发布于 2020-11-24 01:29:13

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

编辑:

你可以在这里查看代码:

https://github.com/mohatb/kubego

票数 -3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64957065

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档