From a539185c185614d6996e8dcf3d471fafaf190b04 Mon Sep 17 00:00:00 2001 From: Alessio Trivisonno Date: Mon, 23 Dec 2024 19:49:52 +0100 Subject: [PATCH] feat: expose namespace selector labels conf --- ...t-add-prometheuscr-namespaceselectors.yaml | 17 ++++ README.md | 7 ++ apis/v1beta1/targetallocator_types.go | 24 +++++ controllers/builder_test.go | 63 +++++++++--- .../manifests/targetallocator/configmap.go | 8 ++ .../targetallocator/configmap_test.go | 99 +++++++++++++++++++ 6 files changed, 204 insertions(+), 14 deletions(-) create mode 100644 .chloggen/feat-add-prometheuscr-namespaceselectors.yaml diff --git a/.chloggen/feat-add-prometheuscr-namespaceselectors.yaml b/.chloggen/feat-add-prometheuscr-namespaceselectors.yaml new file mode 100644 index 0000000000..d4bbbe20af --- /dev/null +++ b/.chloggen/feat-add-prometheuscr-namespaceselectors.yaml @@ -0,0 +1,17 @@ +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: 'enhancement' + +# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action) +component: 'operator' + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: "Add Prometheus CR namespace selectors" + +# One or more tracking issues related to the change +issues: + - 3086 + +# (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/README.md b/README.md index 71619cdc74..bb31070c3d 100644 --- a/README.md +++ b/README.md @@ -709,8 +709,15 @@ spec: serviceAccount: everything-prometheus-operator-needs prometheusCR: enabled: true + scrapeInterval: 30s serviceMonitorSelector: {} + serviceMonitorNamespaceSelector: {} podMonitorSelector: {} + podMonitorNamespaceSelector: {} + scrapeConfigSelector: {} + scrapeConfigNamespaceSelector: {} + probeSelector: {} + probeNamespaceSelector: {} config: receivers: prometheus: diff --git a/apis/v1beta1/targetallocator_types.go b/apis/v1beta1/targetallocator_types.go index ca9bc9aa0c..8a398c9c63 100644 --- a/apis/v1beta1/targetallocator_types.go +++ b/apis/v1beta1/targetallocator_types.go @@ -36,24 +36,48 @@ type TargetAllocatorPrometheusCR struct { // label selector matches no objects. // +optional PodMonitorSelector *metav1.LabelSelector `json:"podMonitorSelector,omitempty"` + // Namespaces to be selected to look for PodMonitors for target discovery. + // A label selector is a label query over a set of resources. The result of matchLabels and + // matchExpressions are ANDed. An empty label selector matches all objects. A null + // label selector matches no objects. + // +optional + PodMonitorNamespaceSelector *metav1.LabelSelector `json:"podMonitorNamespaceSelector,omitempty"` // ServiceMonitors to be selected for target discovery. // A label selector is a label query over a set of resources. The result of matchLabels and // matchExpressions are ANDed. An empty label selector matches all objects. A null // label selector matches no objects. // +optional ServiceMonitorSelector *metav1.LabelSelector `json:"serviceMonitorSelector,omitempty"` + // Namespaces to be selected to look for ServiceMonitors for target discovery. + // A label selector is a label query over a set of resources. The result of matchLabels and + // matchExpressions are ANDed. An empty label selector matches all objects. A null + // label selector matches no objects. + // +optional + ServiceMonitorNamespaceSelector *metav1.LabelSelector `json:"serviceMonitorNamespaceSelector,omitempty"` // ScrapeConfigs to be selected for target discovery. // A label selector is a label query over a set of resources. The result of matchLabels and // matchExpressions are ANDed. An empty label selector matches all objects. A null // label selector matches no objects. // +optional ScrapeConfigSelector *metav1.LabelSelector `json:"scrapeConfigSelector,omitempty"` + // Namespaces to be selected to look for ScrapeConfigs for target discovery. + // A label selector is a label query over a set of resources. The result of matchLabels and + // matchExpressions are ANDed. An empty label selector matches all objects. A null + // label selector matches no objects. + // +optional + ScrapeConfigNamespaceSelector *metav1.LabelSelector `json:"scrapeConfigNamespaceSelector,omitempty"` // Probes to be selected for target discovery. // A label selector is a label query over a set of resources. The result of matchLabels and // matchExpressions are ANDed. An empty label selector matches all objects. A null // label selector matches no objects. // +optional ProbeSelector *metav1.LabelSelector `json:"probeSelector,omitempty"` + // Namespaces to be selected to look for Probes for target discovery. + // A label selector is a label query over a set of resources. The result of matchLabels and + // matchExpressions are ANDed. An empty label selector matches all objects. A null + // label selector matches no objects. + // +optional + ProbeNamespaceSelector *metav1.LabelSelector `json:"probeNamespaceSelector,omitempty"` } type ( diff --git a/controllers/builder_test.go b/controllers/builder_test.go index 053bf6966a..2dadd3fe52 100644 --- a/controllers/builder_test.go +++ b/controllers/builder_test.go @@ -1512,9 +1512,14 @@ config: filter_strategy: relabel-config prometheus_cr: enabled: true + pod_monitor_namespace_selector: null pod_monitor_selector: null + probe_namespace_selector: null probe_selector: null + scrape_config_namespace_selector: null scrape_config_selector: null + scrape_interval: 30s + service_monitor_namespace_selector: null service_monitor_selector: null `, }, @@ -1548,7 +1553,7 @@ prometheus_cr: "app.kubernetes.io/version": "latest", }, Annotations: map[string]string{ - "opentelemetry-targetallocator-config/hash": "286a5a4e7ec6d2ce652a4ce23e135c10053b4c87fd080242daa5bf21dcd5a337", + "opentelemetry-targetallocator-config/hash": "a81383bc4e7ebbf141d2fd9cde3dcd9758bc47f27af6cbaeb0f14ab6360e08c6", }, }, Spec: corev1.PodSpec{ @@ -1680,7 +1685,7 @@ prometheus_cr: "app.kubernetes.io/version": "latest", }, Annotations: map[string]string{ - "opentelemetry-targetallocator-config/hash": "286a5a4e7ec6d2ce652a4ce23e135c10053b4c87fd080242daa5bf21dcd5a337", + "opentelemetry-targetallocator-config/hash": "a81383bc4e7ebbf141d2fd9cde3dcd9758bc47f27af6cbaeb0f14ab6360e08c6", }, }, Spec: policyV1.PodDisruptionBudgetSpec{ @@ -1971,9 +1976,14 @@ config: filter_strategy: relabel-config prometheus_cr: enabled: true + pod_monitor_namespace_selector: null pod_monitor_selector: null + probe_namespace_selector: null probe_selector: null + scrape_config_namespace_selector: null scrape_config_selector: null + scrape_interval: 30s + service_monitor_namespace_selector: null service_monitor_selector: null `, }, @@ -2007,7 +2017,7 @@ prometheus_cr: "app.kubernetes.io/version": "latest", }, Annotations: map[string]string{ - "opentelemetry-targetallocator-config/hash": "286a5a4e7ec6d2ce652a4ce23e135c10053b4c87fd080242daa5bf21dcd5a337", + "opentelemetry-targetallocator-config/hash": "a81383bc4e7ebbf141d2fd9cde3dcd9758bc47f27af6cbaeb0f14ab6360e08c6", }, }, Spec: corev1.PodSpec{ @@ -2139,7 +2149,7 @@ prometheus_cr: "app.kubernetes.io/version": "latest", }, Annotations: map[string]string{ - "opentelemetry-targetallocator-config/hash": "286a5a4e7ec6d2ce652a4ce23e135c10053b4c87fd080242daa5bf21dcd5a337", + "opentelemetry-targetallocator-config/hash": "a81383bc4e7ebbf141d2fd9cde3dcd9758bc47f27af6cbaeb0f14ab6360e08c6", }, }, Spec: policyV1.PodDisruptionBudgetSpec{ @@ -2475,9 +2485,14 @@ https: tls_key_file_path: /tls/tls.key prometheus_cr: enabled: true + pod_monitor_namespace_selector: null pod_monitor_selector: null + probe_namespace_selector: null probe_selector: null + scrape_config_namespace_selector: null scrape_config_selector: null + scrape_interval: 30s + service_monitor_namespace_selector: null service_monitor_selector: null `, }, @@ -2511,7 +2526,7 @@ prometheus_cr: "app.kubernetes.io/version": "latest", }, Annotations: map[string]string{ - "opentelemetry-targetallocator-config/hash": "3e2818ab54d866289de7837779e86e9c95803c43c0c4b58b25123e809ae9b771", + "opentelemetry-targetallocator-config/hash": "02ef308f21c5312c388985bd8ca91246d1df7a3a5031135ec176f3c975e2fa37", }, }, Spec: corev1.PodSpec{ @@ -2669,7 +2684,7 @@ prometheus_cr: "app.kubernetes.io/version": "latest", }, Annotations: map[string]string{ - "opentelemetry-targetallocator-config/hash": "3e2818ab54d866289de7837779e86e9c95803c43c0c4b58b25123e809ae9b771", + "opentelemetry-targetallocator-config/hash": "02ef308f21c5312c388985bd8ca91246d1df7a3a5031135ec176f3c975e2fa37", }, }, Spec: policyV1.PodDisruptionBudgetSpec{ @@ -3544,9 +3559,14 @@ config: filter_strategy: relabel-config prometheus_cr: enabled: true + pod_monitor_namespace_selector: null pod_monitor_selector: null + probe_namespace_selector: null probe_selector: null + scrape_config_namespace_selector: null scrape_config_selector: null + scrape_interval: 30s + service_monitor_namespace_selector: null service_monitor_selector: null `, }, @@ -3580,7 +3600,7 @@ prometheus_cr: "app.kubernetes.io/version": "latest", }, Annotations: map[string]string{ - "opentelemetry-targetallocator-config/hash": "f80c054419fe2f9030368557da143e200c70772d1d5f1be50ed55ae960b4b17d", + "opentelemetry-targetallocator-config/hash": "7a839fe32950e427672bf7038e88d953ceecf1531457af7c43dc78300dc85eca", }, }, Spec: corev1.PodSpec{ @@ -3712,7 +3732,7 @@ prometheus_cr: "app.kubernetes.io/version": "latest", }, Annotations: map[string]string{ - "opentelemetry-targetallocator-config/hash": "f80c054419fe2f9030368557da143e200c70772d1d5f1be50ed55ae960b4b17d", + "opentelemetry-targetallocator-config/hash": "7a839fe32950e427672bf7038e88d953ceecf1531457af7c43dc78300dc85eca", }, }, Spec: policyV1.PodDisruptionBudgetSpec{ @@ -3819,9 +3839,14 @@ config: filter_strategy: relabel-config prometheus_cr: enabled: true + pod_monitor_namespace_selector: null pod_monitor_selector: null + probe_namespace_selector: null probe_selector: null + scrape_config_namespace_selector: null scrape_config_selector: null + scrape_interval: 30s + service_monitor_namespace_selector: null service_monitor_selector: null `, }, @@ -3855,7 +3880,7 @@ prometheus_cr: "app.kubernetes.io/version": "latest", }, Annotations: map[string]string{ - "opentelemetry-targetallocator-config/hash": "f80c054419fe2f9030368557da143e200c70772d1d5f1be50ed55ae960b4b17d", + "opentelemetry-targetallocator-config/hash": "7a839fe32950e427672bf7038e88d953ceecf1531457af7c43dc78300dc85eca", }, }, Spec: corev1.PodSpec{ @@ -3987,7 +4012,7 @@ prometheus_cr: "app.kubernetes.io/version": "latest", }, Annotations: map[string]string{ - "opentelemetry-targetallocator-config/hash": "f80c054419fe2f9030368557da143e200c70772d1d5f1be50ed55ae960b4b17d", + "opentelemetry-targetallocator-config/hash": "7a839fe32950e427672bf7038e88d953ceecf1531457af7c43dc78300dc85eca", }, }, Spec: policyV1.PodDisruptionBudgetSpec{ @@ -4144,9 +4169,14 @@ config: filter_strategy: relabel-config prometheus_cr: enabled: true + pod_monitor_namespace_selector: null pod_monitor_selector: null + probe_namespace_selector: null probe_selector: null + scrape_config_namespace_selector: null scrape_config_selector: null + scrape_interval: 30s + service_monitor_namespace_selector: null service_monitor_selector: null `, }, @@ -4180,7 +4210,7 @@ prometheus_cr: "app.kubernetes.io/version": "latest", }, Annotations: map[string]string{ - "opentelemetry-targetallocator-config/hash": "286a5a4e7ec6d2ce652a4ce23e135c10053b4c87fd080242daa5bf21dcd5a337", + "opentelemetry-targetallocator-config/hash": "a81383bc4e7ebbf141d2fd9cde3dcd9758bc47f27af6cbaeb0f14ab6360e08c6", }, }, Spec: corev1.PodSpec{ @@ -4312,7 +4342,7 @@ prometheus_cr: "app.kubernetes.io/version": "latest", }, Annotations: map[string]string{ - "opentelemetry-targetallocator-config/hash": "286a5a4e7ec6d2ce652a4ce23e135c10053b4c87fd080242daa5bf21dcd5a337", + "opentelemetry-targetallocator-config/hash": "a81383bc4e7ebbf141d2fd9cde3dcd9758bc47f27af6cbaeb0f14ab6360e08c6", }, }, Spec: policyV1.PodDisruptionBudgetSpec{ @@ -4443,9 +4473,14 @@ https: tls_key_file_path: /tls/tls.key prometheus_cr: enabled: true + pod_monitor_namespace_selector: null pod_monitor_selector: null + probe_namespace_selector: null probe_selector: null + scrape_config_namespace_selector: null scrape_config_selector: null + scrape_interval: 30s + service_monitor_namespace_selector: null service_monitor_selector: null `, }, @@ -4479,7 +4514,7 @@ prometheus_cr: "app.kubernetes.io/version": "latest", }, Annotations: map[string]string{ - "opentelemetry-targetallocator-config/hash": "3e2818ab54d866289de7837779e86e9c95803c43c0c4b58b25123e809ae9b771", + "opentelemetry-targetallocator-config/hash": "02ef308f21c5312c388985bd8ca91246d1df7a3a5031135ec176f3c975e2fa37", }, }, Spec: corev1.PodSpec{ @@ -4637,7 +4672,7 @@ prometheus_cr: "app.kubernetes.io/version": "latest", }, Annotations: map[string]string{ - "opentelemetry-targetallocator-config/hash": "3e2818ab54d866289de7837779e86e9c95803c43c0c4b58b25123e809ae9b771", + "opentelemetry-targetallocator-config/hash": "02ef308f21c5312c388985bd8ca91246d1df7a3a5031135ec176f3c975e2fa37", }, }, Spec: policyV1.PodDisruptionBudgetSpec{ diff --git a/internal/manifests/targetallocator/configmap.go b/internal/manifests/targetallocator/configmap.go index 27eb606b47..ae2e29a57b 100644 --- a/internal/manifests/targetallocator/configmap.go +++ b/internal/manifests/targetallocator/configmap.go @@ -107,12 +107,20 @@ func ConfigMap(params Params) (*corev1.ConfigMap, error) { prometheusCRConfig["service_monitor_selector"] = taSpec.PrometheusCR.ServiceMonitorSelector + prometheusCRConfig["service_monitor_namespace_selector"] = taSpec.PrometheusCR.ServiceMonitorNamespaceSelector + prometheusCRConfig["pod_monitor_selector"] = taSpec.PrometheusCR.PodMonitorSelector + prometheusCRConfig["pod_monitor_namespace_selector"] = taSpec.PrometheusCR.PodMonitorNamespaceSelector + prometheusCRConfig["scrape_config_selector"] = taSpec.PrometheusCR.ScrapeConfigSelector + prometheusCRConfig["scrape_config_namespace_selector"] = taSpec.PrometheusCR.ScrapeConfigNamespaceSelector + prometheusCRConfig["probe_selector"] = taSpec.PrometheusCR.ProbeSelector + prometheusCRConfig["probe_namespace_selector"] = taSpec.PrometheusCR.ProbeNamespaceSelector + taConfig["prometheus_cr"] = prometheusCRConfig } diff --git a/internal/manifests/targetallocator/configmap_test.go b/internal/manifests/targetallocator/configmap_test.go index 7cd6318b25..11a9a7f61c 100644 --- a/internal/manifests/targetallocator/configmap_test.go +++ b/internal/manifests/targetallocator/configmap_test.go @@ -167,18 +167,22 @@ config: filter_strategy: relabel-config prometheus_cr: enabled: true + pod_monitor_namespace_selector: null pod_monitor_selector: matchlabels: release: my-instance matchexpressions: [] + probe_namespace_selector: null probe_selector: matchlabels: release: my-instance matchexpressions: [] + scrape_config_namespace_selector: null scrape_config_selector: matchlabels: release: my-instance matchexpressions: [] + service_monitor_namespace_selector: null service_monitor_selector: matchlabels: release: my-instance @@ -217,6 +221,89 @@ prometheus_cr: assert.Equal(t, expectedLabels, actual.Labels) assert.Equal(t, expectedData, actual.Data) + }) + t.Run("should return expected target allocator config map with namespace label selectors", func(t *testing.T) { + expectedData := map[string]string{ + targetAllocatorFilename: `allocation_strategy: consistent-hashing +collector_selector: + matchlabels: + app.kubernetes.io/component: opentelemetry-collector + app.kubernetes.io/instance: default.my-instance + app.kubernetes.io/managed-by: opentelemetry-operator + app.kubernetes.io/part-of: opentelemetry + matchexpressions: [] +config: + global: + scrape_interval: 30s + scrape_protocols: + - PrometheusProto + - OpenMetricsText1.0.0 + - OpenMetricsText0.0.1 + - PrometheusText0.0.4 + scrape_configs: + - job_name: otel-collector + scrape_interval: 10s + static_configs: + - targets: + - 0.0.0.0:8888 + - 0.0.0.0:9999 +filter_strategy: relabel-config +prometheus_cr: + enabled: true + pod_monitor_namespace_selector: + matchlabels: + release: my-instance + matchexpressions: [] + pod_monitor_selector: null + probe_namespace_selector: + matchlabels: + release: my-instance + matchexpressions: [] + probe_selector: null + scrape_config_namespace_selector: + matchlabels: + release: my-instance + matchexpressions: [] + scrape_config_selector: null + service_monitor_namespace_selector: + matchlabels: + release: my-instance + matchexpressions: [] + service_monitor_selector: null +`, + } + targetAllocator = targetAllocatorInstance() + targetAllocator.Spec.PrometheusCR.Enabled = true + targetAllocator.Spec.PrometheusCR.PodMonitorNamespaceSelector = &metav1.LabelSelector{ + MatchLabels: map[string]string{ + "release": "my-instance", + }, + } + targetAllocator.Spec.PrometheusCR.ServiceMonitorNamespaceSelector = &metav1.LabelSelector{ + MatchLabels: map[string]string{ + "release": "my-instance", + }} + targetAllocator.Spec.PrometheusCR.ScrapeConfigNamespaceSelector = &metav1.LabelSelector{ + MatchLabels: map[string]string{ + "release": "my-instance", + }} + targetAllocator.Spec.PrometheusCR.ProbeNamespaceSelector = &metav1.LabelSelector{ + MatchLabels: map[string]string{ + "release": "my-instance", + }} + targetAllocator.Spec.GlobalConfig = v1beta1.AnyConfig{ + Object: map[string]interface{}{ + "scrape_interval": "30s", + "scrape_protocols": []string{"PrometheusProto", "OpenMetricsText1.0.0", "OpenMetricsText0.0.1", "PrometheusText0.0.4"}, + }, + } + params.TargetAllocator = targetAllocator + actual, err := ConfigMap(params) + assert.NoError(t, err) + assert.Equal(t, "my-instance-targetallocator", actual.Name) + assert.Equal(t, expectedLabels, actual.Labels) + assert.Equal(t, expectedData, actual.Data) + }) t.Run("should return expected target allocator config map with scrape interval set", func(t *testing.T) { expectedData := map[string]string{ @@ -239,10 +326,14 @@ config: filter_strategy: relabel-config prometheus_cr: enabled: true + pod_monitor_namespace_selector: null pod_monitor_selector: null + probe_namespace_selector: null probe_selector: null + scrape_config_namespace_selector: null scrape_config_selector: null scrape_interval: 30s + service_monitor_namespace_selector: null service_monitor_selector: null `, } @@ -302,10 +393,14 @@ https: tls_key_file_path: /tls/tls.key prometheus_cr: enabled: true + pod_monitor_namespace_selector: null pod_monitor_selector: null + probe_namespace_selector: null probe_selector: null + scrape_config_namespace_selector: null scrape_config_selector: null scrape_interval: 30s + service_monitor_namespace_selector: null service_monitor_selector: null `, } @@ -361,10 +456,14 @@ https: tls_key_file_path: /tls/tls.key prometheus_cr: enabled: true + pod_monitor_namespace_selector: null pod_monitor_selector: null + probe_namespace_selector: null probe_selector: null + scrape_config_namespace_selector: null scrape_config_selector: null scrape_interval: 30s + service_monitor_namespace_selector: null service_monitor_selector: null `, }