-
Notifications
You must be signed in to change notification settings - Fork 6
/
helper.go
41 lines (34 loc) · 997 Bytes
/
helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package helper
import (
"os"
"path/filepath"
"k8s.io/client-go/tools/clientcmd"
"strings"
)
// GetKubeConfigPath returns the default location of a kubeconfig file
func GetKubeConfigPath() []string {
if fl := os.Getenv("KUBECONFIG"); fl != "" {
// split KUBECONFIG string to handle multiple kube config files
kubeConfigs := strings.Split(fl, ":")
return kubeConfigs
}
home, err := os.UserHomeDir()
if err != nil { // Can't find home dir
panic(err.Error())
}
return []string {filepath.Join(home, ".kube", "config")}
}
// GetContexts returns a list of clusters from a config file
func GetContexts(configpath string) []string {
config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: configpath},
&clientcmd.ConfigOverrides{}).RawConfig()
if err != nil {
panic(err.Error())
}
ctxs := make([]string, 0, len(config.Contexts))
for k := range config.Contexts {
ctxs = append(ctxs, k)
}
return ctxs
}