Patch sourced dashboard content
Shows how to use spec.patch to modify a dashboard’s JSON model with jq scripts.
Unlike spec.variables/spec.datasources/spec.envs, which target specific, purpose-built substitution points (template variable defaults, ${...} placeholders, and env var interpolation, respectively), spec.patch allows arbitrary modification of the dashboard model.
It runs last, after those other mechanisms have already resolved.
An example use case is to replace the tags for a dashboard whose JSON comes from an external source (grafanaCom, remote url, oci, etc…).
Each entry in scripts is a jq expression evaluated against the model in order, using the previous script’s output.
The spec.patch.env array makes secret/configmap/Grafana instance values available to those scripts as env.NAME.
spec.patch.env[].valueFrom.grafanaRef selects a field off the matching Grafana instance (e.g. metadata.name).
Unlike secretKeyRef/configMapKeyRef, a grafanaRef value is resolved separately for each matching Grafana instance, so the patched dashboard content can vary across instances.
The operator protects the uid field it manages itself from patch scripts.
If a script changes it, the operator restores the original value.
---
apiVersion: grafana.integreatly.org/v1beta1
kind: Grafana
metadata:
name: grafana
labels:
dashboards: "grafana"
spec:
config:
log:
mode: "console"
auth:
disable_login_form: "false"
security:
admin_user: root
admin_password: secret
---
apiVersion: grafana.integreatly.org/v1beta1
kind: GrafanaDashboard
metadata:
name: grafanadashboard-patch
spec:
instanceSelector:
matchLabels:
dashboards: "grafana"
# Externally sourced, with its own tags that we can't modify in-line.
grafanaCom:
id: 1860
# Patch scripts are jq expressions that evaluate against the resolved dashboard JSON.
# They run in order, each seeing the output of the previous script.
# This script amends the existing tags with an "override" tag and the name of
# the Grafana instance it was applied to. grafanaRef is resolved separately for
# each matching Grafana instance, so the tag can vary across instances.
patch:
env:
- name: INSTANCE_NAME
valueFrom:
grafanaRef:
fieldPath: metadata.name
scripts:
- '.tags = ((.tags // []) + ["override", "instance-" + env.INSTANCE_NAME]) | .tags |= unique'