operator-framework / operator-sdk

SDK for building Kubernetes applications. Provides high level APIs, useful abstractions, and project scaffolding.
https://sdk.operatorframework.io
Apache License 2.0
7.21k stars 1.74k forks source link

Operators Scope documentation requires update #6682

Closed blez closed 5 months ago

blez commented 7 months ago

https://sdk.operatorframework.io/docs/building-operators/golang/operator-scope/#watching-resources-in-a-single-namespace

Namespace filed is not a part of manager.Options struct

OchiengEd commented 7 months ago

In the past, the manager.Options struct had a Namespace field which could be used to restrict the scope of the operator. However, this field was deprecated but the same effect could be achieved by customizing the cache options as shown in the example below:

    mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
        Scheme:             scheme,
        MetricsBindAddress: metricsAddr,
        Port:               9443,
        LeaderElection:     enableLeaderElection,
        LeaderElectionID:   "852d23b0.example.com",
        Cache: cache.Options{
            Namespaces: []string{"namespace1", "namespace2"},
        },
    })

Below is a link to an out of date operator code that uses the old approach to scope the operator to a single namespace. Hopefully it will be a satisfactory reference / guide in your endeavor.

OchiengEd commented 7 months ago

Well, the most recent controller-runtime code show the cache.Options struct has changed further. As a result, the Namespaces field no longer exists has has been replaced by the DefaultNamespaces field.

So, ideally, depending on the version of operator-sdk you are using and the version of dependencies and more specifically controller-runtime is in use, the solution could look different.

See https://github.com/kubernetes-sigs/controller-runtime/blob/5f8d96b043ef8ccadc544535415395be336630e8/pkg/cache/cache.go#L188

With that being said, what version of operator-sdk and controller-runtime are used in your operator?

blez commented 7 months ago

Thanks! The purpose of this issue was to say that documentation is wrong and should be fixed. I use the DefaultNamespaces field.

OchiengEd commented 6 months ago

Thank you for pointing that out. It was not clear in your initial post. If you would like to add a PR to update the documentation, that would be greatly appreciated.