Skip to content

Instantly share code, notes, and snippets.

@int128 int128/main.go
Created Mar 20, 2018

Embed
What would you like to do?
Read and write Kubernetes client config (~/.kube/config) in Go
package main
import (
"log"
"os"
"github.com/mitchellh/go-homedir"
"k8s.io/client-go/tools/clientcmd"
)
func main() {
kubeConfigPath, err := findKubeConfig()
if err != nil {
log.Fatal(err)
}
kubeConfig, err := clientcmd.LoadFromFile(kubeConfigPath)
if err != nil {
log.Fatal(err)
}
log.Printf("current context is %s", kubeConfig.CurrentContext)
err = clientcmd.WriteToFile(*kubeConfig, "copy.yaml")
if err != nil {
log.Fatal(err)
}
}
// findKubeConfig finds path from env:KUBECONFIG or ~/.kube/config
func findKubeConfig() (string, error) {
env := os.Getenv("KUBECONFIG")
if env != "" {
return env, nil
}
path, err := homedir.Expand("~/.kube/config")
if err != nil {
return "", err
}
return path, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.