Last active
August 29, 2020 07:35
-
-
Save guitarrapc/52ace43177d651910a0c1c2b5a980183 to your computer and use it in GitHub Desktop.
Generate Configmap for each dashboard json based on file name.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apiVersion: v1 | |
kind: ConfigMap | |
metadata: | |
name: dashboard | |
labels: | |
grafana_dashboard: 1 | |
data: | |
dashboard.json: | | |
{ | |
"id": null, | |
"uid": "cLV5GDCkz", | |
"title": "New dashboard", | |
"tags": [], | |
"style": "dark", | |
"timezone": "browser", | |
"editable": true, | |
"hideControls": false, | |
"graphTooltip": 1, | |
"panels": [], | |
"time": { | |
"from": "now-6h", | |
"to": "now" | |
}, | |
"timepicker": { | |
"time_options": [], | |
"refresh_intervals": [] | |
}, | |
"templating": { | |
"list": [] | |
}, | |
"annotations": { | |
"list": [] | |
}, | |
"refresh": "5s", | |
"schemaVersion": 17, | |
"version": 0, | |
"links": [] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"id": null, | |
"uid": "cLV5GDCkz", | |
"title": "New dashboard", | |
"tags": [], | |
"style": "dark", | |
"timezone": "browser", | |
"editable": true, | |
"hideControls": false, | |
"graphTooltip": 1, | |
"panels": [], | |
"time": { | |
"from": "now-6h", | |
"to": "now" | |
}, | |
"timepicker": { | |
"time_options": [], | |
"refresh_intervals": [] | |
}, | |
"templating": { | |
"list": [] | |
}, | |
"annotations": { | |
"list": [] | |
}, | |
"refresh": "5s", | |
"schemaVersion": 17, | |
"version": 0, | |
"links": [] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# pick script dir | |
here=$(cd $(dirname $0); pwd) | |
# dashboard counter | |
i=1 | |
# increment counter | |
function incrementCounter() { | |
((i=i+1)) | |
} | |
# replace space to - for k8s name restriction | |
function string:replaceSpace() { | |
echo "${1}" | sed "s/ /${2}/g" | |
} | |
function path:getFiles() { | |
(cd "${here}" && ls ${1} | cat) | |
} | |
# pick filename w/o extensions | |
function file:getName() { | |
echo "${1%.*}" | sed "s/ /-/g" | |
} | |
# add indent | |
function file:addIndentEachline() { | |
cat "${here}/${1}" | sed 's/^/ /g' | |
} | |
function genConfigmap() { | |
local file="${1}" | |
local replaced=$(string:replaceSpace "${file}" "-") | |
local name=$(file:getName "${replaced}") | |
local json=$(file:addIndentEachline "${file}") | |
cat <<EOF > "${here}/configmap-ds${i}.yaml" | |
apiVersion: v1 | |
kind: ConfigMap | |
metadata: | |
name: grafana-dashboard-${name} | |
labels: | |
grafana_dashboard: "1" | |
data: | |
${replaced}: | | |
${json} | |
EOF | |
} | |
# prepare | |
ORG_IFS=$IFS | |
IFS=$(echo -en "\n\b") | |
# gen | |
for file in $(path:getFiles "*.json"); do | |
genConfigmap "${file}" | |
incrementCounter | |
done | |
# restore | |
IFS=$ORG_IFS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
. ./gen-configmap-dashboards.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sidecar: | |
dashboards: | |
enabled: true |
You should use Kustomization instead of this script.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
before
after