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