-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
draft: add namespace scoped operator mode
This adds automation and docs for restricting the operator scope from cluster wide to namespace restricted. Signed-off-by: NymanRobin <[email protected]>
- Loading branch information
1 parent
efae71e
commit 11dbdad
Showing
35 changed files
with
748 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
apiVersion: kustomize.config.k8s.io/v1beta1 | ||
kind: Kustomization | ||
|
||
resources: | ||
- ../e2e | ||
|
||
# The subjects and roleRef needs to be update here otherwise we lose name-prefix | ||
# This is of course not ideal if the name-prefix changes | ||
patchesStrategicMerge: | ||
- namespaced-manager-patch.yaml | ||
- | | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRoleBinding | ||
metadata: | ||
name: baremetal-operator-manager-rolebinding | ||
roleRef: | ||
kind: Role | ||
name: baremetal-operator-manager-role | ||
subjects: | ||
- kind: ServiceAccount | ||
name: baremetal-operator-controller-manager | ||
namespace: baremetal-operator-system | ||
patches: | ||
- patch: | | ||
# Add a namespace to watch | ||
- op: replace | ||
path: /kind | ||
value: RoleBinding | ||
target: | ||
group: rbac.authorization.k8s.io | ||
kind: ClusterRoleBinding | ||
name: baremetal-operator-manager-rolebinding | ||
|
||
- patch: | | ||
# Add a namespace to watch | ||
- op: replace | ||
path: /kind | ||
value: Role | ||
target: | ||
group: rbac.authorization.k8s.io | ||
kind: ClusterRole | ||
name: baremetal-operator-manager-role | ||
|
||
transformers: | ||
- roles-ns-annotator.yaml |
13 changes: 13 additions & 0 deletions
13
config/overlays/namespaced-e2e/namespaced-manager-patch.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: controller-manager | ||
namespace: system | ||
spec: | ||
template: | ||
spec: | ||
containers: | ||
- env: | ||
- name: WATCH_NAMESPACE | ||
value: basic-ops-test,external-inspection-test,inspection-test,live-iso-ops-test,provisioning-ops-test,re-inspection-test | ||
name: manager |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
apiVersion: transformers.example.co/v1 | ||
kind: ValueAnnotator | ||
metadata: | ||
annotations: | ||
config.kubernetes.io/function: "container:\n image: bmo/roleannotator:1.0.0\ | ||
\ \n" | ||
name: notImportantHere | ||
values: | ||
- basic-ops-test | ||
- external-inspection-test | ||
- inspection-test | ||
- live-iso-ops-test | ||
- provisioning-ops-test | ||
- re-inspection-test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
apiVersion: kustomize.config.k8s.io/v1beta1 | ||
kind: Kustomization | ||
|
||
resources: | ||
- ../../base | ||
# The subjects and roleRef needs to be update here otherwise we lose name-prefix | ||
# This is of course not ideal if the name-prefix changes | ||
patchesStrategicMerge: | ||
- namespaced-manager-patch.yaml | ||
- | | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRoleBinding | ||
metadata: | ||
name: baremetal-operator-manager-rolebinding | ||
roleRef: | ||
kind: Role | ||
name: baremetal-operator-manager-role | ||
subjects: | ||
- kind: ServiceAccount | ||
name: baremetal-operator-controller-manager | ||
namespace: baremetal-operator-system | ||
patches: | ||
- patch: | | ||
# Add a namespace to watch | ||
- op: replace | ||
path: /kind | ||
value: RoleBinding | ||
target: | ||
group: rbac.authorization.k8s.io | ||
kind: ClusterRoleBinding | ||
name: baremetal-operator-manager-rolebinding | ||
|
||
- patch: | | ||
# Add a namespace to watch | ||
- op: replace | ||
path: /kind | ||
value: Role | ||
target: | ||
group: rbac.authorization.k8s.io | ||
kind: ClusterRole | ||
name: baremetal-operator-manager-role | ||
|
||
transformers: | ||
- roles-ns-annotator.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: controller-manager | ||
namespace: system | ||
spec: | ||
template: | ||
spec: | ||
containers: | ||
- env: | ||
- name: WATCH_NAMESPACE | ||
value: metal3 | ||
name: manager |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
apiVersion: transformers.example.co/v1 | ||
kind: ValueAnnotator | ||
metadata: | ||
annotations: | ||
config.kubernetes.io/function: "container:\n image: bmo/roleannotator:1.0.0 \ | ||
\ \n" | ||
name: notImportantHere | ||
values: | ||
- metal3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# How to restrict the Baremetal Operator scope | ||
|
||
The guide is based on the instructions in the | ||
[operator framework documentation][operator-scope]. | ||
|
||
[operator-scope]: https://sdk.operatorframework.io/docs/building-operators/golang/operator-scope/ | ||
|
||
## To generate manifests for namespace scoped BMO | ||
|
||
To generate namespace-scoped manifests, run the `manifests-namespaced` | ||
make target. This command will create manifests specifically configured for the | ||
namespaces defined in. It uses the `config/overlays/namespaced` kustomize | ||
overlay to replace cluster-scoped resources with their namespace-scoped counterparts. | ||
|
||
## Watching resources in specific Namespaces | ||
|
||
When setting up the manager, you can use the environment variable | ||
`WATCH_NAMESPACE` to restrict the operator to a specific namespace. If | ||
`WATCH_NAMESPACE` is unset or set to an empty string, the operator will | ||
monitor all namespaces. To limit it to a specific namespace, set | ||
`WATCH_NAMESPACE` to that namespace. | ||
|
||
For example, to configure the operator to watch the same namespace where | ||
it is deployed, update the `config/base/manager.yaml` file. Add the | ||
following configuration under `spec.template.spec.containers.env`: | ||
|
||
```yaml | ||
- name: WATCH_NAMESPACE | ||
valueFrom: | ||
fieldRef: | ||
fieldPath: metadata.namespace | ||
``` | ||
## Restricting Roles and permissions | ||
When BMO is restricted to a single namespace, the RBAC permissions need | ||
to be updated accordingly. Instead of using `ClusterRole`, you will use | ||
`Role`. | ||
|
||
The `Role` is defined in the file `config/base/rbac/role_ns.yaml`. This | ||
file is auto-generated based on Kubebuilder RBAC markers, specifically those | ||
in `<some>_controller.go`. The default namespace marking is set to `""`, | ||
which results in a `ClusterRole`. To restrict it to a specific namespace, | ||
update this value accordingly. | ||
|
||
After updating the markers, generate the new manifests by running: | ||
|
||
```bash | ||
make manifests | ||
``` | ||
|
||
Ensure that `config/base/rbac/role_ns.yaml` has been updated to a `Role`. | ||
|
||
Due to limitations in Kubebuilder generation, the `RoleBinding` will not | ||
be updated automatically.You can manually update `config/base/rbac/role_binding.yaml` | ||
to achieve the desired outcome. Below is an example of how to modify the | ||
`role_binding.yaml` file: | ||
|
||
```yaml: | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: RoleBinding | ||
metadata: | ||
name: manager-rolebinding | ||
namespace: your-namespace | ||
roleRef: | ||
apiGroup: rbac.authorization.k8s.io | ||
kind: Role | ||
name: manager-role | ||
namespace: your-namespace | ||
subjects: | ||
- kind: ServiceAccount | ||
name: controller-manager | ||
namespace: system | ||
``` | ||
Replace `your-namespace` and other fields as necessary to match your | ||
specific configuration. | ||
|
||
After this you can run `make manifests-kustomize` to get correct RoleBinding generated |
Oops, something went wrong.