-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubeconfig.go
61 lines (50 loc) · 1.47 KB
/
kubeconfig.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package kubeconfig
import (
"fmt"
"os"
"os/exec"
"github.com/go-logr/logr"
"github.com/nais/narcos/internal/gcp"
kubeClient "k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
"k8s.io/klog/v2"
)
func CreateKubeconfig(email string, clusters []gcp.Cluster, overwrite, clean, verbose bool) error {
configLoad := kubeClient.NewDefaultClientConfigLoadingRules()
// If KUBECONFIG is set, but the file does not exist, kubeClient will throw a warning.
// since we're creating the file, we can safely ignore this warning.
klog.SetLogger(logr.Discard())
config, err := configLoad.Load()
if err != nil {
return err
}
if clean {
config.AuthInfos = map[string]*api.AuthInfo{}
config.Contexts = map[string]*api.Context{}
config.Clusters = map[string]*api.Cluster{}
}
err = addUsers(config, clusters, email, overwrite, verbose)
if err != nil {
return err
}
err = addClusters(config, clusters, email, overwrite, verbose)
if err != nil {
return err
}
err = kubeClient.WriteToFile(*config, configLoad.GetDefaultFilename())
if err != nil {
return err
}
fmt.Println("Kubeconfig written to", configLoad.GetDefaultFilename())
for _, user := range config.AuthInfos {
if user == nil || user.Exec == nil {
continue
}
_, err = exec.LookPath(user.Exec.Command)
if err != nil {
fmt.Printf("%v\nWARNING: %v not found in PATH.\n", os.Stderr, user.Exec.Command)
fmt.Printf("%v\n%v\n", os.Stderr, user.Exec.InstallHint)
}
}
return nil
}