From 67b2c29875c5ecc8d15020266f8933f69bea6bcd Mon Sep 17 00:00:00 2001
From: Frederic Branczyk <fbranczyk@gmail.com>
Date: Fri, 1 Mar 2019 18:13:11 +0100
Subject: [PATCH] kube-prometheus/docs: Update how to handle existing rules

---
 ...prometheus-rules-and-grafana-dashboards.md | 23 +++++++++++--------
 examples/existingrule.json                    |  1 +
 .../{example.rules.yaml => existingrule.yaml} |  0
 ...s-additional-rendered-rule-example.jsonnet |  9 +-------
 4 files changed, 16 insertions(+), 17 deletions(-)
 create mode 100644 examples/existingrule.json
 rename examples/{example.rules.yaml => existingrule.yaml} (100%)

diff --git a/docs/developing-prometheus-rules-and-grafana-dashboards.md b/docs/developing-prometheus-rules-and-grafana-dashboards.md
index a0c1ff76..dcd0ad72 100644
--- a/docs/developing-prometheus-rules-and-grafana-dashboards.md
+++ b/docs/developing-prometheus-rules-and-grafana-dashboards.md
@@ -111,19 +111,24 @@ local kp = (import 'kube-prometheus/kube-prometheus.libsonnet') + {
 
 ### Pre-rendered rules
 
-We acknowledge, that users may need to transition existing rules, and therefore allow an option to add additional pre-rendered rules. This can be done simply by importing the existing rules in the [Prometheus rule format](https://prometheus.io/docs/prometheus/latest/configuration/recording_rules/) using the jsonnet function `importstr`. In this example we are importing a [provided example rule](../examples/example.rules.yaml).
+We acknowledge, that users may need to transition existing rules, and therefore allow an option to add additional pre-rendered rules. Luckily the yaml and json formats are very close so the yaml rules just need to be converted to json without any manual interaction needed. Just a tool to convert yaml to json is needed:
+
+```
+go get -u -v github.com/brancz/gojsontoyaml
+```
+
+And convert the existing rule file:
+
+```
+cat existingrule.yaml | gojsontoyaml -yamltojson > existingrule.json
+```
+
+Then import it in jsonnet:
 
 [embedmd]:# (../examples/prometheus-additional-rendered-rule-example.jsonnet)
 ```jsonnet
 local kp = (import 'kube-prometheus/kube-prometheus.libsonnet') + {
-  _config+:: {
-    namespace: 'monitoring',
-    prometheus+:: {
-      renderedRules: {
-        'example.rules.yaml': (importstr 'example.rules.yaml'),
-      },
-    },
-  },
+  prometheusAlerts+:: (import 'existingrule.json'),
 };
 
 { ['00namespace-' + name]: kp.kubePrometheus[name] for name in std.objectFields(kp.kubePrometheus) } +
diff --git a/examples/existingrule.json b/examples/existingrule.json
new file mode 100644
index 00000000..b29a5c45
--- /dev/null
+++ b/examples/existingrule.json
@@ -0,0 +1 @@
+{"groups":[{"name":"example-group","rules":[{"alert":"DeadMansSwitch","annotations":{"description":"This is a DeadMansSwitch meant to ensure that the entire alerting pipeline is functional."},"expr":"vector(1)","labels":{"severity":"none"}}]}]}
\ No newline at end of file
diff --git a/examples/example.rules.yaml b/examples/existingrule.yaml
similarity index 100%
rename from examples/example.rules.yaml
rename to examples/existingrule.yaml
diff --git a/examples/prometheus-additional-rendered-rule-example.jsonnet b/examples/prometheus-additional-rendered-rule-example.jsonnet
index 4ee7317d..07ef0e50 100644
--- a/examples/prometheus-additional-rendered-rule-example.jsonnet
+++ b/examples/prometheus-additional-rendered-rule-example.jsonnet
@@ -1,12 +1,5 @@
 local kp = (import 'kube-prometheus/kube-prometheus.libsonnet') + {
-  _config+:: {
-    namespace: 'monitoring',
-    prometheus+:: {
-      renderedRules: {
-        'example.rules.yaml': (importstr 'example.rules.yaml'),
-      },
-    },
-  },
+  prometheusAlerts+:: (import 'existingrule.json'),
 };
 
 { ['00namespace-' + name]: kp.kubePrometheus[name] for name in std.objectFields(kp.kubePrometheus) } +
-- 
GitLab