-
Notifications
You must be signed in to change notification settings - Fork 0
/
pod.go
236 lines (200 loc) · 6.13 KB
/
pod.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
package cli
import (
"context"
"fmt"
"log"
"math"
"time"
ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
tb "github.com/nsf/termbox-go"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
metricsv1beta1 "k8s.io/metrics/pkg/apis/metrics/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
var (
containerName string
interval time.Duration
)
func podCmd() *cobra.Command {
return &cobra.Command{
Use: "pod <pod-name>",
Short: "Live pod metrics",
Args: cobra.MatchAll(cobra.ExactArgs(1)),
RunE: func(cmd *cobra.Command, args []string) error {
c, ns, err := newClient()
if err != nil {
return err
}
return runPodMetrics(ns, args[0], c)
},
}
}
func init() {
cmd := podCmd()
rootCmd.AddCommand(cmd)
cmd.PersistentFlags().StringVar(&containerName, "container", "", "A container name to show")
cmd.PersistentFlags().DurationVar(&interval, "interval", time.Second, "The interval in seconds to fetch metrics.")
}
func runPodMetrics(ns string, podName string, apiReader client.Reader) error {
if err := ui.Init(); err != nil {
log.Fatalf("failed to initialize termui: %v", err)
}
defer ui.Close()
headerHeight := 4
ctx := context.Background()
pod := &corev1.Pod{}
err := apiReader.Get(ctx, client.ObjectKey{Namespace: ns, Name: podName}, pod)
if err != nil {
return err
}
selectedContainers := selectContainers(pod.Spec.Containers)
if len(selectedContainers) == 0 {
return fmt.Errorf(`selected container %q not found in pod "%s/%s"`, containerName, ns, podName)
}
width, height := tb.Size()
cpuData, memData, title, cpuPlots, memPlots := buildGraphs(ns, podName, headerHeight, pod, selectedContainers, width, height)
draw := func() {
cpu, mem, _ := getPodMetrics(ctx, apiReader, ns, podName)
plots := []ui.Drawable{title}
nbrPrinter := numberPrinter()
for _, container := range selectedContainers {
n := container.Name
cpuData[n].data = append(cpuData[n].data[1:], cpu[n])
memData[n].data = append(memData[n].data[1:], mem[n])
cpuData[n].max = math.Max(cpuData[n].max, cpu[n])
memData[n].max = math.Max(memData[n].max, mem[n])
cpuPlots[n].Data[0] = cpuData[n].data
memPlots[n].Data[0] = memData[n].data
cpuPlots[n].Title = fmt.Sprintf(" %s CPU (Req: %s / Lim: %s / Curr: %s / Max: %s) ",
container.Name,
container.Resources.Requests.Cpu(),
container.Resources.Limits.Cpu(),
numberPrinter().Sprintf("%.0fm", cpu[n]*1000),
numberPrinter().Sprintf("%.0fm", cpuData[n].max*1000),
)
memPlots[n].Title = fmt.Sprintf(" %s Memory (Req: %s / Lim: %s / Curr: %s / Max: %s) ",
container.Name,
container.Resources.Requests.Memory(),
container.Resources.Limits.Memory(),
nbrPrinter.Sprintf("%.0fMi", mem[n]),
nbrPrinter.Sprintf("%.0fMi", memData[n].max),
)
plots = append(plots, cpuPlots[n], memPlots[n])
}
ui.Render(plots...)
}
draw()
uiEvents := ui.PollEvents()
ticker := time.NewTicker(interval).C
for {
select {
case e := <-uiEvents:
switch e.ID {
case "q", "<C-c>":
return nil
case "<Resize>":
payload := e.Payload.(ui.Resize)
cpuData, memData, title, cpuPlots, memPlots = buildGraphs(ns, podName, headerHeight, pod,
selectedContainers, payload.Width, payload.Height)
ui.Clear()
draw()
}
case <-ticker:
draw()
}
}
}
func buildGraphs(
ns string,
podName string,
headerHeight int,
pod *corev1.Pod,
selectedContainers []corev1.Container,
width int, height int,
) (
map[string]*plotData,
map[string]*plotData,
*widgets.Paragraph,
map[string]*widgets.Plot,
map[string]*widgets.Plot,
) {
height = height - headerHeight
if containerName == "" {
height = height / len(pod.Spec.Containers)
}
cpuData := initData(selectedContainers, width/2-5)
memData := initData(selectedContainers, width/2-5)
cpuPlots := make(map[string]*widgets.Plot)
memPlots := make(map[string]*widgets.Plot)
p := widgets.NewParagraph()
p.Title = " Namespace / Pod "
p.Text = fmt.Sprintf(" %s / %s\n Press q to quit", ns, podName)
p.SetRect(0, 0, width, headerHeight)
p.TextStyle.Fg = ui.ColorWhite
p.BorderStyle.Fg = ui.ColorYellow
for i, container := range selectedContainers {
lc := newPlot()
lc.Data[0] = cpuData[container.Name].data
lc.SetRect(0, i*height+headerHeight, width/2, (i+1)*height+headerHeight)
lc.LineColors[0] = ui.ColorGreen
cpuPlots[container.Name] = lc
lc2 := newPlot()
lc2.Data[0] = memData[container.Name].data
lc2.SetRect(width/2, i*height+headerHeight, width, (i+1)*height+headerHeight)
lc2.LineColors[0] = ui.ColorYellow
memPlots[container.Name] = lc2
}
return cpuData, memData, p, cpuPlots, memPlots
}
func newPlot() *widgets.Plot {
p := widgets.NewPlot()
p.Data = make([][]float64, 1)
p.AxesColor = ui.ColorWhite
p.ShowXAxisLabels = false
p.TitleStyle.Fg = ui.ColorCyan
// clone line colors as both instances have the same instance of the color array
p.LineColors = append([]ui.Color{}, p.LineColors...)
return p
}
func getPodMetrics(ctx context.Context, apiReader client.Reader, namespace string, podName string) (
map[string]float64, map[string]float64, error,
) {
metrics := &metricsv1beta1.PodMetrics{}
err := apiReader.Get(ctx, client.ObjectKey{Namespace: namespace, Name: podName}, metrics)
if err != nil {
return nil, nil, err
}
cpu := make(map[string]float64)
mem := make(map[string]float64)
for _, c := range metrics.Containers {
cpuRl := c.Usage[corev1.ResourceCPU]
cpu[c.Name] = float64(cpuRl.MilliValue()) / 1000
memRl := c.Usage[corev1.ResourceMemory]
mem[c.Name] = float64(memRl.Value() / (1024 * 1024))
}
return cpu, mem, nil
}
func initData(containers []corev1.Container, size int) map[string]*plotData {
data := make(map[string]*plotData)
for _, container := range containers {
if containerName == "" || container.Name == containerName {
data[container.Name] = &plotData{data: make([]float64, size)}
}
}
return data
}
func selectContainers(containers []corev1.Container) []corev1.Container {
var filtered []corev1.Container
for _, c := range containers {
if containerName == "" || c.Name == containerName {
filtered = append(filtered, c)
}
}
return filtered
}
type plotData struct {
data []float64
max float64
}