Skip to content

Commit 86b96b0

Browse files
committed
Limit the Prometheus role to two namespaces
Unless clusterRole is specified, the Prometheus role will be restricted to scraping from only a single namespace. This fixes issue: #717 where I user complained that they did not want to create a ClusterRole in their cluster. It has been tested with k3d and K8s 1.19 with and without the --set clusterRole=true flag passed into the faas-netes helm chart. The second Role and RoleBinding needed a different name to the ones in the primary namespace in order for the RBAC error to go away in Prometheus. Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
1 parent 2c470fc commit 86b96b0

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

chart/openfaas/templates/prometheus-rbac.yaml

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{{- $functionNs := default .Release.Namespace .Values.functionNamespace }}
22
{{- if .Values.prometheus.create }}
3+
34
---
45
apiVersion: v1
56
kind: ServiceAccount
@@ -13,6 +14,9 @@ metadata:
1314
heritage: {{ .Release.Service }}
1415
release: {{ .Release.Name }}
1516
---
17+
18+
{{- if .Values.clusterRole }}
19+
1620
apiVersion: rbac.authorization.k8s.io/v1
1721
kind: ClusterRole
1822
metadata:
@@ -72,4 +76,87 @@ subjects:
7276
name: {{ .Release.Name }}-prometheus
7377
namespace: {{ .Release.Namespace | quote }}
7478
{{- end }}
79+
80+
{{- else -}}
81+
82+
apiVersion: rbac.authorization.k8s.io/v1
83+
kind: Role
84+
metadata:
85+
name: {{ .Release.Name }}-prometheus
86+
labels:
87+
app: {{ template "openfaas.name" . }}
88+
chart: {{ .Chart.Name }}-{{ .Chart.Version }}
89+
component: prometheus
90+
heritage: {{ .Release.Service }}
91+
release: {{ .Release.Name }}
92+
rules:
93+
- apiGroups: [""]
94+
resources:
95+
- services
96+
- endpoints
97+
- pods
98+
verbs: ["get", "list", "watch"]
99+
---
100+
apiVersion: rbac.authorization.k8s.io/v1
101+
kind: RoleBinding
102+
metadata:
103+
name: {{ .Release.Name }}-prometheus
104+
namespace: {{ .Release.Namespace | quote }}
105+
labels:
106+
app: {{ template "openfaas.name" . }}
107+
chart: {{ .Chart.Name }}-{{ .Chart.Version }}
108+
component: prometheus
109+
heritage: {{ .Release.Service }}
110+
release: {{ .Release.Name }}
111+
roleRef:
112+
apiGroup: rbac.authorization.k8s.io
113+
kind: Role
114+
name: {{ .Release.Name }}-prometheus
115+
subjects:
116+
- kind: ServiceAccount
117+
name: {{ .Release.Name }}-prometheus
118+
namespace: {{ .Release.Namespace | quote }}
119+
{{- if ne $functionNs (.Release.Namespace | toString) }}
120+
---
121+
apiVersion: rbac.authorization.k8s.io/v1
122+
kind: Role
123+
metadata:
124+
name: {{ .Release.Name }}-prometheus-fn
125+
namespace: {{ $functionNs | quote }}
126+
labels:
127+
app: {{ template "openfaas.name" . }}
128+
chart: {{ .Chart.Name }}-{{ .Chart.Version }}
129+
component: prometheus
130+
heritage: {{ .Release.Service }}
131+
release: {{ .Release.Name }}
132+
rules:
133+
- apiGroups: [""]
134+
resources:
135+
- services
136+
- endpoints
137+
- pods
138+
verbs: ["get", "list", "watch"]
139+
---
140+
apiVersion: rbac.authorization.k8s.io/v1
141+
kind: RoleBinding
142+
metadata:
143+
name: {{ .Release.Name }}-prometheus-fn
144+
namespace: {{ $functionNs | quote }}
145+
labels:
146+
app: {{ template "openfaas.name" . }}
147+
chart: {{ .Chart.Name }}-{{ .Chart.Version }}
148+
component: prometheus
149+
heritage: {{ .Release.Service }}
150+
release: {{ .Release.Name }}
151+
roleRef:
152+
apiGroup: rbac.authorization.k8s.io
153+
kind: Role
154+
name: {{ .Release.Name }}-prometheus-fn
155+
subjects:
156+
- kind: ServiceAccount
157+
name: {{ .Release.Name }}-prometheus
158+
namespace: {{ .Release.Namespace | quote }}
159+
{{- end }}
160+
75161
{{- end }}
162+
{{- end }}

0 commit comments

Comments
 (0)