Skip to content

Instantly share code, notes, and snippets.

@bonifaido
Last active May 11, 2018 18:49
Show Gist options
  • Save bonifaido/4655a6460fbbfc0b98b151ca90622ef3 to your computer and use it in GitHub Desktop.
Save bonifaido/4655a6460fbbfc0b98b151ca90622ef3 to your computer and use it in GitHub Desktop.
This code, I have written at least 3 times, now, I don't want to forget it anymore.
package main
import (
"os"
"k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/remotecommand"
)
func main() {
config, err := clientcmd.BuildConfigFromFlags("", os.Getenv("HOME")+"/.kube/config")
if err != nil {
panic(err)
}
client, err := kubernetes.NewForConfig(config)
req := client.CoreV1().RESTClient().Post().
Resource("pods").
Name("cp-launcher-vault-696c6c8f79-fqmkq").
Namespace("default").
SubResource("exec").
Param("container", "vault").
VersionedParams(&v1.PodExecOptions{
Container: "vault",
Command: []string{"cat", "/vault/config/config.json"},
Stdin: false,
Stdout: true,
Stderr: true,
TTY: false,
}, scheme.ParameterCodec)
exec, err := remotecommand.NewSPDYExecutor(config, "POST", req.URL())
if err != nil {
panic(err)
}
err = exec.Stream(remotecommand.StreamOptions{Stdout: os.Stdout, Stderr: os.Stderr, Tty: false})
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment