Skip to content

Commit 9911dbc

Browse files
authored
Merge pull request #229 from mittal-ishaan/openshift_in_cluster_prom
Add kubeRBACProxy property to support querying in cluster prometheus in openshift
2 parents 2485c35 + 2d48654 commit 9911dbc

File tree

7 files changed

+81
-1
lines changed

7 files changed

+81
-1
lines changed

charts/opencost/Chart.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ keywords:
99
- finops
1010
- monitoring
1111
- opencost
12-
version: 1.42.3
12+
version: 1.43.0
1313
maintainers:
1414
- name: mattray
1515
url: https://mattray.dev

charts/opencost/templates/_helpers.tpl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,15 @@ Check that either thanos external or internal is defined
120120
{{- end -}}
121121
{{- end -}}
122122

123+
{{/*
124+
Fail if both kube-rbac-proxy and bearer token are set
125+
*/}}
126+
{{- define "kubeRBACProxyBearerTokenCheck" -}}
127+
{{- if and .Values.opencost.prometheus.kubeRBACProxy .Values.opencost.prometheus.bearer_token }}
128+
{{- fail "Both kubeRBACProxy and bearer_token are set. Please specify only one." -}}
129+
{{- end -}}
130+
{{- end -}}
131+
123132
{{/*
124133
Check that the config is valid
125134
*/}}

charts/opencost/templates/clusterrolebinding.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,27 @@ subjects:
1515
- kind: ServiceAccount
1616
name: {{ template "opencost.serviceAccountName" . }}
1717
namespace: {{ include "opencost.namespace" . }}
18+
---
1819
{{- end }}
20+
{{- if .Values.opencost.prometheus.createMonitoringClusterRoleBinding }}
21+
apiVersion: rbac.authorization.k8s.io/v1
22+
kind: ClusterRoleBinding
23+
metadata:
24+
name: {{ include "opencost.fullname" . }}-operator
25+
labels: {{- include "opencost.labels" . | nindent 4 }}
26+
{{- with .Values.annotations }}
27+
annotations: {{- toYaml . | nindent 4 }}
28+
{{- end }}
29+
roleRef:
30+
# Grant the OpenCost ServiceAccount the cluster-monitoring-view role to enable it to query a KUBE_RBAC_PROXY enabled Prometheus.
31+
# This is necessary for OpenCost to get access and query the in-cluster Prometheus instance using its service account token.
32+
# https://docs.redhat.com/en/documentation/openshift_container_platform/4.2/html/monitoring/cluster-monitoring#monitoring-accessing-prometheus-alerting-ui-grafana-using-the-web-console_accessing-prometheus
33+
apiGroup: rbac.authorization.k8s.io
34+
kind: ClusterRole
35+
name: cluster-monitoring-view
36+
subjects:
37+
- kind: ServiceAccount
38+
name: {{ template "opencost.serviceAccountName" . }}
39+
namespace: {{ include "opencost.namespace" . }}
40+
{{- end }}
41+
---

charts/opencost/templates/deployment.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{{- include "isPrometheusConfigValid" . }}
2+
{{- include "kubeRBACProxyBearerTokenCheck" . }}
23
apiVersion: apps/v1
34
kind: Deployment
45
metadata:
@@ -166,6 +167,10 @@ spec:
166167
name: {{ .Values.opencost.prometheus.existingSecretName | default (include "opencost.prometheus.secretname" .) }}
167168
key: {{ .Values.opencost.prometheus.bearer_token_key }}
168169
{{- end }}
170+
{{- if .Values.opencost.prometheus.kubeRBACProxy }}
171+
- name: KUBE_RBAC_PROXY_ENABLED
172+
value: {{ (quote .Values.opencost.prometheus.kubeRBACProxy) }}
173+
{{- end }}
169174
{{- if and .Values.opencost.exporter.persistence.enabled .Values.opencost.exporter.csv_path }}
170175
- name: EXPORT_CSV_FILE
171176
value: {{ .Values.opencost.exporter.csv_path | quote }}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{{- if (.Values.opencost.prometheus.createMonitoringResourceReaderRoleBinding) }}
2+
apiVersion: rbac.authorization.k8s.io/v1
3+
kind: RoleBinding
4+
metadata:
5+
namespace: {{ include "opencost.namespace" . }}
6+
name: {{ include "opencost.fullname" . }}-reader
7+
subjects:
8+
- kind: ServiceAccount
9+
name: {{ .Values.opencost.prometheus.monitoringServiceAccountName | quote }}
10+
namespace: {{ .Values.opencost.prometheus.monitoringServiceAccountNamespace | quote }}
11+
roleRef:
12+
kind: Role
13+
name: {{ include "opencost.fullname" . }}-reader
14+
apiGroup: rbac.authorization.k8s.io
15+
{{- end -}}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{{- if (.Values.opencost.prometheus.createMonitoringResourceReaderRoleBinding) }}
2+
apiVersion: rbac.authorization.k8s.io/v1
3+
kind: Role
4+
metadata:
5+
namespace: {{ include "opencost.namespace" . }}
6+
name: {{ include "opencost.fullname" . }}-reader
7+
rules:
8+
- apiGroups:
9+
- ''
10+
resources:
11+
- "pods"
12+
- "services"
13+
- "endpoints"
14+
verbs:
15+
- list
16+
- watch
17+
{{- end -}}

charts/opencost/values.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,17 @@ opencost:
333333
# -- Prometheus Bearer token
334334
bearer_token: ""
335335
bearer_token_key: DB_BEARER_TOKEN
336+
# -- If true, opencost will use kube-rbac-proxy to authenticate with in cluster Prometheus for openshift
337+
kubeRBACProxy: false
338+
# OPTIONAL. The following configs only to be enabled when using a Prometheus instance already installed in the cluster.
339+
# -- If true, the helm chart will create a ClusterRoleBinding to grant the OpenCost ServiceAccount access to query Prometheus.
340+
createMonitoringClusterRoleBinding: false
341+
# -- If true, create a Role and RoleBinding to allow Prometheus to list and watch OpenCost resources.
342+
createMonitoringResourceReaderRoleBinding: false
343+
# -- Name of the Prometheus serviceaccount to bind to the Resource Reader Role Binding.
344+
monitoringServiceAccountName: prometheus-k8s
345+
# -- Namespace of the Prometheus serviceaccount to bind to the Resource Reader Role Binding.
346+
monitoringServiceAccountNamespace: openshift-monitoring
336347
external:
337348
# -- Use external Prometheus (eg. Grafana Cloud)
338349
enabled: false

0 commit comments

Comments
 (0)