Skip to content

Add cost views and workload rightsizing#1101

Open
nadaverell wants to merge 32 commits into
mainfrom
add-workload-cost-tab
Open

Add cost views and workload rightsizing#1101
nadaverell wants to merge 32 commits into
mainfrom
add-workload-cost-tab

Conversation

@nadaverell

@nadaverell nadaverell commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds OpenCost-backed cost visibility for clusters, applications, and workloads, plus an on-demand Rightsizing view for finding CPU and memory requests worth reviewing.

  • Workload Cost tabs for Deployments, StatefulSets, and DaemonSets.
  • Application Cost tabs with contributor drill-through to workload cost.
  • Shared historical allocation charts with consistent ranges, ticks, and hover details.
  • A Rightsizing scan across the currently visible namespaces, plus per-workload guidance.
  • Provider-aware cloud-console links when provider identity is reliable.

Rightsizing methodology

Rightsizing is advisory evidence, not an efficiency grade, automatic YAML change, capacity plan, or guaranteed savings estimate.

  • Uses seven days of 5-minute Prometheus samples.
  • CPU demand target: P95 usage plus 15% headroom.
  • Memory demand target: maximum observed working set plus 15% headroom.
  • Rounds to practical Kubernetes values with wider steps at larger scales.
  • Keeps CPU at or above 10m and memory at or above 64Mi.
  • Separates the demand-based target from a conservative next step.
  • Stages large reductions: memory and CPU requests of at least one core are reduced by at most 50% per review window; smaller CPU requests have proportional floors.
  • Applies the conservative reduction limit to bursty or throttled CPU workloads.
  • Keeps CPU and memory independent and surfaces HPA, OOM, throttling, scaled-to-zero, ownership, and history-quality signals.
  • Withholds unsafe conclusions when evidence or ownership is insufficient.

The fleet scan runs only when the user starts it. Queries are concurrency-bounded and use exact kube-state-metrics ownership; workload detail can fall back to exact current pods when historical ownership is unavailable.

Cost behavior

  • RBAC-preflighted workload and application OpenCost APIs.
  • Exact controller attribution: Pod → ReplicaSet → Deployment, or direct Pod ownership for StatefulSets and DaemonSets.
  • Application totals, stacked trends, contributor rows, partial-coverage messaging, and scaled-to-zero handling.
  • Monthly-first projections with hourly rates explaining the current composition.
  • Explicit unavailable states for missing Prometheus/OpenCost data and query failures.

Scope

Batch/job cost, storage/PVC attribution, network attribution, VPA integration, and automatic request changes remain out of scope. Capacity reductions are requested resources across replicas, not guaranteed reductions in the cloud bill.

Validation

  • make tsc
  • make test
  • make build
  • git diff --check
  • Focused Go and Vitest coverage for Rightsizing calculations, safety gates, scan presentation, and workload/application cost states.
  • Live GKE validation against real Prometheus and OpenCost data across 136 workloads.
  • Visual review at desktop and narrow widths, including loading, results, expanded guidance, chart hover behavior, and console cleanliness.
  • Screenshots: /Users/nadaverell/workspace/sky/ws10/radar/.playwright-mcp/visual-test/20260713-rightsizing-conservative/

Note

Medium Risk
Large new surface area (Prometheus-heavy rightsizing math, batched scans, OpenCost RBAC APIs) with behavior changes on discovery failures; mistakes could mis-rank recommendations or mis-report cost, but changes are mostly additive behind new routes and optional UI tabs.

Overview
Introduces OpenCost-backed cost at cluster-adjacent APIs and in the UI: optional Cost tabs on applications and workloads (Deployments/StatefulSets/DaemonSets), plus server routes for application and per-workload current cost and trends with RBAC preflight, partial coverage, and scaled-to-zero handling.

Rightsizing is rebuilt around a 7-day Prometheus window: rightsizing classification (balanced/oversized/under-requested, etc.), confidence and sample coverage, HPA/OOM/throttle signals, and conservative staged reduction rules. A new fleet scan (POST /prometheus/rightsizing/scan) batches workloads (50 at a time) with kube-state-metrics ownership vectors and namespace-scoped RBAC.

Prometheus client changes improve reliability: serialized discovery, a short TTL cache for repeated discovery failures, ErrPrometheusNotFound for empty discovery, and preserving cached connections when probes fail due to context cancellation. OpenCost handlers map connection errors to ReasonNoPrometheus vs ReasonQueryError, and node cost responses can include ProviderID from the node informer.

pkg/opencost adds application cost/trend aggregation and hardens Prom queries (deduped node/PV series, shared cost expr helpers). UI adds SelectMenu and workload/application hooks for host-rendered cost bodies.

Reviewed by Cursor Bugbot for commit ae29b5c. Bugbot is set up for automated code reviews on this repo. Configure here.

@nadaverell nadaverell requested a review from hisco as a code owner July 5, 2026 14:16
Comment thread web/src/components/cost/WorkloadCostTab.tsx
Comment thread web/src/components/cost/WorkloadCostTab.tsx Outdated
Comment thread internal/server/opencost_workload.go Fixed
Comment thread internal/server/opencost_workload.go Fixed
Comment thread internal/server/opencost_workload.go Fixed
Comment thread internal/server/opencost_workload.go Fixed
Comment thread internal/server/opencost_workload.go Fixed
Comment thread internal/server/opencost_workload.go Fixed
Comment thread pkg/opencost/workload_trend.go Fixed
Comment thread pkg/opencost/workload_trend.go Fixed
Comment thread pkg/opencost/workload_trend.go Fixed
Comment thread pkg/opencost/workload_trend.go Fixed
Comment thread web/src/components/cost/WorkloadCostTab.tsx
Comment thread web/src/api/client.ts
@nadaverell nadaverell force-pushed the add-workload-cost-tab branch from 9fe6e3c to 9cf2aa5 Compare July 9, 2026 23:43
@nadaverell nadaverell changed the title Add workload cost tab Add workload and application cost tabs Jul 10, 2026
Comment thread internal/server/opencost_application.go Fixed
Comment thread internal/server/opencost_application.go Fixed
Comment thread pkg/opencost/application_trend.go Fixed
Comment thread pkg/opencost/application_trend.go Fixed
Comment thread internal/server/opencost_application.go Outdated
Comment thread web/src/api/client.ts
Comment thread web/src/components/cost/ApplicationCostTab.tsx
Comment thread web/src/api/client.ts Outdated
Comment thread internal/prometheus/client.go
Comment thread pkg/opencost/workload_trend.go
@nadaverell nadaverell force-pushed the add-workload-cost-tab branch from 5654415 to 7f7f6fc Compare July 12, 2026 09:32
@nadaverell nadaverell changed the title Add workload and application cost tabs Add cost views and workload request fit Jul 12, 2026
Comment thread pkg/opencost/workloads.go Outdated
Comment thread internal/server/opencost_workload.go
Comment thread internal/prometheus/rightsizing.go
Comment thread internal/opencost/handlers.go
Comment thread pkg/opencost/workloads.go
Comment thread pkg/opencost/application_cost.go Outdated
Comment thread internal/prometheus/rightsizing.go Outdated
# Conflicts:
#	packages/k8s-ui/src/components/applications/ApplicationDetail.test.tsx
#	packages/k8s-ui/src/components/applications/ApplicationDetail.tsx
#	packages/k8s-ui/src/components/workload/WorkloadView.tsx
#	web/src/components/applications/ApplicationsView.tsx
@nadaverell nadaverell changed the title Add cost views and workload request fit Add cost views and workload rightsizing Jul 12, 2026
Comment thread packages/k8s-ui/src/components/applications/ApplicationDetail.tsx

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit ae29b5c. Configure here.

unique = append(unique, prom.EscapeRegexMeta(value))
prev = value
}
return strings.Join(unique, "|")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsafe workload names in trends

Medium Severity

Application cost trend queries build owner_name=~ alternations with EscapeRegexMeta only, unlike rightsizing scan helpers that also run SanitizeLabelValue. Workload names containing PromQL-special characters can break or skew trend queries.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit ae29b5c. Configure here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants