Skip to content

feat(init): bundle Prometheus + fix OpenCost RBAC (follow-up to #21)#22

Merged
mayur-tolexo merged 4 commits into
mainfrom
feat/init-bundle-prometheus
Jun 3, 2026
Merged

feat(init): bundle Prometheus + fix OpenCost RBAC (follow-up to #21)#22
mayur-tolexo merged 4 commits into
mainfrom
feat/init-bundle-prometheus

Conversation

@mayur-tolexo

Copy link
Copy Markdown
Owner

Why a follow-up

PR #21 was merged right after its first commit, before two further commits landed on the branch. Both were found while testing #21 on a real cluster (running SigNoz, no Prometheus). This PR brings them into main.

1. Fix: OpenCost RBAC missing policy/poddisruptionbudgets

OpenCost watches PodDisruptionBudgets; without the rule its reflector spammed poddisruptionbudgets.policy is forbidden. Adds the policy group to the embedded ClusterRole.

2. Feature: bundle Prometheus so init --with-opencost is self-contained

OpenCost has a hard Prometheus dependency — on a cluster without one it crash-loops (FTL Failed to create Prometheus data source ... no such host), no matter the endpoint. That's exactly what happened in testing. So init now deploys the whole Tier-2 stack:

  • Embedded minimal Prometheus (internal/installer/assets/prometheus.yaml) — monitoring namespace, scrapes kubelet/cAdvisor, at prometheus-server.monitoring.svc:80. That address is both OpenCost's default endpoint and a kubetidy auto-detect candidate, so scan Tier-1 and OpenCost Tier-2 both find it with zero config.
  • init --with-opencost now also deploys the bundled Prometheus unless --prometheus-url points at an existing one (then the bundle is skipped).
  • init --with-prometheus deploys just Prometheus (unlocks Tier-1 history) on its own.
  • uninstall --with-prometheus removes it — but never the shared monitoring namespace (deleteManifestSkipNamespaces), so a user's other tooling is safe.
  • --print includes whatever --with-* flags select.
kubectl tidy init --with-opencost                          # OpenCost + bundled Prometheus
kubectl tidy init --with-opencost --prometheus-url <url>   # use your own Prometheus
kubectl tidy init --with-prometheus                        # just Prometheus (Tier 1)

The bundled Prometheus is deliberately minimal (single replica, ephemeral storage, 15-day window) — great for getting cost working immediately; run your own and pass --prometheus-url for durable metrics.

Tests

  • internal/installer/opencost_test.go: Prometheus manifest decode (7 kinds) + correct default endpoint; Uninstall --with-prometheus logs removal and keeps the namespace; deleteManifestSkipNamespaces preserves a seeded monitoring namespace.
  • internal/cli/init_test.go: includePrometheus() truth table; --print bundles Prometheus with --with-opencost/--with-prometheus, omits the bundle when --prometheus-url is given.
  • Flag presence for both new flags.

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

🤖 Generated with Claude Code

OpenCost watches PodDisruptionBudgets; without the rule its reflector spams
"poddisruptionbudgets.policy is forbidden". Caught on a real cluster.
…ined

OpenCost has a hard Prometheus dependency; on a cluster without Prometheus it
crash-loops (FTL) no matter the endpoint. Make init deploy the whole Tier-2
stack so cost works in one command, no prerequisites — caught on a real cluster
running SigNoz (no Prometheus).

- Embed a minimal Prometheus manifest (monitoring namespace, scrapes
  kubelet/cAdvisor) at prometheus-server.monitoring.svc:80 — which is both
  OpenCost's default endpoint AND a kubetidy auto-detect candidate, so scan
  Tier-1 and OpenCost Tier-2 both find it with zero config.
- `init --with-opencost` now also deploys the bundled Prometheus unless
  --prometheus-url points at an existing one (then the bundle is skipped).
- `init --with-prometheus` deploys just Prometheus (unlocks Tier 1) on its own.
- `uninstall --with-prometheus` removes it — but never the shared `monitoring`
  namespace (deleteManifestSkipNamespaces), so a user's other tooling is safe.
- --print includes whatever --with-* flags select.
- Docs: README setup + Tier 2, ROADMAP, CHANGELOG.
… proxy

scan runs on the user's machine, but auto-detection returned an in-cluster
Service DNS name (*.svc) that only resolves inside the cluster. Every query
failed with "no such host" and the scan silently reported a misleading
100/100 $0 (zero usage mistaken for zero waste) — hit on a real cluster.

- Reach an auto-detected Prometheus/OpenCost through the Kubernetes API server
  proxy (/api/v1/namespaces/<ns>/services/<svc>:<port>/proxy), reusing the
  kubeconfig's API server address + credentials. Works wherever kubectl works,
  no port-forward.
- Validate reachability (a trivial `up` query / live allocation fetch) before
  committing to Tier 1 / Tier 2; an unreachable endpoint falls back to the
  operator / metrics-server / derived pricing with a clear note.
- Expose kube.Clients.RESTConfig; add DetectPrometheusEndpoint /
  DetectOpenCostEndpoint (+ struct), NewPrometheusProviderViaAPIProxy,
  NewOpenCostProviderViaAPIProxy, and usage.Reachable. Auto-detect now sits
  behind provider seams (prometheusAutoProvider / openCostAutoProvider).
- An explicit --prometheus-url / --opencost-url is still used directly.
…ntext

Workload names with dots (CSI drivers like rbd.csi.ceph.com-nodeplugin) or
other regex metacharacters produced `unknown escape sequence U+002E '.'`:
regexp.QuoteMeta yields `\.`, which is an invalid escape inside a PromQL
double-quoted string, so those workloads were skipped. Double the backslashes
when embedding the pod regex. Caught on a real cluster.
@mayur-tolexo
mayur-tolexo merged commit 8a68023 into main Jun 3, 2026
2 checks passed
@mayur-tolexo
mayur-tolexo deleted the feat/init-bundle-prometheus branch June 3, 2026 04:25
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