-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
269 lines (207 loc) · 6.8 KB
/
main.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"net/url"
"os"
"sort"
"strings"
"time"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kubernetes "k8s.io/client-go/kubernetes"
clientcmd "k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
_ "k8s.io/client-go/plugin/pkg/client/auth/azure"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
)
type contextNamespaceTuple struct {
k8sContext string
k8sNamespace string
}
var (
kubeconfLocation = os.Getenv("HOME") + "/.kube/config"
mergedConfig *clientcmdapi.Config
)
func main() {
var err error
if len(os.Args) > 1 {
if os.Args[1] == "-h" || os.Args[1] == "--help" {
printUsage()
}
}
if len(os.Getenv("KUBECONFIG")) > 0 {
kubeconfLocation = os.Getenv("KUBECONFIG")
}
loadingRules := &clientcmd.ClientConfigLoadingRules{Precedence: strings.Split(kubeconfLocation, ":")}
mergedConfig, err = loadingRules.Load()
if err != nil {
log.Fatalln(err)
}
if len(os.Args) > 1 {
quickSwitch()
}
app := tview.NewApplication()
nodeRoot := tview.NewTreeNode("Contexts").SetSelectable(false)
expandedNode := new(tview.TreeNode)
highlightNode := nodeRoot
for _, thisContextName := range mapKeysToSortedArray(mergedConfig.Contexts) {
nodeContextName := tview.NewTreeNode(" " + thisContextName)
namespacesInThisContextsCluster, err := getNamespacesInContextsCluster(thisContextName)
if err != nil {
nodeContextName.SetColor(tcell.ColorRed).SetText(" " + thisContextName + " (" + err.Error() + ")").SetSelectable(false)
} else if thisContextName == mergedConfig.CurrentContext {
nodeContextName.SetColor(tcell.ColorGreen).SetText(" " + thisContextName + " (active)")
} else {
nodeContextName.SetColor(tcell.ColorTurquoise)
}
nodeContextName.Collapse()
nodeContextName.SetSelectedFunc(func() {
nodeContextName.SetExpanded(!nodeContextName.IsExpanded())
if nodeContextName.IsExpanded() && expandedNode != nodeContextName {
expandedNode.Collapse()
expandedNode = nodeContextName
}
})
nodeRoot.AddChild(nodeContextName)
for _, thisNamespace := range namespacesInThisContextsCluster {
nodeNamespace := tview.NewTreeNode(" " + thisNamespace.Name).SetReference(contextNamespaceTuple{thisContextName, thisNamespace.Name})
if thisContextName == mergedConfig.CurrentContext {
nodeContextName.Expand()
expandedNode = nodeContextName
if thisNamespace.Name == mergedConfig.Contexts[thisContextName].Namespace {
nodeNamespace.SetColor(tcell.ColorGreen)
highlightNode = nodeNamespace
}
}
nodeNamespace.SetSelectedFunc(func() {
app.Stop()
switchContext(nodeNamespace.GetReference().(contextNamespaceTuple))
})
nodeContextName.AddChild(nodeNamespace)
}
}
tree := tview.NewTreeView().SetRoot(nodeRoot).SetCurrentNode(highlightNode)
if err := app.SetRoot(tree, true).Run(); err != nil {
log.Fatalln(err)
}
}
func getNamespacesInContextsCluster(k8sContext string) ([]corev1.Namespace, error) {
config, err := clientcmd.NewDefaultClientConfig(*mergedConfig, &clientcmd.ConfigOverrides{CurrentContext: k8sContext}).ClientConfig()
if err != nil {
log.Fatalln(err)
}
config.Timeout = time.Second
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatalln(err)
}
namespaces, err := clientset.CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{})
if err != nil {
switch err.(type) {
case *url.Error:
return []corev1.Namespace{}, fmt.Errorf("unreachable")
case *apierrors.StatusError:
return []corev1.Namespace{}, fmt.Errorf("error from api: " + err.Error())
default:
return []corev1.Namespace{}, fmt.Errorf("error")
}
}
return namespaces.Items, nil
}
func switchContext(rh contextNamespaceTuple) {
mergedConfig.CurrentContext = rh.k8sContext
mergedConfig.Contexts[rh.k8sContext].Namespace = rh.k8sNamespace
removeStaleContextConfigs()
configAccess := clientcmd.NewDefaultClientConfigLoadingRules()
if err := clientcmd.ModifyConfig(configAccess, *mergedConfig, false); err != nil {
log.Fatalln(err)
}
log.Printf("switched to %s/%s", rh.k8sContext, rh.k8sNamespace)
}
func quickSwitch() {
if len(os.Args) == 2 {
if !namespaceExists(mergedConfig.CurrentContext, os.Args[1]) {
log.Fatalf("namespace %s not found in context %s\n", os.Args[1], mergedConfig.CurrentContext)
}
switchContext(contextNamespaceTuple{mergedConfig.CurrentContext, os.Args[1]})
os.Exit(0)
}
if len(os.Args) == 3 && os.Args[2] == "." {
if !contextExists(os.Args[1]) || !namespaceExists(os.Args[1], "default") {
log.Fatalf("namespace %s not found in context %s\n", "default", os.Args[1])
}
switchContext(contextNamespaceTuple{os.Args[1], "default"})
os.Exit(0)
}
if len(os.Args) == 3 {
if !contextExists(os.Args[1]) || !namespaceExists(os.Args[1], os.Args[2]) {
log.Fatalf("namespace %s not found in context %s\n", os.Args[2], os.Args[1])
}
switchContext(contextNamespaceTuple{os.Args[1], os.Args[2]})
os.Exit(0)
}
}
func removeStaleContextConfigs() {
for _, configFilename := range strings.Split(kubeconfLocation, ":") {
var output []string
cfStat, err := os.Stat(configFilename)
if err != nil {
log.Fatalln("could not stat kubeconfig files")
}
cfFileMode := cfStat.Mode()
cfContent, err := ioutil.ReadFile(configFilename)
if err != nil {
log.Fatalln("could not read kubeconfig files")
}
cfLines := strings.Split(string(cfContent), "\n")
for _, line := range cfLines {
if strings.Contains(line, "current-context:") {
continue
}
output = append(output, line)
}
if err := ioutil.WriteFile(configFilename, []byte(strings.Join(output, "\n")), cfFileMode); err != nil {
log.Fatalln("could not update kubeconfig files")
}
}
}
func contextExists(k8sContext string) bool {
_, exists := mergedConfig.Contexts[k8sContext]
return exists
}
func namespaceExists(k8sContext, k8sNamespace string) bool {
namespacesInThisContextsCluster, err := getNamespacesInContextsCluster(k8sContext)
if err != nil {
log.Fatalln(err)
}
for _, ns := range namespacesInThisContextsCluster {
if ns.Name == k8sNamespace {
return true
}
}
return false
}
func mapKeysToSortedArray(m map[string]*clientcmdapi.Context) []string {
var s []string
for k := range m {
s = append(s, k)
}
sort.Strings(s)
return s
}
func printUsage() {
usageText := `usage:
./kubeswitch select context/namespace graphically
./kubeswitch <namespace> switch to namespace in current context quickly
./kubeswitch <context> <namespace> switch to namespace in context quickly
./kubeswitch <context>/<namespace> switch to namespace in context quickly`
fmt.Println(usageText)
os.Exit(2)
}