-
Notifications
You must be signed in to change notification settings - Fork 0
/
PodList.go
79 lines (64 loc) · 1.68 KB
/
PodList.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
package main
import (
"flag"
"fmt"
"sort"
"path/filepath"
termbox "github.com/nsf/termbox-go"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
)
var kubeconfig *string
func init() {
if home := homedir.HomeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
}
func runPodList() {
// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err.Error())
}
// create the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
pods, err := clientset.CoreV1().Pods("").List( /*context.TODO(),*/ metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
namespace := "default"
pods, _ = clientset.CoreV1().Pods(namespace).List(metav1.ListOptions{})
items := pods.DeepCopy().Items
sort.Sort(byAge(items))
y := 0
for _, pod := range items {
tbprint(0, y, coldef, coldef, pod.Name)
w, _ := termbox.Size()
status := string(pod.Status.Phase)
if pod.Status.Reason != "" {
status = pod.Status.Reason
}
desc := getDescription(pod)
if desc != "" {
status += fmt.Sprintf(" (%s)", desc)
}
x := w - 10 - len(status)
tbprint(x, y, coldef, coldef, status)
tm := pod.Status.StartTime
if tm != nil {
ago := timeSince(tm.Time)
x = w - len(ago)
tbprint(x, y, coldef, coldef, ago)
}
y++
}
}