Merge pull request #489 from binarycoded/resource_limits

Show resource limit % next to request % in Pod view (fix #413)
mine
Fernand Galiana 2020-01-22 11:51:55 -07:00 committed by GitHub
commit 995158d446
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 8 deletions

View File

@ -31,11 +31,11 @@ func asSelector(s *metav1.LabelSelector) string {
}
type metric struct {
cpu, mem string
cpu, mem, cpuLim, memLim string
}
func noMetric() metric {
return metric{cpu: NAValue, mem: NAValue}
return metric{cpu: NAValue, mem: NAValue, cpuLim: NAValue, memLim: NAValue}
}
// ToSelector flattens a map selector to a string selector.

View File

@ -78,8 +78,8 @@ func (Pod) Header(ns string) HeaderRow {
Header{Name: "RESTART", Align: tview.AlignRight},
Header{Name: "CPU", Align: tview.AlignRight},
Header{Name: "MEM", Align: tview.AlignRight},
Header{Name: "%CPU", Align: tview.AlignRight},
Header{Name: "%MEM", Align: tview.AlignRight},
Header{Name: "%CPU (LIM)", Align: tview.AlignRight},
Header{Name: "%MEM (LIM)", Align: tview.AlignRight},
Header{Name: "IP"},
Header{Name: "NODE"},
Header{Name: "QOS"},
@ -116,8 +116,8 @@ func (p Pod) Render(o interface{}, ns string, r *Row) error {
strconv.Itoa(rc),
c.cpu,
c.mem,
perc.cpu,
perc.mem,
perc.cpu+" ("+fmt.Sprintf("%3v",perc.cpuLim)+")",
perc.mem+" ("+fmt.Sprintf("%3v",perc.memLim)+")",
na(po.Status.PodIP),
na(po.Spec.NodeName),
p.mapQOS(po.Status.QOSClass),
@ -159,9 +159,12 @@ func (*Pod) gatherPodMX(pod *v1.Pod, mx *mv1beta1.PodMetrics) (c, p metric) {
}
rc, rm := requestedRes(pod)
lc, lm := resourceLimits(pod)
p = metric{
cpu: AsPerc(toPerc(float64(cpu.MilliValue()), float64(rc.MilliValue()))),
mem: AsPerc(toPerc(ToMB(mem.Value()), ToMB(rm.Value()))),
cpuLim: AsPerc(toPerc(float64(cpu.MilliValue()), float64(lc.MilliValue()))),
memLim: AsPerc(toPerc(ToMB(mem.Value()), ToMB(lm.Value()))),
}
return
@ -180,6 +183,21 @@ func containerResources(co v1.Container) (cpu, mem *resource.Quantity) {
return
}
func resourceLimits(po *v1.Pod) (cpu, mem resource.Quantity) {
for _, co := range po.Spec.Containers {
limit := co.Resources.Limits
if len(limit) != 0 {
if limit.Cpu() != nil {
cpu.Add(*limit.Cpu())
}
if limit.Memory() != nil {
mem.Add(*limit.Memory())
}
}
}
return
}
func requestedRes(po *v1.Pod) (cpu, mem resource.Quantity) {
for _, co := range po.Spec.Containers {
c, m := containerResources(co)

View File

@ -70,7 +70,7 @@ func TestPodRender(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, "default/nginx", r.ID)
e := render.Fields{"default", "nginx", "1/1", "Running", "0", "10", "10", "10", "14", "172.17.0.6", "minikube", "BE"}
e := render.Fields{"default", "nginx", "1/1", "Running", "0", "10", "10", "10 ( 0)", "14 ( 5)", "172.17.0.6", "minikube", "BE"}
assert.Equal(t, e, r.Fields[:12])
}
@ -101,7 +101,7 @@ func TestPodInitRender(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, "default/nginx", r.ID)
e := render.Fields{"default", "nginx", "1/1", "Init:0/1", "0", "10", "10", "10", "14", "172.17.0.6", "minikube", "BE"}
e := render.Fields{"default", "nginx", "1/1", "Init:0/1", "0", "10", "10", "10 ( 0)", "14 ( 5)", "172.17.0.6", "minikube", "BE"}
assert.Equal(t, e, r.Fields[:12])
}