在运行特定命令时,出现错误。

huangapple go评论92阅读模式
英文:

While running specific command into a pod getting error

问题

我正在尝试在运行中的 Pod 中执行一个命令。我正在使用 Go 的 K8sclient 来实现这个功能,但是遇到了一个问题。我也不知道解决方案是否正确。请问有人可以检查并提供正确的解决方案吗?

这是我的代码:

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

// 设置一个流来捕获输出
execStream, err := req.Stream()
if err != nil {
    fmt.Println(err)
    os.Exit(1)
}

// 打印输出
buf := new(bytes.Buffer)
buf.ReadFrom(execStream)
fmt.Println(buf.String())

我得到的错误是:

clientset.CoreV1().Pods(namespace).Exec undefined (type "k8s.io/client-go/kubernetes/typed/core/v1".PodInterface has no field or method Exec)
英文:

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)

答案1

得分: 1

如@David Maze所分享的,要使用k8的go客户端在pod中执行命令,请按照以下代码进行操作:

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在特定的pod上执行命令并等待命令的输出。
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
}

更多信息请参考此链接

英文:

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

huangapple
  • 本文由 发表于 2023年1月14日 15:20:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75116409.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定