From 0e7dc97bc51161ed131c7884772d7675d0518c1e Mon Sep 17 00:00:00 2001
From: Maxime Brunet <maxime.brunet@paytm.com>
Date: Mon, 2 Aug 2021 12:21:49 -0700
Subject: [PATCH] Create Thanos Sidecar rules separately from Prometheus ones

---
 .../components/prometheus.libsonnet           | 45 +++++++++++++------
 1 file changed, 31 insertions(+), 14 deletions(-)

diff --git a/jsonnet/kube-prometheus/components/prometheus.libsonnet b/jsonnet/kube-prometheus/components/prometheus.libsonnet
index 5e1c9e33..a9d0100a 100644
--- a/jsonnet/kube-prometheus/components/prometheus.libsonnet
+++ b/jsonnet/kube-prometheus/components/prometheus.libsonnet
@@ -50,20 +50,21 @@ function(params) {
   assert std.isObject(p._config.resources),
   assert std.isObject(p._config.mixin._config),
 
-  mixin:: (import 'github.com/prometheus/prometheus/documentation/prometheus-mixin/mixin.libsonnet') +
-          (import 'github.com/kubernetes-monitoring/kubernetes-mixin/lib/add-runbook-links.libsonnet') + (
-    if p._config.thanos != {} then
-      (import 'github.com/thanos-io/thanos/mixin/alerts/sidecar.libsonnet') + {
-        targetGroups: {},
-        sidecar: {
-          selector: p._config.mixin._config.thanosSelector,
-          dimensions: std.join(', ', ['job', 'instance']),
-        },
-      }
-    else {}
-  ) {
-    _config+:: p._config.mixin._config,
-  },
+  mixin::
+    (import 'github.com/prometheus/prometheus/documentation/prometheus-mixin/mixin.libsonnet') +
+    (import 'github.com/kubernetes-monitoring/kubernetes-mixin/lib/add-runbook-links.libsonnet') + {
+      _config+:: p._config.mixin._config,
+    },
+
+  mixinThanos::
+    (import 'github.com/thanos-io/thanos/mixin/alerts/sidecar.libsonnet') +
+    (import 'github.com/kubernetes-monitoring/kubernetes-mixin/lib/add-runbook-links.libsonnet') + {
+      targetGroups: {},
+      sidecar: {
+        selector: p._config.mixin._config.thanosSelector,
+        dimensions: std.join(', ', ['job', 'instance']),
+      },
+    },
 
   prometheusRule: {
     apiVersion: 'monitoring.coreos.com/v1',
@@ -327,6 +328,22 @@ function(params) {
     },
   },
 
+  // Include thanos sidecar PrometheusRule only if thanos config was passed by user
+  [if std.objectHas(params, 'thanos') && std.length(params.thanos) > 0 then 'prometheusRuleThanosSidecar']: {
+    apiVersion: 'monitoring.coreos.com/v1',
+    kind: 'PrometheusRule',
+    metadata: {
+      labels: p._config.commonLabels + p._config.mixin.ruleLabels,
+      name: 'prometheus-' + p._config.name + '-thanos-sidecar-rules',
+      namespace: p._config.namespace,
+    },
+    spec: {
+      local r = if std.objectHasAll(p.mixinThanos, 'prometheusRules') then p.mixinThanos.prometheusRules.groups else [],
+      local a = if std.objectHasAll(p.mixinThanos, 'prometheusAlerts') then p.mixinThanos.prometheusAlerts.groups else [],
+      groups: a + r,
+    },
+  },
+
   // Include thanos sidecar Service only if thanos config was passed by user
   [if std.objectHas(params, 'thanos') && std.length(params.thanos) > 0 then 'serviceThanosSidecar']: {
     apiVersion: 'v1',
-- 
GitLab