diff --git a/examples/thanos-sidecar.jsonnet b/examples/thanos-sidecar.jsonnet index 35eca8e19e0689c2a491fcd367ad2be9e3621462..24a0d5cee3788267def3c74903782933523f7a25 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 64a1e1816be7eb511863271ba5aca81d43f38e0e..0000000000000000000000000000000000000000 --- 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 63d6d0610dca669413d604d26b810d04fb359006..cf048c09362b851261785e92a026d778209d294e 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', + }], + }, + }, } diff --git a/manifests/prometheus-prometheus.yaml b/manifests/prometheus-prometheus.yaml index f1f8ef2f2999cc0ee466d44bc7049d20ec4edb75..10f74df4046301db55c2b8dab0cf982d864d9aa1 100644 --- a/manifests/prometheus-prometheus.yaml +++ b/manifests/prometheus-prometheus.yaml @@ -44,4 +44,5 @@ spec: serviceAccountName: prometheus-k8s serviceMonitorNamespaceSelector: {} serviceMonitorSelector: {} + thanos: {} version: 2.24.0