访问 kubectl explain

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

Access kubectl explain

问题

kubectl explain 是用于解释 kubectl 中对象属性的命令。例如,k explain pod.spec.volumes.secret 可以帮助你了解如何将密钥挂载到你的 Pod 中。

我想在 Go 中编写一个包装器来实现这个功能。我该如何访问 explain 命令?据我所知,go client lib 无法访问 explain 命令,因为 explain 是 kubectl 的一个特性,而不是集群本身的特性。我希望避免编写一个 bash 脚本,在 Go 中执行它并解析输出。

英文:

kubectl explain tells explains attribute of an object in kubectl. E.g. k explain pod.spec.volumes.secret gives you some insight about how to mount secrets into your pod.

I would like to write a wrapper for this in Go. How do I access explain? Afaik the go client lib cannot access this, because explain is a feature of kubectl and not the cluster itself. I would like to avoid writing a bash script, executing this from go and parsing the output.

答案1

得分: 1

你可以使用os/exec包来执行kubectl命令并解析输出。

使用该包,你可以执行kubectl命令并解析输出。

func main() {
    cmd := exec.Command("kubectl", "arg1", "arg2")
   
    var out bytes.Buffer
    cmd.Stdout = &out

    err := cmd.Run()

    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf(string(out.Bytes()))
}

请在这里找到该包的文档。

另外,你可以尝试导入kubectl包的重要部分,并从代码中调用这些函数。这可能有效,因为kubectl是用Go编写的。

可以查看这个仓库:https://github.com/kubernetes/kubectl

我从未尝试过这样做,所以无法给出更多提示。

英文:

You could use the os/exec package.

Using that package you can execute your kubectl command and parse the output.

func main() {
    cmd := exec.Command("kubectl", "arg1", "arg2")
   
    var out bytes.Buffer
    cmd.Stdout = &out

    err := cmd.Run()

    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf(string(out.Bytes()))
}

Please find the documentation of that package here

Alternatively, you could try to import the important bits of the kubectl package and call these functions from within your code. This could work, as kubectl is written in Go.

Have a look at that repo: https://github.com/kubernetes/kubectl

I have never tried that, so i can't give further hints.

huangapple
  • 本文由 发表于 2021年10月13日 17:57:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/69553380.html
匿名

发表评论

匿名网友

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

确定