Skip to content

feat(init): close the OpenCost cost loop — scrape OpenCost + bundle kube-state-metrics#23

Open
mayur-tolexo wants to merge 1 commit into
mainfrom
feat/opencost-scrape-loop
Open

feat(init): close the OpenCost cost loop — scrape OpenCost + bundle kube-state-metrics#23
mayur-tolexo wants to merge 1 commit into
mainfrom
feat/opencost-scrape-loop

Conversation

@mayur-tolexo

Copy link
Copy Markdown
Owner

Why (follow-up to #22)

After #22, init --with-opencost deploys OpenCost + a bundled Prometheus and the proxy makes both reachable — but on a real cluster OpenCost still reported "no cost data returned" and kubetidy fell back to derived pricing.

Root cause: OpenCost computes allocation cost by reading metrics back from Prometheus — its own exported pricing metrics (node_total_hourly_cost, …) plus kube-state-metrics (node/pod capacity). The bundled Prometheus scraped only cAdvisor, so OpenCost had the usage half but neither the pricing nor the capacity half → zero cost.

What this does

Closes the loop so the bundled stack actually produces Tier-2 cost:

  • Prometheus now scrapes three jobs: kubelet/cAdvisor (as before), OpenCost's /metrics (opencost.opencost.svc:9003), and kube-state-metrics (kubetidy-kube-state-metrics.monitoring.svc:8080). The OpenCost target is simply down until --with-opencost deploys it — harmless.
  • Bundles a minimal, read-only kube-state-metrics (registry.k8s.io/kube-state-metrics/kube-state-metrics:v2.13.0) alongside the Prometheus stack. All objects are prefixed kubetidy- so they never collide with a user's own KSM, and uninstall --with-prometheus removes them while still preserving the shared monitoring namespace.

Tests

  • internal/installer/opencost_test.go: object count 7 → 12 (Prometheus 7 + KSM 5); new assertion that the manifest contains the opencost + kube-state-metrics scrape jobs and the bundled KSM image.

Full gate green: build, vet, gofmt, go test ./..., golangci-lint (0 issues).

Caveats (honest)

  • OpenCost still needs ~10–15 min after install to warm up before the first allocation numbers appear.
  • On an unsupported/bare-metal provider (e.g. NeevCloud), OpenCost uses its built-in default node rates; cost is an estimate from those defaults, not cloud-billing-accurate. Recommendations are unaffected — only the dollar figure's precision.

🤖 Generated with Claude Code

…ube-state-metrics

OpenCost computes allocation cost by reading metrics BACK from Prometheus: its
own exported pricing metrics (node_total_hourly_cost, ...) plus
kube-state-metrics. The bundled Prometheus scraped only cAdvisor, so OpenCost
had usage but no pricing/capacity metrics and returned zero cost — kubetidy
then fell back to derived pricing ("no cost data returned"). Found on a real
cluster.

- Bundled Prometheus now scrapes OpenCost's /metrics and kube-state-metrics in
  addition to kubelet/cAdvisor.
- Bundle a minimal, read-only kube-state-metrics (prefixed kubetidy- so it
  never collides with a user's own) alongside the Prometheus stack. Removed by
  uninstall --with-prometheus, which still preserves the monitoring namespace.
- Docs: README Tier-2 section, CHANGELOG. Allow ~10–15 min post-install to warm up.
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.

1 participant