/
kubeconfig.go
100 lines (88 loc) · 2.51 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package selector
import (
"errors"
"os"
"path/filepath"
"strings"
"github.com/mitchellh/go-homedir"
"github.com/sirupsen/logrus"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
)
const (
mark = "# Written by rs"
)
func loadActiveKubeConfig(dir string, file string) api.Config {
dir, _ = homedir.Expand(dir)
config, err := clientcmd.LoadFromFile(filepath.Join(dir, file))
if err != nil {
logrus.Debugf("Error loading kubeConfig %s/%s \nError:%v", dir, file, err)
return api.Config{}
}
return *config
}
func loadKubeConfig(dir string, file string) (api.Config, error) {
config, err := clientcmd.LoadFromFile(filepath.Join(dir, file))
if err != nil {
logrus.Debugf("Error loading kubeConfig %s/%s \nError:%v", dir, file, err)
return api.Config{}, err
}
return *config, nil
}
func loadKubeConfigsFromDirectory(dir string) []api.Config {
var apiConfigs []api.Config
dir, _ = homedir.Expand(dir)
files, err := os.ReadDir(dir)
if err != nil {
logrus.Fatalf("Error reading directory: %s (%v)", dir, err)
}
for _, file := range files {
if !file.IsDir() {
if strings.HasSuffix(file.Name(), ".yaml") {
config, err := loadKubeConfig(dir, file.Name())
if err == nil {
apiConfigs = append(apiConfigs, config)
}
}
}
}
return apiConfigs
}
func loadKubeConfigs(appconfig *AppConfig) ([]api.Config, api.Config) {
apiConfigs := loadKubeConfigsFromDirectory(appconfig.KubeconfigDir)
for _, dir := range appconfig.ExtraKubeconfigDirs {
apiConfigs = append(apiConfigs, loadKubeConfigsFromDirectory(dir)...)
}
activeConfig := loadActiveKubeConfig(appconfig.KubeconfigDir, appconfig.KubeconfigFile)
return apiConfigs, activeConfig
}
func markKubeConfig(path string) {
data, _ := os.ReadFile(path)
config := mark + "\n" + string(data)
os.WriteFile(path, []byte(config), 0600)
}
func checkMark(path string) bool {
_, err := os.Stat(path)
if errors.Is(err, os.ErrNotExist) {
return true
}
data, _ := os.ReadFile(path)
if strings.HasPrefix(string(data), mark) {
return true
}
return false
}
func saveKubeConfig(config *api.Config, context string, dir string, file string) error {
dir, _ = homedir.Expand(dir)
path := filepath.Join(dir, file)
config.CurrentContext = context
if !checkMark(path) {
return errors.New("Kubeconfig (" + path + ") is not managed by rs. Remove/rename this file first.")
}
err := clientcmd.WriteToFile(*config, path)
markKubeConfig(path)
if err != nil {
return errors.New("Unable to write " + path + " Error: " + err.Error())
}
return nil
}