【问题标题】:exec to pod using client-go [duplicate]使用client-go执行到pod [重复]
【发布时间】:2020-11-22 17:08:06
【问题描述】:

我是新来的客户和客户:)。

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

PS。我知道如何创建客户端集以使用 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
}

【问题讨论】:

  • 不知道有没有办法获取远程执行命令的退出码?

标签: go kubernetes client-go


【解决方案1】:

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

编辑:

你可以看这里的代码:

https://github.com/mohatb/kubego

【讨论】:

  • 请编辑您的答案以帮助有相同问题的人。
猜你喜欢
  • 1970-01-01
  • 2020-06-29
  • 1970-01-01
  • 2020-09-30
  • 2016-01-04
  • 2016-09-16
  • 2021-03-28
  • 2021-08-01
  • 2020-12-06
相关资源
最近更新 更多