feat(metrics): show per-pod right-sizing (usage vs request and limit)

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 = `<used> 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) <noreply@anthropic.com>
This commit is contained in:
Erfan YousefiandClaude Opus 4.8 authored and Erfan committed 2026-06-25 01:22:31 +02:00
1 parent f1ab5b8cc4
commit 2c17eca7b5
4 files changed
+93 -32

No files matched your search

+3
View File
@@ -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.
+8 -4
View File
@@ -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
+51 -7
View File
@@ -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<u64>,
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<chrono::DateTime<chrono::Utc>>) -> 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]
+31 -21
View File
@@ -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