Skip to content
This repository was archived by the owner on Sep 10, 2025. It is now read-only.

Commit 3ddf349

Browse files
committed
🔧 Parameterize the automatic creation of Grafana dashboards
1 parent ae78762 commit 3ddf349

File tree

3 files changed

+130
-124
lines changed

3 files changed

+130
-124
lines changed

helm/templates/grafana-seeding.yaml

Lines changed: 0 additions & 124 deletions
This file was deleted.

helm/templates/grafana.yaml

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,132 @@ spec:
9898
targetPort: 3000
9999
selector:
100100
app: grafana
101+
---
102+
apiVersion: batch/v1
103+
kind: Job
104+
metadata:
105+
name: grafana-init
106+
namespace: {{ .Values.namespace }}
107+
labels:
108+
app: grafana-init
109+
spec:
110+
template:
111+
metadata:
112+
labels:
113+
app: grafana-init
114+
spec:
115+
{{ toYaml .Values.podDefaults | nindent 6 }}
116+
containers:
117+
- name: seed
118+
image: {{ .Values.docker.registry }}/cloud-native-workstation-initializers:{{ .Values.docker.tag }}
119+
imagePullPolicy: Always
120+
command: ["/bin/bash", "-c"]
121+
args:
122+
- |
123+
/opt/seed.sh || exit $?
124+
env:
125+
- name: USERNAME
126+
value: {{ .Values.authentication.username }}
127+
- name: PASSWORD
128+
value: {{ .Values.authentication.password }}
129+
securityContext:
130+
readOnlyRootFilesystem: true
131+
runAsNonRoot: true
132+
resources: {{- toYaml .Values.components.initializers.resources | nindent 10 }}
133+
volumeMounts:
134+
- name: grafana-seed-sh
135+
mountPath: /opt/seed.sh
136+
subPath: seed.sh
137+
volumes:
138+
- name: grafana-seed-sh
139+
configMap:
140+
name: grafana-seed-sh
141+
defaultMode: 0555
142+
restartPolicy: Never
143+
backoffLimit: 32
144+
---
145+
apiVersion: v1
146+
kind: ConfigMap
147+
metadata:
148+
name: grafana-seed-sh
149+
namespace: {{ .Values.namespace }}
150+
data:
151+
seed.sh: |
152+
#!/bin/bash
153+
# https://community.grafana.com/t/is-there-an-equivalent-http-api-to-import-a-dashboard-from-grafana-com/9581/2
154+
grafana_host="http://grafana"
155+
grafana_cred="$USERNAME:$PASSWORD"
156+
grafana_datasource="Prometheus"
157+
158+
paths=({{ join " " .Values.components.grafana.dashboards.prometheus.plugin }} )
159+
for path in "${paths[@]}"; do
160+
echo "Processing $path: "
161+
curl -s -k -u "$grafana_cred" -XPOST -H "Accept: application/json" \
162+
-H "Content-Type: application/json" \
163+
-d "{
164+
\"overwrite\":true, \
165+
\"pluginId\":\"prometheus\", \
166+
\"path\":\"$path\",
167+
\"inputs\":[{ \
168+
\"name\":\"*\", \
169+
\"type\":\"datasource\", \
170+
\"pluginId\":\"prometheus\", \
171+
\"value\":\"$grafana_datasource\" \
172+
}] \
173+
}" \
174+
$grafana_host/api/dashboards/import || exit $?
175+
echo ""
176+
done
177+
178+
ds=({{ join " " .Values.components.grafana.dashboards.prometheus.community }} )
179+
for d in "${ds[@]}"; do
180+
echo "Processing $d: "
181+
j=$(curl -s -k -u "$grafana_cred" $grafana_host/api/gnet/dashboards/$d | jq .json)
182+
curl -s -k -u "$grafana_cred" -XPOST -H "Accept: application/json" \
183+
-H "Content-Type: application/json" \
184+
-d "{ \
185+
\"dashboard\":$j, \
186+
\"overwrite\":true, \
187+
\"inputs\":[ \
188+
{ \
189+
\"name\":\"DS_PROMETHEUS\", \
190+
\"type\":\"datasource\", \
191+
\"pluginId\":\"prometheus\", \
192+
\"value\":\"$grafana_datasource\" \
193+
}, \
194+
{ \
195+
\"name\":\"VAR_DATASOURCE\", \
196+
\"type\":\"constant\", \
197+
\"value\":\"$grafana_datasource\" \
198+
} \
199+
] \
200+
}" \
201+
$grafana_host/api/dashboards/import; echo ""
202+
done
101203
{{- if eq .Values.policies.enabled true }}
102204
---
103205
apiVersion: networking.k8s.io/v1
104206
kind: NetworkPolicy
207+
metadata:
208+
name: grafana-init
209+
namespace: cloud-native-workstation
210+
spec:
211+
podSelector:
212+
matchLabels:
213+
app: grafana-init
214+
policyTypes:
215+
- Egress
216+
- Ingress
217+
ingress:
218+
- {}
219+
egress:
220+
- to:
221+
- podSelector:
222+
matchLabels:
223+
app: grafana
224+
---
225+
apiVersion: networking.k8s.io/v1
226+
kind: NetworkPolicy
105227
metadata:
106228
name: grafana
107229
namespace: cloud-native-workstation

helm/values.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,14 @@ components:
153153
cpu: "1"
154154
grafana:
155155
enabled: true
156+
dashboards:
157+
prometheus:
158+
plugin:
159+
- dashboards/prometheus_stats.json
160+
- dashboards/prometheus_2_stats.json
161+
- dashboards/grafana_stats.json
162+
community:
163+
- 13332
156164
resources:
157165
requests:
158166
memory: 0

0 commit comments

Comments
 (0)