From 2c17eca7b5470682c0e33c66bedc6920b8a0f50a Mon Sep 17 00:00:00 2001 From: Erfan Yousefi Date: Thu, 25 Jun 2026 01:17:23 +0200 Subject: [PATCH] feat(metrics): show per-pod right-sizing (usage vs request and limit) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Pods CPU/MEM columns now show actual usage AND how it compares to the pod's request and limit, so you can spot mis-sized pods at a glance: CPU/MEM cell = ` R<%req> L<%limit>` e.g. 1.50c R150 L75 R% = used / request (low → over-provisioned, >=100 → under-requested) L% = used / limit (risk: throttle/OOM as it approaches 100%) Cells color by the worse signal: yellow at >=100% of request, red at >=90% of limit. Missing request/limit render as R-/L-; no metrics renders as `-`. The table title carries a one-line legend. Uses the request/limit already extracted into the lean entity (1.1) plus the live pod metrics (4.2) — no new fetch. Validation: fmt, check, test (87 passed / 4 ignored, +right-sizing render test), release build all green. Co-Authored-By: Claude Opus 4.8 (1M context) --- ROADMAP.md | 3 ++ docs/operator-guide.md | 12 +++++--- src/ui/app.rs | 58 ++++++++++++++++++++++++++++++++++----- src/ui/app/render_loop.rs | 52 +++++++++++++++++++++-------------- 4 files changed, 93 insertions(+), 32 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index b64114a..93dd95d 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -171,6 +171,9 @@ Root problem: full object JSON kept for every entity. "metrics-server n/a" when absent). **Verified on a real cluster: 45 nodes / 821 pods reporting real usage; graceful 404 fallback on kwok.** Unit tests: real unit formats, pod columns render + unavailable note. + - **Right-sizing:** Pods CPU/MEM cells show `‹used› R‹%req› L‹%limit›` (e.g. `1.50c R150 L75`) — + actual usage plus utilization vs request and limit; cell yellow ≥100% request, red ≥90% limit. + `pod_usage_cell` helper; unit-tested. - [ ] **4.3 Resource-correlated events** — selecting a pod shows its events; today Events pane only renders Event objects directly (detail_pane.rs:170 "planned"). - [ ] **4.4 Rendered describe** — kubectl-style human `describe` output alongside YAML/JSON. diff --git a/docs/operator-guide.md b/docs/operator-guide.md index 9e425a6..9836ec5 100644 --- a/docs/operator-guide.md +++ b/docs/operator-guide.md @@ -64,11 +64,15 @@ All of the above are remappable via `keymap.toml`; the on-screen help line refle ## Metrics -If the cluster has a metrics-server (`metrics.k8s.io`), krust shows live usage: -- the **Pods table** gains **CPU** and **MEM** columns (actual usage per pod, summed across containers) -- the **Cluster Pulse** panel shows a `[USE]` row: cluster cpu/mem used vs allocatable + util% +If the cluster has a metrics-server (`metrics.k8s.io`), krust shows live usage and right-sizing: +- the **Pods table** **CPU** and **MEM** columns show `‹used› R‹%req› L‹%limit›` per pod — e.g. + `1.50c R150 L75` means using 1.5 cores, 150% of its request, 75% of its limit. `R%` is the + right-sizing signal (low = over-provisioned, ≥100 = under-requested); `L%` is the risk signal. + Cells turn yellow at ≥100% of request and red at ≥90% of limit (throttle/OOM risk). +- the **Cluster Pulse** panel shows a `[USE]` row: cluster cpu/mem used vs allocatable + util%. -Without a metrics-server these degrade gracefully (columns show `-`, the table title notes `metrics-server n/a`, and the pulse falls back to request/limit-based numbers). +Without a metrics-server these degrade gracefully (columns show `-`, the table title notes +`metrics-server n/a`, and the pulse falls back to request/limit-based numbers). ## Logs and Runtime Inspect diff --git a/src/ui/app.rs b/src/ui/app.rs index eeaf2bf..fa9c996 100644 --- a/src/ui/app.rs +++ b/src/ui/app.rs @@ -2112,6 +2112,34 @@ fn percent(numerator: u64, denominator: u64) -> String { format!("{pct:.0}%") } +/// Format a pod's per-resource usage cell as `‹used› R‹%req› L‹%limit›` and pick a severity: +/// red when usage is ≥90% of the limit (throttle/OOM risk), yellow when ≥100% of the request +/// (under-requested). A low `R%` shows over-provisioning. Missing pieces render as `R-`/`L-`, +/// and no usage at all renders as `-`. +fn pod_usage_cell( + used: Option, + request: u64, + limit: u64, + render: fn(u64) -> String, +) -> (String, Severity) { + let Some(used) = used else { + return ("-".to_string(), Severity::Ok); + }; + let pct = |denom: u64| (denom > 0).then(|| used.saturating_mul(100) / denom); + let req_pct = pct(request); + let lim_pct = pct(limit); + let req_s = req_pct.map_or_else(|| "R-".to_string(), |v| format!("R{v}")); + let lim_s = lim_pct.map_or_else(|| "L-".to_string(), |v| format!("L{v}")); + let severity = if lim_pct.is_some_and(|v| v >= 90) { + Severity::Err + } else if req_pct.is_some_and(|v| v >= 100) { + Severity::Warn + } else { + Severity::Ok + }; + (format!("{} {req_s} {lim_s}", render(used)), severity) +} + /// Compact age (e.g. "3d", "5h", "12m") for dynamic-resource rows. fn short_age(when: Option>) -> String { let Some(when) = when else { @@ -2903,21 +2931,37 @@ mod tests { } #[test] - fn pods_table_renders_live_cpu_mem_columns() { + fn pods_table_shows_usage_and_right_sizing() { let mut app = test_app(); - app.store.apply(StateDelta::Upsert(mk_entity( + let mut entity = mk_entity( "ctx-dev", ResourceKind::Pods, Some("default"), "pod-a", "Running", - ))); + ); + entity.extracted.pod_resources = Some(crate::model::PodResources { + cpu_request_m: 1000, + cpu_limit_m: 2000, + mem_request_b: 128 * 1024 * 1024, + mem_limit_b: 512 * 1024 * 1024, + }); + app.store.apply(StateDelta::Upsert(entity)); app.seed_pod_metrics_for_test("default", "pod-a", 1500, 256 * 1024 * 1024); - let snap = render_snapshot(&mut app, 140, 20); - assert!(snap.contains("CPU") && snap.contains("MEM"), "{snap}"); - assert!(snap.contains("1.50c"), "expected cpu usage column: {snap}"); - assert!(snap.contains("256Mi"), "expected mem usage column: {snap}"); + let snap = render_snapshot(&mut app, 160, 20); + // actual usage + assert!(snap.contains("1.50c"), "cpu used: {snap}"); + assert!(snap.contains("256Mi"), "mem used: {snap}"); + // right-sizing vs request and limit + assert!( + snap.contains("R150") && snap.contains("L75"), + "cpu R/L: {snap}" + ); // 1500/1000, 1500/2000 + assert!( + snap.contains("R200") && snap.contains("L50"), + "mem R/L: {snap}" + ); // 256/128, 256/512 } #[test] diff --git a/src/ui/app/render_loop.rs b/src/ui/app/render_loop.rs index 909fa7d..397f8ac 100644 --- a/src/ui/app/render_loop.rs +++ b/src/ui/app/render_loop.rs @@ -654,17 +654,27 @@ impl App { let sev = classify_status_severity(&row.status); let status = format!("{} {}", severity_tag(sev), row.status); let cells = if pods_view { - let (cpu, mem) = - match self.pod_usage(key.namespace.as_deref(), &key.name) { - Some((c, m)) => (format_millicpu(c), format_bytes(m)), - None => ("-".to_string(), "-".to_string()), - }; + let usage = self.pod_usage(key.namespace.as_deref(), &key.name); + let res = + self.store.get(key).and_then(|e| e.extracted.pod_resources); + let (cpu_str, cpu_sev) = pod_usage_cell( + usage.map(|(c, _)| c), + res.map(|r| r.cpu_request_m).unwrap_or(0), + res.map(|r| r.cpu_limit_m).unwrap_or(0), + format_millicpu, + ); + let (mem_str, mem_sev) = pod_usage_cell( + usage.map(|(_, m)| m), + res.map(|r| r.mem_request_b).unwrap_or(0), + res.map(|r| r.mem_limit_b).unwrap_or(0), + format_bytes, + ); vec![ Cell::from(row.namespace), Cell::from(row.name), Cell::from(status), - Cell::from(cpu), - Cell::from(mem), + Cell::from(cpu_str).style(severity_style(&theme, cpu_sev)), + Cell::from(mem_str).style(severity_style(&theme, mem_sev)), Cell::from(row.age), ] } else { @@ -684,12 +694,12 @@ impl App { ( vec!["Namespace", "Name", "Status", "CPU", "MEM", "Age"], vec![ + Constraint::Length(16), + Constraint::Min(18), Constraint::Length(18), - Constraint::Min(20), - Constraint::Length(20), - Constraint::Length(9), - Constraint::Length(10), - Constraint::Length(8), + Constraint::Length(17), + Constraint::Length(17), + Constraint::Length(6), ], ) } else { @@ -704,19 +714,19 @@ impl App { ], ) }; - let metrics_note = if pods_view && self.pod_metrics.is_empty() { - " (CPU/MEM: metrics-server n/a)" + let title = if pods_view { + let legend = if self.pod_metrics.is_empty() { + "CPU/MEM: metrics-server n/a" + } else { + "CPU/MEM = used R%req L%limit" + }; + format!("[KIND] {} ({}) · {legend}", active.kind(), vm.len()) } else { - "" + format!("[KIND] {} ({})", active.kind(), vm.len()) }; let table = Table::new(rows, widths) .header(Row::new(header).style(theme.table_header)) - .block(Block::default().borders(Borders::ALL).title(format!( - "[KIND] {} ({}){}", - active.kind(), - vm.len(), - metrics_note - ))) + .block(Block::default().borders(Borders::ALL).title(title)) .row_highlight_style(theme.row_highlight); // Rows are pre-sliced to the window, so the widget offset is 0 and the