Skip to content

Commit

Permalink
Use v1.LabelSelector in target allocator configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
swiatekm committed Jan 24, 2024
1 parent 0821383 commit dd73dd6
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 41 deletions.
8 changes: 4 additions & 4 deletions cmd/otel-allocator/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ type Config struct {
AllocationStrategy string `yaml:"allocation_strategy,omitempty"`
FilterStrategy string `yaml:"filter_strategy,omitempty"`
PrometheusCR PrometheusCRConfig `yaml:"prometheus_cr,omitempty"`
PodMonitorSelector map[string]string `yaml:"pod_monitor_selector,omitempty"`
ServiceMonitorSelector map[string]string `yaml:"service_monitor_selector,omitempty"`
ServiceMonitorNamespaceSelector *metav1.LabelSelector `yaml:"service_monitor_namespace_selector,omitempty"`
PodMonitorNamespaceSelector *metav1.LabelSelector `yaml:"pod_monitor_namespace_selector,omitempty"`
}

type PrometheusCRConfig struct {
Enabled bool `yaml:"enabled,omitempty"`
ScrapeInterval model.Duration `yaml:"scrape_interval,omitempty"`
Enabled bool `yaml:"enabled,omitempty"`
PodMonitorSelector *metav1.LabelSelector `yaml:"pod_monitor_selector,omitempty"`
ServiceMonitorSelector *metav1.LabelSelector `yaml:"service_monitor_selector,omitempty"`
ScrapeInterval model.Duration `yaml:"scrape_interval,omitempty"`
}

func LoadFromFile(file string, target *Config) error {
Expand Down
16 changes: 10 additions & 6 deletions cmd/otel-allocator/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ func TestLoad(t *testing.T) {
},
FilterStrategy: DefaultFilterStrategy,
PrometheusCR: PrometheusCRConfig{
PodMonitorSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"release": "test",
},
},
ServiceMonitorSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"release": "test",
},
},
ScrapeInterval: DefaultCRScrapeInterval,
},
PromConfig: &promconfig.Config{
Expand Down Expand Up @@ -160,12 +170,6 @@ func TestLoad(t *testing.T) {
},
},
},
PodMonitorSelector: map[string]string{
"release": "test",
},
ServiceMonitorSelector: map[string]string{
"release": "test",
},
},
wantErr: assert.NoError,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ collector_selector:
matchlabels:
app.kubernetes.io/instance: default.test
app.kubernetes.io/managed-by: opentelemetry-operator
pod_monitor_selector:
release: test
service_monitor_selector:
release: test
prometheus_cr:
pod_monitor_selector:
matchlabels:
release: test
service_monitor_selector:
matchlabels:
release: test
config:
scrape_configs:
- job_name: prometheus
Expand Down
10 changes: 3 additions & 7 deletions cmd/otel-allocator/watcher/promOperator.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,9 @@ func NewPrometheusCRWatcher(ctx context.Context, logger logr.Logger, cfg allocat
prom := &monitoringv1.Prometheus{
Spec: monitoringv1.PrometheusSpec{
CommonPrometheusFields: monitoringv1.CommonPrometheusFields{
ScrapeInterval: monitoringv1.Duration(cfg.PrometheusCR.ScrapeInterval.String()),
ServiceMonitorSelector: &metav1.LabelSelector{
MatchLabels: cfg.ServiceMonitorSelector,
},
PodMonitorSelector: &metav1.LabelSelector{
MatchLabels: cfg.PodMonitorSelector,
},
ScrapeInterval: monitoringv1.Duration(cfg.PrometheusCR.ScrapeInterval.String()),
ServiceMonitorSelector: cfg.PrometheusCR.ServiceMonitorSelector,
PodMonitorSelector: cfg.PrometheusCR.PodMonitorSelector,
ServiceMonitorNamespaceSelector: cfg.ServiceMonitorNamespaceSelector,
PodMonitorNamespaceSelector: cfg.PodMonitorNamespaceSelector,
},
Expand Down
73 changes: 57 additions & 16 deletions cmd/otel-allocator/watcher/promOperator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,12 @@ func TestLoadConfig(t *testing.T) {
},
},
},
cfg: allocatorconfig.Config{},
cfg: allocatorconfig.Config{
PrometheusCR: allocatorconfig.PrometheusCRConfig{
ServiceMonitorSelector: &metav1.LabelSelector{},
PodMonitorSelector: &metav1.LabelSelector{},
},
},
want: &promconfig.Config{
ScrapeConfigs: []*promconfig.ScrapeConfig{
{
Expand Down Expand Up @@ -171,7 +176,12 @@ func TestLoadConfig(t *testing.T) {
},
},
},
cfg: allocatorconfig.Config{},
cfg: allocatorconfig.Config{
PrometheusCR: allocatorconfig.PrometheusCRConfig{
ServiceMonitorSelector: &metav1.LabelSelector{},
PodMonitorSelector: &metav1.LabelSelector{},
},
},
want: &promconfig.Config{
GlobalConfig: promconfig.GlobalConfig{},
ScrapeConfigs: []*promconfig.ScrapeConfig{
Expand Down Expand Up @@ -232,7 +242,12 @@ func TestLoadConfig(t *testing.T) {
},
},
},
cfg: allocatorconfig.Config{},
cfg: allocatorconfig.Config{
PrometheusCR: allocatorconfig.PrometheusCRConfig{
ServiceMonitorSelector: &metav1.LabelSelector{},
PodMonitorSelector: &metav1.LabelSelector{},
},
},
want: &promconfig.Config{
GlobalConfig: promconfig.GlobalConfig{},
ScrapeConfigs: []*promconfig.ScrapeConfig{
Expand Down Expand Up @@ -322,7 +337,12 @@ func TestLoadConfig(t *testing.T) {
},
},
},
cfg: allocatorconfig.Config{},
cfg: allocatorconfig.Config{
PrometheusCR: allocatorconfig.PrometheusCRConfig{
ServiceMonitorSelector: &metav1.LabelSelector{},
PodMonitorSelector: &metav1.LabelSelector{},
},
},
want: &promconfig.Config{
ScrapeConfigs: []*promconfig.ScrapeConfig{
{
Expand Down Expand Up @@ -424,7 +444,12 @@ func TestLoadConfig(t *testing.T) {
},
},
},
cfg: allocatorconfig.Config{},
cfg: allocatorconfig.Config{
PrometheusCR: allocatorconfig.PrometheusCRConfig{
ServiceMonitorSelector: &metav1.LabelSelector{},
PodMonitorSelector: &metav1.LabelSelector{},
},
},
want: &promconfig.Config{
ScrapeConfigs: []*promconfig.ScrapeConfig{
{
Expand Down Expand Up @@ -506,8 +531,12 @@ func TestLoadConfig(t *testing.T) {
},
},
cfg: allocatorconfig.Config{
ServiceMonitorSelector: map[string]string{
"testsvc": "testsvc",
PrometheusCR: allocatorconfig.PrometheusCRConfig{
ServiceMonitorSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"testsvc": "testsvc",
},
},
},
},
want: &promconfig.Config{
Expand Down Expand Up @@ -571,8 +600,12 @@ func TestLoadConfig(t *testing.T) {
},
},
cfg: allocatorconfig.Config{
PodMonitorSelector: map[string]string{
"testpod": "testpod",
PrometheusCR: allocatorconfig.PrometheusCRConfig{
PodMonitorSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"testpod": "testpod",
},
},
},
},
want: &promconfig.Config{
Expand Down Expand Up @@ -633,6 +666,10 @@ func TestLoadConfig(t *testing.T) {
},
},
cfg: allocatorconfig.Config{
PrometheusCR: allocatorconfig.PrometheusCRConfig{
ServiceMonitorSelector: &metav1.LabelSelector{},
PodMonitorSelector: &metav1.LabelSelector{},
},
ServiceMonitorNamespaceSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"label1": "label1",
Expand Down Expand Up @@ -697,6 +734,10 @@ func TestLoadConfig(t *testing.T) {
},
},
cfg: allocatorconfig.Config{
PrometheusCR: allocatorconfig.PrometheusCRConfig{
ServiceMonitorSelector: &metav1.LabelSelector{},
PodMonitorSelector: &metav1.LabelSelector{},
},
PodMonitorNamespaceSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"label1": "label1",
Expand Down Expand Up @@ -794,6 +835,10 @@ func TestNamespaceLabelUpdate(t *testing.T) {
}

cfg := allocatorconfig.Config{
PrometheusCR: allocatorconfig.PrometheusCRConfig{
ServiceMonitorSelector: &metav1.LabelSelector{},
PodMonitorSelector: &metav1.LabelSelector{},
},
PodMonitorNamespaceSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"label1": "label1",
Expand Down Expand Up @@ -1011,13 +1056,9 @@ func getTestPrometheusCRWatcher(t *testing.T, svcMonitors []*monitoringv1.Servic
prom := &monitoringv1.Prometheus{
Spec: monitoringv1.PrometheusSpec{
CommonPrometheusFields: monitoringv1.CommonPrometheusFields{
ScrapeInterval: monitoringv1.Duration("30s"),
ServiceMonitorSelector: &metav1.LabelSelector{
MatchLabels: cfg.ServiceMonitorSelector,
},
PodMonitorSelector: &metav1.LabelSelector{
MatchLabels: cfg.PodMonitorSelector,
},
ScrapeInterval: monitoringv1.Duration("30s"),
ServiceMonitorSelector: cfg.PrometheusCR.ServiceMonitorSelector,
PodMonitorSelector: cfg.PrometheusCR.PodMonitorSelector,
ServiceMonitorNamespaceSelector: cfg.ServiceMonitorNamespaceSelector,
PodMonitorNamespaceSelector: cfg.PodMonitorNamespaceSelector,
},
Expand Down
18 changes: 16 additions & 2 deletions controllers/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,13 @@ label_selector:
app.kubernetes.io/instance: test.test
app.kubernetes.io/managed-by: opentelemetry-operator
app.kubernetes.io/part-of: opentelemetry
prometheus_cr:
pod_monitor_selector:
matchlabels: {}
matchexpressions: []
service_monitor_selector:
matchlabels: {}
matchexpressions: []
`,
},
},
Expand Down Expand Up @@ -1384,7 +1391,7 @@ label_selector:
"app.kubernetes.io/version": "latest",
},
Annotations: map[string]string{
"opentelemetry-targetallocator-config/hash": "bf084cbbdcb09d03a40ad2352e0869ccf75d01f5dec977938b94d5a3239ea491",
"opentelemetry-targetallocator-config/hash": "51477b182d2c9e7c0db27a2cbc9c7d35b24895b1cf0774d51a41b8d1753696ed",
},
},
Spec: corev1.PodSpec{
Expand Down Expand Up @@ -1744,6 +1751,13 @@ label_selector:
app.kubernetes.io/instance: test.test
app.kubernetes.io/managed-by: opentelemetry-operator
app.kubernetes.io/part-of: opentelemetry
prometheus_cr:
pod_monitor_selector:
matchlabels: {}
matchexpressions: []
service_monitor_selector:
matchlabels: {}
matchexpressions: []
`,
},
},
Expand Down Expand Up @@ -1776,7 +1790,7 @@ label_selector:
"app.kubernetes.io/version": "latest",
},
Annotations: map[string]string{
"opentelemetry-targetallocator-config/hash": "bf084cbbdcb09d03a40ad2352e0869ccf75d01f5dec977938b94d5a3239ea491",
"opentelemetry-targetallocator-config/hash": "51477b182d2c9e7c0db27a2cbc9c7d35b24895b1cf0774d51a41b8d1753696ed",
},
},
Spec: corev1.PodSpec{
Expand Down
6 changes: 4 additions & 2 deletions controllers/reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,10 @@ func TestOpenTelemetryCollectorReconciler_Reconcile(t *testing.T) {
taConfig["config"] = promConfig["config"]
taConfig["allocation_strategy"] = "consistent-hashing"
taConfig["filter_strategy"] = "relabel-config"
taConfig["prometheus_cr"] = map[string]string{
"scrape_interval": "30s",
taConfig["prometheus_cr"] = map[string]any{
"scrape_interval": "30s",
"pod_monitor_selector": &metav1.LabelSelector{},
"service_monitor_selector": &metav1.LabelSelector{},
}
taConfigYAML, _ := yaml.Marshal(taConfig)
assert.Equal(t, string(taConfigYAML), actual.Data["targetallocator.yaml"])
Expand Down
10 changes: 10 additions & 0 deletions internal/manifests/targetallocator/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,20 @@ func ConfigMap(params manifests.Params) (*corev1.ConfigMap, error) {
prometheusCRConfig["scrape_interval"] = params.OtelCol.Spec.TargetAllocator.PrometheusCR.ScrapeInterval.Duration
}

prometheusCRConfig["service_monitor_selector"] = &metav1.LabelSelector{
MatchLabels: params.OtelCol.Spec.TargetAllocator.PrometheusCR.ServiceMonitorSelector,
}
// The below instruction is here for compatibility with the previous target allocator version
// TODO: Drop it after 3 more versions
if params.OtelCol.Spec.TargetAllocator.PrometheusCR.ServiceMonitorSelector != nil {
taConfig["service_monitor_selector"] = &params.OtelCol.Spec.TargetAllocator.PrometheusCR.ServiceMonitorSelector
}

prometheusCRConfig["pod_monitor_selector"] = &metav1.LabelSelector{
MatchLabels: params.OtelCol.Spec.TargetAllocator.PrometheusCR.PodMonitorSelector,
}
// The below instruction is here for compatibility with the previous target allocator version
// TODO: Drop it after 3 more versions
if params.OtelCol.Spec.TargetAllocator.PrometheusCR.PodMonitorSelector != nil {
taConfig["pod_monitor_selector"] = &params.OtelCol.Spec.TargetAllocator.PrometheusCR.PodMonitorSelector
}
Expand Down
22 changes: 22 additions & 0 deletions internal/manifests/targetallocator/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ label_selector:
app.kubernetes.io/instance: default.my-instance
app.kubernetes.io/managed-by: opentelemetry-operator
app.kubernetes.io/part-of: opentelemetry
prometheus_cr:
pod_monitor_selector:
matchlabels: {}
matchexpressions: []
service_monitor_selector:
matchlabels: {}
matchexpressions: []
`,
}
instance := collectorInstance()
Expand Down Expand Up @@ -105,6 +112,15 @@ label_selector:
app.kubernetes.io/part-of: opentelemetry
pod_monitor_selector:
release: my-instance
prometheus_cr:
pod_monitor_selector:
matchlabels:
release: my-instance
matchexpressions: []
service_monitor_selector:
matchlabels:
release: my-instance
matchexpressions: []
service_monitor_selector:
release: my-instance
`,
Expand Down Expand Up @@ -157,7 +173,13 @@ label_selector:
app.kubernetes.io/managed-by: opentelemetry-operator
app.kubernetes.io/part-of: opentelemetry
prometheus_cr:
pod_monitor_selector:
matchlabels: {}
matchexpressions: []
scrape_interval: 30s
service_monitor_selector:
matchlabels: {}
matchexpressions: []
`,
}

Expand Down

0 comments on commit dd73dd6

Please sign in to comment.