-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkubeconfig.go
150 lines (133 loc) · 3.34 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package kubeconfig
import (
"fmt"
"os"
"path"
"github.com/jfreeland/kube-tmuxp/pkg/commander"
"github.com/jfreeland/kube-tmuxp/pkg/filesystem"
)
// KubeConfig exposes methods to perform actions on kubeconfig
type KubeConfig struct {
filesystem filesystem.FileSystem
commander commander.Commander
kubeCfgsDir string
}
// Delete deletes the given kubeconfig file
func (k *KubeConfig) Delete(kubeCfgFile string) error {
if err := k.filesystem.Remove(kubeCfgFile); err != nil && !os.IsNotExist(err) {
return err
}
return nil
}
// AddRegionalEKSCluster imports Kubernetes context for a regional EKS cluster
func (k *KubeConfig) AddRegionalEKSCluster(cluster string, region string, role string, env map[string]string, kubeCfgFile string) error {
var args []string
if len(role) > 0 {
args = []string{
"eks",
"update-kubeconfig",
"--name",
cluster,
"--kubeconfig",
kubeCfgFile,
"--region",
region,
"--role-arn",
role,
}
} else {
args = []string{
"eks",
"update-kubeconfig",
"--name",
cluster,
"--kubeconfig",
kubeCfgFile,
"--region",
region,
}
}
envs := []string{}
for k, v := range env {
envs = append(envs, k+"="+v)
}
if _, err := k.commander.Execute("aws", args, envs); err != nil {
return err
}
return nil
}
// AddRegionalGKECluster imports Kubernetes context for
// a regional Kubernetes cluster
func (k *KubeConfig) AddRegionalGKECluster(project string, cluster string, region string, kubeCfgFile string) error {
args := []string{
"beta",
// Is beta needed here? You can run into an issue where if
// you haven't updated gcloud lately, beta will stop and
// report that it needs updated, but gcloud itself will not.
"container",
"clusters",
"get-credentials",
cluster,
fmt.Sprintf("--region=%s", region),
fmt.Sprintf("--project=%s", project),
}
envs := []string{
fmt.Sprintf("KUBECONFIG=%s", kubeCfgFile),
}
if _, err := k.commander.Execute("gcloud", args, envs); err != nil {
return err
}
return nil
}
// AddZonalGKECluster imports Kubernetes context for
// a zonal Kubernetes cluster
func (k *KubeConfig) AddZonalGKECluster(project string, cluster string, zone string, kubeCfgFile string) error {
args := []string{
"container",
"clusters",
"get-credentials",
cluster,
fmt.Sprintf("--zone=%s", zone),
fmt.Sprintf("--project=%s", project),
}
envs := []string{
fmt.Sprintf("KUBECONFIG=%s", kubeCfgFile),
}
if _, err := k.commander.Execute("gcloud", args, envs); err != nil {
return err
}
return nil
}
// RenameContext renames a Kubernetes context
func (k *KubeConfig) RenameContext(oldCtx string, newCtx string, kubeCfgFile string) error {
args := []string{
"config",
"rename-context",
oldCtx,
newCtx,
}
envs := []string{
fmt.Sprintf("KUBECONFIG=%s", kubeCfgFile),
}
if _, err := k.commander.Execute("kubectl", args, envs); err != nil {
return err
}
return nil
}
// KubeCfgsDir returns the directory in which kube configs are stored
func (k *KubeConfig) KubeCfgsDir() string {
return k.kubeCfgsDir
}
// New returns a new KubeConfig
func New(fs filesystem.FileSystem, cmdr commander.Commander) (KubeConfig, error) {
home, err := fs.HomeDir()
if err != nil {
return KubeConfig{}, err
}
kubeConfigsDir := path.Join(home, ".kube/configs")
return KubeConfig{
filesystem: fs,
commander: cmdr,
kubeCfgsDir: kubeConfigsDir,
}, nil
}