From 3ec88e526b4c9f28ab9a92051bced096d43541fc Mon Sep 17 00:00:00 2001
From: Cyril Jouve <jv.cyril@gmail.com>
Date: Tue, 8 Jan 2019 19:48:24 +0100
Subject: [PATCH] add service monitor to grafana

---
 .../kube-prometheus/kube-prometheus.libsonnet | 25 +++++++++++++++++++
 manifests/grafana-service.yaml                |  2 ++
 manifests/grafana-serviceMonitor.yaml         | 12 +++++++++
 3 files changed, 39 insertions(+)
 create mode 100644 manifests/grafana-serviceMonitor.yaml

diff --git a/jsonnet/kube-prometheus/kube-prometheus.libsonnet b/jsonnet/kube-prometheus/kube-prometheus.libsonnet
index f59cc80f..b751ce71 100644
--- a/jsonnet/kube-prometheus/kube-prometheus.libsonnet
+++ b/jsonnet/kube-prometheus/kube-prometheus.libsonnet
@@ -15,7 +15,32 @@ local configMapList = k.core.v1.configMapList;
     namespace: k.core.v1.namespace.new($._config.namespace),
   },
   grafana+:: {
+    local serviceLabels = {
+      app: 'grafana',
+    },
     dashboardDefinitions: configMapList.new(super.dashboardDefinitions),
+    service+: {
+      labels+: serviceLabels,
+    },
+    serviceMonitor: {
+      apiVersion: 'monitoring.coreos.com/v1',
+      kind: 'ServiceMonitor',
+      metadata: {
+        name: 'grafana',
+        namespace: $._config.namespace,
+      },
+      spec: {
+        selector: {
+          matchLabels: serviceLabels,
+        },
+        endpoints: [
+          {
+            port: 'http',
+            interval: '15s',
+          },
+        ],
+      },
+    },
   },
 } + {
   _config+:: {
diff --git a/manifests/grafana-service.yaml b/manifests/grafana-service.yaml
index 45f77a0d..09ec1f97 100644
--- a/manifests/grafana-service.yaml
+++ b/manifests/grafana-service.yaml
@@ -1,5 +1,7 @@
 apiVersion: v1
 kind: Service
+labels:
+  app: grafana
 metadata:
   name: grafana
   namespace: monitoring
diff --git a/manifests/grafana-serviceMonitor.yaml b/manifests/grafana-serviceMonitor.yaml
new file mode 100644
index 00000000..7ede266a
--- /dev/null
+++ b/manifests/grafana-serviceMonitor.yaml
@@ -0,0 +1,12 @@
+apiVersion: monitoring.coreos.com/v1
+kind: ServiceMonitor
+metadata:
+  name: grafana
+  namespace: monitoring
+spec:
+  endpoints:
+  - interval: 15s
+    port: http
+  selector:
+    matchLabels:
+      app: grafana
-- 
GitLab