Automate the Dashboard Import

I want to some thoughts on my scenario:

  1. I have an application that pushedsmetrics to prometheus and these metrics are put into dashboards in Grafana
  2. So the usual process I follow is deploy prometheus(push mode enabled) and then deploy grafana, and the create a data source to that prometheus so that grafana has the datasource in place
  3. Now I have dashboards that use these metrics.

My requirement now is to automate the dashboard/datasource imports, I do not usually download the charts and make changes rather I deploy them individually through helm and change the values as needed.

My understanding as of now (and correct me if I am wrong) is that In order for the dashboards to be imported , I definitely need to download the grafana chart and make changes to it and then apply.
But is there a way or Can I utilize “dashboardProviders” “dashboardConfigMaps” method to add the dashboard file and the configmap to my application (and by this time Grafana is already installed/deployed using helm) so that by the time I deploy my application, grafana is already present (deployed) and will consume the dashboard from my application chart/templates folder and import the dashboards.

Do you think this scenario is even a possible one or the only way to go is downloading the Grafana chart and making changes manually.

Any input is highly appreciated !!

Why do you need to edit the dashboard it so often?

So I got it to work using the below

  1. The dashboardProviders and Dashboard
  2. The dashboardsProviders and Configmap reference
  3. Side car container for real time checking and updating the dashboards

I will put them here for future reference in case,

# Setup Data Source (prometheus)
  datasources:
    datasources.yaml:
      apiVersion: 1
      datasources:
        - name: Prometheus
          type: prometheus
          url: http://prometheus-server
          access: proxy
          isDefault: true
  dashboardProviders:
    dashboardproviders.yaml:
     apiVersion: 1
     providers:
      - name: 'app1'
        orgId: 1
        folder: ''
        type: file
        disableDeletion: false
        updateIntervalSeconds: 10 
        allowUiUpdates: true
        editable: true
        options:
          path: /var/lib/grafana/dashboards/app1
      - name: 'app2'
        orgId: 1
        folder: ''
        type: file
        disableDeletion: false
        updateIntervalSeconds: 10
        allowUiUpdates: true
        editable: true
        options:
          path: /var/lib/grafana/dashboards/app2
  dashboards:
    app1:
      app1-dashboard:
        url: http://
    app2:
      app2-dashboard:
        url: http://
# Setup Data Source (prometheus)
  datasources:
    datasources.yaml:
      apiVersion: 1
      datasources:
        - name: Prometheus
          type: prometheus
          url: http://prometheus-server
          access: proxy
          isDefault: true
  dashboardProviders:
    dashboardproviders.yaml:
     apiVersion: 1
     providers:
      - name: 'app1'
        orgId: 1
        folder: ''
        type: file
        disableDeletion: false
        updateIntervalSeconds: 10 
        allowUiUpdates: true
        editable: true
        options:
          path: /var/lib/grafana/dashboards/app1
      - name: 'app2'
        orgId: 1
        folder: ''
        type: file
        disableDeletion: false
        updateIntervalSeconds: 10
        allowUiUpdates: true
        editable: true
        options:
          path: /var/lib/grafana/dashboards/app2
  dashboardsConfigMaps:
    fid: "app1-dashboard"
    zookeeper: "app2-dashboard"
# Setup Data Source (prometheus)
  datasources:
    datasources.yaml:
      apiVersion: 1
      datasources:
        - name: Prometheus
          type: prometheus
          url: http://prometheus-server
          access: proxy
          isDefault: true
# Setup dashboards
  dashboardProviders:
    dashboardproviders.yaml:
     apiVersion: 1
     providers:
      - name: 'app1'
        orgId: 1
        folder: ''
        type: file
        disableDeletion: false
        updateIntervalSeconds: 10 
        allowUiUpdates: true
        editable: true
        options:
          path: /var/lib/grafana/dashboards/app1
      - name: 'app2'
        orgId: 1
        folder: ''
        type: file
        disableDeletion: false
        updateIntervalSeconds: 10
        allowUiUpdates: true
        editable: true
        options:
          path: /var/lib/grafana/dashboards/app2

  securityContext:
    runAsUser: 0
    runAsGroup: 0
    fsGroup: 0
  rbac:
    extraRoleRules:
      # Allow k8s-sidecar image to read ConfigMap objects in same namespace
      - apiGroups: [""]
        resources: ["configmaps"]
        verbs: ["get", "watch", "list"]
  
  extraContainers: |
    - name: collect-dashboard-configmaps-in-directory
      image: kiwigrid/k8s-sidecar:latest
      volumeMounts:
        - name: collection
          mountPath: /tmp/collection
      env:
        - name: LABEL
          value: "dashboard-collect"
        - name: LABEL_VALUE
          value: "grafana-dashboard"
        - name: FOLDER
          value: /tmp/collection
        - name: RESOURCE
          value: configmap
  
  extraVolumeMounts:
    - name: collection
      mountPath: /var/lib/grafana/dashboards/default
      readOnly: true

For (3) the config map should have a label = dashboard-collect

All of these work and are useful in different situations.

1 Like

it is requirement for the application as the contents and metrics keep changing constantly

1 Like