Skip to content

Commit

Permalink
Change docs to reflect updated controller runtime options for caching…
Browse files Browse the repository at this point in the history
… select namespaces

Signed-off-by: jberkhahn <[email protected]>
  • Loading branch information
jberkhahn committed Apr 9, 2024
1 parent 3969b9b commit 6619e38
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
35 changes: 21 additions & 14 deletions website/content/en/docs/building-operators/golang/operator-scope.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ as cluster-scoped. For example, the [cert-manager][cert-manager] operator is oft
permissions and watches so that it can manage and issue certificates for an entire cluster.

**IMPORTANT**: When a [Manager][ctrl-manager] instance is created in the `main.go` file, the
Namespaces are set via [Manager Options][ctrl-options] as described below. These Namespaces should be watched and
Namespaces are set via [Cache Config][cache-config] as described below. These Namespaces should be watched and
cached for the Client which is provided by the Manager. Only clients provided by cluster-scoped Managers are able
to manage cluster-scoped CRD's. For further information see: [CRD scope doc][crd-scope-doc].

## Manager watching options

### Watching resources in all Namespaces (default)

A [Manager][ctrl-manager] is initialized with no Namespace option specified, or `Namespace: ""` will
A [Manager][ctrl-manager] is initialized with no Cache option specified, or with a Cache.DefaultNamespaces of `Namespace: ""` will
watch all Namespaces:

```go
Expand All @@ -42,10 +42,10 @@ mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
...
```

### Watching resources in a single Namespace
### Watching resources in specific Namespaces

To restrict the scope of the [Manager's][ctrl-manager] cache to a specific Namespace set the `Namespace` field
in [Options][ctrl-options]:
To restrict the scope of the [Manager's][ctrl-manager] cache to a specific Namespace set the `Cache.DefaultNamespaces'
field in [Options][ctrl-options]:

```go
...
Expand All @@ -55,32 +55,39 @@ mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Port: 9443,
LeaderElection: enableLeaderElection,
LeaderElectionID: "f1c5ece8.example.com",
Namespace: "operator-namespace",
Cache: cache.Options{
DefaultNamespaces: map[string]cache.Config{"operator-namespace": cache.Config{}},
},
})
...
```

### Watching resources in a set of Namespaces

It is possible to use [`MultiNamespacedCacheBuilder`][multi-namespaced-cache-builder] from
[Options][ctrl-options] to watch and manage resources in a set of Namespaces:
It is also possible to use 'DefaultNamepsaces' to watch and manage resources in a set of Namespaces:

```go
...
namespaces := []string{"foo", "bar"} // List of Namespaces
...
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: 9443,
LeaderElection: enableLeaderElection,
LeaderElectionID: "f1c5ece8.example.com",
NewCache: cache.MultiNamespacedCacheBuilder(namespaces),
Cache: cache.Options{
DefaultNamespaces: map[string]cache.Config{
"operator-namespace1": cache.Config{},
"operator-namespace2": cache.Config{},
},
},
})
...
```
In the above example, a CR created in a Namespace not in the set passed to `Options` will not be reconciled by
its controller because the [Manager][ctrl-manager] does not manage that Namespace.

In the above example, a CR created in a Namespace not in the set passed to `Cache.DefaultNamespaces` will not be reconciled by
its controller because the [Manager][ctrl-manager] does not manage that Namespace. Further restrictions and qualifications
can created on a per-namespace basis by setting fields in the cache.Config object, for further information see the
[controller runtime docs][cache-config]

**IMPORTANT:** Note that this is not intended to be used for excluding Namespaces, this is better done via a Predicate.

Expand Down Expand Up @@ -299,10 +306,10 @@ If the operator can watch multiple namespaces, set the following in your `spec.i
supported: true
```

[cache-config]: https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/cache#Config
[cert-manager]: https://github.com/jetstack/cert-manager
[ctrl-manager]: https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/manager#Manager
[ctrl-options]: https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/manager#Options
[multi-namespaced-cache-builder]: https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/cache#MultiNamespacedCacheBuilder
[k8s-rbac]: https://kubernetes.io/docs/reference/access-authn-authz/rbac/
[kube-rbac-proxy]: https://github.com/brancz/kube-rbac-proxy
[rbac-clusterrole]: https://kubernetes.io/docs/reference/access-authn-authz/rbac/#role-and-clusterrole
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,6 @@ Next, check out the following:
[markers]: https://book.kubebuilder.io/reference/markers.html
[memcached_controller]: https://github.com/operator-framework/operator-sdk/blob/latest/testdata/go/v4/memcached-operator/internal/controller/memcached_controller.go
[migration-guide]:/docs/building-operators/golang/migration
[multi-namespaced-cache-builder]: https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/cache#MultiNamespacedCacheBuilder
[multigroup-kubebuilder-doc]: https://book.kubebuilder.io/migration/multi-group.html
[olm-integration]: /docs/olm-integration
[openapi-validation]: /docs/building-operators/golang/references/openapi-validation
Expand Down

0 comments on commit 6619e38

Please sign in to comment.