Skip to content

Commit 07cde15

Browse files
committed
support multiple watch namespaces
1 parent 8a53f24 commit 07cde15

File tree

5 files changed

+111
-66
lines changed

5 files changed

+111
-66
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
- apiGroups:
2+
- ""
3+
resources:
4+
- pods
5+
- services
6+
- configmaps
7+
- secrets
8+
verbs:
9+
- create
10+
- delete
11+
- get
12+
- list
13+
- patch
14+
- update
15+
- watch
16+
- apiGroups:
17+
- apps
18+
resources:
19+
- statefulsets
20+
verbs:
21+
- create
22+
- delete
23+
- get
24+
- list
25+
- patch
26+
- update
27+
- watch
28+
- apiGroups:
29+
- mongodbcommunity.mongodb.com
30+
resources:
31+
- mongodbcommunity
32+
- mongodbcommunity/status
33+
- mongodbcommunity/spec
34+
- mongodbcommunity/finalizers
35+
verbs:
36+
- get
37+
- patch
38+
- list
39+
- update
40+
- watch
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{{/*
2+
Operator's watch namespaces
3+
*/}}
4+
{{- define "community-operator.watchNamespaces" -}}
5+
{{- $defaultNamespaces := list $.Release.Namespace }}
6+
{{- $namespaces := default $defaultNamespaces .Values.operator.watchNamespaces }}
7+
{{- if has "*" $namespaces }}
8+
{{- list "*" | toYaml }}
9+
{{- else }}
10+
{{- $namespaces | toYaml }}
11+
{{- end }}
12+
{{- end }}

charts/community-operator/templates/operator.yaml

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{{- $operatorWatchNamespaces := include "community-operator.watchNamespaces" . | fromYamlArray }}
12
---
23
apiVersion: apps/v1
34
kind: Deployment
@@ -43,13 +44,9 @@ spec:
4344
{{- if .Values.operator.extraEnvs }}
4445
{{ toYaml .Values.operator.extraEnvs | nindent 12 }}
4546
{{- end }}
47+
{{- if $operatorWatchNamespaces }}
4648
- name: WATCH_NAMESPACE
47-
{{- if .Values.operator.watchNamespace}}
48-
value: "{{ .Values.operator.watchNamespace }}"
49-
{{- else }}
50-
valueFrom:
51-
fieldRef:
52-
fieldPath: metadata.namespace
49+
value: "{{ join "," $operatorWatchNamespaces }}"
5350
{{- end }}
5451
- name: POD_NAME
5552
valueFrom:
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,71 @@
1+
{{- $operatorWatchNamespaces := include "community-operator.watchNamespaces" . | fromYamlArray }}
2+
{{- $operatorWatchAllNamespace := has "*" $operatorWatchNamespaces }}
3+
{{- $operatorName := .Values.operator.name -}}
4+
{{- $operatorRBAC := (.Files.Lines "operator_rbac.yaml")}}
5+
16
---
27
apiVersion: v1
38
kind: ServiceAccount
49
metadata:
5-
name: {{ .Values.operator.name }}
10+
name: {{ $operatorName }}
611
namespace: {{ .Release.Namespace }}
712

13+
{{- if $operatorWatchAllNamespace }}
14+
815
---
916
apiVersion: rbac.authorization.k8s.io/v1
10-
kind: {{ if eq (.Values.operator.watchNamespace | default "") "*" }} ClusterRole {{ else }} Role {{ end }}
17+
kind: ClusterRole
1118
metadata:
12-
name: {{ .Values.operator.name }}
13-
{{- if not (eq (.Values.operator.watchNamespace | default "*") "*") }}
14-
namespace: {{ .Values.operator.watchNamespace }}
15-
{{- else }}
16-
namespace: {{ .Release.Namespace }}
17-
{{- end }}
19+
name: {{ $operatorName }}
1820
rules:
19-
- apiGroups:
20-
- ""
21-
resources:
22-
- pods
23-
- services
24-
- configmaps
25-
- secrets
26-
verbs:
27-
- create
28-
- delete
29-
- get
30-
- list
31-
- patch
32-
- update
33-
- watch
34-
- apiGroups:
35-
- apps
36-
resources:
37-
- statefulsets
38-
verbs:
39-
- create
40-
- delete
41-
- get
42-
- list
43-
- patch
44-
- update
45-
- watch
46-
- apiGroups:
47-
- mongodbcommunity.mongodb.com
48-
resources:
49-
- mongodbcommunity
50-
- mongodbcommunity/status
51-
- mongodbcommunity/spec
52-
- mongodbcommunity/finalizers
53-
verbs:
54-
- get
55-
- patch
56-
- list
57-
- update
58-
- watch
21+
{{- range $operatorRBAC }}
22+
{{ . -}}
23+
{{- end }}
5924

6025
---
61-
kind: {{ if eq (.Values.operator.watchNamespace | default "") "*" }} ClusterRoleBinding {{ else }} RoleBinding {{ end }}
26+
kind: ClusterRoleBinding
6227
apiVersion: rbac.authorization.k8s.io/v1
6328
metadata:
64-
name: {{ .Values.operator.name }}
65-
{{- if ne (.Values.operator.watchNamespace | default "*") "*" }}
66-
namespace: {{ .Values.operator.watchNamespace }}
67-
{{- else }}
68-
namespace: {{ .Release.Namespace }}
29+
name: {{ $operatorName }}
30+
subjects:
31+
- kind: ServiceAccount
32+
name: {{ $operatorName }}
33+
namespace: {{ $.Release.Namespace }}
34+
roleRef:
35+
kind: ClusterRole
36+
name: {{ $operatorName }}
37+
apiGroup: rbac.authorization.k8s.io
38+
39+
{{- else }}
40+
41+
{{- range $namespace := $operatorWatchNamespaces }}
42+
43+
---
44+
apiVersion: rbac.authorization.k8s.io/v1
45+
kind: Role
46+
metadata:
47+
name: {{ $operatorName }}
48+
namespace: {{ $namespace }}
49+
rules:
50+
{{- range $operatorRBAC }}
51+
{{ . -}}
6952
{{- end }}
53+
54+
---
55+
kind: RoleBinding
56+
apiVersion: rbac.authorization.k8s.io/v1
57+
metadata:
58+
name: {{ $operatorName }}
59+
namespace: {{ $namespace }}
7060
subjects:
7161
- kind: ServiceAccount
72-
name: {{ .Values.operator.name }}
73-
namespace: {{ .Release.Namespace }}
62+
name: {{ $operatorName }}
63+
namespace: {{ $.Release.Namespace }}
7464
roleRef:
75-
kind: {{ if eq (.Values.operator.watchNamespace | default "") "*" }} ClusterRole {{ else }} Role {{ end }}
76-
name: {{ .Values.operator.name }}
65+
kind: Role
66+
name: {{ $operatorName }}
7767
apiGroup: rbac.authorization.k8s.io
68+
69+
{{- end }}
70+
71+
{{- end }}

charts/community-operator/values.yaml

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ operator:
1717
# Version of mongodb-kubernetes-operator
1818
version: 0.11.0
1919

20-
# Uncomment this line to watch all namespaces
21-
# watchNamespace: "*"
20+
# watchNamespaces is the set of namespaces that are watched by the Operator.
21+
# - empty (watch namespace where the Operator is installed to)
22+
# - ["*"] (watch all namespaces)
23+
# watchNamespaces: []
2224

2325
# Resources allocated to Operator Pod
2426
resources:

0 commit comments

Comments
 (0)