feat(grafana): Add dashboard auto-downloader (#41665)

**Description**
<!--
Please include a summary of the change and which issue is fixed. Please
also include relevant motivation and context. List any dependencies that
are required for this change.
-->

Currently, this chart allows provisioning of 3 predefined dashboards.

This is not ideal because:

- It is not flexible (only 3 statically defined in the chart dashboards
are supported)
- It bloats the chart unnecessarily
- Dashboards can get out of date quickly

To fix these shortcomings, I have added a curl init container that
executes a script which is generated during chart rendering based on the
values defined in the new map in the chart values
(`.Values.extraDashboards`). The map allows defining dashboards to be
provisioned both directly from `grafana.com` and via any URL that
returns a valid JSON Grafana dashboard file.

⚒️ Fixes  # <!--(issue)-->

**⚙️ Type of change**

- [x] ⚙️ Feature/App addition
- [ ] 🪛 Bugfix
- [ ] ⚠️ Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] 🔃 Refactor of current code
- [ ] 📜 Documentation Changes

**🧪 How Has This Been Tested?**
<!--
Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce. Please also list any relevant details
for your test configuration
-->

- Rendered the chart manually with the values defined under
`.Values.extraDashboards`.
- Checked that the dashboards are present in Grafana after it starts

**📃 Notes:**

- The Grafana image technically already has `curl` as a command;
however, I chose to use a separate image just for the init container
because `curl` might be removed without notice from the Grafana image in
the future, and I do not want to depend on it being present.
- I have left the old way of deploying dashboards to prevent making this
a breaking change. After this is merged, I will open a new PR for
removing them (that will be marked breaking), and it can be merged when
appropriate.

**✔️ Checklist:**

- [x] ⚖️ My code follows the style guidelines of this project
- [x] 👀 I have performed a self-review of my own code
- [x] #️⃣ I have commented my code, particularly in hard-to-understand
areas
- [ ] 📄 I have made changes to the documentation
- [x] 🧪 I have added tests to this description that prove my fix is
effective or that my feature works
- [x] ⬆️ I increased versions for any altered app according to semantic
versioning
- [x] I made sure the title starts with `feat(chart-name):`,
`fix(chart-name):`, `chore(chart-name):`, `docs(chart-name):` or
`fix(docs):`

** App addition**

If this PR is an app addition please make sure you have done the
following.

- [ ] 🖼️ I have added an icon in the Chart's root directory called
`icon.png`

---

_Please don't blindly check all the boxes. Read them and only check
those that apply.
Those checkboxes are there for the reviewer to see what is this all
about and
the status of this PR with a quick glance._

---------

Signed-off-by: Boemeltrein <130394941+Boemeltrein@users.noreply.github.com>
Signed-off-by: Kjeld Schouten <info@kjeldschouten.nl>
Co-authored-by: Boemeltrein <info@boemeltrein.nl>
Co-authored-by: Boemeltrein <130394941+Boemeltrein@users.noreply.github.com>
Co-authored-by: Kjeld Schouten <info@kjeldschouten.nl>
This commit is contained in:
astro-stan
2025-11-23 13:44:59 +00:00
committed by GitHub
parent 8369807d3e
commit e4ef820b30
10 changed files with 354 additions and 5162 deletions

View File

@@ -37,7 +37,8 @@
"github>trueforge-org/truecharts//.github/renovate/special/customRules.json5",
"github>trueforge-org/truecharts//.github/renovate/special/customVersioning.json5",
"github>trueforge-org/truecharts//.github/renovate/special/customTalosClustertool.json5",
"github>trueforge-org/truecharts//.github/renovate/special/devcontainer.json5"
"github>trueforge-org/truecharts//.github/renovate/special/devcontainer.json5",
"github>trueforge-org/truecharts//.github/renovate/special/grafanadashboards.json5"
],
// Main Renovate configuration that lists package rules for various paths
"packageRules": [

View File

@@ -0,0 +1,38 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"customDatasources": {
"grafana-dashboards": {
"defaultRegistryUrlTemplate": "https://grafana.com/api/dashboards/{{packageName}}",
"format": "json",
"transformTemplates": [
"{\"releases\":[{\"version\": $string(revision)}]}"
]
}
},
"customManagers": [
{
"customType": "regex",
"description": ["Process Grafana dashboards"],
"fileMatch": [
"(^|/)kubernetes/.+\\.ya?ml(?:\\.j2)?$"
],
"matchStrings": [
"depName=\"(?<depName>.*)\"\\n(?<indentation>\\s+)id: (?<packageName>\\d+)\\n.+revision: (?<currentValue>\\d+)"
],
"autoReplaceStringTemplate": "depName=\"{{{depName}}}\"\n{{{indentation}}}id: {{{packageName}}}\n{{{indentation}}}revision: {{{newValue}}}",
"datasourceTemplate": "custom.grafana-dashboards",
"versioningTemplate": "regex:^(?<major>\\d+)$"
}
],
"packageRules": [
{
"addLabels": ["renovate/grafana-dashboard"],
"matchDatasources": ["custom.grafana-dashboards"],
"matchUpdateTypes": ["major"],
"semanticCommitType": "chore",
"semanticCommitScope": "grafana-dashboards",
"commitMessageTopic": "dashboard {{depName}}",
"commitMessageExtra": "( {{currentVersion}} → {{newVersion}} )"
}
]
}

View File

@@ -42,5 +42,4 @@ sources:
- https://grafana.com/
- https://hub.docker.com/r/grafana/grafana
type: application
version: 20.2.1
version: 21.0.0

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,56 @@
---
title: Grafana Dashboards
---
There are multiple options how to add dashboard to Grafana.
From chart version 21, this is extended with the option `dashboards`, which allows automatically loading dashboards from the Grafana marketplace or any other URL that returns a JSON with a Grafana dashboard. Scraping dashboards from configmaps continues to be possible and hasn't changed.
## Adding a dashboard via marketplace
Add the following part to your `.Values`:
```yaml
dashboards:
grafana:
volsync:
enabled: true
failOnError: false
b64content: false
datasource:
- name: "${DS_PROMETHEUS}"
value: Prometheus
- name: "${VAR_REPLICATIONDESTNAME}"
value: ".*-dst|.*-bootstrap"
marketplace:
id: 21356
revision: 3
```
## Adding a dashboard via url
Add the following part to your `.Values`:
```yaml
dashboards:
grafana:
truenas:
enabled: true
failOnError: false
b64content: false
datasource:
- name: "${DS_MIMIR}"
value: Prometheus
url: https://raw.githubusercontent.com/Supporterino/truenas-graphite-to-prometheus/refs/heads/main/dashboards/truenas_scale.json
```
### Some explanation of the values:
- `enabled` turn on/off the dashboard
- `failOnError` if enabled any failure during the download, decoding, or provisioning of the dashboard will cause the Grafana deployment to stop and error out, preventing Grafana from deploying.
- `b64content` automaticly decodes base64 encoded dashboards
- `datasource: { name: "", value: "" }` defines a list of mappings used to replace specific variables within a dashboard's JSON file. You must consult the dashboard's JSON structure to identify which variables need replacement. The variable substitutions must match their exact appearance in the JSON, including any brackets (e.g., `${...}`). Be aware that specific deployment environments, such as Flux, might require escaping certain characters.
- `id` numbered id from https://grafana.com/grafana/dashboards/.
- `revision` numbered revision from https://grafana.com/grafana/dashboards/.
- `url` url where to download the dashboard from.
Note: It have to be `marketplace:` or `url:` both cannot be set at a time per dashboard.

View File

@@ -0,0 +1,104 @@
{{/* Define the dashboard download validation logic */}}
{{- define "grafana.download_dashboards.validate" -}}
{{- $dashboards := .dashboards -}}
{{- range $name, $objectData := $dashboards }}
{{- if $objectData.enabled -}}
{{- if not (or $objectData.url $objectData.marketplace) -}}
{{- fail (printf "Grafana Dashboards - either [url] or [marketplace] must be defined for dashboard [%v]" $name) -}}
{{- end -}}
{{- if and $objectData.url $objectData.marketplace -}}
{{- fail (printf "Grafana Dashboards - only one of [url] or [marketplace] must be defined for dashboard [%v]" $name) -}}
{{- end -}}
{{- if and ($objectData.url) (or (not (kindIs "string" $objectData.url)) (eq (len $objectData.url) 0)) -}}
{{- fail (printf "Grafana Dashboards - [%v.url] must be a non-zero length string" $name) -}}
{{- end -}}
{{- if $objectData.marketplace -}}
{{- if not (kindIs "map" $objectData.marketplace) -}}
{{- fail (printf "Grafana Dashboards - [%v.marketplace] must be a map" $name) -}}
{{- end -}}
{{- if and (not (mustHas (kindOf $objectData.marketplace.id) (list "int" "int64" "float64"))) (or (not (kindIs "string" $objectData.marketplace.id)) (eq (len $objectData.marketplace.id) 0)) -}}
{{- fail (printf "Grafana Dashboards - [%v.marketplace.id] must be an integer or non-zero length string" $name) -}}
{{- end -}}
{{- if and (not (mustHas (kindOf $objectData.marketplace.revision) (list "int" "int64" "float64"))) (or (not (kindIs "string" $objectData.marketplace.revision)) (eq (len $objectData.marketplace.revision) 0)) -}}
{{- fail (printf "Grafana Dashboards - [%v.marketplace.revision] must be an integer or non-zero length string" $name) -}}
{{- end -}}
{{- end -}}
{{- if $objectData.datasource -}}
{{- range $index, $source := $objectData.datasource -}}
{{- if not (kindIs "string" $source.name) -}}
{{- fail (printf "Grafana Dashboards - [%v.marketplace.datasource.%v.name] must be a string" $name $index) -}}
{{- end -}}
{{- if not (kindIs "string" $source.value) -}}
{{- fail (printf "Grafana Dashboards - [%v.marketplace.datasource.%v.value] must be a string" $name $index) -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{/* Define the dashboard downloading script */}}
{{- define "grafana.download_dashboards" -}}
{{- $dashboards := .dashboards -}}
{{- $outputDir := .outputDir -}}
{{- $grafanaApiUrl := "https://grafana.com/api/dashboards/%v/revisions/%v/download" -}}
{{- $requestHeaders := (dict
"Accept" "application/json"
"Content-Type" "application/json;charset=UTF-8") -}}
{{- include "grafana.download_dashboards.validate" (dict "dashboards" $dashboards) -}}
download-dashboards:
enabled: true
data:
download-dashboards.sh: |
#!/usr/bin/env sh
set -eo pipefail
{{- range $name, $objectData := $dashboards }}
{{- if $objectData.enabled -}}
{{ $fileName := (printf "%v/%v.json" $outputDir $name) -}}
{{- $url := "" -}}
{{- if $objectData.url -}}
{{- $url = $objectData.url -}}
{{- else if kindIs "map" $objectData.marketplace -}}
{{- $url = (printf $grafanaApiUrl $objectData.marketplace.id $objectData.marketplace.revision) -}}
{{- end -}}
{{- if $url }}
echo "Downloading '{{ $url }}' into '{{ $fileName }}'"
wget \
{{- range $headerKey, $headerValue := $requestHeaders }}
--header "{{ $headerKey }}: {{ $headerValue }}" \
{{- end }}
-Y on \
-T 60 \
-O - \
{{ $url }} \
{{- if $objectData.b64content }}
| base64 -d \
{{- end }}{{ if $objectData.datasource }}
| jq '
(.. | strings) |=
{{- range $index, $source := $objectData.datasource }}
{{- if eq $index 0 }}
if . == "{{ $source.name }}" then
"{{ $source.value }}"
{{- else }}
elif . == "{{ $source.name }}" then
"{{ $source.value }}"
{{- end }}
{{- if eq $index (sub (len $objectData.datasource) 1) }}
else
.
end
{{- end }}
{{- end }}
' \{{ end }}
> {{ $fileName }}{{if not $objectData.failOnError }} || true{{ end }}
echo
{{- end }}
{{- end -}}
{{- end -}}
{{- end -}}

View File

@@ -1 +1,12 @@
{{ include "tc.v1.common.loader.all" . }}
{{- include "tc.v1.common.loader.init" . }}
{{/* Render generated configmap */}}
{{- $configmap := include "grafana.download_dashboards" (dict
"dashboards" .Values.dashboards.grafana
"outputDir" (index .Values.persistence "sc-dashboard-volume" "mountPath")) | fromYaml -}}
{{- if $configmap -}}
{{ $configmaps := (mustMergeOverwrite .Values.configmap $configmap) }}
{{- $_ := set .Values "configmap" $configmaps -}}
{{- end -}}
{{- include "tc.v1.common.loader.apply" . }}

View File

@@ -7,6 +7,130 @@ sidecarImage:
repository: ghcr.io/kiwigrid/k8s-sidecar
tag: 2.1.2@sha256:5304aa2b45f80a19f84a5e9dfb139c9caeaeecf1f9e507d0bdb9bc1cdb87a58a
# Define extra dashboards to be provisioned
# Supports 2 formats:
# - Importing directly from `grafana.com` via Grafana dashboard ID & revision
# - Importing from any URL that returns a JSON file
#
# Additionally, each configured dashboard can be set to be:
# - base64 decoded
# - have its datasources replaced
#
# See values below for more examples
dashboards:
grafana:
ceph-cluster:
enabled: false
failOnError: false
b64content: false
datasource:
- name: "${DS_PROMETHEUS}"
value: Prometheus
url: https://raw.githubusercontent.com/rook/rook/refs/tags/v1.18.7/deploy/examples/monitoring/grafana/Ceph%20Cluster%20Dashboard.json
ceph-osd-single:
enabled: false
failOnError: false
b64content: false
datasource:
- name: "${DS_PROMETHEUS}"
value: Prometheus
url: https://github.com/rook/rook/raw/refs/tags/v1.18.7/deploy/examples/monitoring/grafana/Ceph%20OSD%20Single%20Dashboard.json
ceph-pools:
enabled: false
failOnError: false
b64content: false
datasource:
- name: "${DS_PROMETHEUS}"
value: Prometheus
url: https://github.com/rook/rook/raw/refs/tags/v1.18.7/deploy/examples/monitoring/grafana/Ceph%20Pools%20Dashboard.json
cert-manager:
enabled: true
failOnError: false
b64content: false
datasource:
- name: "${DS_PROMETHEUS}"
value: Prometheus
marketplace:
# renovate: depName="Cert-manager-Kubernetes"
id: 20842
revision: 3
cnpg:
enabled: false
failOnError: false
b64content: false
datasource:
- name: "${DS_PROMETHEUS}"
value: Prometheus
marketplace:
# renovate: depName="CloudNativePG"
id: 20417
revision: 4
flux-cluster:
enabled: false
failOnError: false
b64content: false
datasource:
- name: "${DS_PROMETHEUS}"
value: Prometheus
url: https://github.com/fluxcd/flux2-monitoring-example/raw/82c37257787bba13a40b17bed9bd8191ee7aa049/monitoring/configs/dashboards/cluster.json
flux-control-plane:
enabled: false
failOnError: false
b64content: false
datasource:
- name: "${DS_PROMETHEUS}"
value: Prometheus
url: https://github.com/fluxcd/flux2-monitoring-example/raw/82c37257787bba13a40b17bed9bd8191ee7aa049/monitoring/configs/dashboards/control-plane.json
metallb:
enabled: false
failOnError: false
b64content: false
datasource:
- name: "${DS_PROMETHEUS}"
value: Prometheus
marketplace:
# renovate: depName="Metallb"
id: 20162
revision: 6
nginx:
enabled: false
failOnError: false
b64content: false
datasource:
- name: "${DS_PROMETHEUS}"
value: Prometheus
url: https://github.com/kubernetes/ingress-nginx/raw/refs/tags/ingress-nginx-3.15.2/deploy/grafana/dashboards/nginx.json
node-feature-discovery:
enabled: false
failOnError: false
b64content: false
datasource:
- name: "${DS_PROMETHEUS}"
value: Prometheus
url: https://github.com/kubernetes-sigs/node-feature-discovery/raw/refs/tags/v0.18.3/examples/grafana-dashboard.json
prometheus-smartctl-exporter:
enabled: false
failOnError: false
b64content: false
datasource:
- name: "${DS_PROMETHEUS}"
value: Prometheus
marketplace:
# renovate: depName="SMARTctl Exporter Dashboard"
id: 22604
revision: 2
traefik:
enabled: false
failOnError: false
b64content: false
datasource:
- name: "${DS_PROMETHEUS}"
value: Prometheus
marketplace:
# renovate: depName="Traefik Official Standalone Dashboard"
id: 17346
revision: 9
securityContext:
container:
readOnlyRootFilesystem: false
@@ -22,6 +146,12 @@ workload:
replicas: 1
strategy: Recreate
podSpec:
initContainers:
download-dashboards:
enabled: true
type: init
imageSelector: alpineImage
command: /download-dashboards.sh
containers:
main:
env:
@@ -237,28 +367,6 @@ configmap:
domain: "{{ if (and .Values.ingress.main.enabled .Values.ingress.main.hosts) }}{{ .Values.ingress.main.hosts | first }}{{ else }}''{{ end }}"
ldap.toml: |-
# nope
# Some included dashboards, which can be enabled when prefered
dashboard-certmanager:
enabled: true
labels:
grafana_dashboard: "1"
data:
dashboard_certmanager.json: >-
{{ .Files.Get "dashboard_certmanager.json" | indent 8 }}
dashboard-traefik:
enabled: false
labels:
grafana_dashboard: "1"
data:
dashboard_traefik.json: >-
{{ .Files.Get "dashboard_traefik.json" | indent 8 }}
dashboard-nginx:
enabled: false
labels:
grafana_dashboard: "1"
data:
dashboard_nginx.json: >-
{{ .Files.Get "dashboard_nginx.json" | indent 8 }}
persistence:
config:
enabled: true
@@ -291,6 +399,17 @@ persistence:
objectName: dashboard-provider
mountPath: /etc/grafana/provisioning/dashboards/sc-dashboardproviders.yaml
subPath: provider.yaml
download-dashboards-config:
enabled: true
type: configmap
objectName: download-dashboards
subPath: download-dashboards.sh
mountPath: /download-dashboards.sh
defaultMode: "0555"
readOnly: true
targetSelector:
main:
download-dashboards: {}
sc-datasource-volume:
enabled: true
type: emptyDir