-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubtop.go
189 lines (170 loc) 路 4.77 KB
/
kubtop.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
"path/filepath"
"sync"
"time"
"github.com/hako/durafmt"
"github.com/henkman/kubtop/k8s"
"github.com/jedib0t/go-pretty/table"
tui "github.com/marcusolsson/tui-go"
)
type Stage struct {
Name string `json:"name"`
ConfigFile string `json:"configFile"`
}
func main() {
root := tui.NewHBox()
var config struct {
AllNamespaces bool `json:"allNamespaces"`
RefreshSeconds int `json:"refreshSeconds`
StageIndex int `json:"stageIndex`
Stages []Stage `json:"stages`
}
{
exe, err := os.Executable()
if err != nil {
panic(err)
}
dir := filepath.Dir(exe)
fd, err := os.Open(filepath.Join(dir, "kubtop.json"))
if err != nil {
panic(err)
}
if err := json.NewDecoder(fd).Decode(&config); err != nil {
fd.Close()
panic(err)
}
fd.Close()
}
labels := []*tui.Label{}
boxes := []*tui.Box{}
refreshText := tui.NewLabel(fmt.Sprint(config.RefreshSeconds) + "s")
stageText := tui.NewLabel(config.Stages[config.StageIndex].Name)
allText := tui.NewLabel(map[bool]string{true: "all", false: "default"}[config.AllNamespaces])
infoBox := tui.NewHBox(refreshText, stageText, allText)
infoBox.SetBorder(true)
v := tui.NewVBox()
base := tui.NewVBox(infoBox, v)
scrollbox := tui.NewScrollArea(base)
root.Append(scrollbox)
ui, err := tui.New(root)
if err != nil {
panic(err)
}
var refreshLock sync.Mutex
var buf bytes.Buffer
refresh := func() {
refreshLock.Lock()
defer refreshLock.Unlock()
nodes, err := k8s.GetNodeOverview(config.Stages[config.StageIndex].ConfigFile, &buf, config.AllNamespaces)
buf.Reset()
if err != nil {
for _, label := range labels {
label.SetText("")
}
return
}
if len(nodes) > len(boxes) {
d := len(nodes) - len(boxes)
for i := 0; i < d; i++ {
label := tui.NewLabel("")
box := tui.NewVBox(label)
box.SetBorder(true)
labels = append(labels, label)
boxes = append(boxes, box)
v.Append(box)
}
} else if len(nodes) < len(boxes) {
d := len(boxes) - len(nodes)
for i := 0; i < d; i++ {
v.Remove(v.Length() - 1)
}
boxes = boxes[:len(nodes)]
labels = labels[:len(nodes)]
}
for i, node := range nodes {
boxes[i].SetTitle(node.Name + fmt.Sprintf(" [CPU = %dm(%d%%) MEM = %dMi(%d%%)]",
node.MilliCPU, node.CPUPercent, node.MemoryMi, node.MemoryPercent))
t := table.NewWriter()
t.AppendHeader(table.Row{"Name", "Phase", "Since", "CPU", "Mem",
"Limit", "Image"})
for _, pod := range node.Pods {
since := durafmt.ParseShort(time.Since(pod.PhaseStart)).LimitFirstN(2)
t.AppendRow(table.Row{
pod.Name, pod.Phase, since, fmt.Sprint(pod.MilliCPU, "m"),
fmt.Sprint(pod.MemoryMi, "Mi"), fmt.Sprint(pod.MemoryLimitMi, "Mi"), pod.Image,
})
}
t.Style().Options.DrawBorder = false
t.Style().Options.SeparateHeader = false
t.Style().Options.SeparateColumns = false
labels[i].SetText(t.Render())
}
allText.SetText(map[bool]string{true: "all", false: "default"}[config.AllNamespaces])
stageText.SetText(config.Stages[config.StageIndex].Name)
ui.Repaint()
}
ui.SetKeybinding("Esc", func() { ui.Quit() })
ui.SetKeybinding("c", func() { ui.Quit() })
ui.SetKeybinding("q", func() { ui.Quit() })
ui.SetKeybinding("a", func() {
config.AllNamespaces = !config.AllNamespaces
allText.SetText("changing to " + map[bool]string{true: "all", false: "default"}[config.AllNamespaces] + " ...")
ui.Repaint()
refresh()
})
ui.SetKeybinding("Up", func() { scrollbox.Scroll(0, -1) })
ui.SetKeybinding("Down", func() { scrollbox.Scroll(0, 1) })
ui.SetKeybinding("Left", func() { scrollbox.Scroll(-1, 0) })
ui.SetKeybinding("Right", func() { scrollbox.Scroll(1, 0) })
ui.SetKeybinding("t", func() { scrollbox.ScrollToTop() })
ui.SetKeybinding("b", func() { scrollbox.ScrollToBottom() })
ui.SetKeybinding("+", func() {
config.RefreshSeconds++
refreshText.SetText(fmt.Sprint(config.RefreshSeconds) + "s")
ui.Repaint()
})
ui.SetKeybinding("-", func() {
if config.RefreshSeconds > 1 {
config.RefreshSeconds--
refreshText.SetText(fmt.Sprint(config.RefreshSeconds) + "s")
ui.Repaint()
}
})
ui.SetKeybinding("n", func() {
config.StageIndex = (config.StageIndex + 1) % len(config.Stages)
stageText.SetText("changing to " +
config.Stages[config.StageIndex].Name + "...")
ui.Repaint()
refresh()
})
ui.SetKeybinding("p", func() {
config.StageIndex--
if config.StageIndex < 0 {
config.StageIndex = len(config.Stages) - 1
}
stageText.SetText("changing to " +
config.Stages[config.StageIndex].Name + "...")
ui.Repaint()
refresh()
})
refresh()
go func() {
o := 0
t := time.NewTicker(time.Second)
for range t.C {
o++
if o >= config.RefreshSeconds {
refresh()
o = 0
}
}
}()
if err := ui.Run(); err != nil {
panic(err)
}
}