Skip to content

Commit 1cec647

Browse files
refactor(helm): remove rbac.clusterScoped, derive RBAC scope from rbac.namespaces (#134)
Replicate of https://github.com/kagent-dev/kagent/pull/1728/changes to kmcp charts Signed-off-by: Jet Chiang <pokyuen.jetchiang-ext@solo.io>
1 parent a5240a6 commit 1cec647

7 files changed

Lines changed: 134 additions & 59 deletions

File tree

helm/kmcp/templates/_helpers.tpl

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,21 @@ Create the image reference
6969
{{- printf "%s:%s" .Values.image.repository $tag }}
7070
{{- end }}
7171

72+
{{/*
73+
Guards on the rbac block
74+
*/}}
75+
{{- define "kmcp.rbac.validate" -}}
76+
{{- if and .Values.rbac (hasKey .Values.rbac "clusterScoped") -}}
77+
{{- fail "rbac.clusterScoped has been removed. Leave rbac.namespaces empty for cluster-scoped RBAC, or set rbac.namespaces=[<ns>, ...] for namespaced RBAC." -}}
78+
{{- end -}}
79+
{{- if and .Values.rbac .Values.rbac.namespaces -}}
80+
{{- $installNs := include "kmcp.namespace" . -}}
81+
{{- if not (has $installNs .Values.rbac.namespaces) -}}
82+
{{- fail (printf "rbac.namespaces is set but does not include the install namespace %q" $installNs) -}}
83+
{{- end -}}
84+
{{- end -}}
85+
{{- end -}}
86+
7287
{{/*
7388
Create controller manager container args
7489
*/}}
@@ -83,8 +98,8 @@ Create controller manager container args
8398
{{- if .Values.controller.metrics.enabled }}
8499
{{- $args = append $args (printf "--metrics-bind-address=%s" .Values.controller.metrics.bindAddress) }}
85100
{{- end }}
86-
{{- if not .Values.rbac.clusterScoped }}
87-
{{- $namespaces := .Values.rbac.namespaces | default (list (include "kmcp.namespace" .)) }}
101+
{{- if and .Values.rbac .Values.rbac.namespaces }}
102+
{{- $namespaces := .Values.rbac.namespaces | uniq }}
88103
{{- $args = append $args (printf "--watch-namespaces=%s" (join "," $namespaces)) }}
89104
{{- end }}
90105
{{- toYaml $args }}

helm/kmcp/templates/rbac/clusterrole.yaml

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,9 @@
5454
{{- end -}}
5555

5656
{{- if .Values.rbac.create }}
57-
58-
{{- if .Values.rbac.clusterScoped }}
59-
apiVersion: rbac.authorization.k8s.io/v1
60-
kind: ClusterRole
61-
metadata:
62-
name: {{ include "kmcp.fullname" . }}-manager-role
63-
labels:
64-
{{- include "kmcp.labels" . | nindent 4 }}
65-
rules:
66-
{{- include "kmcp.manager.rules" . | nindent 2 }}
67-
{{- else }}
68-
{{- $namespaces := .Values.rbac.namespaces | default (list (include "kmcp.namespace" .)) }}
69-
{{- range $namespace := $namespaces }}
57+
{{- include "kmcp.rbac.validate" . -}}
58+
{{- if .Values.rbac.namespaces }}
59+
{{- range $namespace := (.Values.rbac.namespaces | uniq | sortAlpha) }}
7060
---
7161
apiVersion: rbac.authorization.k8s.io/v1
7262
kind: Role
@@ -78,5 +68,14 @@ metadata:
7868
rules:
7969
{{- include "kmcp.manager.rules" $ | nindent 2 }}
8070
{{- end }}
71+
{{- else }}
72+
apiVersion: rbac.authorization.k8s.io/v1
73+
kind: ClusterRole
74+
metadata:
75+
name: {{ include "kmcp.fullname" . }}-manager-role
76+
labels:
77+
{{- include "kmcp.labels" . | nindent 4 }}
78+
rules:
79+
{{- include "kmcp.manager.rules" . | nindent 2 }}
8180
{{- end }}
8281
{{- end }}
Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,7 @@
11
{{- if .Values.rbac.create }}
2-
{{- if .Values.rbac.clusterScoped }}
3-
apiVersion: rbac.authorization.k8s.io/v1
4-
kind: ClusterRoleBinding
5-
metadata:
6-
name: {{ include "kmcp.fullname" . }}-manager-rolebinding
7-
labels:
8-
{{- include "kmcp.labels" . | nindent 4 }}
9-
roleRef:
10-
apiGroup: rbac.authorization.k8s.io
11-
kind: ClusterRole
12-
name: {{ include "kmcp.fullname" . }}-manager-role
13-
subjects:
14-
- kind: ServiceAccount
15-
name: {{ include "kmcp.serviceAccountName" . }}
16-
namespace: {{ include "kmcp.namespace" . }}
17-
{{- else }}
18-
{{- $namespaces := .Values.rbac.namespaces | default (list (include "kmcp.namespace" .)) }}
19-
{{- range $namespace := $namespaces }}
2+
{{- include "kmcp.rbac.validate" . -}}
3+
{{- if .Values.rbac.namespaces }}
4+
{{- range $namespace := (.Values.rbac.namespaces | uniq | sortAlpha) }}
205
---
216
apiVersion: rbac.authorization.k8s.io/v1
227
kind: RoleBinding
@@ -34,5 +19,20 @@ subjects:
3419
name: {{ include "kmcp.serviceAccountName" $ }}
3520
namespace: {{ include "kmcp.namespace" $ }}
3621
{{- end }}
22+
{{- else }}
23+
apiVersion: rbac.authorization.k8s.io/v1
24+
kind: ClusterRoleBinding
25+
metadata:
26+
name: {{ include "kmcp.fullname" . }}-manager-rolebinding
27+
labels:
28+
{{- include "kmcp.labels" . | nindent 4 }}
29+
roleRef:
30+
apiGroup: rbac.authorization.k8s.io
31+
kind: ClusterRole
32+
name: {{ include "kmcp.fullname" . }}-manager-role
33+
subjects:
34+
- kind: ServiceAccount
35+
name: {{ include "kmcp.serviceAccountName" . }}
36+
namespace: {{ include "kmcp.namespace" . }}
3737
{{- end }}
3838
{{- end }}

helm/kmcp/tests/__snapshot__/rbac_test.yaml.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
should create Role when clusterScoped is false:
1+
should create Role when rbac.namespaces is set:
22
1: |
33
apiVersion: rbac.authorization.k8s.io/v1
44
kind: Role
@@ -64,7 +64,7 @@ should create Role when clusterScoped is false:
6464
- get
6565
- patch
6666
- update
67-
should create RoleBinding when clusterScoped is false:
67+
should create RoleBinding when rbac.namespaces is set:
6868
1: |
6969
apiVersion: rbac.authorization.k8s.io/v1
7070
kind: RoleBinding
@@ -310,7 +310,7 @@ should create multiple Roles when multiple namespaces are provided:
310310
control-plane: controller-manager
311311
helm.sh/chart: kmcp-1.0.0
312312
name: RELEASE-NAME-manager-role
313-
namespace: ns1
313+
namespace: NAMESPACE
314314
rules:
315315
- apiGroups:
316316
- ""

helm/kmcp/tests/deployment_test.yaml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,23 +155,24 @@ tests:
155155
count: 1
156156
- matchSnapshot: {}
157157

158-
- it: should not include watch-namespaces arg when clusterScoped is true
158+
- it: should not include watch-namespaces arg when rbac.namespaces is empty
159159
template: deployment.yaml
160160
set:
161-
rbac.clusterScoped: true
161+
rbac.namespaces: []
162162
image.repository: test-repo
163163
image.tag: v1.0.0
164164
asserts:
165165
- hasDocuments:
166166
count: 1
167167
- notContains:
168168
path: spec.template.spec.containers[0].args
169-
content: --watch-namespaces=NAMESPACE
169+
content: --watch-namespaces
170170

171-
- it: should include watch-namespaces arg when clusterScoped is false with default namespace
171+
- it: should include watch-namespaces arg when rbac.namespaces is set
172172
template: deployment.yaml
173173
set:
174-
rbac.clusterScoped: false
174+
rbac.namespaces:
175+
- NAMESPACE
175176
image.repository: test-repo
176177
image.tag: v1.0.0
177178
asserts:
@@ -181,11 +182,11 @@ tests:
181182
path: spec.template.spec.containers[0].args
182183
content: --watch-namespaces=NAMESPACE
183184

184-
- it: should include watch-namespaces arg with explicit namespaces when clusterScoped is false
185+
- it: should include watch-namespaces arg with explicit namespaces
185186
template: deployment.yaml
186187
set:
187-
rbac.clusterScoped: false
188188
rbac.namespaces:
189+
- NAMESPACE
189190
- ns1
190191
- ns2
191192
image.repository: test-repo
@@ -195,4 +196,4 @@ tests:
195196
count: 1
196197
- contains:
197198
path: spec.template.spec.containers[0].args
198-
content: --watch-namespaces=ns1,ns2
199+
content: --watch-namespaces=NAMESPACE,ns1,ns2

helm/kmcp/tests/rbac_test.yaml

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,12 @@ tests:
144144
count: 1
145145
- matchSnapshot: {}
146146

147-
- it: should create Role when clusterScoped is false
147+
- it: should create Role when rbac.namespaces is set
148148
template: rbac/clusterrole.yaml
149149
set:
150150
rbac.create: true
151-
rbac.clusterScoped: false
151+
rbac.namespaces:
152+
- NAMESPACE
152153
asserts:
153154
- hasDocuments:
154155
count: 1
@@ -157,11 +158,12 @@ tests:
157158
apiVersion: rbac.authorization.k8s.io/v1
158159
- matchSnapshot: {}
159160

160-
- it: should create RoleBinding when clusterScoped is false
161+
- it: should create RoleBinding when rbac.namespaces is set
161162
template: rbac/clusterrolebinding.yaml
162163
set:
163164
rbac.create: true
164-
rbac.clusterScoped: false
165+
rbac.namespaces:
166+
- NAMESPACE
165167
asserts:
166168
- hasDocuments:
167169
count: 1
@@ -170,20 +172,40 @@ tests:
170172
apiVersion: rbac.authorization.k8s.io/v1
171173
- matchSnapshot: {}
172174

175+
- it: should render a single role/binding in the listed namespace only (no release-ns fallback)
176+
set:
177+
rbac.create: true
178+
rbac.namespaces:
179+
- NAMESPACE
180+
asserts:
181+
- hasDocuments:
182+
count: 1
183+
template: rbac/clusterrole.yaml
184+
- equal:
185+
path: metadata.namespace
186+
value: NAMESPACE
187+
template: rbac/clusterrole.yaml
188+
- hasDocuments:
189+
count: 1
190+
template: rbac/clusterrolebinding.yaml
191+
- equal:
192+
path: metadata.namespace
193+
value: NAMESPACE
194+
template: rbac/clusterrolebinding.yaml
195+
173196
- it: should create multiple Roles when multiple namespaces are provided
174197
template: rbac/clusterrole.yaml
175198
set:
176199
rbac.create: true
177-
rbac.clusterScoped: false
178200
rbac.namespaces:
179-
- ns1
201+
- NAMESPACE
180202
- ns2
181203
asserts:
182204
- hasDocuments:
183205
count: 2
184206
- equal:
185207
path: metadata.namespace
186-
value: ns1
208+
value: NAMESPACE
187209
documentIndex: 0
188210
- equal:
189211
path: metadata.namespace
@@ -195,4 +217,43 @@ tests:
195217
- isKind:
196218
of: Role
197219
documentIndex: 1
198-
- matchSnapshot: {}
220+
- matchSnapshot: {}
221+
222+
- it: should fail rendering if the removed rbac.clusterScoped field is set
223+
set:
224+
rbac.create: true
225+
rbac.clusterScoped: false
226+
template: rbac/clusterrolebinding.yaml
227+
asserts:
228+
- failedTemplate:
229+
errorMessage: "rbac.clusterScoped has been removed. Leave rbac.namespaces empty for cluster-scoped RBAC, or set rbac.namespaces=[<ns>, ...] for namespaced RBAC."
230+
231+
- it: should fail rendering if rbac.namespaces is set but does not include the install namespace
232+
set:
233+
rbac.create: true
234+
rbac.namespaces:
235+
- some-other-ns
236+
template: rbac/clusterrolebinding.yaml
237+
asserts:
238+
- failedTemplate:
239+
errorMessage: "rbac.namespaces is set but does not include the install namespace \"NAMESPACE\""
240+
241+
- it: should accept a custom install namespace when listed in rbac.namespaces
242+
set:
243+
namespaceOverride: my-ns
244+
rbac.create: true
245+
rbac.namespaces:
246+
- my-ns
247+
- other-ns
248+
template: rbac/clusterrole.yaml
249+
asserts:
250+
- hasDocuments:
251+
count: 2
252+
- equal:
253+
path: metadata.namespace
254+
value: my-ns
255+
documentIndex: 0
256+
- equal:
257+
path: metadata.namespace
258+
value: other-ns
259+
documentIndex: 1

helm/kmcp/values.yaml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,12 @@ serviceAccount:
9595
rbac:
9696
# Specifies whether RBAC resources should be created
9797
create: true
98-
99-
# -- If true, creates ClusterRole and ClusterRoleBinding resources.
100-
# If false, creates Role and RoleBinding resources instead.
101-
clusterScoped: true
102-
103-
# -- When clusterScoped is false, specify additional namespaces to create Roles and RoleBindings in.
104-
# If empty, defaults to the release namespace.
98+
99+
# -- Namespaces in which to create Role and RoleBinding resources.
100+
# If empty (default), the chart creates cluster-scoped ClusterRole and ClusterRoleBinding
101+
# resources and the controller watches all namespaces.
102+
# If set, the chart creates a Role + RoleBinding per listed namespace and the controller's
103+
# WATCH_NAMESPACES is derived from this list.
105104
namespaces: []
106105

107106
# Service configuration for metrics

0 commit comments

Comments
 (0)