From ea1e00a2515ea6dfdab9fa67d2c9e3d7b17a0aa9 Mon Sep 17 00:00:00 2001 From: Yuri Sa Date: Mon, 29 Jan 2024 12:06:01 +0100 Subject: [PATCH 01/14] Add missing label for Service/Pod Monitors Signed-off-by: Yuri Sa Add missing label for Service/Pod Monitors Signed-off-by: Yuri Sa Add missing label for Service/Pod Monitors Signed-off-by: Yuri Sa --- .../add-missing-label-servicemonitor.yaml | 16 ++++++++++++++++ internal/manifests/collector/podmonitor.go | 1 + .../manifests/collector/podmonitor_test.go | 18 ++++++++++++++++-- internal/manifests/collector/servicemonitor.go | 1 + .../manifests/collector/servicemonitor_test.go | 18 ++++++++++++++++-- .../01-assert.yaml | 1 + .../01-assert.yaml | 1 + .../02-assert.yaml | 1 + .../05-assert.yaml | 1 + .../06-assert.yaml | 1 + 10 files changed, 55 insertions(+), 4 deletions(-) create mode 100755 .chloggen/add-missing-label-servicemonitor.yaml diff --git a/.chloggen/add-missing-label-servicemonitor.yaml b/.chloggen/add-missing-label-servicemonitor.yaml new file mode 100755 index 0000000000..d7a1a8f28a --- /dev/null +++ b/.chloggen/add-missing-label-servicemonitor.yaml @@ -0,0 +1,16 @@ +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. operator, target allocator, github action) +component: operator + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Added missing label for Service/Pod Monitors + +# One or more tracking issues related to the change +issues: [2251] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: diff --git a/internal/manifests/collector/podmonitor.go b/internal/manifests/collector/podmonitor.go index 428d2673a0..3db64de3cc 100644 --- a/internal/manifests/collector/podmonitor.go +++ b/internal/manifests/collector/podmonitor.go @@ -63,6 +63,7 @@ func PodMonitor(params manifests.Params) (*monitoringv1.PodMonitor, error) { MatchLabels: map[string]string{ "app.kubernetes.io/managed-by": "opentelemetry-operator", "app.kubernetes.io/instance": fmt.Sprintf("%s.%s", params.OtelCol.Namespace, params.OtelCol.Name), + "app.kubernetes.io/name": "otel-collector-monitoring", }, }, PodMetricsEndpoints: append( diff --git a/internal/manifests/collector/podmonitor_test.go b/internal/manifests/collector/podmonitor_test.go index 9066b50cbc..9d238eaf68 100644 --- a/internal/manifests/collector/podmonitor_test.go +++ b/internal/manifests/collector/podmonitor_test.go @@ -43,12 +43,20 @@ func TestDesiredPodMonitors(t *testing.T) { assert.Equal(t, fmt.Sprintf("%s-collector", params.OtelCol.Name), actual.Name) assert.Equal(t, params.OtelCol.Namespace, actual.Namespace) assert.Equal(t, "monitoring", actual.Spec.PodMetricsEndpoints[0].Port) + expectedSelectorLabels := map[string]string{ + "app.kubernetes.io/managed-by": "opentelemetry-operator", + "app.kubernetes.io/instance": fmt.Sprintf("%s.%s", params.OtelCol.Namespace, params.OtelCol.Name), + "app.kubernetes.io/name": "otel-collector-monitoring", + } + assert.Equal(t, expectedSelectorLabels, actual.Spec.Selector.MatchLabels) +} - params, err = newParams("", "testdata/prometheus-exporter.yaml") +func TestDesiredPodMonitorsWithPrometheus(t *testing.T) { + params, err := newParams("", "testdata/prometheus-exporter.yaml") assert.NoError(t, err) params.OtelCol.Spec.Mode = v1alpha1.ModeSidecar params.OtelCol.Spec.Observability.Metrics.EnableMetrics = true - actual, err = PodMonitor(params) + actual, err := PodMonitor(params) assert.NoError(t, err) assert.NotNil(t, actual) assert.Equal(t, fmt.Sprintf("%s-collector", params.OtelCol.Name), actual.Name) @@ -56,4 +64,10 @@ func TestDesiredPodMonitors(t *testing.T) { assert.Equal(t, "monitoring", actual.Spec.PodMetricsEndpoints[0].Port) assert.Equal(t, "prometheus-dev", actual.Spec.PodMetricsEndpoints[1].Port) assert.Equal(t, "prometheus-prod", actual.Spec.PodMetricsEndpoints[2].Port) + expectedSelectorLabels := map[string]string{ + "app.kubernetes.io/managed-by": "opentelemetry-operator", + "app.kubernetes.io/instance": fmt.Sprintf("%s.%s", params.OtelCol.Namespace, params.OtelCol.Name), + "app.kubernetes.io/name": "otel-collector-monitoring", + } + assert.Equal(t, expectedSelectorLabels, actual.Spec.Selector.MatchLabels) } diff --git a/internal/manifests/collector/servicemonitor.go b/internal/manifests/collector/servicemonitor.go index db5dcd34d0..f3e1918ce6 100644 --- a/internal/manifests/collector/servicemonitor.go +++ b/internal/manifests/collector/servicemonitor.go @@ -65,6 +65,7 @@ func ServiceMonitor(params manifests.Params) (*monitoringv1.ServiceMonitor, erro MatchLabels: map[string]string{ "app.kubernetes.io/managed-by": "opentelemetry-operator", "app.kubernetes.io/instance": fmt.Sprintf("%s.%s", params.OtelCol.Namespace, params.OtelCol.Name), + "app.kubernetes.io/name": "otel-collector-monitoring", }, }, }, diff --git a/internal/manifests/collector/servicemonitor_test.go b/internal/manifests/collector/servicemonitor_test.go index c3fdf0c960..925deff353 100644 --- a/internal/manifests/collector/servicemonitor_test.go +++ b/internal/manifests/collector/servicemonitor_test.go @@ -35,11 +35,19 @@ func TestDesiredServiceMonitors(t *testing.T) { assert.Equal(t, fmt.Sprintf("%s-collector", params.OtelCol.Name), actual.Name) assert.Equal(t, params.OtelCol.Namespace, actual.Namespace) assert.Equal(t, "monitoring", actual.Spec.Endpoints[0].Port) + expectedSelectorLabels := map[string]string{ + "app.kubernetes.io/managed-by": "opentelemetry-operator", + "app.kubernetes.io/instance": fmt.Sprintf("%s.%s", params.OtelCol.Namespace, params.OtelCol.Name), + "app.kubernetes.io/name": "otel-collector-monitoring", + } + assert.Equal(t, expectedSelectorLabels, actual.Spec.Selector.MatchLabels) +} - params, err = newParams("", "testdata/prometheus-exporter.yaml") +func TestDesiredServiceMonitorsWithPrometheus(t *testing.T) { + params, err := newParams("", "testdata/prometheus-exporter.yaml") assert.NoError(t, err) params.OtelCol.Spec.Observability.Metrics.EnableMetrics = true - actual, err = ServiceMonitor(params) + actual, err := ServiceMonitor(params) assert.NoError(t, err) assert.NotNil(t, actual) assert.Equal(t, fmt.Sprintf("%s-collector", params.OtelCol.Name), actual.Name) @@ -47,4 +55,10 @@ func TestDesiredServiceMonitors(t *testing.T) { assert.Equal(t, "monitoring", actual.Spec.Endpoints[0].Port) assert.Equal(t, "prometheus-dev", actual.Spec.Endpoints[1].Port) assert.Equal(t, "prometheus-prod", actual.Spec.Endpoints[2].Port) + expectedSelectorLabels := map[string]string{ + "app.kubernetes.io/managed-by": "opentelemetry-operator", + "app.kubernetes.io/instance": fmt.Sprintf("%s.%s", params.OtelCol.Namespace, params.OtelCol.Name), + "app.kubernetes.io/name": "otel-collector-monitoring", + } + assert.Equal(t, expectedSelectorLabels, actual.Spec.Selector.MatchLabels) } diff --git a/tests/e2e-prometheuscr/create-pm-prometheus-exporters/01-assert.yaml b/tests/e2e-prometheuscr/create-pm-prometheus-exporters/01-assert.yaml index d89c68dd59..89146fd592 100644 --- a/tests/e2e-prometheuscr/create-pm-prometheus-exporters/01-assert.yaml +++ b/tests/e2e-prometheuscr/create-pm-prometheus-exporters/01-assert.yaml @@ -42,3 +42,4 @@ spec: matchLabels: app.kubernetes.io/managed-by: opentelemetry-operator app.kubernetes.io/instance: create-pm-prometheus.simplest + app.kubernetes.io/name: otel-collector-monitoring diff --git a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/01-assert.yaml b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/01-assert.yaml index 0a3c07d13b..6516509cc6 100644 --- a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/01-assert.yaml +++ b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/01-assert.yaml @@ -19,6 +19,7 @@ spec: matchLabels: app.kubernetes.io/managed-by: opentelemetry-operator app.kubernetes.io/instance: create-sm-prometheus.simplest + app.kubernetes.io/name: otel-collector-monitoring --- apiVersion: v1 kind: Service diff --git a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/02-assert.yaml b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/02-assert.yaml index b79840724e..542d2c319b 100644 --- a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/02-assert.yaml +++ b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/02-assert.yaml @@ -18,6 +18,7 @@ spec: matchLabels: app.kubernetes.io/managed-by: opentelemetry-operator app.kubernetes.io/instance: create-sm-prometheus.simplest + app.kubernetes.io/name: otel-collector-monitoring --- apiVersion: v1 kind: Service diff --git a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/05-assert.yaml b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/05-assert.yaml index f56ec033cd..a1521fc0c8 100644 --- a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/05-assert.yaml +++ b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/05-assert.yaml @@ -19,3 +19,4 @@ spec: matchLabels: app.kubernetes.io/managed-by: opentelemetry-operator app.kubernetes.io/instance: create-sm-prometheus.simplest + app.kubernetes.io/name: otel-collector-monitoring diff --git a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/06-assert.yaml b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/06-assert.yaml index 8df94a5984..442d63e1ce 100644 --- a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/06-assert.yaml +++ b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/06-assert.yaml @@ -18,3 +18,4 @@ spec: matchLabels: app.kubernetes.io/managed-by: opentelemetry-operator app.kubernetes.io/instance: create-sm-prometheus.simplest + app.kubernetes.io/name: otel-collector-monitoring From 3f6fd0b370c358372612793f5d0ac2341daec9e5 Mon Sep 17 00:00:00 2001 From: Yuri Sa Date: Tue, 30 Jan 2024 20:49:12 +0100 Subject: [PATCH 02/14] Added manifests.utils to set labels Signed-off-by: Yuri Sa --- internal/manifests/collector/podmonitor.go | 20 +++++++------------ .../manifests/collector/podmonitor_test.go | 8 ++++++-- .../manifests/collector/servicemonitor.go | 19 +++++++----------- .../collector/servicemonitor_test.go | 8 ++++++-- internal/manifests/manifestutils/labels.go | 10 ++++++++++ .../manifests/manifestutils/labels_test.go | 20 +++++++++++++++++++ 6 files changed, 56 insertions(+), 29 deletions(-) diff --git a/internal/manifests/collector/podmonitor.go b/internal/manifests/collector/podmonitor.go index 3db64de3cc..bbb33c1a30 100644 --- a/internal/manifests/collector/podmonitor.go +++ b/internal/manifests/collector/podmonitor.go @@ -15,7 +15,6 @@ package collector import ( - "fmt" "strings" "github.com/go-logr/logr" @@ -25,6 +24,7 @@ import ( "github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1" "github.com/open-telemetry/opentelemetry-operator/internal/manifests" "github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector/adapters" + "github.com/open-telemetry/opentelemetry-operator/internal/manifests/manifestutils" "github.com/open-telemetry/opentelemetry-operator/internal/naming" ) @@ -42,16 +42,14 @@ func PodMonitor(params manifests.Params) (*monitoringv1.PodMonitor, error) { if params.OtelCol.Spec.Mode != v1alpha1.ModeSidecar { return nil, nil } - + name := naming.ServiceMonitor(params.OtelCol.Name) + labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, []string{}) + selectorMatchLabels := manifestutils.SelectorMatchLabels(params.OtelCol.ObjectMeta, ComponentOpenTelemetryCollector) pm = monitoringv1.PodMonitor{ ObjectMeta: metav1.ObjectMeta{ Namespace: params.OtelCol.Namespace, - Name: naming.PodMonitor(params.OtelCol.Name), - Labels: map[string]string{ - "app.kubernetes.io/name": naming.PodMonitor(params.OtelCol.Name), - "app.kubernetes.io/instance": fmt.Sprintf("%s.%s", params.OtelCol.Namespace, params.OtelCol.Name), - "app.kubernetes.io/managed-by": "opentelemetry-operator", - }, + Name: name, + Labels: labels, }, Spec: monitoringv1.PodMonitorSpec{ JobLabel: "app.kubernetes.io/instance", @@ -60,11 +58,7 @@ func PodMonitor(params manifests.Params) (*monitoringv1.PodMonitor, error) { MatchNames: []string{params.OtelCol.Namespace}, }, Selector: metav1.LabelSelector{ - MatchLabels: map[string]string{ - "app.kubernetes.io/managed-by": "opentelemetry-operator", - "app.kubernetes.io/instance": fmt.Sprintf("%s.%s", params.OtelCol.Namespace, params.OtelCol.Name), - "app.kubernetes.io/name": "otel-collector-monitoring", - }, + MatchLabels: selectorMatchLabels, }, PodMetricsEndpoints: append( []monitoringv1.PodMetricsEndpoint{ diff --git a/internal/manifests/collector/podmonitor_test.go b/internal/manifests/collector/podmonitor_test.go index 9d238eaf68..aef27fd1c3 100644 --- a/internal/manifests/collector/podmonitor_test.go +++ b/internal/manifests/collector/podmonitor_test.go @@ -44,8 +44,10 @@ func TestDesiredPodMonitors(t *testing.T) { assert.Equal(t, params.OtelCol.Namespace, actual.Namespace) assert.Equal(t, "monitoring", actual.Spec.PodMetricsEndpoints[0].Port) expectedSelectorLabels := map[string]string{ - "app.kubernetes.io/managed-by": "opentelemetry-operator", + "app.kubernetes.io/component": "opentelemetry-collector", "app.kubernetes.io/instance": fmt.Sprintf("%s.%s", params.OtelCol.Namespace, params.OtelCol.Name), + "app.kubernetes.io/managed-by": "opentelemetry-operator", + "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/name": "otel-collector-monitoring", } assert.Equal(t, expectedSelectorLabels, actual.Spec.Selector.MatchLabels) @@ -65,8 +67,10 @@ func TestDesiredPodMonitorsWithPrometheus(t *testing.T) { assert.Equal(t, "prometheus-dev", actual.Spec.PodMetricsEndpoints[1].Port) assert.Equal(t, "prometheus-prod", actual.Spec.PodMetricsEndpoints[2].Port) expectedSelectorLabels := map[string]string{ - "app.kubernetes.io/managed-by": "opentelemetry-operator", + "app.kubernetes.io/component": "opentelemetry-collector", "app.kubernetes.io/instance": fmt.Sprintf("%s.%s", params.OtelCol.Namespace, params.OtelCol.Name), + "app.kubernetes.io/managed-by": "opentelemetry-operator", + "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/name": "otel-collector-monitoring", } assert.Equal(t, expectedSelectorLabels, actual.Spec.Selector.MatchLabels) diff --git a/internal/manifests/collector/servicemonitor.go b/internal/manifests/collector/servicemonitor.go index f3e1918ce6..2d977d1fd7 100644 --- a/internal/manifests/collector/servicemonitor.go +++ b/internal/manifests/collector/servicemonitor.go @@ -15,7 +15,6 @@ package collector import ( - "fmt" "strings" "github.com/go-logr/logr" @@ -25,6 +24,7 @@ import ( "github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1" "github.com/open-telemetry/opentelemetry-operator/internal/manifests" "github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector/adapters" + "github.com/open-telemetry/opentelemetry-operator/internal/manifests/manifestutils" "github.com/open-telemetry/opentelemetry-operator/internal/naming" ) @@ -42,15 +42,14 @@ func ServiceMonitor(params manifests.Params) (*monitoringv1.ServiceMonitor, erro if params.OtelCol.Spec.Mode == v1alpha1.ModeSidecar { return nil, nil } + name := naming.ServiceMonitor(params.OtelCol.Name) + labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, []string{}) + selectorMatchLabels := manifestutils.SelectorMatchLabels(params.OtelCol.ObjectMeta, ComponentOpenTelemetryCollector) sm = monitoringv1.ServiceMonitor{ ObjectMeta: metav1.ObjectMeta{ Namespace: params.OtelCol.Namespace, - Name: naming.ServiceMonitor(params.OtelCol.Name), - Labels: map[string]string{ - "app.kubernetes.io/name": naming.ServiceMonitor(params.OtelCol.Name), - "app.kubernetes.io/instance": fmt.Sprintf("%s.%s", params.OtelCol.Namespace, params.OtelCol.Name), - "app.kubernetes.io/managed-by": "opentelemetry-operator", - }, + Name: name, + Labels: labels, }, Spec: monitoringv1.ServiceMonitorSpec{ Endpoints: append([]monitoringv1.Endpoint{ @@ -62,11 +61,7 @@ func ServiceMonitor(params manifests.Params) (*monitoringv1.ServiceMonitor, erro MatchNames: []string{params.OtelCol.Namespace}, }, Selector: metav1.LabelSelector{ - MatchLabels: map[string]string{ - "app.kubernetes.io/managed-by": "opentelemetry-operator", - "app.kubernetes.io/instance": fmt.Sprintf("%s.%s", params.OtelCol.Namespace, params.OtelCol.Name), - "app.kubernetes.io/name": "otel-collector-monitoring", - }, + MatchLabels: selectorMatchLabels, }, }, } diff --git a/internal/manifests/collector/servicemonitor_test.go b/internal/manifests/collector/servicemonitor_test.go index 925deff353..9fcab60b81 100644 --- a/internal/manifests/collector/servicemonitor_test.go +++ b/internal/manifests/collector/servicemonitor_test.go @@ -36,8 +36,10 @@ func TestDesiredServiceMonitors(t *testing.T) { assert.Equal(t, params.OtelCol.Namespace, actual.Namespace) assert.Equal(t, "monitoring", actual.Spec.Endpoints[0].Port) expectedSelectorLabels := map[string]string{ - "app.kubernetes.io/managed-by": "opentelemetry-operator", + "app.kubernetes.io/component": "opentelemetry-collector", "app.kubernetes.io/instance": fmt.Sprintf("%s.%s", params.OtelCol.Namespace, params.OtelCol.Name), + "app.kubernetes.io/managed-by": "opentelemetry-operator", + "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/name": "otel-collector-monitoring", } assert.Equal(t, expectedSelectorLabels, actual.Spec.Selector.MatchLabels) @@ -56,8 +58,10 @@ func TestDesiredServiceMonitorsWithPrometheus(t *testing.T) { assert.Equal(t, "prometheus-dev", actual.Spec.Endpoints[1].Port) assert.Equal(t, "prometheus-prod", actual.Spec.Endpoints[2].Port) expectedSelectorLabels := map[string]string{ - "app.kubernetes.io/managed-by": "opentelemetry-operator", + "app.kubernetes.io/component": "opentelemetry-collector", "app.kubernetes.io/instance": fmt.Sprintf("%s.%s", params.OtelCol.Namespace, params.OtelCol.Name), + "app.kubernetes.io/managed-by": "opentelemetry-operator", + "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/name": "otel-collector-monitoring", } assert.Equal(t, expectedSelectorLabels, actual.Spec.Selector.MatchLabels) diff --git a/internal/manifests/manifestutils/labels.go b/internal/manifests/manifestutils/labels.go index cfcf3d2430..640ba9bfef 100644 --- a/internal/manifests/manifestutils/labels.go +++ b/internal/manifests/manifestutils/labels.go @@ -81,3 +81,13 @@ func SelectorLabels(instance metav1.ObjectMeta, component string) map[string]str "app.kubernetes.io/component": component, } } + +func SelectorMatchLabels(instance metav1.ObjectMeta, component string) map[string]string { + return map[string]string{ + "app.kubernetes.io/managed-by": "opentelemetry-operator", + "app.kubernetes.io/instance": naming.Truncate("%s.%s", 63, instance.Namespace, instance.Name), + "app.kubernetes.io/part-of": "opentelemetry", + "app.kubernetes.io/component": component, + "app.kubernetes.io/name": "otel-collector-monitoring", + } +} diff --git a/internal/manifests/manifestutils/labels_test.go b/internal/manifests/manifestutils/labels_test.go index 4a0bd794de..c706053f13 100644 --- a/internal/manifests/manifestutils/labels_test.go +++ b/internal/manifests/manifestutils/labels_test.go @@ -168,3 +168,23 @@ func TestSelectorLabels(t *testing.T) { // verify assert.Equal(t, expected, result) } + +func TestSelectorMatchLabels(t *testing.T) { + // prepare + expected := map[string]string{ + "app.kubernetes.io/component": "opentelemetry-collector", + "app.kubernetes.io/instance": "my-namespace.my-opentelemetry-collector", + "app.kubernetes.io/managed-by": "opentelemetry-operator", + "app.kubernetes.io/part-of": "opentelemetry", + "app.kubernetes.io/name": "otel-collector-monitoring", + } + otelcol := v1alpha1.OpenTelemetryCollector{ + ObjectMeta: metav1.ObjectMeta{Name: "my-opentelemetry-collector", Namespace: "my-namespace"}, + } + + // test + result := SelectorMatchLabels(otelcol.ObjectMeta, "opentelemetry-collector") + + // verify + assert.Equal(t, expected, result) +} From 32bf984b99dc26f5cb6898b07c955fc862943a5e Mon Sep 17 00:00:00 2001 From: Yuri Sa Date: Wed, 31 Jan 2024 10:25:00 +0100 Subject: [PATCH 03/14] Fixed labels names Signed-off-by: Yuri Sa --- internal/manifests/collector/podmonitor_test.go | 4 ++-- internal/manifests/collector/servicemonitor_test.go | 4 ++-- internal/manifests/manifestutils/labels.go | 3 ++- internal/manifests/manifestutils/labels_test.go | 2 +- .../create-pm-prometheus-exporters/01-assert.yaml | 2 +- .../create-sm-prometheus-exporters/01-assert.yaml | 2 +- .../create-sm-prometheus-exporters/02-assert.yaml | 2 +- .../create-sm-prometheus-exporters/05-assert.yaml | 2 +- .../create-sm-prometheus-exporters/06-assert.yaml | 2 +- 9 files changed, 12 insertions(+), 11 deletions(-) diff --git a/internal/manifests/collector/podmonitor_test.go b/internal/manifests/collector/podmonitor_test.go index aef27fd1c3..70fc6c16d1 100644 --- a/internal/manifests/collector/podmonitor_test.go +++ b/internal/manifests/collector/podmonitor_test.go @@ -48,7 +48,7 @@ func TestDesiredPodMonitors(t *testing.T) { "app.kubernetes.io/instance": fmt.Sprintf("%s.%s", params.OtelCol.Namespace, params.OtelCol.Name), "app.kubernetes.io/managed-by": "opentelemetry-operator", "app.kubernetes.io/part-of": "opentelemetry", - "app.kubernetes.io/name": "otel-collector-monitoring", + "app.kubernetes.io/name": fmt.Sprintf("%s-monitoring", params.OtelCol.Name), } assert.Equal(t, expectedSelectorLabels, actual.Spec.Selector.MatchLabels) } @@ -71,7 +71,7 @@ func TestDesiredPodMonitorsWithPrometheus(t *testing.T) { "app.kubernetes.io/instance": fmt.Sprintf("%s.%s", params.OtelCol.Namespace, params.OtelCol.Name), "app.kubernetes.io/managed-by": "opentelemetry-operator", "app.kubernetes.io/part-of": "opentelemetry", - "app.kubernetes.io/name": "otel-collector-monitoring", + "app.kubernetes.io/name": fmt.Sprintf("%s-monitoring", params.OtelCol.Name), } assert.Equal(t, expectedSelectorLabels, actual.Spec.Selector.MatchLabels) } diff --git a/internal/manifests/collector/servicemonitor_test.go b/internal/manifests/collector/servicemonitor_test.go index 9fcab60b81..9b71dc3e19 100644 --- a/internal/manifests/collector/servicemonitor_test.go +++ b/internal/manifests/collector/servicemonitor_test.go @@ -40,7 +40,7 @@ func TestDesiredServiceMonitors(t *testing.T) { "app.kubernetes.io/instance": fmt.Sprintf("%s.%s", params.OtelCol.Namespace, params.OtelCol.Name), "app.kubernetes.io/managed-by": "opentelemetry-operator", "app.kubernetes.io/part-of": "opentelemetry", - "app.kubernetes.io/name": "otel-collector-monitoring", + "app.kubernetes.io/name": fmt.Sprintf("%s-monitoring", params.OtelCol.Name), } assert.Equal(t, expectedSelectorLabels, actual.Spec.Selector.MatchLabels) } @@ -62,7 +62,7 @@ func TestDesiredServiceMonitorsWithPrometheus(t *testing.T) { "app.kubernetes.io/instance": fmt.Sprintf("%s.%s", params.OtelCol.Namespace, params.OtelCol.Name), "app.kubernetes.io/managed-by": "opentelemetry-operator", "app.kubernetes.io/part-of": "opentelemetry", - "app.kubernetes.io/name": "otel-collector-monitoring", + "app.kubernetes.io/name": fmt.Sprintf("%s-monitoring", params.OtelCol.Name), } assert.Equal(t, expectedSelectorLabels, actual.Spec.Selector.MatchLabels) } diff --git a/internal/manifests/manifestutils/labels.go b/internal/manifests/manifestutils/labels.go index 640ba9bfef..1694808a74 100644 --- a/internal/manifests/manifestutils/labels.go +++ b/internal/manifests/manifestutils/labels.go @@ -15,6 +15,7 @@ package manifestutils import ( + "fmt" "regexp" "strings" @@ -88,6 +89,6 @@ func SelectorMatchLabels(instance metav1.ObjectMeta, component string) map[strin "app.kubernetes.io/instance": naming.Truncate("%s.%s", 63, instance.Namespace, instance.Name), "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/component": component, - "app.kubernetes.io/name": "otel-collector-monitoring", + "app.kubernetes.io/name": fmt.Sprintf("%s-monitoring", instance.Name), } } diff --git a/internal/manifests/manifestutils/labels_test.go b/internal/manifests/manifestutils/labels_test.go index c706053f13..573dc42e20 100644 --- a/internal/manifests/manifestutils/labels_test.go +++ b/internal/manifests/manifestutils/labels_test.go @@ -176,7 +176,7 @@ func TestSelectorMatchLabels(t *testing.T) { "app.kubernetes.io/instance": "my-namespace.my-opentelemetry-collector", "app.kubernetes.io/managed-by": "opentelemetry-operator", "app.kubernetes.io/part-of": "opentelemetry", - "app.kubernetes.io/name": "otel-collector-monitoring", + "app.kubernetes.io/name": "my-opentelemetry-collector-monitoring", } otelcol := v1alpha1.OpenTelemetryCollector{ ObjectMeta: metav1.ObjectMeta{Name: "my-opentelemetry-collector", Namespace: "my-namespace"}, diff --git a/tests/e2e-prometheuscr/create-pm-prometheus-exporters/01-assert.yaml b/tests/e2e-prometheuscr/create-pm-prometheus-exporters/01-assert.yaml index 89146fd592..43f12b5fc9 100644 --- a/tests/e2e-prometheuscr/create-pm-prometheus-exporters/01-assert.yaml +++ b/tests/e2e-prometheuscr/create-pm-prometheus-exporters/01-assert.yaml @@ -42,4 +42,4 @@ spec: matchLabels: app.kubernetes.io/managed-by: opentelemetry-operator app.kubernetes.io/instance: create-pm-prometheus.simplest - app.kubernetes.io/name: otel-collector-monitoring + app.kubernetes.io/name: simplest-collector-monitoring diff --git a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/01-assert.yaml b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/01-assert.yaml index 6516509cc6..6d450e51dc 100644 --- a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/01-assert.yaml +++ b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/01-assert.yaml @@ -19,7 +19,7 @@ spec: matchLabels: app.kubernetes.io/managed-by: opentelemetry-operator app.kubernetes.io/instance: create-sm-prometheus.simplest - app.kubernetes.io/name: otel-collector-monitoring + app.kubernetes.io/name: simplest-collector-monitoring --- apiVersion: v1 kind: Service diff --git a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/02-assert.yaml b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/02-assert.yaml index 542d2c319b..5d3db59dc2 100644 --- a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/02-assert.yaml +++ b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/02-assert.yaml @@ -18,7 +18,7 @@ spec: matchLabels: app.kubernetes.io/managed-by: opentelemetry-operator app.kubernetes.io/instance: create-sm-prometheus.simplest - app.kubernetes.io/name: otel-collector-monitoring + app.kubernetes.io/name: simplest-collector-monitoring --- apiVersion: v1 kind: Service diff --git a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/05-assert.yaml b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/05-assert.yaml index a1521fc0c8..20042f507f 100644 --- a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/05-assert.yaml +++ b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/05-assert.yaml @@ -19,4 +19,4 @@ spec: matchLabels: app.kubernetes.io/managed-by: opentelemetry-operator app.kubernetes.io/instance: create-sm-prometheus.simplest - app.kubernetes.io/name: otel-collector-monitoring + app.kubernetes.io/name: simplest-collector-monitoring diff --git a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/06-assert.yaml b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/06-assert.yaml index 442d63e1ce..778453cd94 100644 --- a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/06-assert.yaml +++ b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/06-assert.yaml @@ -18,4 +18,4 @@ spec: matchLabels: app.kubernetes.io/managed-by: opentelemetry-operator app.kubernetes.io/instance: create-sm-prometheus.simplest - app.kubernetes.io/name: otel-collector-monitoring + app.kubernetes.io/name: simplest-collector-monitoring From b456ba67e43ef0d0f0b23d252209e3c01f35555d Mon Sep 17 00:00:00 2001 From: Yuri Sa Date: Mon, 12 Feb 2024 12:40:04 +0100 Subject: [PATCH 04/14] Fixed labels for PodMonitor Signed-off-by: Yuri Sa --- internal/manifests/collector/podmonitor.go | 8 ++++---- internal/manifests/collector/podmonitor_test.go | 6 ++---- internal/manifests/manifestutils/labels.go | 1 + 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/internal/manifests/collector/podmonitor.go b/internal/manifests/collector/podmonitor.go index bbb33c1a30..91cde24bb3 100644 --- a/internal/manifests/collector/podmonitor.go +++ b/internal/manifests/collector/podmonitor.go @@ -28,7 +28,7 @@ import ( "github.com/open-telemetry/opentelemetry-operator/internal/naming" ) -// ServiceMonitor returns the service monitor for the given instance. +// PodMonitor returns the pod monitor for the given instance. func PodMonitor(params manifests.Params) (*monitoringv1.PodMonitor, error) { if !params.OtelCol.Spec.Observability.Metrics.EnableMetrics { params.Log.V(2).Info("Metrics disabled for this OTEL Collector", @@ -42,9 +42,9 @@ func PodMonitor(params manifests.Params) (*monitoringv1.PodMonitor, error) { if params.OtelCol.Spec.Mode != v1alpha1.ModeSidecar { return nil, nil } - name := naming.ServiceMonitor(params.OtelCol.Name) + name := naming.PodMonitor(params.OtelCol.Name) labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, []string{}) - selectorMatchLabels := manifestutils.SelectorMatchLabels(params.OtelCol.ObjectMeta, ComponentOpenTelemetryCollector) + selectorLabels := manifestutils.SelectorLabels(params.OtelCol.ObjectMeta, ComponentOpenTelemetryCollector) pm = monitoringv1.PodMonitor{ ObjectMeta: metav1.ObjectMeta{ Namespace: params.OtelCol.Namespace, @@ -58,7 +58,7 @@ func PodMonitor(params manifests.Params) (*monitoringv1.PodMonitor, error) { MatchNames: []string{params.OtelCol.Namespace}, }, Selector: metav1.LabelSelector{ - MatchLabels: selectorMatchLabels, + MatchLabels: selectorLabels, }, PodMetricsEndpoints: append( []monitoringv1.PodMetricsEndpoint{ diff --git a/internal/manifests/collector/podmonitor_test.go b/internal/manifests/collector/podmonitor_test.go index 70fc6c16d1..1ce9da8405 100644 --- a/internal/manifests/collector/podmonitor_test.go +++ b/internal/manifests/collector/podmonitor_test.go @@ -48,7 +48,6 @@ func TestDesiredPodMonitors(t *testing.T) { "app.kubernetes.io/instance": fmt.Sprintf("%s.%s", params.OtelCol.Namespace, params.OtelCol.Name), "app.kubernetes.io/managed-by": "opentelemetry-operator", "app.kubernetes.io/part-of": "opentelemetry", - "app.kubernetes.io/name": fmt.Sprintf("%s-monitoring", params.OtelCol.Name), } assert.Equal(t, expectedSelectorLabels, actual.Spec.Selector.MatchLabels) } @@ -67,11 +66,10 @@ func TestDesiredPodMonitorsWithPrometheus(t *testing.T) { assert.Equal(t, "prometheus-dev", actual.Spec.PodMetricsEndpoints[1].Port) assert.Equal(t, "prometheus-prod", actual.Spec.PodMetricsEndpoints[2].Port) expectedSelectorLabels := map[string]string{ - "app.kubernetes.io/component": "opentelemetry-collector", - "app.kubernetes.io/instance": fmt.Sprintf("%s.%s", params.OtelCol.Namespace, params.OtelCol.Name), "app.kubernetes.io/managed-by": "opentelemetry-operator", + "app.kubernetes.io/instance": fmt.Sprintf("%s.%s", params.OtelCol.Namespace, params.OtelCol.Name), "app.kubernetes.io/part-of": "opentelemetry", - "app.kubernetes.io/name": fmt.Sprintf("%s-monitoring", params.OtelCol.Name), + "app.kubernetes.io/component": "opentelemetry-collector", } assert.Equal(t, expectedSelectorLabels, actual.Spec.Selector.MatchLabels) } diff --git a/internal/manifests/manifestutils/labels.go b/internal/manifests/manifestutils/labels.go index 1694808a74..9de80be75b 100644 --- a/internal/manifests/manifestutils/labels.go +++ b/internal/manifests/manifestutils/labels.go @@ -83,6 +83,7 @@ func SelectorLabels(instance metav1.ObjectMeta, component string) map[string]str } } +// SelectorMatchLabels return the common labels that should be used on ServiceMonitor when the Spec.Observability.Metrics.EnableMetrics is true. func SelectorMatchLabels(instance metav1.ObjectMeta, component string) map[string]string { return map[string]string{ "app.kubernetes.io/managed-by": "opentelemetry-operator", From fc03579e37b3ba52a46309c4bfab877e25d71367 Mon Sep 17 00:00:00 2001 From: Yuri Sa Date: Mon, 12 Feb 2024 21:08:01 +0100 Subject: [PATCH 05/14] Fixed e2e test Signed-off-by: Yuri Sa --- .../create-pm-prometheus-exporters/01-assert.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/e2e-prometheuscr/create-pm-prometheus-exporters/01-assert.yaml b/tests/e2e-prometheuscr/create-pm-prometheus-exporters/01-assert.yaml index 43f12b5fc9..1572a5fb07 100644 --- a/tests/e2e-prometheuscr/create-pm-prometheus-exporters/01-assert.yaml +++ b/tests/e2e-prometheuscr/create-pm-prometheus-exporters/01-assert.yaml @@ -42,4 +42,4 @@ spec: matchLabels: app.kubernetes.io/managed-by: opentelemetry-operator app.kubernetes.io/instance: create-pm-prometheus.simplest - app.kubernetes.io/name: simplest-collector-monitoring + From 00d2a0472fa9ce03f3b777899069a1f56f244846 Mon Sep 17 00:00:00 2001 From: Yuri Sa Date: Tue, 13 Feb 2024 12:02:34 +0100 Subject: [PATCH 06/14] Fixed e2e test Signed-off-by: Yuri Sa --- internal/manifests/manifestutils/labels.go | 3 +-- internal/naming/main.go | 4 ++++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/internal/manifests/manifestutils/labels.go b/internal/manifests/manifestutils/labels.go index 9de80be75b..3f851adea5 100644 --- a/internal/manifests/manifestutils/labels.go +++ b/internal/manifests/manifestutils/labels.go @@ -15,7 +15,6 @@ package manifestutils import ( - "fmt" "regexp" "strings" @@ -90,6 +89,6 @@ func SelectorMatchLabels(instance metav1.ObjectMeta, component string) map[strin "app.kubernetes.io/instance": naming.Truncate("%s.%s", 63, instance.Namespace, instance.Name), "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/component": component, - "app.kubernetes.io/name": fmt.Sprintf("%s-monitoring", instance.Name), + "app.kubernetes.io/name": naming.ServiceMonitorSelectorName(instance.Name), } } diff --git a/internal/naming/main.go b/internal/naming/main.go index a28b84cde4..a9919f38ae 100644 --- a/internal/naming/main.go +++ b/internal/naming/main.go @@ -159,6 +159,10 @@ func ServiceAccount(otelcol string) string { func ServiceMonitor(otelcol string) string { return DNSName(Truncate("%s-collector", 63, otelcol)) } +// ServiceMonitorSelectorName builds the service Monitor name based on the instance. +func ServiceMonitorSelectorName(otelcol string) string { + return DNSName(Truncate("%s-monitoring", 63, otelcol)) +} // PodMonitor builds the pod Monitor name based on the instance. func PodMonitor(otelcol string) string { From 2ee13bb97a1b38ab4ec21464111a7989298fbe99 Mon Sep 17 00:00:00 2001 From: Yuri Sa Date: Tue, 13 Feb 2024 17:55:42 +0100 Subject: [PATCH 07/14] Fixed selectorMatchLabels Signed-off-by: Yuri Sa --- internal/manifests/collector/servicemonitor_test.go | 4 ++-- internal/naming/main.go | 3 ++- internal/naming/triming.go | 11 +++++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/internal/manifests/collector/servicemonitor_test.go b/internal/manifests/collector/servicemonitor_test.go index 9b71dc3e19..c975710847 100644 --- a/internal/manifests/collector/servicemonitor_test.go +++ b/internal/manifests/collector/servicemonitor_test.go @@ -40,7 +40,7 @@ func TestDesiredServiceMonitors(t *testing.T) { "app.kubernetes.io/instance": fmt.Sprintf("%s.%s", params.OtelCol.Namespace, params.OtelCol.Name), "app.kubernetes.io/managed-by": "opentelemetry-operator", "app.kubernetes.io/part-of": "opentelemetry", - "app.kubernetes.io/name": fmt.Sprintf("%s-monitoring", params.OtelCol.Name), + "app.kubernetes.io/name": fmt.Sprintf("%s-collector-monitoring", params.OtelCol.Name), } assert.Equal(t, expectedSelectorLabels, actual.Spec.Selector.MatchLabels) } @@ -62,7 +62,7 @@ func TestDesiredServiceMonitorsWithPrometheus(t *testing.T) { "app.kubernetes.io/instance": fmt.Sprintf("%s.%s", params.OtelCol.Namespace, params.OtelCol.Name), "app.kubernetes.io/managed-by": "opentelemetry-operator", "app.kubernetes.io/part-of": "opentelemetry", - "app.kubernetes.io/name": fmt.Sprintf("%s-monitoring", params.OtelCol.Name), + "app.kubernetes.io/name": fmt.Sprintf("%s-collector-monitoring", params.OtelCol.Name), } assert.Equal(t, expectedSelectorLabels, actual.Spec.Selector.MatchLabels) } diff --git a/internal/naming/main.go b/internal/naming/main.go index a9919f38ae..811734303b 100644 --- a/internal/naming/main.go +++ b/internal/naming/main.go @@ -159,9 +159,10 @@ func ServiceAccount(otelcol string) string { func ServiceMonitor(otelcol string) string { return DNSName(Truncate("%s-collector", 63, otelcol)) } + // ServiceMonitorSelectorName builds the service Monitor name based on the instance. func ServiceMonitorSelectorName(otelcol string) string { - return DNSName(Truncate("%s-monitoring", 63, otelcol)) + return DNSName(Truncate("%s-monitoring", 63, checkCollectorPrefix(otelcol))) } // PodMonitor builds the pod Monitor name based on the instance. diff --git a/internal/naming/triming.go b/internal/naming/triming.go index 4b2dde37e4..e97dc2dee1 100644 --- a/internal/naming/triming.go +++ b/internal/naming/triming.go @@ -20,6 +20,7 @@ package naming import ( "fmt" "regexp" + "strings" ) var regexpEndReplace, regexpBeginReplace *regexp.Regexp @@ -72,3 +73,13 @@ func trimNonAlphaNumeric(text string) string { newText := regexpEndReplace.ReplaceAllString(text, "") return regexpBeginReplace.ReplaceAllString(newText, "") } + +// checkCollectorPrefix adds -collecto suffix in case the ObjectName doesn't contain it +// used on ServiceMonitor selector labels to filter the monitoring service out. +func checkCollectorPrefix(text string) string { + if !strings.Contains(text, "-collector") { + var newText = text + "-collector" + return newText + } + return text +} From 81ae4ab65671ab0785715f82ec20dba4022f77c9 Mon Sep 17 00:00:00 2001 From: Yuri Sa Date: Thu, 29 Feb 2024 16:18:58 +0100 Subject: [PATCH 08/14] Removed the extra method Signed-off-by: Yuri Sa --- internal/manifests/collector/servicemonitor_test.go | 6 ++++-- internal/manifests/manifestutils/labels.go | 2 +- internal/manifests/manifestutils/labels_test.go | 4 ++-- internal/naming/main.go | 5 ----- internal/naming/triming.go | 11 ----------- 5 files changed, 7 insertions(+), 21 deletions(-) diff --git a/internal/manifests/collector/servicemonitor_test.go b/internal/manifests/collector/servicemonitor_test.go index c975710847..5731640fbd 100644 --- a/internal/manifests/collector/servicemonitor_test.go +++ b/internal/manifests/collector/servicemonitor_test.go @@ -18,6 +18,8 @@ import ( "fmt" "testing" + "github.com/open-telemetry/opentelemetry-operator/internal/naming" + "github.com/stretchr/testify/assert" ) @@ -40,7 +42,7 @@ func TestDesiredServiceMonitors(t *testing.T) { "app.kubernetes.io/instance": fmt.Sprintf("%s.%s", params.OtelCol.Namespace, params.OtelCol.Name), "app.kubernetes.io/managed-by": "opentelemetry-operator", "app.kubernetes.io/part-of": "opentelemetry", - "app.kubernetes.io/name": fmt.Sprintf("%s-collector-monitoring", params.OtelCol.Name), + "app.kubernetes.io/name": naming.MonitoringService(params.OtelCol.Name), } assert.Equal(t, expectedSelectorLabels, actual.Spec.Selector.MatchLabels) } @@ -62,7 +64,7 @@ func TestDesiredServiceMonitorsWithPrometheus(t *testing.T) { "app.kubernetes.io/instance": fmt.Sprintf("%s.%s", params.OtelCol.Namespace, params.OtelCol.Name), "app.kubernetes.io/managed-by": "opentelemetry-operator", "app.kubernetes.io/part-of": "opentelemetry", - "app.kubernetes.io/name": fmt.Sprintf("%s-collector-monitoring", params.OtelCol.Name), + "app.kubernetes.io/name": naming.MonitoringService(params.OtelCol.Name), } assert.Equal(t, expectedSelectorLabels, actual.Spec.Selector.MatchLabels) } diff --git a/internal/manifests/manifestutils/labels.go b/internal/manifests/manifestutils/labels.go index 3f851adea5..6e0b7d2b86 100644 --- a/internal/manifests/manifestutils/labels.go +++ b/internal/manifests/manifestutils/labels.go @@ -89,6 +89,6 @@ func SelectorMatchLabels(instance metav1.ObjectMeta, component string) map[strin "app.kubernetes.io/instance": naming.Truncate("%s.%s", 63, instance.Namespace, instance.Name), "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/component": component, - "app.kubernetes.io/name": naming.ServiceMonitorSelectorName(instance.Name), + "app.kubernetes.io/name": naming.MonitoringService(instance.Name), } } diff --git a/internal/manifests/manifestutils/labels_test.go b/internal/manifests/manifestutils/labels_test.go index 573dc42e20..b53cbe1648 100644 --- a/internal/manifests/manifestutils/labels_test.go +++ b/internal/manifests/manifestutils/labels_test.go @@ -173,13 +173,13 @@ func TestSelectorMatchLabels(t *testing.T) { // prepare expected := map[string]string{ "app.kubernetes.io/component": "opentelemetry-collector", - "app.kubernetes.io/instance": "my-namespace.my-opentelemetry-collector", + "app.kubernetes.io/instance": "my-namespace.my-opentelemetry", "app.kubernetes.io/managed-by": "opentelemetry-operator", "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/name": "my-opentelemetry-collector-monitoring", } otelcol := v1alpha1.OpenTelemetryCollector{ - ObjectMeta: metav1.ObjectMeta{Name: "my-opentelemetry-collector", Namespace: "my-namespace"}, + ObjectMeta: metav1.ObjectMeta{Name: "my-opentelemetry", Namespace: "my-namespace"}, } // test diff --git a/internal/naming/main.go b/internal/naming/main.go index 811734303b..a28b84cde4 100644 --- a/internal/naming/main.go +++ b/internal/naming/main.go @@ -160,11 +160,6 @@ func ServiceMonitor(otelcol string) string { return DNSName(Truncate("%s-collector", 63, otelcol)) } -// ServiceMonitorSelectorName builds the service Monitor name based on the instance. -func ServiceMonitorSelectorName(otelcol string) string { - return DNSName(Truncate("%s-monitoring", 63, checkCollectorPrefix(otelcol))) -} - // PodMonitor builds the pod Monitor name based on the instance. func PodMonitor(otelcol string) string { return DNSName(Truncate("%s-collector", 63, otelcol)) diff --git a/internal/naming/triming.go b/internal/naming/triming.go index e97dc2dee1..4b2dde37e4 100644 --- a/internal/naming/triming.go +++ b/internal/naming/triming.go @@ -20,7 +20,6 @@ package naming import ( "fmt" "regexp" - "strings" ) var regexpEndReplace, regexpBeginReplace *regexp.Regexp @@ -73,13 +72,3 @@ func trimNonAlphaNumeric(text string) string { newText := regexpEndReplace.ReplaceAllString(text, "") return regexpBeginReplace.ReplaceAllString(newText, "") } - -// checkCollectorPrefix adds -collecto suffix in case the ObjectName doesn't contain it -// used on ServiceMonitor selector labels to filter the monitoring service out. -func checkCollectorPrefix(text string) string { - if !strings.Contains(text, "-collector") { - var newText = text + "-collector" - return newText - } - return text -} From a8a518a7e4c5b9fe8aa5edb6a697360b7daa19ea Mon Sep 17 00:00:00 2001 From: Yuri Sa Date: Fri, 1 Mar 2024 13:52:16 +0100 Subject: [PATCH 09/14] Adjusted nil Signed-off-by: Yuri Sa --- internal/manifests/collector/podmonitor.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/manifests/collector/podmonitor.go b/internal/manifests/collector/podmonitor.go index 91cde24bb3..4af23ca8f2 100644 --- a/internal/manifests/collector/podmonitor.go +++ b/internal/manifests/collector/podmonitor.go @@ -43,7 +43,7 @@ func PodMonitor(params manifests.Params) (*monitoringv1.PodMonitor, error) { return nil, nil } name := naming.PodMonitor(params.OtelCol.Name) - labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, []string{}) + labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, nil) selectorLabels := manifestutils.SelectorLabels(params.OtelCol.ObjectMeta, ComponentOpenTelemetryCollector) pm = monitoringv1.PodMonitor{ ObjectMeta: metav1.ObjectMeta{ From fd39eb94b69cf38c62714a793deb49c2686c042c Mon Sep 17 00:00:00 2001 From: Yuri Sa Date: Thu, 7 Mar 2024 11:20:04 +0100 Subject: [PATCH 10/14] Changed labels Signed-off-by: Yuri Sa --- internal/manifests/collector/service.go | 11 ++++++---- .../manifests/collector/servicemonitor.go | 5 +++-- .../collector/servicemonitor_test.go | 6 +----- internal/manifests/manifestutils/labels.go | 11 ---------- .../manifests/manifestutils/labels_test.go | 20 ------------------- .../01-assert.yaml | 4 +--- .../02-assert.yaml | 5 ++--- .../05-assert.yaml | 4 +--- .../06-assert.yaml | 4 +--- 9 files changed, 16 insertions(+), 54 deletions(-) diff --git a/internal/manifests/collector/service.go b/internal/manifests/collector/service.go index 8ac13a3e6d..54c1ec008d 100644 --- a/internal/manifests/collector/service.go +++ b/internal/manifests/collector/service.go @@ -29,10 +29,12 @@ import ( "github.com/open-telemetry/opentelemetry-operator/internal/naming" ) -// headless label is to differentiate the headless service from the clusterIP service. +// headless and monitoring labels are to differentiate the headless/monitoring services from the clusterIP service. const ( - headlessLabel = "operator.opentelemetry.io/collector-headless-service" - headlessExists = "Exists" + headlessLabel = "operator.opentelemetry.io/collector-headless-service" + headlessExists = "Exists" + monitoringLabel = "operator.opentelemetry.io/collector-monitoring-service" + monitoringExists = "Exists" ) func HeadlessService(params manifests.Params) (*corev1.Service, error) { @@ -44,7 +46,7 @@ func HeadlessService(params manifests.Params) (*corev1.Service, error) { h.Name = naming.HeadlessService(params.OtelCol.Name) h.Labels[headlessLabel] = headlessExists - // copy to avoid modifying params.OtelCol.Annotations + // copy to avoid modifying params.OtelCol.annotations annotations := map[string]string{ "service.beta.openshift.io/serving-cert-secret-name": fmt.Sprintf("%s-tls", h.Name), } @@ -61,6 +63,7 @@ func MonitoringService(params manifests.Params) (*corev1.Service, error) { name := naming.MonitoringService(params.OtelCol.Name) labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, []string{}) + labels[monitoringLabel] = monitoringExists out, err := params.OtelCol.Spec.Config.Yaml() if err != nil { diff --git a/internal/manifests/collector/servicemonitor.go b/internal/manifests/collector/servicemonitor.go index 4160f88fef..ae7fe2a623 100644 --- a/internal/manifests/collector/servicemonitor.go +++ b/internal/manifests/collector/servicemonitor.go @@ -44,7 +44,6 @@ func ServiceMonitor(params manifests.Params) (*monitoringv1.ServiceMonitor, erro } name := naming.ServiceMonitor(params.OtelCol.Name) labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, []string{}) - selectorMatchLabels := manifestutils.SelectorMatchLabels(params.OtelCol.ObjectMeta, ComponentOpenTelemetryCollector) sm = monitoringv1.ServiceMonitor{ ObjectMeta: metav1.ObjectMeta{ Namespace: params.OtelCol.Namespace, @@ -61,7 +60,9 @@ func ServiceMonitor(params manifests.Params) (*monitoringv1.ServiceMonitor, erro MatchNames: []string{params.OtelCol.Namespace}, }, Selector: metav1.LabelSelector{ - MatchLabels: selectorMatchLabels, + MatchLabels: map[string]string{ + "operator.opentelemetry.io/collector-monitoring-service": "Exists", + }, }, }, } diff --git a/internal/manifests/collector/servicemonitor_test.go b/internal/manifests/collector/servicemonitor_test.go index 5731640fbd..bc9d49e385 100644 --- a/internal/manifests/collector/servicemonitor_test.go +++ b/internal/manifests/collector/servicemonitor_test.go @@ -60,11 +60,7 @@ func TestDesiredServiceMonitorsWithPrometheus(t *testing.T) { assert.Equal(t, "prometheus-dev", actual.Spec.Endpoints[1].Port) assert.Equal(t, "prometheus-prod", actual.Spec.Endpoints[2].Port) expectedSelectorLabels := map[string]string{ - "app.kubernetes.io/component": "opentelemetry-collector", - "app.kubernetes.io/instance": fmt.Sprintf("%s.%s", params.OtelCol.Namespace, params.OtelCol.Name), - "app.kubernetes.io/managed-by": "opentelemetry-operator", - "app.kubernetes.io/part-of": "opentelemetry", - "app.kubernetes.io/name": naming.MonitoringService(params.OtelCol.Name), + "operator.opentelemetry.io/collector-monitoring-service": "Exists", } assert.Equal(t, expectedSelectorLabels, actual.Spec.Selector.MatchLabels) } diff --git a/internal/manifests/manifestutils/labels.go b/internal/manifests/manifestutils/labels.go index 6e0b7d2b86..cfcf3d2430 100644 --- a/internal/manifests/manifestutils/labels.go +++ b/internal/manifests/manifestutils/labels.go @@ -81,14 +81,3 @@ func SelectorLabels(instance metav1.ObjectMeta, component string) map[string]str "app.kubernetes.io/component": component, } } - -// SelectorMatchLabels return the common labels that should be used on ServiceMonitor when the Spec.Observability.Metrics.EnableMetrics is true. -func SelectorMatchLabels(instance metav1.ObjectMeta, component string) map[string]string { - return map[string]string{ - "app.kubernetes.io/managed-by": "opentelemetry-operator", - "app.kubernetes.io/instance": naming.Truncate("%s.%s", 63, instance.Namespace, instance.Name), - "app.kubernetes.io/part-of": "opentelemetry", - "app.kubernetes.io/component": component, - "app.kubernetes.io/name": naming.MonitoringService(instance.Name), - } -} diff --git a/internal/manifests/manifestutils/labels_test.go b/internal/manifests/manifestutils/labels_test.go index b53cbe1648..4a0bd794de 100644 --- a/internal/manifests/manifestutils/labels_test.go +++ b/internal/manifests/manifestutils/labels_test.go @@ -168,23 +168,3 @@ func TestSelectorLabels(t *testing.T) { // verify assert.Equal(t, expected, result) } - -func TestSelectorMatchLabels(t *testing.T) { - // prepare - expected := map[string]string{ - "app.kubernetes.io/component": "opentelemetry-collector", - "app.kubernetes.io/instance": "my-namespace.my-opentelemetry", - "app.kubernetes.io/managed-by": "opentelemetry-operator", - "app.kubernetes.io/part-of": "opentelemetry", - "app.kubernetes.io/name": "my-opentelemetry-collector-monitoring", - } - otelcol := v1alpha1.OpenTelemetryCollector{ - ObjectMeta: metav1.ObjectMeta{Name: "my-opentelemetry", Namespace: "my-namespace"}, - } - - // test - result := SelectorMatchLabels(otelcol.ObjectMeta, "opentelemetry-collector") - - // verify - assert.Equal(t, expected, result) -} diff --git a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/01-assert.yaml b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/01-assert.yaml index 6d450e51dc..6743ae8da6 100644 --- a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/01-assert.yaml +++ b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/01-assert.yaml @@ -17,9 +17,7 @@ spec: - create-sm-prometheus selector: matchLabels: - app.kubernetes.io/managed-by: opentelemetry-operator - app.kubernetes.io/instance: create-sm-prometheus.simplest - app.kubernetes.io/name: simplest-collector-monitoring + operator.opentelemetry.io/collector-monitoring-service: "Exists" --- apiVersion: v1 kind: Service diff --git a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/02-assert.yaml b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/02-assert.yaml index 5d3db59dc2..5347115aeb 100644 --- a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/02-assert.yaml +++ b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/02-assert.yaml @@ -16,9 +16,8 @@ spec: - create-sm-prometheus selector: matchLabels: - app.kubernetes.io/managed-by: opentelemetry-operator - app.kubernetes.io/instance: create-sm-prometheus.simplest - app.kubernetes.io/name: simplest-collector-monitoring + operator.opentelemetry.io/collector-monitoring-service: "Exists" + --- apiVersion: v1 kind: Service diff --git a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/05-assert.yaml b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/05-assert.yaml index 20042f507f..a9d4f4c909 100644 --- a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/05-assert.yaml +++ b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/05-assert.yaml @@ -17,6 +17,4 @@ spec: - create-sm-prometheus selector: matchLabels: - app.kubernetes.io/managed-by: opentelemetry-operator - app.kubernetes.io/instance: create-sm-prometheus.simplest - app.kubernetes.io/name: simplest-collector-monitoring + operator.opentelemetry.io/collector-monitoring-service: "Exists" diff --git a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/06-assert.yaml b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/06-assert.yaml index 778453cd94..1ead937b11 100644 --- a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/06-assert.yaml +++ b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/06-assert.yaml @@ -16,6 +16,4 @@ spec: - create-sm-prometheus selector: matchLabels: - app.kubernetes.io/managed-by: opentelemetry-operator - app.kubernetes.io/instance: create-sm-prometheus.simplest - app.kubernetes.io/name: simplest-collector-monitoring + operator.opentelemetry.io/collector-monitoring-service: "Exists" From 50a375dcff1def55727fb05f0aa85c2166bf4c36 Mon Sep 17 00:00:00 2001 From: Yuri Sa Date: Thu, 7 Mar 2024 20:20:39 +0100 Subject: [PATCH 11/14] Changed labels Signed-off-by: Yuri Sa --- controllers/builder_test.go | 71 ++++++++++--------- .../collector/servicemonitor_test.go | 8 +-- 2 files changed, 37 insertions(+), 42 deletions(-) diff --git a/controllers/builder_test.go b/controllers/builder_test.go index 4076ccd463..5c03983652 100644 --- a/controllers/builder_test.go +++ b/controllers/builder_test.go @@ -317,12 +317,13 @@ service: Name: "test-collector-monitoring", Namespace: "test", Labels: map[string]string{ - "app.kubernetes.io/component": "opentelemetry-collector", - "app.kubernetes.io/instance": "test.test", - "app.kubernetes.io/managed-by": "opentelemetry-operator", - "app.kubernetes.io/name": "test-collector-monitoring", - "app.kubernetes.io/part-of": "opentelemetry", - "app.kubernetes.io/version": "latest", + "app.kubernetes.io/component": "opentelemetry-collector", + "app.kubernetes.io/instance": "test.test", + "app.kubernetes.io/managed-by": "opentelemetry-operator", + "app.kubernetes.io/name": "test-collector-monitoring", + "app.kubernetes.io/part-of": "opentelemetry", + "app.kubernetes.io/version": "latest", + "operator.opentelemetry.io/collector-monitoring-service": "Exists", }, Annotations: nil, }, @@ -563,12 +564,13 @@ service: Name: "test-collector-monitoring", Namespace: "test", Labels: map[string]string{ - "app.kubernetes.io/component": "opentelemetry-collector", - "app.kubernetes.io/instance": "test.test", - "app.kubernetes.io/managed-by": "opentelemetry-operator", - "app.kubernetes.io/name": "test-collector-monitoring", - "app.kubernetes.io/part-of": "opentelemetry", - "app.kubernetes.io/version": "latest", + "app.kubernetes.io/component": "opentelemetry-collector", + "app.kubernetes.io/instance": "test.test", + "app.kubernetes.io/managed-by": "opentelemetry-operator", + "app.kubernetes.io/name": "test-collector-monitoring", + "app.kubernetes.io/part-of": "opentelemetry", + "app.kubernetes.io/version": "latest", + "operator.opentelemetry.io/collector-monitoring-service": "Exists", }, Annotations: nil, }, @@ -830,12 +832,13 @@ service: Name: "test-collector-monitoring", Namespace: "test", Labels: map[string]string{ - "app.kubernetes.io/component": "opentelemetry-collector", - "app.kubernetes.io/instance": "test.test", - "app.kubernetes.io/managed-by": "opentelemetry-operator", - "app.kubernetes.io/name": "test-collector-monitoring", - "app.kubernetes.io/part-of": "opentelemetry", - "app.kubernetes.io/version": "latest", + "app.kubernetes.io/component": "opentelemetry-collector", + "app.kubernetes.io/instance": "test.test", + "app.kubernetes.io/managed-by": "opentelemetry-operator", + "app.kubernetes.io/name": "test-collector-monitoring", + "app.kubernetes.io/part-of": "opentelemetry", + "app.kubernetes.io/version": "latest", + "operator.opentelemetry.io/collector-monitoring-service": "Exists", }, Annotations: nil, }, @@ -1309,12 +1312,13 @@ service: Name: "test-collector-monitoring", Namespace: "test", Labels: map[string]string{ - "app.kubernetes.io/component": "opentelemetry-collector", - "app.kubernetes.io/instance": "test.test", - "app.kubernetes.io/managed-by": "opentelemetry-operator", - "app.kubernetes.io/name": "test-collector-monitoring", - "app.kubernetes.io/part-of": "opentelemetry", - "app.kubernetes.io/version": "latest", + "app.kubernetes.io/component": "opentelemetry-collector", + "app.kubernetes.io/instance": "test.test", + "app.kubernetes.io/managed-by": "opentelemetry-operator", + "app.kubernetes.io/name": "test-collector-monitoring", + "app.kubernetes.io/part-of": "opentelemetry", + "app.kubernetes.io/version": "latest", + "operator.opentelemetry.io/collector-monitoring-service": "Exists", }, Annotations: nil, }, @@ -1706,12 +1710,13 @@ prometheus_cr: Name: "test-collector-monitoring", Namespace: "test", Labels: map[string]string{ - "app.kubernetes.io/component": "opentelemetry-collector", - "app.kubernetes.io/instance": "test.test", - "app.kubernetes.io/managed-by": "opentelemetry-operator", - "app.kubernetes.io/name": "test-collector-monitoring", - "app.kubernetes.io/part-of": "opentelemetry", - "app.kubernetes.io/version": "latest", + "app.kubernetes.io/component": "opentelemetry-collector", + "app.kubernetes.io/instance": "test.test", + "app.kubernetes.io/managed-by": "opentelemetry-operator", + "app.kubernetes.io/name": "test-collector-monitoring", + "app.kubernetes.io/part-of": "opentelemetry", + "app.kubernetes.io/version": "latest", + "operator.opentelemetry.io/collector-monitoring-service": "Exists", }, Annotations: nil, }, @@ -1944,11 +1949,7 @@ prometheus_cr: }, Selector: v1.LabelSelector{ MatchLabels: map[string]string{ - "app.kubernetes.io/component": "opentelemetry-targetallocator", - "app.kubernetes.io/instance": "test.test", - "app.kubernetes.io/managed-by": "opentelemetry-operator", - "app.kubernetes.io/name": "test-targetallocator", - "app.kubernetes.io/part-of": "opentelemetry", + "operator.opentelemetry.io/collector-monitoring-service": "Exists", }, }, NamespaceSelector: monitoringv1.NamespaceSelector{ diff --git a/internal/manifests/collector/servicemonitor_test.go b/internal/manifests/collector/servicemonitor_test.go index bc9d49e385..50b9945466 100644 --- a/internal/manifests/collector/servicemonitor_test.go +++ b/internal/manifests/collector/servicemonitor_test.go @@ -18,8 +18,6 @@ import ( "fmt" "testing" - "github.com/open-telemetry/opentelemetry-operator/internal/naming" - "github.com/stretchr/testify/assert" ) @@ -38,11 +36,7 @@ func TestDesiredServiceMonitors(t *testing.T) { assert.Equal(t, params.OtelCol.Namespace, actual.Namespace) assert.Equal(t, "monitoring", actual.Spec.Endpoints[0].Port) expectedSelectorLabels := map[string]string{ - "app.kubernetes.io/component": "opentelemetry-collector", - "app.kubernetes.io/instance": fmt.Sprintf("%s.%s", params.OtelCol.Namespace, params.OtelCol.Name), - "app.kubernetes.io/managed-by": "opentelemetry-operator", - "app.kubernetes.io/part-of": "opentelemetry", - "app.kubernetes.io/name": naming.MonitoringService(params.OtelCol.Name), + "operator.opentelemetry.io/collector-monitoring-service": "Exists", } assert.Equal(t, expectedSelectorLabels, actual.Spec.Selector.MatchLabels) } From 956b94ee50a585295a8d129b2e1094f6d0f04e59 Mon Sep 17 00:00:00 2001 From: Yuri Sa Date: Thu, 7 Mar 2024 22:31:13 +0100 Subject: [PATCH 12/14] Readded common labels Signed-off-by: Yuri Sa --- controllers/builder_test.go | 6 +++++- internal/manifests/collector/service.go | 13 ++++++------- internal/manifests/collector/servicemonitor.go | 7 ++++--- internal/manifests/collector/servicemonitor_test.go | 8 ++++++++ .../create-sm-prometheus-exporters/01-assert.yaml | 3 +++ .../create-sm-prometheus-exporters/02-assert.yaml | 3 +++ .../create-sm-prometheus-exporters/05-assert.yaml | 3 +++ .../create-sm-prometheus-exporters/06-assert.yaml | 3 +++ 8 files changed, 35 insertions(+), 11 deletions(-) diff --git a/controllers/builder_test.go b/controllers/builder_test.go index 5c03983652..4d2d84e96d 100644 --- a/controllers/builder_test.go +++ b/controllers/builder_test.go @@ -1949,7 +1949,11 @@ prometheus_cr: }, Selector: v1.LabelSelector{ MatchLabels: map[string]string{ - "operator.opentelemetry.io/collector-monitoring-service": "Exists", + "app.kubernetes.io/component": "opentelemetry-targetallocator", + "app.kubernetes.io/instance": "test.test", + "app.kubernetes.io/managed-by": "opentelemetry-operator", + "app.kubernetes.io/name": "test-targetallocator", + "app.kubernetes.io/part-of": "opentelemetry", }, }, NamespaceSelector: monitoringv1.NamespaceSelector{ diff --git a/internal/manifests/collector/service.go b/internal/manifests/collector/service.go index 54c1ec008d..333b3e5254 100644 --- a/internal/manifests/collector/service.go +++ b/internal/manifests/collector/service.go @@ -31,10 +31,9 @@ import ( // headless and monitoring labels are to differentiate the headless/monitoring services from the clusterIP service. const ( - headlessLabel = "operator.opentelemetry.io/collector-headless-service" - headlessExists = "Exists" - monitoringLabel = "operator.opentelemetry.io/collector-monitoring-service" - monitoringExists = "Exists" + headlessLabel = "operator.opentelemetry.io/collector-headless-service" + monitoringLabel = "operator.opentelemetry.io/collector-monitoring-service" + valueExists = "Exists" ) func HeadlessService(params manifests.Params) (*corev1.Service, error) { @@ -44,9 +43,9 @@ func HeadlessService(params manifests.Params) (*corev1.Service, error) { } h.Name = naming.HeadlessService(params.OtelCol.Name) - h.Labels[headlessLabel] = headlessExists + h.Labels[headlessLabel] = valueExists - // copy to avoid modifying params.OtelCol.annotations + // copy to avoid modifying params.OtelCol.Annotations annotations := map[string]string{ "service.beta.openshift.io/serving-cert-secret-name": fmt.Sprintf("%s-tls", h.Name), } @@ -63,7 +62,7 @@ func MonitoringService(params manifests.Params) (*corev1.Service, error) { name := naming.MonitoringService(params.OtelCol.Name) labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, []string{}) - labels[monitoringLabel] = monitoringExists + labels[monitoringLabel] = valueExists out, err := params.OtelCol.Spec.Config.Yaml() if err != nil { diff --git a/internal/manifests/collector/servicemonitor.go b/internal/manifests/collector/servicemonitor.go index ae7fe2a623..21ff254e09 100644 --- a/internal/manifests/collector/servicemonitor.go +++ b/internal/manifests/collector/servicemonitor.go @@ -44,6 +44,9 @@ func ServiceMonitor(params manifests.Params) (*monitoringv1.ServiceMonitor, erro } name := naming.ServiceMonitor(params.OtelCol.Name) labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, []string{}) + selectorLabels := manifestutils.SelectorLabels(params.OtelCol.ObjectMeta, ComponentOpenTelemetryCollector) + selectorLabels[monitoringLabel] = valueExists + sm = monitoringv1.ServiceMonitor{ ObjectMeta: metav1.ObjectMeta{ Namespace: params.OtelCol.Namespace, @@ -60,9 +63,7 @@ func ServiceMonitor(params manifests.Params) (*monitoringv1.ServiceMonitor, erro MatchNames: []string{params.OtelCol.Namespace}, }, Selector: metav1.LabelSelector{ - MatchLabels: map[string]string{ - "operator.opentelemetry.io/collector-monitoring-service": "Exists", - }, + MatchLabels: selectorLabels, }, }, } diff --git a/internal/manifests/collector/servicemonitor_test.go b/internal/manifests/collector/servicemonitor_test.go index 50b9945466..9cd133e080 100644 --- a/internal/manifests/collector/servicemonitor_test.go +++ b/internal/manifests/collector/servicemonitor_test.go @@ -36,6 +36,10 @@ func TestDesiredServiceMonitors(t *testing.T) { assert.Equal(t, params.OtelCol.Namespace, actual.Namespace) assert.Equal(t, "monitoring", actual.Spec.Endpoints[0].Port) expectedSelectorLabels := map[string]string{ + "app.kubernetes.io/component": "opentelemetry-collector", + "app.kubernetes.io/instance": "default.test", + "app.kubernetes.io/managed-by": "opentelemetry-operator", + "app.kubernetes.io/part-of": "opentelemetry", "operator.opentelemetry.io/collector-monitoring-service": "Exists", } assert.Equal(t, expectedSelectorLabels, actual.Spec.Selector.MatchLabels) @@ -54,6 +58,10 @@ func TestDesiredServiceMonitorsWithPrometheus(t *testing.T) { assert.Equal(t, "prometheus-dev", actual.Spec.Endpoints[1].Port) assert.Equal(t, "prometheus-prod", actual.Spec.Endpoints[2].Port) expectedSelectorLabels := map[string]string{ + "app.kubernetes.io/component": "opentelemetry-collector", + "app.kubernetes.io/instance": "default.test", + "app.kubernetes.io/managed-by": "opentelemetry-operator", + "app.kubernetes.io/part-of": "opentelemetry", "operator.opentelemetry.io/collector-monitoring-service": "Exists", } assert.Equal(t, expectedSelectorLabels, actual.Spec.Selector.MatchLabels) diff --git a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/01-assert.yaml b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/01-assert.yaml index 6743ae8da6..6cea0e3663 100644 --- a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/01-assert.yaml +++ b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/01-assert.yaml @@ -17,6 +17,9 @@ spec: - create-sm-prometheus selector: matchLabels: + app.kubernetes.io/instance: create-sm-prometheus.simplest + app.kubernetes.io/managed-by: opentelemetry-operator + app.kubernetes.io/name: simplest-collector operator.opentelemetry.io/collector-monitoring-service: "Exists" --- apiVersion: v1 diff --git a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/02-assert.yaml b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/02-assert.yaml index 5347115aeb..2a70703ceb 100644 --- a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/02-assert.yaml +++ b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/02-assert.yaml @@ -16,6 +16,9 @@ spec: - create-sm-prometheus selector: matchLabels: + app.kubernetes.io/instance: create-sm-prometheus.simplest + app.kubernetes.io/managed-by: opentelemetry-operator + app.kubernetes.io/name: simplest-collector operator.opentelemetry.io/collector-monitoring-service: "Exists" --- diff --git a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/05-assert.yaml b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/05-assert.yaml index a9d4f4c909..2fa33adbf8 100644 --- a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/05-assert.yaml +++ b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/05-assert.yaml @@ -17,4 +17,7 @@ spec: - create-sm-prometheus selector: matchLabels: + app.kubernetes.io/instance: create-sm-prometheus.simplest + app.kubernetes.io/managed-by: opentelemetry-operator + app.kubernetes.io/name: simplest-collector operator.opentelemetry.io/collector-monitoring-service: "Exists" diff --git a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/06-assert.yaml b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/06-assert.yaml index 1ead937b11..4c044d2df1 100644 --- a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/06-assert.yaml +++ b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/06-assert.yaml @@ -16,4 +16,7 @@ spec: - create-sm-prometheus selector: matchLabels: + app.kubernetes.io/instance: create-sm-prometheus.simplest + app.kubernetes.io/managed-by: opentelemetry-operator + app.kubernetes.io/name: simplest-collector operator.opentelemetry.io/collector-monitoring-service: "Exists" From d41662bdd8a37141526eeb5120e42c331640b3db Mon Sep 17 00:00:00 2001 From: Yuri Sa Date: Thu, 7 Mar 2024 22:50:01 +0100 Subject: [PATCH 13/14] Readded common labels Signed-off-by: Yuri Sa --- .../create-sm-prometheus-exporters/01-assert.yaml | 3 ++- .../create-sm-prometheus-exporters/02-assert.yaml | 3 ++- .../create-sm-prometheus-exporters/05-assert.yaml | 2 +- .../create-sm-prometheus-exporters/06-assert.yaml | 1 - 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/01-assert.yaml b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/01-assert.yaml index 6cea0e3663..56794fadb7 100644 --- a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/01-assert.yaml +++ b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/01-assert.yaml @@ -17,9 +17,10 @@ spec: - create-sm-prometheus selector: matchLabels: + app.kubernetes.io/component: opentelemetry-collector app.kubernetes.io/instance: create-sm-prometheus.simplest app.kubernetes.io/managed-by: opentelemetry-operator - app.kubernetes.io/name: simplest-collector + app.kubernetes.io/part-of: opentelemetry operator.opentelemetry.io/collector-monitoring-service: "Exists" --- apiVersion: v1 diff --git a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/02-assert.yaml b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/02-assert.yaml index 2a70703ceb..e4e8acc685 100644 --- a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/02-assert.yaml +++ b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/02-assert.yaml @@ -16,9 +16,10 @@ spec: - create-sm-prometheus selector: matchLabels: + app.kubernetes.io/component: opentelemetry-collector app.kubernetes.io/instance: create-sm-prometheus.simplest app.kubernetes.io/managed-by: opentelemetry-operator - app.kubernetes.io/name: simplest-collector + app.kubernetes.io/part-of: opentelemetry operator.opentelemetry.io/collector-monitoring-service: "Exists" --- diff --git a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/05-assert.yaml b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/05-assert.yaml index 2fa33adbf8..989e2bc7bb 100644 --- a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/05-assert.yaml +++ b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/05-assert.yaml @@ -19,5 +19,5 @@ spec: matchLabels: app.kubernetes.io/instance: create-sm-prometheus.simplest app.kubernetes.io/managed-by: opentelemetry-operator - app.kubernetes.io/name: simplest-collector + app.kubernetes.io/part-of: opentelemetry operator.opentelemetry.io/collector-monitoring-service: "Exists" diff --git a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/06-assert.yaml b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/06-assert.yaml index 4c044d2df1..aa590cc37e 100644 --- a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/06-assert.yaml +++ b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/06-assert.yaml @@ -18,5 +18,4 @@ spec: matchLabels: app.kubernetes.io/instance: create-sm-prometheus.simplest app.kubernetes.io/managed-by: opentelemetry-operator - app.kubernetes.io/name: simplest-collector operator.opentelemetry.io/collector-monitoring-service: "Exists" From 38464fb4060a81260e0c6e636121d1752c30e3db Mon Sep 17 00:00:00 2001 From: Yuri Sa Date: Fri, 8 Mar 2024 21:16:44 +0100 Subject: [PATCH 14/14] Added labels assert Signed-off-by: Yuri Sa --- .../01-assert.yaml | 1 + .../02-assert.yaml | 21 ++++++++++++++++++- .../05-assert.yaml | 20 ++++++++++++++++++ .../06-assert.yaml | 21 +++++++++++++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) diff --git a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/01-assert.yaml b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/01-assert.yaml index 56794fadb7..eb0652f517 100644 --- a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/01-assert.yaml +++ b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/01-assert.yaml @@ -58,6 +58,7 @@ metadata: app.kubernetes.io/managed-by: opentelemetry-operator app.kubernetes.io/name: simplest-collector-monitoring app.kubernetes.io/part-of: opentelemetry + operator.opentelemetry.io/collector-monitoring-service: "Exists" name: simplest-collector-monitoring namespace: create-sm-prometheus spec: diff --git a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/02-assert.yaml b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/02-assert.yaml index e4e8acc685..4c5b8bd5b8 100644 --- a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/02-assert.yaml +++ b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/02-assert.yaml @@ -43,4 +43,23 @@ spec: - name: prometheus-prod port: 8884 protocol: TCP - targetPort: 8884 \ No newline at end of file + targetPort: 8884 +--- +apiVersion: v1 +kind: Service +metadata: + labels: + app.kubernetes.io/component: opentelemetry-collector + app.kubernetes.io/instance: create-sm-prometheus.simplest + app.kubernetes.io/managed-by: opentelemetry-operator + app.kubernetes.io/name: simplest-collector-monitoring + app.kubernetes.io/part-of: opentelemetry + operator.opentelemetry.io/collector-monitoring-service: "Exists" + name: simplest-collector-monitoring + namespace: create-sm-prometheus +spec: + ports: + - name: monitoring + port: 8888 + protocol: TCP + targetPort: 8888 \ No newline at end of file diff --git a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/05-assert.yaml b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/05-assert.yaml index 989e2bc7bb..3e8205803c 100644 --- a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/05-assert.yaml +++ b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/05-assert.yaml @@ -20,4 +20,24 @@ spec: app.kubernetes.io/instance: create-sm-prometheus.simplest app.kubernetes.io/managed-by: opentelemetry-operator app.kubernetes.io/part-of: opentelemetry + app.kubernetes.io/component: opentelemetry-collector operator.opentelemetry.io/collector-monitoring-service: "Exists" +--- +apiVersion: v1 +kind: Service +metadata: + labels: + app.kubernetes.io/component: opentelemetry-collector + app.kubernetes.io/instance: create-sm-prometheus.simplest + app.kubernetes.io/managed-by: opentelemetry-operator + app.kubernetes.io/name: simplest-collector-monitoring + app.kubernetes.io/part-of: opentelemetry + operator.opentelemetry.io/collector-monitoring-service: "Exists" + name: simplest-collector-monitoring + namespace: create-sm-prometheus +spec: + ports: + - name: monitoring + port: 8888 + protocol: TCP + targetPort: 8888 \ No newline at end of file diff --git a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/06-assert.yaml b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/06-assert.yaml index aa590cc37e..dcfecf5d81 100644 --- a/tests/e2e-prometheuscr/create-sm-prometheus-exporters/06-assert.yaml +++ b/tests/e2e-prometheuscr/create-sm-prometheus-exporters/06-assert.yaml @@ -16,6 +16,27 @@ spec: - create-sm-prometheus selector: matchLabels: + app.kubernetes.io/component: opentelemetry-collector app.kubernetes.io/instance: create-sm-prometheus.simplest app.kubernetes.io/managed-by: opentelemetry-operator + app.kubernetes.io/part-of: opentelemetry operator.opentelemetry.io/collector-monitoring-service: "Exists" +--- +apiVersion: v1 +kind: Service +metadata: + labels: + app.kubernetes.io/component: opentelemetry-collector + app.kubernetes.io/instance: create-sm-prometheus.simplest + app.kubernetes.io/managed-by: opentelemetry-operator + app.kubernetes.io/name: simplest-collector-monitoring + app.kubernetes.io/part-of: opentelemetry + operator.opentelemetry.io/collector-monitoring-service: "Exists" + name: simplest-collector-monitoring + namespace: create-sm-prometheus +spec: + ports: + - name: monitoring + port: 8888 + protocol: TCP + targetPort: 8888 \ No newline at end of file