From df3cf36802a0d442a39df7d57e467e401820c1be Mon Sep 17 00:00:00 2001
From: Dmitry Chepurovskiy <dm3ch@dm3ch.net>
Date: Sat, 31 Jul 2021 01:21:45 +0300
Subject: [PATCH] Added postgres grafana datasource creation

---
 .pre-commit-config.yaml                       |  2 +-
 .../files/subgraph-indexing-status.json       | 23 ++++++-------------
 .../templates/_postgres-datasource.tpl        | 17 ++++++++++++++
 .../templates/datasources-secret.yaml         |  8 +++++++
 charts/graph-grafana-dashboards/values.yaml   |  5 ++++
 helmfile/helmfile-infra.yaml                  |  8 ++++---
 6 files changed, 43 insertions(+), 20 deletions(-)
 create mode 100644 charts/graph-grafana-dashboards/templates/_postgres-datasource.tpl
 create mode 100644 charts/graph-grafana-dashboards/templates/datasources-secret.yaml

diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 8f61480..c4521d0 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -6,7 +6,7 @@ repos:
     -   id: end-of-file-fixer
     -   id: check-added-large-files
     -   id: check-yaml
-        exclude: ^charts/.*/templates/
+        exclude: ^(charts/.*/templates/|helmfile/helmfile-.*\.yaml)
 - repo: git://github.com/antonbabenko/pre-commit-terraform
   rev: v1.50.0
   hooks:
diff --git a/charts/graph-grafana-dashboards/files/subgraph-indexing-status.json b/charts/graph-grafana-dashboards/files/subgraph-indexing-status.json
index 042aa01..c16a510 100644
--- a/charts/graph-grafana-dashboards/files/subgraph-indexing-status.json
+++ b/charts/graph-grafana-dashboards/files/subgraph-indexing-status.json
@@ -1,14 +1,5 @@
 {
-  "__inputs": [
-    {
-      "name": "DS_POSTGRESQL",
-      "label": "PostgreSQL",
-      "description": "",
-      "type": "datasource",
-      "pluginId": "postgres",
-      "pluginName": "PostgreSQL"
-    }
-  ],
+  "__inputs": [],
   "__requires": [
     {
       "type": "grafana",
@@ -56,7 +47,7 @@
   "links": [],
   "panels": [
     {
-      "datasource": "${DS_POSTGRESQL}",
+      "datasource": "postgres",
       "fieldConfig": {
         "defaults": {
           "color": {
@@ -374,7 +365,7 @@
     },
     {
       "columns": [],
-      "datasource": "${DS_POSTGRESQL}",
+      "datasource": "postgres",
       "fontSize": "100%",
       "gridPos": {
         "h": 8,
@@ -575,7 +566,7 @@
       "type": "table-old"
     },
     {
-      "datasource": "${DS_POSTGRESQL}",
+      "datasource": "postgres",
       "fieldConfig": {
         "defaults": {
           "custom": {
@@ -832,7 +823,7 @@
       "type": "table"
     },
     {
-      "datasource": "${DS_POSTGRESQL}",
+      "datasource": "postgres",
       "fieldConfig": {
         "defaults": {
           "color": {
@@ -1251,7 +1242,7 @@
     },
     {
       "columns": [],
-      "datasource": "${DS_POSTGRESQL}",
+      "datasource": "postgres",
       "fontSize": "80%",
       "gridPos": {
         "h": 4,
@@ -1359,7 +1350,7 @@
     },
     {
       "columns": [],
-      "datasource": "${DS_POSTGRESQL}",
+      "datasource": "postgres",
       "fontSize": "80%",
       "gridPos": {
         "h": 4,
diff --git a/charts/graph-grafana-dashboards/templates/_postgres-datasource.tpl b/charts/graph-grafana-dashboards/templates/_postgres-datasource.tpl
new file mode 100644
index 0000000..83d0088
--- /dev/null
+++ b/charts/graph-grafana-dashboards/templates/_postgres-datasource.tpl
@@ -0,0 +1,17 @@
+{{- define "graph-grafana-dashboards.postgres" -}}
+apiVersion: 1
+datasources:
+ - access: proxy
+   editable: true
+   name: postgres
+   orgId: 1
+   type: postgres
+   url: {{ .Values.postgresDatasource.url }}
+   user: {{ .Values.postgresDatasource.user }}
+   database: {{ .Values.postgresDatasource.db }}
+   secureJsonData:
+     password: {{ .Values.postgresDatasource.password }}
+   jsonData:
+     sslmode: disable
+     postgresVersion: 1200
+{{- end }}
diff --git a/charts/graph-grafana-dashboards/templates/datasources-secret.yaml b/charts/graph-grafana-dashboards/templates/datasources-secret.yaml
new file mode 100644
index 0000000..7c6ed3f
--- /dev/null
+++ b/charts/graph-grafana-dashboards/templates/datasources-secret.yaml
@@ -0,0 +1,8 @@
+apiVersion: v1
+kind: Secret
+metadata:
+  name: {{ .Release.Name }}
+  labels:
+    grafana_datasource: "1"
+data:
+  postgres.yaml: {{ include "graph-grafana-dashboards.postgres" . | b64enc }}
diff --git a/charts/graph-grafana-dashboards/values.yaml b/charts/graph-grafana-dashboards/values.yaml
index 792d600..d9140ac 100644
--- a/charts/graph-grafana-dashboards/values.yaml
+++ b/charts/graph-grafana-dashboards/values.yaml
@@ -1 +1,6 @@
 #
+postgresDatasource:
+  url:
+  user:
+  db:
+  password:
diff --git a/helmfile/helmfile-infra.yaml b/helmfile/helmfile-infra.yaml
index b7581b0..283137b 100644
--- a/helmfile/helmfile-infra.yaml
+++ b/helmfile/helmfile-infra.yaml
@@ -34,9 +34,6 @@ releases:
   chart: kubernetes-dashboard/kubernetes-dashboard
   version: ~4.3.1
 
-- name: graph-grafana-dashboards
-  chart: ../charts/graph-grafana-dashboards
-
 - name: prometheus-stack
   chart: prometheus-community/kube-prometheus-stack
   version: ~17.0.3
@@ -62,3 +59,8 @@ releases:
             foldersFromFilesStructure: true
       persistence:
         enabled: true
+
+- name: graph-grafana-dashboards
+  chart: ../charts/graph-grafana-dashboards
+  values:
+  - postgresDatasource: {{ toYaml .Values.postgresDatasource | nindent 6 }}