Skip to content

Conversation

@desmax74
Copy link
Collaborator

@desmax74 desmax74 commented Nov 28, 2025

Summary by Sourcery

Configure the operator to watch either a single namespace or all namespaces based on an environment variable.

New Features:

  • Allow configuring the controller-runtime cache to restrict watching to a specific namespace via the WATCH_NAMESPACE environment variable.

Enhancements:

  • Populate WATCH_NAMESPACE from the operator pod's namespace in both the CSV and manager deployment manifests so the operator defaults to watching its own namespace.

@sourcery-ai
Copy link

sourcery-ai bot commented Nov 28, 2025

Reviewer's Guide

Configure the controller-runtime manager to optionally watch a single namespace via a WATCH_NAMESPACE environment variable, and wire that variable into both the controller deployment and CSV manifests.

Sequence diagram for manager startup with optional single-namespace cache

sequenceDiagram
    participant Kubelet
    participant OperatorPod
    participant Main as main_go
    participant Manager as ControllerRuntimeManager
    participant KubeAPI as KubernetesAPI

    Kubelet->>OperatorPod: Start pod with env WATCH_NAMESPACE (from metadata.namespace)
    OperatorPod->>Main: Execute main()
    Main->>Main: Read WATCH_NAMESPACE from environment
    alt WATCH_NAMESPACE is set
        Main->>Main: Build cache.Options with DefaultNamespaces[WATCH_NAMESPACE]
        Main->>Manager: Create NewManager(config, Options{Cache: cache.Options})
        Manager->>KubeAPI: List/watch resources in WATCH_NAMESPACE only
    else WATCH_NAMESPACE is empty
        Main->>Main: Use default cache.Options (all namespaces)
        Main->>Manager: Create NewManager(config, Options{Cache: default})
        Manager->>KubeAPI: List/watch resources in all namespaces
    end
    Manager-->>Main: Manager initialized
    Main->>Manager: Start()
Loading

File-Level Changes

Change Details Files
Make the controller manager watch either a specific namespace from WATCH_NAMESPACE or all namespaces by default.
  • Import controller-runtime cache package and read WATCH_NAMESPACE from environment at startup.
  • Define cache options that restrict the manager cache to the configured namespace when WATCH_NAMESPACE is set, otherwise log that all namespaces are watched.
  • Pass the computed cache options into ctrl.NewManager via the Cache field in ctrl.Options.
main.go
Expose WATCH_NAMESPACE environment variable to the manager container in Kubernetes manifests.
  • Add WATCH_NAMESPACE env var to the operator CSV manifest, sourced from the pod namespace via fieldRef metadata.namespace.
  • Add WATCH_NAMESPACE env var to the manager deployment manifest, also sourced from metadata.namespace so the operator watches its own namespace.
bundle/manifests/rhtpa-operator.clusterserviceversion.yaml
config/manager/manager.yaml

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes - here's some feedback:

  • Instead of using a package-level watchNamespace variable initialized via os.Getenv, consider reading the environment variable inside main() to avoid global state and make the configuration behavior easier to test and reason about.
  • Since this PR only supports a single namespace, using ctrl.Options{Namespace: watchNamespace} may be clearer and more idiomatic than configuring cache.Options{DefaultNamespaces: map[string]cache.Config{...}} for a single entry.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Instead of using a package-level `watchNamespace` variable initialized via `os.Getenv`, consider reading the environment variable inside `main()` to avoid global state and make the configuration behavior easier to test and reason about.
- Since this PR only supports a single namespace, using `ctrl.Options{Namespace: watchNamespace}` may be clearer and more idiomatic than configuring `cache.Options{DefaultNamespaces: map[string]cache.Config{...}}` for a single entry.

## Individual Comments

### Comment 1
<location> `main.go:83` </location>
<code_context>
+	cacheOpts = cache.Options{
</code_context>

<issue_to_address>
**issue (bug_risk):** Using DefaultNamespaces for a single namespace may change cluster-scoped resource behavior compared to the default cache.

`DefaultNamespaces` is designed for multi-namespace caches and changes how cluster-scoped resources are handled, which can differ from the default single-namespace/cluster-wide cache. If this operator uses cluster-scoped resources (CRDs, ClusterRoles, etc.), this may introduce subtle behavior differences. For a single-namespace watch, prefer setting `Namespace: watchNamespace` in `cache.Options` to preserve default cache behavior while restricting namespaced objects.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

if watchNamespace != "" {
setupLog.Info("Watching specific namespace", "namespace", watchNamespace)
cacheOpts = cache.Options{
DefaultNamespaces: map[string]cache.Config{
Copy link

Choose a reason for hiding this comment

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

issue (bug_risk): Using DefaultNamespaces for a single namespace may change cluster-scoped resource behavior compared to the default cache.

DefaultNamespaces is designed for multi-namespace caches and changes how cluster-scoped resources are handled, which can differ from the default single-namespace/cluster-wide cache. If this operator uses cluster-scoped resources (CRDs, ClusterRoles, etc.), this may introduce subtle behavior differences. For a single-namespace watch, prefer setting Namespace: watchNamespace in cache.Options to preserve default cache behavior while restricting namespaced objects.

Signed-off-by: desmax74 <[email protected]>
@desmax74 desmax74 force-pushed the singleNamespaceRestore branch from 7187cd0 to 06542b9 Compare November 28, 2025 14:45
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