fix: adjust datasource and encoding of grafana dashboard tooling

This commit is contained in:
Stefan Reimer 2021-04-25 21:24:39 +02:00
parent 4bf189a1e9
commit af5092475d
2 changed files with 22 additions and 3 deletions

View File

@ -13,6 +13,22 @@ config_file = sys.argv[1]
configmap_file = sys.argv[2] configmap_file = sys.argv[2]
def traverse_json(obj):
if isinstance(obj, dict):
for k, v in obj.items():
# CloudBender::StackRef
if k == "datasource" and v:
obj[k] = "$datasource"
if isinstance(v, dict) or isinstance(v, list):
traverse_json(v)
elif isinstance(obj, list):
for k in obj:
if isinstance(k, dict) or isinstance(k, list):
traverse_json(k)
# read config file # read config file
with open(config_file, 'r') as yaml_contents: with open(config_file, 'r') as yaml_contents:
config = yaml.safe_load(yaml_contents.read()) config = yaml.safe_load(yaml_contents.read())
@ -58,17 +74,18 @@ for b in config['dashboards']:
obj = json.loads(raw_text) obj = json.loads(raw_text)
# Customize each dashboard # replace datasources
traverse_json(obj)
# Set default tim in all charts to 1h # Set default tim in all charts to 1h
obj['time'] = { "from": "now-1h", "to": "now" } obj['time'] = { "from": "now-1h", "to": "now" }
obj['refresh'] = "30s" obj['refresh'] = "30s"
# set tags
if 'tags' in b: if 'tags' in b:
obj['tags'] = b['tags'] obj['tags'] = b['tags']
text = json.dumps(obj, indent=2) text = json.dumps(obj, indent=2)
text = text.replace("{{", "{{`{{").replace("}}", "}}`}}").replace("{{`{{", "{{`{{`}}").replace("}}`}}", "{{`}}`}}")
if 'gzip' in config and config['gzip']: if 'gzip' in config and config['gzip']:
# compress and base64 encode # compress and base64 encode
@ -83,6 +100,8 @@ for b in config['dashboards']:
''' % b ''' % b
configmap += textb64+'\n' configmap += textb64+'\n'
else: else:
# encode otherwise helm will mess with raw json
text = text.replace("{{", "{{`{{").replace("}}", "}}`}}").replace("{{`{{", "{{`{{`}}").replace("}}`}}", "{{`}}`}}")
text = textwrap.indent(text, ' '*4) text = textwrap.indent(text, ' '*4)
configmap += ''' %(name)s.json: configmap += ''' %(name)s.json:
''' % b ''' % b

View File

@ -8,4 +8,4 @@ metadata:
data: data:
script.sh: |- script.sh: |-
#!/bin/sh #!/bin/sh
/bin/gunzip -k /tmp/dashboards/*/*.gz /bin/gunzip -f -k /tmp/dashboards/*/*.gz