Using jsonnet for grafana dashboard

1.4k views Asked by At

I am trying jsonnet (my-custom-grafana.jsonnet) for grafana dashboard.I tried below code.

local grafana = import 'grafonnet/grafana.libsonnet';
local dashboard = grafana.dashboard;
local row = grafana.row;
local prometheus = grafana.prometheus;
local template = grafana.template;
local graphPanel = grafana.graphPanel;local kp = (import 'kube-prometheus/kube-prometheus.libsonnet') + {
  _config+:: {
    namespace: 'monitoring',
  },
  grafana+:: {
    dashboards+:: {
      'cluster-autoscaler-dashboard.json': (import 'grafana-cluster-autoscaler.json'),
  },
  },
};{ ['00namespace-' + name]: kp.kubePrometheus[name] for name in std.objectFields(kp.kubePrometheus) } +
{ ['0prometheus-operator-' + name]: kp.prometheusOperator[name] for name in std.objectFields(kp.prometheusOperator) } +
{ ['node-exporter-' + name]: kp.nodeExporter[name] for name in std.objectFields(kp.nodeExporter) } +
{ ['kube-state-metrics-' + name]: kp.kubeStateMetrics[name] for name in std.objectFields(kp.kubeStateMetrics) } +
{ ['alertmanager-' + name]: kp.alertmanager[name] for name in std.objectFields(kp.alertmanager) } +
{ ['prometheus-' + name]: kp.prometheus[name] for name in std.objectFields(kp.prometheus) } +
{ ['grafana-' + name]: kp.grafana[name] for name in std.objectFields(kp.grafana) }

Then I executed,

jsonnet -J vendor -m manifests "my-custom-grafana.jsonnet" | xargs -I{} sh -c 'cat {} | gojsontoyaml > {}.yaml' -- {}

I dont have any error during this execution but I dont see any files under manifests getting updated with this grafana dashboard update.I had tried the same procedure with prometheus rules and I found manifests/prometheus-rules.yaml got updated and I was able to deploy the yaml file without issues. But with the grafana dashboard I am not sure which file gets updated and how to deploy it.Could anyone help on this please?

1

There are 1 answers

0
Rad4 On

I got this working by removing -m manifests.

jsonnet -J vendor my-custom-grafana.jsonnet > grafana-test.json

However it would be great if someone could validate the below steps..

  1. Custom grafana dashboard (Eg: cluster autoscaler dashboard) is added in a jsonnet file. PFA my-custom-grafana.jsonnet. Note: import 'grafana-cluster-autoscaler.json' --> value of Configmap "data" key

  2. Below command executed to compile the jsonnet,

 jsonnet -J vendor my-custom-grafana.jsonnet > grafana-test.json
  1. Converting json to yaml,
cat grafana-test.json|gojsontoyaml > grafana-test.yaml
  1. The yaml file contains all the resources such as deployment,services,namespace,etc,. As we have Prometheus operator,it has already deployed all the resources. And also "grafana-dashboardDefinitions" contains all the dashboards deployed by grafana since we have enabled "defaultDashboardsEnabled: true"

  2. So I have extracted only the ConfigMap related to "grafana-dashboard-cluster-autoscaler-dashboard" from grafana-test.yaml and deployed it.

Cluster Autoscaler dashboard is now available in grafana.