Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Last active August 29, 2020 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guitarrapc/52ace43177d651910a0c1c2b5a980183 to your computer and use it in GitHub Desktop.
Save guitarrapc/52ace43177d651910a0c1c2b5a980183 to your computer and use it in GitHub Desktop.
Generate Configmap for each dashboard json based on file name.
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": []
}
{
"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": []
}
#!/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
. ./gen-configmap-dashboards.sh
sidecar:
dashboards:
enabled: true
@guitarrapc
Copy link
Author

before

$ ls
dashboard.json gen-configmap-dashboards.sh

after

ls 
configmap-ds1.yaml dashboard.json gen-configmap-dashboards.sh

@guitarrapc
Copy link
Author

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