From cc05619dd83d7b3b00a6dcd6e0c6e76d7c9cd7fc Mon Sep 17 00:00:00 2001 From: Joshua Hoblitt Date: Thu, 14 May 2026 15:40:31 -0700 Subject: [PATCH] core: add cluster label to all servicemonitor scraped metrics Signed-off-by: Joshua Hoblitt --- pkg/operator/k8sutil/prometheus.go | 5 +++++ pkg/operator/k8sutil/prometheus_test.go | 2 ++ 2 files changed, 7 insertions(+) diff --git a/pkg/operator/k8sutil/prometheus.go b/pkg/operator/k8sutil/prometheus.go index 48f69f329..dfea5355b 100644 --- a/pkg/operator/k8sutil/prometheus.go +++ b/pkg/operator/k8sutil/prometheus.go @@ -27,6 +27,7 @@ import ( "github.com/rook/rook/pkg/clusterd" kerrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/utils/ptr" ) func getMonitoringClient(context *clusterd.Context) (*monitoringclient.Clientset, error) { @@ -65,6 +66,10 @@ func GetServiceMonitor(name string, namespace string, portName string) *monitori Path: "/metrics", Interval: "10s", HonorLabels: true, + RelabelConfigs: []monitoringv1.RelabelConfig{{ + TargetLabel: "cluster", + Replacement: ptr.To(namespace), + }}, }, }, }, diff --git a/pkg/operator/k8sutil/prometheus_test.go b/pkg/operator/k8sutil/prometheus_test.go index 18ac52b7a..ce5b91b1c 100644 --- a/pkg/operator/k8sutil/prometheus_test.go +++ b/pkg/operator/k8sutil/prometheus_test.go @@ -38,4 +38,6 @@ func TestGetServiceMonitor(t *testing.T) { assert.NotNil(t, servicemonitor.Spec.NamespaceSelector.MatchNames) assert.NotNil(t, servicemonitor.Spec.Selector.MatchLabels) assert.NotNil(t, servicemonitor.Spec.Endpoints) + assert.Equal(t, "cluster", servicemonitor.Spec.Endpoints[0].RelabelConfigs[0].TargetLabel) + assert.Equal(t, namespace, *servicemonitor.Spec.Endpoints[0].RelabelConfigs[0].Replacement) }