Created
March 20, 2018 07:45
-
-
Save int128/a01b044009d407629fbde6985258d74f to your computer and use it in GitHub Desktop.
Read and write Kubernetes client config (~/.kube/config) in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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