Create a configmap per dashboard

I the dashboard files got too large (>2Mb) Kubernetes was rejecting
the configmap.

Now each dashboard is installed with an own configmap. A sidecar container
is used to register these dashboards with Grafana.

Change-Id: I84062d6e2ac7dc2669945b54575bf239a25900a4
diff --git a/charts/grafana/grafana.yaml b/charts/grafana/grafana.yaml
index e48033e..192dc47 100644
--- a/charts/grafana/grafana.yaml
+++ b/charts/grafana/grafana.yaml
@@ -337,20 +337,7 @@
 ##
 ## `path` must be /var/lib/grafana/dashboards/<provider_name>
 ##
-dashboardProviders:
-  dashboardproviders.yaml:
-    apiVersion: 1
-    providers:
-    - name: 'gerrit'
-      orgId: 1
-      folder: ''
-      type: file
-      disableDeletion: true
-      editable: #@ data.values.grafana.dashboards.editable
-      updateIntervalSeconds: 60
-      allowUiUpdates: #@ data.values.grafana.dashboards.editable
-      options:
-        path: /var/lib/grafana/dashboards/gerrit
+dashboardProviders: {}
 
 ## Configure grafana dashboard to import
 ## NOTE: To use dashboards you must also enable/configure dashboardProviders
@@ -383,8 +370,7 @@
 ##   example-dashboard.json: |
 ##     RAW_JSON
 ##
-dashboardsConfigMaps:
-  gerrit: "grafana-dashboards"
+dashboardsConfigMaps: {}
 
 ## Grafana's primary configuration
 ## NOTE: values in map will be converted to ini format
@@ -459,14 +445,14 @@
   # skipTlsVerify Set to true to skip tls verification for kube api calls
   # skipTlsVerify: true
   dashboards:
-    enabled: false
+    enabled: true
     ## Method to use to detect ConfigMap changes. With WATCH the sidecar will do a WATCH requests, with SLEEP it will list all ConfigMaps, then sleep for 60 seconds.
     watchMethod: WATCH
     SCProvider: true
     # label that the configmaps with dashboards are marked with
     label: grafana_dashboard
     # folder in the pod that should hold the collected dashboards (unless `defaultFolderName` is set)
-    folder: /tmp/dashboards
+    folder: /var/dashboards
     # The default folder name, it will create a subfolder under the `folder` and put dashboards in there instead
     defaultFolderName: null
     # If specified, the sidecar will search for dashboard config-maps inside this namespace.
@@ -484,9 +470,9 @@
       # type of the provider
       type: file
       # disableDelete to activate a import-only behaviour
-      disableDelete: false
+      disableDelete: true
       # allow updating provisioned dashboards from the UI
-      allowUiUpdates: false
+      allowUiUpdates: #@ data.values.grafana.dashboards.editable
   datasources:
     enabled: false
     ## Method to use to detect ConfigMap changes. With WATCH the sidecar will do a WATCH requests, with SLEEP it will list all ConfigMaps, then sleep for 60 seconds.
diff --git a/dashboards/gerrit_caches.json b/dashboards/gerrit-caches.json
similarity index 100%
rename from dashboards/gerrit_caches.json
rename to dashboards/gerrit-caches.json
diff --git a/dashboards/gerrit_fetch_clone.json b/dashboards/gerrit-fetch-clone.json
similarity index 100%
rename from dashboards/gerrit_fetch_clone.json
rename to dashboards/gerrit-fetch-clone.json
diff --git a/dashboards/gerrit_overview.json b/dashboards/gerrit-overview.json
similarity index 100%
rename from dashboards/gerrit_overview.json
rename to dashboards/gerrit-overview.json
diff --git a/dashboards/gerrit_process.json b/dashboards/gerrit-process.json
similarity index 100%
rename from dashboards/gerrit_process.json
rename to dashboards/gerrit-process.json
diff --git a/dashboards/gerrit_queues.json b/dashboards/gerrit-queues.json
similarity index 100%
rename from dashboards/gerrit_queues.json
rename to dashboards/gerrit-queues.json
diff --git a/dashboards/gerrit_replication.json b/dashboards/gerrit-replication.json
similarity index 100%
rename from dashboards/gerrit_replication.json
rename to dashboards/gerrit-replication.json
diff --git a/install.sh b/install.sh
index aea85c1..fe44bbd 100755
--- a/install.sh
+++ b/install.sh
@@ -74,6 +74,23 @@
   sops --set "$COMPONENT['htpasswd'] \"$HTPASSWD\"" $TMP_CONFIG
 }
 
+function addDashboards() {
+  for dashboard in dashboards/*; do
+    local DASHBOARD_NAME="${dashboard%.json}"
+    local DASHBOARD_NAME="${DASHBOARD_NAME#"dashboards/"}"
+
+    kubectl create configmap $DASHBOARD_NAME \
+      --from-file=$dashboard \
+      --dry-run=true \
+      --namespace=$NAMESPACE \
+      -o yaml > $OUTPUT/dashboards/$DASHBOARD_NAME.dashboard.yaml
+
+    yq w -i $OUTPUT/dashboards/$DASHBOARD_NAME.dashboard.yaml \
+      metadata.labels.grafana_dashboard $DASHBOARD_NAME
+  done
+}
+
+
 function runYtt() {
   ytt \
     -f charts/namespace.yaml \
@@ -100,18 +117,16 @@
   echo -e "#@data/values\n---\n$(sops -d $TMP_CONFIG)" | runYtt -
 fi
 
-# Create configmap with dashboards
-kubectl create configmap grafana-dashboards \
-  --from-file=./dashboards \
-  --dry-run=true \
-  --namespace=$NAMESPACE \
-  -o yaml > $OUTPUT/configuration/dashboards.cm.yaml
+# Create configmaps with dashboards
+mkdir -p $OUTPUT/dashboards
+addDashboards
 
 test -n "$DRYRUN" && exit 0
 
 # Install loose components
 kubectl apply -f $OUTPUT/namespace.yaml
 kubectl apply -f $OUTPUT/configuration
+kubectl apply -f $OUTPUT/dashboardsq
 kubectl apply -f $OUTPUT/storage
 
 # Add Loki helm repository