0

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?

Rad4
  • 1,936
  • 8
  • 30
  • 50
  • I have trouble reproducing your issue. I tried running this command with simplified Jsonnet and it worked as expected (in manifests there were `.yaml` files and json files with no extension). – sbarzowski Oct 07 '20 at 19:24
  • 1
    Some things to check (1) what does Jsonnet evaluate to, without `-m manifests` and without the following pipe. Is it what you expect? (2) Can you find the JSON files (without the extension) in manifests directory? Is their content what you expect? (3) Double check "stupid" issues like whether you're editing and running the same Jsonnet file and if you are running the command from the right directory. – sbarzowski Oct 07 '20 at 19:28
  • @sbarzowski ..first of all thanks much for trying to reproduce my issue.Thats awesome. I tried by removing -m manifests and I could find the json produced.And I was also able to convert the json to yaml. Now the file has Kubernetes resources definitions alone with lines such as 00namespace-namespace: ,0prometheus-operator- 0alertmanagerCustomResourceDefinition: ... So please let me know how to deploy this yaml file? Is there anythingelse to be done before deploying this yaml file? – Rad4 Oct 07 '20 at 20:11
  • I think when you deploy all your resources normally, the Grafana dashboard will be deployed as well with your config (my understanding is that Grafana dashboard config will be a part of the grafana resource which you can find in manifests/grafana-....). It should just work. You will need to expose Grafana as explained here: https://github.com/prometheus-operator/kube-prometheus#access-the-dashboards to actually see it. My expertise is in Jsonnet, not kube-prometheus of Grafana, so I'm not 100% sure. – sbarzowski Oct 07 '20 at 22:20
  • Thankyou @sbarzowski ..Much appreciated :) – Rad4 Oct 07 '20 at 23:54

1 Answers1

0

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.

Rad4
  • 1,936
  • 8
  • 30
  • 50