-
Notifications
You must be signed in to change notification settings - Fork 126
/
file.go
97 lines (75 loc) · 2.3 KB
/
file.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
package kubeconfig
import (
"os"
"path/filepath"
"time"
"github.com/pkg/errors"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)
// WriteToFile writes the given config to the kubeconfig file.
func WriteToFile(config clientcmdapi.Config, path string) error {
configFile := filepath.Join(path, "config")
now := time.Now().Format("20060102150405")
// check if config file exists
if _, err := os.Stat(configFile); err == nil {
// if it exists, write a new config file with a timestamp
fileName := "config_" + now + ".yaml"
newKubeConfigFile := filepath.Join(path, fileName)
err = clientcmd.WriteToFile(config, newKubeConfigFile)
if err != nil {
return errors.Wrap(err, "failed to write new kubeconfig file")
}
defer os.Remove(newKubeConfigFile)
load := clientcmd.ClientConfigLoadingRules{
Precedence: []string{configFile, newKubeConfigFile},
}
mergedConfig, err := load.Load()
if err != nil {
return errors.Wrap(err, "failed to load merged kubeconfig")
}
config = *mergedConfig
}
return clientcmd.WriteToFile(config, configFile)
}
// RemoveContextFromFile removes the given context and its related
// cluster and user from the kubeconfig file.
func RemoveContextFromFile(context string, path string) error {
config, err := clientcmd.LoadFromFile(path)
if err != nil {
return errors.Wrap(err, "failed to load kubeconfig file")
}
// remove the context from the config
contextConfig, ok := config.Contexts[context]
if !ok {
return errors.New("context not found in kubeconfig")
}
clusterToRemove := contextConfig.Cluster
userToRemove := contextConfig.AuthInfo
delete(config.Contexts, context)
// check if cluster is used in other contexts
clusterUsed := false
for _, contextConfig := range config.Contexts {
if contextConfig.Cluster == clusterToRemove {
clusterUsed = true
break
}
}
// remove the cluster from the config
if !clusterUsed {
delete(config.Clusters, clusterToRemove)
}
// check if user is used in other contexts
userUsed := false
for _, contextConfig := range config.Contexts {
if contextConfig.AuthInfo == userToRemove {
userUsed = true
break
}
}
// remove the user from the config
if !userUsed {
delete(config.AuthInfos, userToRemove)
}
return clientcmd.WriteToFile(*config, path)
}