Skip to content

Commit

Permalink
feat: expose namespace selector labels conf
Browse files Browse the repository at this point in the history
  • Loading branch information
dexter0195 authored and alessio-trivisonno-shift committed Dec 23, 2024
1 parent c399686 commit a0f8b82
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
24 changes: 24 additions & 0 deletions apis/v1beta1/targetallocator_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
8 changes: 8 additions & 0 deletions internal/manifests/targetallocator/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
83 changes: 83 additions & 0 deletions internal/manifests/targetallocator/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,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{
Expand Down

0 comments on commit a0f8b82

Please sign in to comment.