From c69599c84c67a92595f916612c25cf4bf29b7452 Mon Sep 17 00:00:00 2001 From: QuentinBisson Date: Tue, 3 Dec 2024 15:49:43 +0100 Subject: [PATCH] fix: support capitalized relabel actions to match with prometheus-operator behavior --- CHANGELOG.md | 3 + .../operator/configgen/config_gen.go | 2 + .../config_gen_servicemonitor_test.go | 76 +++++++++++++++++++ 3 files changed, 81 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a912ad5e57..73fd3769e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,7 @@ Main (unreleased) - For sharding targets during clustering, `loki.source.podlogs` now only takes into account some labels. (@ptodev) ### Bugfixes + - Fixed an issue in the `pyroscope.write` component to prevent TLS connection churn to Pyroscope when the `pyroscope.receive_http` clients don't request keepalive (@madaraszg-tulip) - Fixed an issue in the `pyroscope.write` component with multiple endpoints not working correctly for forwarding profiles from `pyroscope.receive_http` (@madaraszg-tulip) @@ -74,6 +75,8 @@ Main (unreleased) - Fixed a crash when updating the configuration of `remote.http`. (@kinolaev) +- Fixed an issue in the `prometheus.operator.servicemonitors`, `prometheus.operator.podmonitors` and `prometheus.operator.probes` to support capitalized actions. (@QuentinBisson) + ### Other changes - Change the stability of the `livedebugging` feature from "experimental" to "generally available". (@wildum) diff --git a/internal/component/prometheus/operator/configgen/config_gen.go b/internal/component/prometheus/operator/configgen/config_gen.go index 7774640766..18324c7fa3 100644 --- a/internal/component/prometheus/operator/configgen/config_gen.go +++ b/internal/component/prometheus/operator/configgen/config_gen.go @@ -4,6 +4,7 @@ package configgen import ( "regexp" + "strings" promopv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" commonConfig "github.com/prometheus/common/config" @@ -197,6 +198,7 @@ func (r *relabeler) add(cfgs ...*relabel.Config) { if cfg.Action == "" { cfg.Action = relabel.DefaultRelabelConfig.Action } + cfg.Action = relabel.Action(strings.ToLower(string(cfg.Action))) if cfg.Separator == "" { cfg.Separator = relabel.DefaultRelabelConfig.Separator } diff --git a/internal/component/prometheus/operator/configgen/config_gen_servicemonitor_test.go b/internal/component/prometheus/operator/configgen/config_gen_servicemonitor_test.go index 2b772b3e3a..568c6c16b7 100644 --- a/internal/component/prometheus/operator/configgen/config_gen_servicemonitor_test.go +++ b/internal/component/prometheus/operator/configgen/config_gen_servicemonitor_test.go @@ -494,6 +494,82 @@ func TestGenerateServiceMonitorConfig(t *testing.T) { LabelValueLengthLimit: 105, }, }, + { + name: "invalid-relabelling-action", + m: &promopv1.ServiceMonitor{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: "operator", + Name: "svcmonitor", + }, + }, + ep: promopv1.Endpoint{ + MetricRelabelConfigs: []*promopv1.RelabelConfig{ + { + SourceLabels: []promopv1.LabelName{"foo"}, + TargetLabel: "bar", + Action: "Replace", + }, + }, + }, + role: promk8s.RoleEndpoint, + expectedRelabels: util.Untab(` + - target_label: __meta_foo + replacement: bar + - source_labels: [job] + target_label: __tmp_prometheus_job_name + - source_labels: [__meta_kubernetes_endpoint_address_target_kind, __meta_kubernetes_endpoint_address_target_name] + regex: Node;(.*) + target_label: node + replacement: ${1} + - source_labels: [__meta_kubernetes_endpoint_address_target_kind, __meta_kubernetes_endpoint_address_target_name] + regex: Pod;(.*) + target_label: pod + action: replace + replacement: ${1} + - source_labels: [__meta_kubernetes_namespace] + target_label: namespace + - source_labels: [__meta_kubernetes_service_name] + target_label: service + - source_labels: [__meta_kubernetes_pod_container_name] + target_label: container + - source_labels: [__meta_kubernetes_pod_name] + target_label: pod + - source_labels: [__meta_kubernetes_pod_phase] + regex: (Failed|Succeeded) + action: drop + - source_labels: [__meta_kubernetes_service_name] + target_label: job + replacement: ${1} + `), + expectedMetricRelabels: util.Untab(` + - action: replace + source_labels: [foo] + target_label: bar + `), + expected: &config.ScrapeConfig{ + JobName: "serviceMonitor/operator/svcmonitor/1", + HonorTimestamps: true, + ScrapeInterval: model.Duration(time.Minute), + ScrapeTimeout: model.Duration(10 * time.Second), + ScrapeProtocols: config.DefaultScrapeProtocols, + EnableCompression: true, + MetricsPath: "/metrics", + Scheme: "http", + HTTPClientConfig: commonConfig.HTTPClientConfig{ + FollowRedirects: true, + EnableHTTP2: true, + }, + ServiceDiscoveryConfigs: discovery.Configs{ + &promk8s.SDConfig{ + Role: "endpoints", + NamespaceDiscovery: promk8s.NamespaceDiscovery{ + IncludeOwnNamespace: false, + Names: []string{"operator"}, + }, + }, + }, + }, + }, } for _, tc := range suite { t.Run(tc.name, func(t *testing.T) {