Skip to content

Instantly share code, notes, and snippets.

@AmaranthLIS
Created October 19, 2018 23:48
Show Gist options
  • Save AmaranthLIS/cbc20f583ca71cb39d6fc697fb67769d to your computer and use it in GitHub Desktop.
Save AmaranthLIS/cbc20f583ca71cb39d6fc697fb67769d to your computer and use it in GitHub Desktop.
Golang, Read and write Kubernetes client config (~/.kube/config)
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