From 0b0b9677644f8e19055c8af0160c556ced28436b Mon Sep 17 00:00:00 2001 From: paulfantom <pawel@krupa.net.pl> Date: Tue, 2 Feb 2021 16:08:41 +0100 Subject: [PATCH] jsonnet: thanos sidecar as first-level addon Signed-off-by: paulfantom <pawel@krupa.net.pl> --- examples/thanos-sidecar.jsonnet | 15 ++-- .../addons/thanos-sidecar.libsonnet | 77 ------------------- .../components/prometheus.libsonnet | 74 +++++++++++++++++- 3 files changed, 79 insertions(+), 87 deletions(-) delete mode 100644 jsonnet/kube-prometheus/addons/thanos-sidecar.libsonnet diff --git a/examples/thanos-sidecar.jsonnet b/examples/thanos-sidecar.jsonnet index 35eca8e1..24a0d5ce 100644 --- a/examples/thanos-sidecar.jsonnet +++ b/examples/thanos-sidecar.jsonnet @@ -1,17 +1,18 @@ local kp = (import 'kube-prometheus/main.libsonnet') + - (import 'kube-prometheus/addons/thanos-sidecar.libsonnet') + { values+:: { common+: { namespace: 'monitoring', }, - thanos: { - version: '0.17.2', - image: 'quay.io/thanos-io/thanos:v' + $.values.thanos.version, - objectStorageConfig: { - key: 'thanos.yaml', // How the file inside the secret is called - name: 'thanos-objectstorage', // This is the name of your Kubernetes secret with the config + prometheus+: { + thanos: { + version: '0.17.2', + image: 'quay.io/thanos-io/thanos:v0.17.2', + objectStorageConfig: { + key: 'thanos.yaml', // How the file inside the secret is called + name: 'thanos-objectstorage', // This is the name of your Kubernetes secret with the config + }, }, }, }, diff --git a/jsonnet/kube-prometheus/addons/thanos-sidecar.libsonnet b/jsonnet/kube-prometheus/addons/thanos-sidecar.libsonnet deleted file mode 100644 index 64a1e181..00000000 --- a/jsonnet/kube-prometheus/addons/thanos-sidecar.libsonnet +++ /dev/null @@ -1,77 +0,0 @@ -(import 'github.com/thanos-io/thanos/mixin/alerts/sidecar.libsonnet') + -{ - values+:: { - thanos: { - version: error 'must provide thanos version', - image: error 'must provide thanos image', - objectStorageConfig: error 'must provide thanos object storage configuration', - }, - }, - prometheus+: { - local p = self, - - // Add the grpc port to the Prometheus service to be able to query it with the Thanos Querier - service+: { - spec+: { - ports+: [ - { name: 'grpc', port: 10901, targetPort: 10901 }, - ], - }, - }, - // Create a new service that exposes both sidecar's HTTP metrics port and gRPC StoreAPI - serviceThanosSidecar: { - apiVersion: 'v1', - kind: 'Service', - metadata: { - name: 'prometheus-' + p.config.name + '-thanos-sidecar', - namespace: p.config.namespace, - labels: { prometheus: p.config.name, app: 'thanos-sidecar' }, - }, - spec: { - ports: [ - { name: 'grpc', port: 10901, targetPort: 10901 }, - { name: 'http', port: 10902, targetPort: 10902 }, - ], - selector: { app: 'prometheus', prometheus: p.config.name }, - clusterIP: 'None', - }, - }, - prometheus+: { - spec+: { - thanos+: { - version: $.values.thanos.version, - image: $.values.thanos.image, - objectStorageConfig: $.values.thanos.objectStorageConfig, - }, - }, - }, - serviceMonitorThanosSidecar: - { - apiVersion: 'monitoring.coreos.com/v1', - kind: 'ServiceMonitor', - metadata: { - name: 'thanos-sidecar', - namespace: p.config.namespace, - labels: { - 'app.kubernetes.io/name': 'prometheus', - }, - }, - spec: { - // Use the service's app label (thanos-sidecar) as the value for the job label. - jobLabel: 'app', - selector: { - matchLabels: { - prometheus: p.config.name, - app: 'thanos-sidecar', - }, - }, - endpoints: [ - { - port: 'http', - interval: '30s', - }, - ], - }, - }, - }, -} diff --git a/jsonnet/kube-prometheus/components/prometheus.libsonnet b/jsonnet/kube-prometheus/components/prometheus.libsonnet index 63d6d061..cf048c09 100644 --- a/jsonnet/kube-prometheus/components/prometheus.libsonnet +++ b/jsonnet/kube-prometheus/components/prometheus.libsonnet @@ -35,8 +35,10 @@ local defaults = { _config: { prometheusSelector: 'job="prometheus-' + defaults.name + '",namespace="' + defaults.namespace + '"', prometheusName: '{{$labels.namespace}}/{{$labels.pod}}', + thanosSelector: 'job="thanos-sidecar"', }, }, + thanos: {}, }; @@ -47,7 +49,15 @@ 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') { + mixin:: (import 'github.com/prometheus/prometheus/documentation/prometheus-mixin/mixin.libsonnet') + ( + if p.config.thanos != {} then + (import 'github.com/thanos-io/thanos/mixin/alerts/sidecar.libsonnet') + { + sidecar: { + selector: p.config.mixin._config.thanosSelector, + }, + } + else {} + ) { _config+:: p.config.mixin._config, }, @@ -86,8 +96,13 @@ function(params) { }, spec: { ports: [ - { name: 'web', targetPort: 'web', port: 9090 }, - ], + { name: 'web', targetPort: 'web', port: 9090 }, + ] + + ( + if p.config.thanos != {} then + [{ name: 'grpc', port: 10901, targetPort: 10901 }] + else [] + ), selector: { app: 'prometheus' } + p.config.selectorLabels, sessionAffinity: 'ClientIP', }, @@ -259,6 +274,7 @@ function(params) { runAsNonRoot: true, fsGroup: 2000, }, + thanos: p.config.thanos, }, }, @@ -491,4 +507,56 @@ function(params) { }], }, }, + + // 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', + kind: 'Service', + metadata+: { + name: 'prometheus-' + p.config.name + '-thanos-sidecar', + namespace: p.config.namespace, + labels+: p.config.commonLabels { + prometheus: p.config.name, + 'app.kubernetes.io/component': 'thanos-sidecar', + }, + }, + spec+: { + ports: [ + { name: 'grpc', port: 10901, targetPort: 10901 }, + { name: 'http', port: 10902, targetPort: 10902 }, + ], + selector: p.config.selectorLabels { + prometheus: p.config.name, + 'app.kubernetes.io/component': 'prometheus', + }, + clusterIP: 'None', + }, + }, + + // Include thanos sidecar ServiceMonitor only if thanos config was passed by user + [if std.objectHas(params, 'thanos') && std.length(params.thanos) > 0 then 'serviceMonitorThanosSidecar']: { + apiVersion: 'monitoring.coreos.com/v1', + kind: 'ServiceMonitor', + metadata+: { + name: 'thanos-sidecar', + namespace: p.config.namespace, + labels: p.config.commonLabels { + prometheus: p.config.name, + 'app.kubernetes.io/component': 'thanos-sidecar', + }, + }, + spec+: { + jobLabel: 'app.kubernetes.io/component', + selector: { + matchLabels: { + prometheus: p.config.name, + 'app.kubernetes.io/component': 'thanos-sidecar', + }, + }, + endpoints: [{ + port: 'http', + interval: '30s', + }], + }, + }, } -- GitLab