Skip to content

Commit 060ad46

Browse files
committed
Add Helm dashboard configMap sources + conversion script
Signed-off-by: Eero Tamminen <eero.t.tamminen@intel.com>
1 parent 54dda5f commit 060ad46

4 files changed

Lines changed: 4513 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Generating Grafana dashboard Helm chart configMap templates
2+
3+
User would need to manually load Grafana dashboard JSON file, but
4+
when Helm Dashboard chart installs it to Grafana namespace inside a
5+
suitably labeled configMap, Grafana will load it automatically.
6+
7+
Here are the dashboard JSON spec files used as sources for those
8+
configMaps. After dashboard is updated in Grafana, it can be saved
9+
again to a JSON file here, and the corresponding configMap updated
10+
with the provided conversion script.
11+
12+
Usage:
13+
14+
```
15+
cd ../templates/
16+
../json/convert-dashboard.sh ../json/*.json
17+
```
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#!/bin/sh
2+
#
3+
# Copyright (C) 2025 Intel Corporation
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
set -e
7+
8+
# OPEA CI requires copyright/license for the converted file
9+
COPYRIGHT="Copyright (C) 2025 Intel Corporation"
10+
LICENSE="SPDX-License-Identifier: Apache-2.0"
11+
12+
# Label needed in configMap to get (Helm installed) Grafana to load it as dashboard
13+
LABEL="grafana_dashboard=1"
14+
15+
# needs to be specified so there's something to override
16+
NS="default"
17+
18+
error_exit ()
19+
{
20+
name=${0##*/}
21+
cat << EOF
22+
23+
ERROR: $1!
24+
25+
Convert given Grafana *.json dashboard specs to Grafana dashboard configMap
26+
templates compatible with OPEA Dashoard Helm chart.
27+
28+
Dashboard 'title' and 'uid' are overridden with values composed of
29+
the OPEA Dashboard Helm chart values and file name, similarly to
30+
the produced configMap name and namespace.
31+
32+
Usage:
33+
$name <JSON files>
34+
35+
ERROR: $1!
36+
EOF
37+
exit 1
38+
}
39+
40+
if [ -z "$(which jq)" ]; then
41+
error_exit "'jq' required for dashboard checks, please install 'jq' first"
42+
fi
43+
44+
if [ -z "$(which kubectl)" ]; then
45+
error_exit "'kubectl' required for dashboard conversion, please install 'kubernetes-client' first"
46+
fi
47+
48+
if ! kubectl version; then
49+
error_exit "Broken/missing 'kubectl' cluster config (script does not need it, but kubectl still fails)"
50+
fi
51+
52+
uid=""
53+
title=""
54+
55+
echo
56+
echo "Got following Grafana dashboards:"
57+
for file in "$@"; do
58+
if [ ! -f "$file" ]; then
59+
error_exit "JSON file '$file' does not exist"
60+
fi
61+
if [ "${file%.json}" = "$file" ]; then
62+
error_exit "JSON file '$file' does not exist"
63+
fi
64+
65+
# Both dashboard 'uid' and title needed
66+
uid=$(jq .uid "$file" | tail -1 | tr -d '"')
67+
if [ -z "$uid" ]; then
68+
error_exit "'$file' dashboard has invalid JSON"
69+
elif [ "$uid" = "null" ]; then
70+
error_exit "'$file' dashboard has no 'uid' field (will be replaced with Helm variable)"
71+
fi
72+
73+
# ...but it should have a title.
74+
title=$(jq .title "$file" | tail -1 | tr -d '"')
75+
if [ "$title" = "null" ]; then
76+
error_exit "'$file' dashboard has no 'title' field (will be replaced with Helm variable)"
77+
fi
78+
79+
echo "- file: $file, uid: '$uid', title: '$title'"
80+
done
81+
82+
echo
83+
echo "Converting:"
84+
for file in "$@"; do
85+
base=${file##*/}
86+
name=${base%.json}
87+
dst="configmap-${name}.yaml"
88+
89+
uid=$(jq .uid "$file" | tail -1 | tr -d '"')
90+
title=$(jq .title "$file" | tail -1 | tr -d '"')
91+
92+
# convert to k8s object name ("[a-z0-9][-a-z0-9]*[a-z0-9]"):
93+
# - upper-case -> lowercase, '_' -> '-'
94+
# - drop anything outside [-a-z]
95+
# - drop '-' prefix & suffix and successive '-' chars
96+
k8name=$(echo "$name" | tr A-Z_ a-z- | tr -d -c a-z- | sed -e 's/^-*//' -e 's/-*$//' -e 's/--*/-/g')
97+
98+
echo "- $base -> $dst"
99+
100+
echo "{{- if .Values.$name }}" > "$dst"
101+
102+
echo "# $COPYRIGHT" >> "$dst"
103+
echo "# $LICENSE" >> "$dst"
104+
echo "#" >> "$dst"
105+
echo "# ${0##*/}: $base -> $dst" >> "$dst"
106+
107+
kubectl create cm -n "$NS" --from-file "$file" --dry-run=client -o yaml "$k8name" |\
108+
kubectl label -f- --local --dry-run=client -o yaml "$LABEL" |\
109+
grep -v -e "^ creationTimestamp:" >> "$dst"
110+
111+
echo "{{- end }}" >> "$dst"
112+
113+
# convert JSON content conflicting with Helm to Helm compatible format
114+
# and add suitable Dashboard chart Helm variables to the configMap
115+
sed -i \
116+
-e 's/\({{[a-z]\+}}\)/{{ printf "\1" }}/' \
117+
-e 's/name:.*$/name: {{ include "dashboard.fullname" . }}'"-${k8name}/" \
118+
-e 's/space:.*$/space: {{ .Values.global.prometheusNamespace }}/' \
119+
-e "s/${title}/{{ .Values.prefix }} $name/" \
120+
-e "s/${uid}/opea-"'{{ include "dashboard.fullname" . }}'"-${k8name}/" \
121+
"$dst"
122+
done
123+
124+
echo
125+
echo "DONE!"

0 commit comments

Comments
 (0)