Skip to content

feat(exporter): add opt-in leader election to deduplicate collection across replicas#1051

Open
somaz94 wants to merge 1 commit into
grafana:mainfrom
somaz94:feat/leader-election
Open

feat(exporter): add opt-in leader election to deduplicate collection across replicas#1051
somaz94 wants to merge 1 commit into
grafana:mainfrom
somaz94:feat/leader-election

Conversation

@somaz94

@somaz94 somaz94 commented Jun 16, 2026

Copy link
Copy Markdown

Adds opt-in, lease-based leader election so running multiple replicas no longer multiplies load on the cloud provider pricing APIs. Off by default, so single-replica deployments are unchanged.

When enabled (-leader-election.enabled):

  • Replicas elect one leader via a Kubernetes Lease.
  • The leader registers the provider collectors and calls the cloud provider APIs.
  • Non-leaders serve /metrics with the base operational metrics plus a cloudcost_exporter_leader_election_is_leader gauge, but do not call the provider APIs.
  • Losing the lease stops collection and shuts the replica down so it rejoins as a candidate.

The result is a single set of upstream API calls regardless of replica count. Sharding (splitting work across replicas) is intentionally out of scope, per the issue.

Validation

  • make lint — 0 issues
  • go test ./... — all pass (new pkg/leaderelection tests including a fake-clientset leadership-acquisition test; updated cmd/exporter tests for the split registry handler)
  • make build-binary — success
  • New dependencies: k8s.io/client-go, k8s.io/apimachinery, k8s.io/api @ v0.34.2 (stable)

Docs

  • docs/deploying/leader-election.md — flags, required RBAC (leases in coordination.k8s.io), and the leader gauge
  • README link from the Kubernetes deployment section

closes: #1027

somaz94 added a commit to somaz94/somaz94 that referenced this pull request Jun 16, 2026
@somaz94 somaz94 force-pushed the feat/leader-election branch from 38be2d5 to 0875452 Compare June 17, 2026 02:02
@somaz94 somaz94 marked this pull request as ready for review June 19, 2026 02:44
@somaz94 somaz94 requested a review from a team June 19, 2026 02:44
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.

Support leader election to avoid duplicate collection across replicas

1 participant