From 868755ea7df9f8f877dfac9a2cbad1dd5204fa20 Mon Sep 17 00:00:00 2001 From: Craig Peterson <192540+captncraig@users.noreply.github.com> Date: Fri, 21 Apr 2023 14:46:14 -0400 Subject: [PATCH] fix filter running logic (#3588) --- pkg/operator/config/metrics_templates_test.go | 81 +++++++++++++++++++ .../component/metrics/pod_monitor.libsonnet | 2 +- .../metrics/service_monitor.libsonnet | 2 +- 3 files changed, 83 insertions(+), 2 deletions(-) diff --git a/pkg/operator/config/metrics_templates_test.go b/pkg/operator/config/metrics_templates_test.go index 675eef334c0c..e0182ea6249c 100644 --- a/pkg/operator/config/metrics_templates_test.go +++ b/pkg/operator/config/metrics_templates_test.go @@ -918,6 +918,87 @@ func TestServiceMonitor(t *testing.T) { - __tmp_hash `), }, + { + name: "no_filter_running", + input: map[string]interface{}{ + "agentNamespace": "operator", + "monitor": prom_v1.ServiceMonitor{ + ObjectMeta: meta_v1.ObjectMeta{ + Namespace: "operator", + Name: "servicemonitor", + }, + }, + "endpoint": prom_v1.Endpoint{ + Port: "metrics", + }, + "index": 0, + "apiServer": prom_v1.APIServerConfig{}, + "overrideHonorLabels": false, + "overrideHonorTimestamps": false, + "ignoreNamespaceSelectors": false, + "enforcedNamespaceLabel": "", + "enforcedSampleLimit": nil, + "enforcedTargetLimit": nil, + "shards": 1, + }, + expect: util.Untab(` + job_name: serviceMonitor/operator/servicemonitor/0 + honor_labels: false + kubernetes_sd_configs: + - role: endpoints + namespaces: + names: [operator] + relabel_configs: + - source_labels: + - job + target_label: __tmp_prometheus_job_name + - action: keep + regex: metrics + source_labels: + - __meta_kubernetes_endpoint_port_name + - regex: Node;(.*) + replacement: $1 + separator: ; + source_labels: + - __meta_kubernetes_endpoint_address_target_kind + - __meta_kubernetes_endpoint_address_target_name + target_label: node + - regex: Pod;(.*) + replacement: $1 + separator: ; + source_labels: + - __meta_kubernetes_endpoint_address_target_kind + - __meta_kubernetes_endpoint_address_target_name + target_label: pod + - source_labels: + - __meta_kubernetes_namespace + target_label: namespace + - source_labels: + - __meta_kubernetes_service_name + target_label: service + - source_labels: + - __meta_kubernetes_pod_name + target_label: pod + - source_labels: + - __meta_kubernetes_pod_container_name + target_label: container + - replacement: $1 + source_labels: + - __meta_kubernetes_service_name + target_label: job + - replacement: metrics + target_label: endpoint + - action: hashmod + modulus: 1 + source_labels: + - __address__ + target_label: __tmp_hash + - action: keep + regex: $(SHARD) + source_labels: + - __tmp_hash + `), + }, } for _, tc := range tt { diff --git a/pkg/operator/config/templates/component/metrics/pod_monitor.libsonnet b/pkg/operator/config/templates/component/metrics/pod_monitor.libsonnet index 3348c4d4c458..862faba1a648 100644 --- a/pkg/operator/config/templates/component/metrics/pod_monitor.libsonnet +++ b/pkg/operator/config/templates/component/metrics/pod_monitor.libsonnet @@ -81,7 +81,7 @@ function( relabel_configs: ( [{ source_labels: ['job'], target_label: '__tmp_prometheus_job_name' }] + - (if endpoint.FilterRunning then [{ + (if endpoint.FilterRunning != null && endpoint.FilterRunning then [{ source_labels: ['__meta_kubernetes_pod_phase'], regex: '(Failed|Succeeded)', action: 'drop', diff --git a/pkg/operator/config/templates/component/metrics/service_monitor.libsonnet b/pkg/operator/config/templates/component/metrics/service_monitor.libsonnet index d5cd1977254e..c5c496647180 100644 --- a/pkg/operator/config/templates/component/metrics/service_monitor.libsonnet +++ b/pkg/operator/config/templates/component/metrics/service_monitor.libsonnet @@ -176,7 +176,7 @@ function( target_label: 'container', }] + - (if endpoint.FilterRunning then [{ + (if endpoint.FilterRunning != null && endpoint.FilterRunning then [{ source_labels: ['__meta_kubernetes_pod_phase'], regex: '(Failed|Succeeded)', action: 'drop',