Skip to content

Commit 819ce7b

Browse files
author
Eric Stroczynski
authored
docs: remove redundancy in Go migration, discuss service account in scope doc (#4637)
Signed-off-by: Eric Stroczynski <[email protected]>
1 parent 6002c70 commit 819ce7b

File tree

2 files changed

+18
-38
lines changed

2 files changed

+18
-38
lines changed

website/content/en/docs/building-operators/golang/migration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ See the complete migrated `memcached_controller.go` code [here][memcached_contro
228228

229229
**Note:** The version of [controller-runtime][controller-runtime] used in the projects scaffolded by SDK `0.19.x+` was `v0.6.0`. Please check [sigs.k8s.io/controller-runtime release docs from 0.7.0+ version][controller-runtime] for breaking changes.
230230

231-
##### Updating your ServiceAccount in Go operator projects
231+
##### Updating your ServiceAccount
232232

233233
New Go projects come with a ServiceAccount `controller-manager` in `config/rbac/service_account.yaml`.
234234
Your project's RoleBinding and ClusterRoleBinding subjects, and Deployment's `spec.template.spec.serviceAccountName`

website/content/en/docs/building-operators/golang/operator-scope.md

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
5353
Scheme: scheme,
5454
MetricsBindAddress: metricsAddr,
5555
Port: 9443,
56-
LeaderElection: enableLeaderElection,
56+
LeaderElection: enableLeaderElection,
5757
LeaderElectionID: "f1c5ece8.example.com",
5858
Namespace: "operator-namespace",
5959
})
@@ -73,7 +73,7 @@ mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
7373
Scheme: scheme,
7474
MetricsBindAddress: metricsAddr,
7575
Port: 9443,
76-
LeaderElection: enableLeaderElection,
76+
LeaderElection: enableLeaderElection,
7777
LeaderElectionID: "f1c5ece8.example.com",
7878
NewCache: cache.MultiNamespacedCacheBuilder(namespaces),
7979
})
@@ -87,8 +87,8 @@ its controller because the [Manager][ctrl-manager] does not manage that Namespac
8787
## Restricting Roles and permissions
8888

8989
An operator's scope defines its [Manager's][ctrl-manager] cache's scope but not the permissions to access the resources.
90-
After updating the Manager's scope to be Namespaced, the cluster's [Role-Based Access Control (RBAC)][k8s-rbac]
91-
permissions should be restricted accordingly.
90+
After updating the Manager's scope to be Namespaced, [Role-Based Access Control (RBAC)][k8s-rbac] permissions
91+
applied to the operator's service account should be restricted accordingly.
9292

9393
These permissions are found in the directory `config/rbac/`. The `ClusterRole` in `role.yaml` and `ClusterRoleBinding`
9494
in `role_binding.yaml` are used to grant the operator permissions to access and manage its resources.
@@ -99,33 +99,13 @@ and `auth_proxy_*.yaml` are not relevant to changing the operator's resource per
9999

100100
### Changing the permissions to Namespaced
101101

102-
To change the scope of the RBAC permissions from cluster-wide to a specific namespace, you will need to use `Role`s
103-
=======
104-
105-
- Inform the Namespace to the [Manager][ctrl-manager]
102+
To change the scope of the RBAC permissions from cluster-wide to a specific namespace, you will need to:
106103

107-
By default, the [Manager][ctrl-manager] does not have any namespace specified in `main.go`, and hence it will watch all the namespaces. In order to restrict the controllers to watch a specific namespace, specify it while creating the manager. Update the `NewManager` to inform the Namespace, in our `Memcahced` example it would like:
108-
109-
```go
110-
...
111-
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
112-
Scheme: scheme,
113-
MetricsBindAddress: metricsAddr,
114-
Port: 9443,
115-
LeaderElection: enableLeaderElection,
116-
LeaderElectionID: "f1c5ece8.example.com",
117-
Namespace: "memcached-operator-system", // operator namespace
118-
})
119-
...
120-
```
121-
122-
- Change the scope of the RBAC permissions from cluster-wide to a specific namespace
123-
124-
You will need to use `Role`s and `RoleBinding`s instead of `ClusterRole`s and `ClusterRoleBinding`s, respectively.
104+
- Use `Role`s instead of `ClusterRole`s.
125105

126106
[`RBAC markers`][rbac-markers] defined in the controller (e.g `controllers/memcached_controller.go`)
127107
are used to generate the operator's [RBAC ClusterRole][rbac-clusterrole] (e.g `config/rbac/role.yaml`). The default
128-
markers don't specify a `namespace` property and will result in a `ClusterRole`.
108+
markers don't specify a `namespace` property and will result in a `ClusterRole`.
129109

130110
Update the [`RBAC markers`][rbac-markers] in `<kind>_controller.go` with `namespace=<namespace>` where the `Role` is to be applied, such as:
131111

@@ -134,7 +114,7 @@ Update the [`RBAC markers`][rbac-markers] in `<kind>_controller.go` with `namesp
134114
//+kubebuilder:rbac:groups=cache.example.com,namespace=memcached-operator-system,resources=memcacheds/status,verbs=get;update;patch
135115
```
136116

137-
- Run `make manifests` to update `config/rbac/role.yaml`. In our example it would like:
117+
Then run `make manifests` to update `config/rbac/role.yaml`. In our example it would like:
138118

139119
```yaml
140120
apiVersion: rbac.authorization.k8s.io/v1
@@ -145,10 +125,9 @@ metadata:
145125
namespace: memcached-operator-system
146126
```
147127
148-
- Replace `ClusterRoleBinding` with `RoleBinding` and `ClusterRole` with `Role` in the `config/rbac/role_binding.yaml` file such as:
128+
- Use `RoleBinding`s instead of `ClusterRoleBinding`s. The `config/rbac/role_binding.yaml` needs to be manually updated:
149129

150130
```yaml
151-
152131
apiVersion: rbac.authorization.k8s.io/v1
153132
kind: RoleBinding
154133
metadata:
@@ -159,17 +138,18 @@ roleRef:
159138
name: manager-role
160139
subjects:
161140
- kind: ServiceAccount
162-
name: default
141+
name: controller-manager
163142
namespace: system
164143
```
165144

166145
<!-- todo(camilamacedo86): The need for the RoleBinding show an issue tracked
167146
in https://github.com/kubernetes-sigs/kubebuilder/issues/1496. -->
168147

169-
## Using environment variables for Namespace
148+
## Configuring watch namespaces dynamically
170149

171-
Instead of having any Namespaces hard-coded in the `main.go` file a good practice is to use environment
172-
variables to allow the restrictive configurations
150+
Instead of having any Namespaces hard-coded in the `main.go` file a good practice is to use an environment
151+
variable to allow the restrictive configurations. The one suggested here is `WATCH_NAMESPACE`, a
152+
comma-separated list of namespaces passed to the manager at deploy time.
173153

174154
### Configuring Namespace scoped operators
175155

@@ -290,11 +270,11 @@ If your operator is [integrated with OLM][olm-integration], you will want to upd
290270
is allowed, so supporting multiple install modes in a CSV is permitted. After doing so, update your
291271
[bundle][bundle-quickstart] or [package manifests][packagemanifests-quickstart] by following the linked guides.
292272

293-
## Watching resources in all Namespaces (default)
273+
### Watching resources in all Namespaces (default)
294274

295275
Only the `AllNamespaces` install mode is `supported: true` by default, so no changes are required.
296276

297-
## Watching resources in a single Namespace
277+
### Watching resources in a single Namespace
298278

299279
If the operator can watch its own namespace, set the following in your `spec.installModes` list:
300280

@@ -310,7 +290,7 @@ If the operator can watch a single namespace that is not its own, set the follow
310290
supported: true
311291
```
312292

313-
## Watching resources in multiple Namespaces
293+
### Watching resources in multiple Namespaces
314294

315295
If the operator can watch multiple namespaces, set the following in your `spec.installModes` list:
316296

0 commit comments

Comments
 (0)