/
menus.go
566 lines (489 loc) · 14.7 KB
/
menus.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
package features
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"time"
survey "github.com/AlecAivazis/survey/v2"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/portforward"
"k8s.io/client-go/transport/spdy"
"k8s.io/client-go/util/homedir"
)
const (
Logo = ` _ ___ _ _
| | _( _ ) ___ ___ ___ _ __ | |_ _____ _| |_
| |/ / _ \/ __|_____ / __/ _ \| '_ \| __/ _ \ \/ / __|
| < (_) \__ \_____| (_| (_) | | | | || __/> <| |_
|_|\_\___/|___/ \___\___/|_| |_|\__\___/_/\_\\__|
------------------------------------------------------`
Author = `
DevOps Corner Indonesia (devopscorner.id)
https://github.com/devopscorner/k8s-context
Apache v2.0 License`
AppName = "K8S-CONTEXT (K8C)"
VERSION = "v1.1.9"
)
func GetCommands() []*cobra.Command {
if home := homedir.HomeDir(); home != "" {
kubeconfig = filepath.Join(home, ".kube", "config")
}
kc := &KubeConfig{}
versionCmd := &cobra.Command{
Use: "version",
Short: "Print the version number of k8s-context",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("k8s-context " + VERSION)
},
}
listContextsCmd := &cobra.Command{
Use: "list",
Short: "List all available Kubernetes contexts",
RunE: func(cmd *cobra.Command, args []string) error {
InitConfig()
if configBytes == nil {
// Print the list of context names
fmt.Println("No available contexts!")
} else {
// Get the map of context name to context config
config, err := clientcmd.Load(configBytes)
if err != nil {
return err
}
ShowDetailList(config)
}
return nil
},
}
loadCmd := &cobra.Command{
Use: "load [file...]",
Short: "Load a kubeconfig file",
Long: "Load one or more kubeconfig files into k8s-context",
RunE: func(cmd *cobra.Command, args []string) error {
var files []string
if len(args) == 0 {
home, err := os.UserHomeDir()
if err != nil {
return err
}
kubeDir := filepath.Join(home, ".kube")
err = filepath.Walk(kubeDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && strings.HasPrefix(info.Name(), "config") {
files = append(files, path)
}
return nil
})
if err != nil {
return err
}
} else {
files = args
}
// Prompt the user to select a kubeconfig file if more than one is found
if len(files) > 1 {
var selected string
prompt := &survey.Select{
Message: "Select a kubeconfig file:",
Options: files,
}
if err := survey.AskOne(prompt, &selected); err != nil {
return err
}
files = []string{selected}
}
kc.Files = files
if err := kc.Load(); err != nil {
return err
}
selectedConfig := strings.Join(kc.Files, "\n")
fmt.Printf("Loaded kubeconfig file(s):\n%s\n", selectedConfig)
// Get the map of context name to context config
configBytes, err := os.ReadFile(selectedConfig)
config, err := clientcmd.Load(configBytes)
if err != nil {
return err
}
var contextNames []string
for contextName := range config.Contexts {
contextNames = append(contextNames, contextName)
}
SelectedConfig(contextNames, config)
return nil
},
}
mergeCmd := &cobra.Command{
Use: "merge",
Short: "Merge multiple kubeconfig files",
RunE: func(cmd *cobra.Command, args []string) error {
kc.Files = args
if err := kc.Load(); err != nil {
return err
}
mergedFile := "merged-config"
if len(args) > 0 {
mergedFile = args[0]
}
if err := kc.SaveToFile(mergedFile); err != nil {
return err
}
fmt.Printf("Merged kubeconfig files:\n%s\n", kc.Files)
fmt.Printf("Saved merged kubeconfig to file: %s\n", mergedFile)
return nil
},
}
getCmd := &cobra.Command{
Use: "get",
Short: "Get Kubernetes resources (ns, svc, deploy, po, ep)",
Long: "Get Kubernetes resources: namespace (ns), services (svc), deployments (deploy), pods (po), endpoints (ep)",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("resource type not specified")
}
clientset, err := GetClientSet(kubeconfig)
if err != nil {
return err
}
ctx := context.Background()
resource := args[0]
namespaces, err := cmd.Flags().GetStringSlice("namespace")
if err != nil {
return err
}
if len(namespaces) == 0 {
// If namespace is not specified, get all namespaces
nsList, err := clientset.CoreV1().Namespaces().List(ctx, metav1.ListOptions{})
if err != nil {
return err
}
for _, ns := range nsList.Items {
namespace := ns.Name
fmt.Printf("Namespace: %s\n", namespace)
switch resource {
case "pods", "po":
pods, err := clientset.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return err
}
ShowPodsByFilter(pods)
case "namespaces", "ns":
var namespaces *corev1.NamespaceList
if namespace != "" {
ns, err := clientset.CoreV1().Namespaces().Get(ctx, namespace, metav1.GetOptions{})
if err != nil {
return err
}
namespaces = &corev1.NamespaceList{Items: []corev1.Namespace{*ns}}
} else {
ns, err := clientset.CoreV1().Namespaces().List(ctx, metav1.ListOptions{})
if err != nil {
return err
}
namespaces = ns
}
ShowNamespaceByFilter(namespaces)
case "services", "svc":
services, err := clientset.CoreV1().Services(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return err
}
ShowServiceByFilter(services)
case "endpoints", "ep":
endpoints, err := clientset.CoreV1().Endpoints(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return err
}
ShowEndpointByFilter(endpoints)
case "deployment", "deploy":
deployments, err := clientset.AppsV1().Deployments(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return err
}
ShowDeploymentByFilter(deployments)
default:
return fmt.Errorf("unknown resource type: %s", resource)
}
}
} else {
// If namespace is specified, get resources only in those namespaces
for _, namespace := range namespaces {
fmt.Printf("Namespace: %s\n", namespace)
switch resource {
case "pods", "po":
pods, err := clientset.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return err
}
ShowPodsByFilter(pods)
case "namespaces", "ns":
var namespaces *corev1.NamespaceList
if namespace != "" {
ns, err := clientset.CoreV1().Namespaces().Get(ctx, namespace, metav1.GetOptions{})
if err != nil {
return err
}
namespaces = &corev1.NamespaceList{Items: []corev1.Namespace{*ns}}
} else {
ns, err := clientset.CoreV1().Namespaces().List(ctx, metav1.ListOptions{})
if err != nil {
return err
}
namespaces = ns
}
ShowNamespaceByFilter(namespaces)
case "services", "svc":
services, err := clientset.CoreV1().Services(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return err
}
ShowServiceByFilter(services)
case "endpoints", "ep":
endpoints, err := clientset.CoreV1().Endpoints(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return err
}
ShowEndpointByFilter(endpoints)
case "deployment", "deploy":
deployments, err := clientset.AppsV1().Deployments(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return err
}
ShowDeploymentByFilter(deployments)
default:
return fmt.Errorf("unknown resource type: %s", resource)
}
}
}
return nil
},
}
showCmd := &cobra.Command{
Use: "show",
Short: "Describe / show kubernetes resources (po, logs, port, node)",
Long: "Describe / show Kubernetes resources: pods (po), logs, port-forward (port), node",
}
// Add subcommands for each resource type
showCmd.AddCommand(&cobra.Command{
Use: "po [pods]",
Short: "Describe a specific pods",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("pod name not specified")
}
clientset, err := GetClientSet(kubeconfig)
if err != nil {
return err
}
ctx := context.Background()
namespaces, err := cmd.Flags().GetStringSlice("namespace")
if err != nil {
return err
}
for _, namespace := range namespaces {
for _, pod := range args {
po, err := clientset.CoreV1().Pods(namespace).Get(ctx, pod, metav1.GetOptions{})
if err != nil {
return err
}
DescribePodsDetail(po)
}
}
return nil
},
})
showCmd.AddCommand(&cobra.Command{
Use: "logs [pods]",
Short: "Show logs from a specific pods",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("pod name not specified")
}
clientset, err := GetClientSet(kubeconfig)
if err != nil {
return err
}
ctx := context.Background()
namespaces, err := cmd.Flags().GetStringSlice("namespace")
if err != nil {
return err
}
for _, namespace := range namespaces {
for _, pod := range args {
// Get logs from the pod
req := clientset.CoreV1().Pods(namespace).GetLogs(pod, &corev1.PodLogOptions{})
stream, err := req.Stream(ctx)
if err != nil {
return err
}
defer stream.Close()
// Print the logs
buf := new(bytes.Buffer)
_, err = io.Copy(buf, stream)
if err != nil {
return err
}
fmt.Printf("Logs from pod %s:\n%s\n", pod, buf.String())
}
}
return nil
},
})
showCmd.AddCommand(&cobra.Command{
Use: "port [pods]",
Short: "Show port-forward information for a specific pods",
RunE: func(cmd *cobra.Command, args []string) error {
InitConfig()
if configBytes == nil {
// Print the list of context names
fmt.Println("No available contexts!")
} else {
// Get the map of context name to context config
config, err := clientcmd.NewDefaultClientConfigLoadingRules().Load()
if err != nil {
return err
}
restConfig, err := clientcmd.NewDefaultClientConfig(*config, &clientcmd.ConfigOverrides{}).ClientConfig()
if err != nil {
return err
}
if err != nil {
return err
}
if len(args) < 1 {
return fmt.Errorf("pod name not specified")
}
clientset, err := GetClientSet(kubeconfig)
if err != nil {
return err
}
ctx := context.Background()
namespaces, err := cmd.Flags().GetStringSlice("namespace")
if err != nil {
return err
}
for _, namespace := range namespaces {
for _, pod := range args {
// Get the pod information
po, err := clientset.CoreV1().Pods(namespace).Get(ctx, pod, metav1.GetOptions{})
if err != nil {
return err
}
// Get a random port to use for port forwarding
port, err := GetFreePort()
if err != nil {
return err
}
// Create the port forwarding request
req := clientset.CoreV1().RESTClient().Post().
Resource("pods").
Name(pod).
Namespace(po.Namespace).
SubResource("portforward")
transport, upgrader, err := spdy.RoundTripperFor(restConfig)
if err != nil {
return err
}
dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, "POST", req.URL())
// Start the port forwarding
stopChan := make(chan struct{})
defer close(stopChan)
go func() {
out := new(bytes.Buffer)
errOut := new(bytes.Buffer)
pf, err := portforward.New(dialer, []string{fmt.Sprintf("%d:%d", port, port)}, stopChan, make(chan struct{}), out, errOut)
if err != nil {
fmt.Printf("Error forwarding port: %v\n", err)
}
err = pf.ForwardPorts()
if err != nil {
fmt.Printf("Error forwarding port: %v\n", err)
}
fmt.Println(out.String())
}()
// Wait for the port forwarding to start
time.Sleep(time.Second)
// Print the port forwarding information
fmt.Printf("Port forwarding for pod %s:\n", pod)
fmt.Printf("Local port: \t%d\n", port)
fmt.Printf("Remote port: \t%d\n", port)
// Prompt the user to stop the port forwarding
var response string
prompt := &survey.Select{
Message: "Press Enter to stop port forwarding...",
Options: []string{"Yes"},
}
survey.AskOne(prompt, &response)
return nil
}
}
}
return nil
},
})
showCmd.AddCommand(&cobra.Command{
Use: "node [node]",
Short: "Describe a specific node",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("node name not specified")
}
clientset, err := GetClientSet(kubeconfig)
if err != nil {
return err
}
ctx := context.Background()
if err != nil {
return err
}
node := args[0]
n, err := clientset.CoreV1().Nodes().Get(ctx, node, metav1.GetOptions{})
if err != nil {
return err
}
DescribeNode(n)
return nil
},
})
switchContextCmd := &cobra.Command{
Use: "switch",
Short: "Switch to different context",
RunE: func(cmd *cobra.Command, args []string) error {
InitConfig()
// Get the map of context name to context config
config, err := clientcmd.Load(configBytes)
if err != nil {
return err
}
var contextNames []string
for contextName := range config.Contexts {
contextNames = append(contextNames, contextName)
}
SelectedConfig(contextNames, config)
return nil
},
}
getCmd.PersistentFlags().StringSliceP("namespace", "n", []string{}, "Namespaces to filter resources by (comma-separated)")
listContextsCmd.Flags().StringVarP(&loadFile, "file", "f", "", "Using spesific kubeconfig file")
// Add the namespace flag to the show command
showCmd.PersistentFlags().StringSliceP("namespace", "n", []string{}, "Namespace to use. Use once for each namespace (default: all namespaces)")
showCmd.Flags().StringVarP(&loadFile, "file", "f", "", "Using spesific kubeconfig file")
switchContextCmd.Flags().StringVarP(&loadFile, "file", "f", "", "Using spesific kubeconfig file")
rootCmd := &cobra.Command{Use: "k8c"}
rootCmd.PersistentFlags().StringVar(&kubeconfig, "kubeconfig", kubeconfig, "Path to kubeconfig file")
rootCmd.AddCommand(versionCmd, getCmd, listContextsCmd, loadCmd, mergeCmd, showCmd, switchContextCmd)
if err := rootCmd.Execute(); err != nil {
logrus.Fatalf("error executing command: %v", err)
}
return []*cobra.Command{versionCmd, getCmd, listContextsCmd, loadCmd, mergeCmd, showCmd, switchContextCmd}
}