Home > database >  Access kubectl explain
Access kubectl explain

Time:10-14

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.

CodePudding user response:

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.

  • Related