Skip to content

feat(main): add --watch-namespace to scope the manager cache#341

Merged
Andrey Kolkov (androndo) merged 1 commit into
cozystack:mainfrom
kjvalencik:kj/watch-namespaces
Jul 21, 2026
Merged

feat(main): add --watch-namespace to scope the manager cache#341
Andrey Kolkov (androndo) merged 1 commit into
cozystack:mainfrom
kjvalencik:kj/watch-namespaces

Conversation

@kjvalencik

Copy link
Copy Markdown
Contributor

Motivation

The manager's controller-runtime cache defaults to cluster-wide watches for every secondary resource it owns (Pods, PVCs, Services, Jobs, PDBs). On large clusters, caching every pod in the cluster can OOM-kill the manager against the chart's default 128Mi limit — even when only a handful of namespaces contain etcd clusters. docs/installation.md previously called this out as an unexposed follow-up.

Related: #199

Changes

  • New --watch-namespace flag, defaulting to $WATCH_NAMESPACE (operator-sdk convention): a comma-separated namespace list wired into the manager's Cache.DefaultNamespaces. Empty/unset keeps today's watch-everything behavior.
  • Chart: manager.watchNamespaces (list) renders the WATCH_NAMESPACE env var onto the manager container. Unset by default.
  • Docs: installation.md RBAC section and chart-values table updated.

RBAC

Intentionally untouched — the existing ClusterRole authorizes the scoped watches. Since every rule in the manager role covers namespaced resources, this change also makes it possible to run with per-namespace Roles instead of the ClusterRole (with the metrics proxy disabled, as its TokenReview/SubjectAccessReview permissions are cluster-scoped) — left as a future improvement.

Testing

  • Unit tests for the namespace-list parsing: empty, whitespace-only, and separator-only inputs yield the zero cache.Options, keeping the unset path identical to the previous behavior.
  • make test passes; helm template/helm lint verified with and without the value set.

@github-actions github-actions Bot added the documentation Improvements or additions to documentation label Jul 13, 2026
@coderabbitai

coderabbitai Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@kjvalencik, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 51 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 415d2107-4af0-4bf4-b053-729bd85b4a98

📥 Commits

Reviewing files that changed from the base of the PR and between 9eb1ec1 and 5b7b50c.

📒 Files selected for processing (5)
  • charts/etcd-operator/templates/deployment.yaml
  • charts/etcd-operator/values.yaml
  • docs/installation.md
  • main.go
  • main_test.go
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request introduces support for scoping the etcd-operator to watch specific namespaces instead of all namespaces. It adds a --watch-namespace flag and a corresponding WATCH_NAMESPACE environment variable to the manager, updates the Helm chart to support this configuration via manager.watchNamespaces, and updates the documentation and tests accordingly. The review feedback suggests making the Helm template more robust by handling cases where manager.watchNamespaces is provided as a string instead of a slice to prevent template rendering errors.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread charts/etcd-operator/templates/deployment.yaml
The manager's default cluster-wide cache holds every Pod, PVC, Service,
Job, and PDB in the cluster; on large clusters this OOM-kills the manager
against the chart's default 128Mi limit.

Add a --watch-namespace flag (defaulting to $WATCH_NAMESPACE, per the
operator-sdk convention) taking a comma-separated namespace list, wired
into the manager's Cache.DefaultNamespaces. Empty/unset preserves the
current watch-everything behavior. The chart exposes it as
manager.watchNamespaces, rendered onto the manager container as the
WATCH_NAMESPACE env var.

RBAC is intentionally untouched: the ClusterRole continues to authorize
the scoped watches.

Signed-off-by: K.J. Valencik <kjvalencik@gmail.com>

@androndo Andrey Kolkov (androndo) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

Reviewed at head 5b7b50c against main (merge base 9eb1ec1). A tidy, well-scoped change that does exactly what the PR body claims: adds a --watch-namespace flag (defaulting to $WATCH_NAMESPACE, operator-sdk convention) wired into Cache.DefaultNamespaces, plus a chart value manager.watchNamespaces and a docs update.

What I verified:

  • Correctness of cache scoping. The one real risk with DefaultNamespaces is a cached read landing in a namespace outside the watch set. I traced every cached Get/List in controllers/ — all are scoped to the CR's own namespace (cluster.Namespace, member.Namespace, snapshot.Namespace), and the associated Pods/PVCs/Services/Secrets all live in that same namespace. No cross-namespace cached reads, so scoping to the watched namespaces is complete: nothing the operator needs falls outside the cache. Cluster-scoped/discovery paths (detectCertManager via discovery client, leader election via direct client) don't go through the manager cache, so they're unaffected. controller-runtime is v0.21.0, where DefaultNamespaces restricts only namespaced resources — consistent with the above.

  • No regression on the default path. Empty/unset input returns cache.Options{}, byte-identical to the previous behavior (no Cache field was set before, i.e. the zero value). Confirmed by test and by helm template with no value producing zero WATCH_NAMESPACE env vars.

  • Helm template edge cases. --set 'manager.watchNamespaces={a,b}'value: "a,b"; string value → verbatim; unset ([], falsy under {{- with }}) → env var omitted. The kindIs "slice" branch handles both list and string inputs. All three verified via helm template.

  • Tests. main_test.go covers the new watchNamespaceCacheOptions thoroughly — valid inputs (single, multiple) AND the invalid/degenerate paths (empty, whitespace-only, separator-only, mixed empty entries), asserting the unset path stays identical to the old config. go test . passes, go vet clean.

  • Documentation. The change correctly replaces the now-stale docs/installation.md paragraph that said single-namespace scoping "is not currently exposed" — the exact follow-up this PR closes. New RBAC text and the values-table row match the implementation, and the [RBAC](#rbac) link resolves to the ## RBAC header.

Non-blocking cosmetic note (recommended, not required):

  • Flag is singular --watch-namespace while the chart value is plural manager.watchNamespaces. This follows the operator-sdk WATCH_NAMESPACE-holds-a-list convention, so it's defensible, but the singular/plural split is a minor readability wrinkle.

@androndo
Andrey Kolkov (androndo) merged commit f0e3c86 into cozystack:main Jul 21, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants