-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
132 lines (114 loc) · 2.89 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
package main
import (
"log"
"os"
"time"
termbox "github.com/nsf/termbox-go"
// Kubernetes
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
// Kubernetes metrics
metricsclientset "k8s.io/metrics/pkg/client/clientset_generated/clientset"
// GKE authentication
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
)
const (
watchSeconds = 5
)
var (
kubeMetrics KubeMetrics
)
func main() {
kubeConfig := ""
kubeContext := ""
// Determine kubeconfig path
if kubeConfig == "" {
if os.Getenv("KUBECONFIG") != "" {
kubeConfig = os.Getenv("KUBECONFIG")
} else {
kubeConfig = clientcmd.RecommendedHomeFile
}
}
// Create the kubernetes client configuration
clientConfig, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{
ExplicitPath: kubeConfig,
},
&clientcmd.ConfigOverrides{
CurrentContext: kubeContext,
},
).ClientConfig()
if err != nil {
log.Fatalf("unable to create k8s client config: %s", err)
}
kubeClient, err := kubernetes.NewForConfig(clientConfig)
if err != nil {
log.Fatalf("unable to create k8s client: %s\n", err)
}
log.Printf("connecting to kubernetes cluster metrics")
metricsClient, err := metricsclientset.NewForConfig(clientConfig)
if err != nil {
log.Fatalf("unable to create metrics client: %s", err)
}
kubeMetrics = KubeMetrics{
metricsClient: metricsClient,
kubeClient: kubeClient,
}
if err := kubeMetrics.FetchMetrics(); err != nil {
log.Fatalf("unable to get kubernetes metrics: %s", err)
}
if err := termbox.Init(); err != nil {
log.Fatalf("error init termbox: %s", err)
}
defer termbox.Close()
termWidth, termHeight = termbox.Size()
termbox.SetInputMode(termbox.InputEsc | termbox.InputAlt | termbox.InputMouse)
go func() {
updateScreen()
for range time.NewTicker(time.Second * watchSeconds).C {
kubeMetrics.FetchMetrics()
updateScreen()
}
}()
for {
switch ev := termbox.PollEvent(); ev.Type {
case termbox.EventMouse:
setMouseClick(ev.MouseX, ev.MouseY, ev.Key)
updateScreen()
case termbox.EventResize:
termWidth, termHeight = ev.Width, ev.Height
updateScreen()
case termbox.EventKey:
switch ev.Key {
case termbox.KeyEsc:
return
case termbox.KeySpace:
snapshot()
case termbox.KeyBackspace, termbox.KeyBackspace2:
if len(filterString) > 0 {
filterString = filterString[:len(filterString)-1]
}
case termbox.KeyArrowDown:
updateSelectedID(1)
case termbox.KeyArrowUp:
updateSelectedID(-1)
default:
switch ch := ev.Ch; ch {
case '1': // key 1
setOrderOption(OrderCPUDec)
case '2': // key 2
setOrderOption(OrderCPUAsc)
case '3': // key 3
setOrderOption(OrderMEMDec)
case '4': // key 4
setOrderOption(OrderMEMAsc)
default:
if (ch >= 'a' && ch <= 'z') || ch == '-' || ch == '_' {
filterString += string(ch)
}
}
}
updateScreen()
}
}
}