Skip to content

Commit

Permalink
Use both collector selector formats in ta config
Browse files Browse the repository at this point in the history
This is to keep backwards compatibility with older target
allocator versions, which makes upgrades easier.
  • Loading branch information
swiatekm committed Jan 2, 2024
1 parent b3805b6 commit a1b0dd9
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 18 deletions.
24 changes: 12 additions & 12 deletions cmd/otel-allocator/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ const DefaultConfigFilePath string = "/conf/targetallocator.yaml"
const DefaultCRScrapeInterval model.Duration = model.Duration(time.Second * 30)

type Config struct {
ListenAddr string `yaml:"listen_addr,omitempty"`
KubeConfigFilePath string `yaml:"kube_config_file_path,omitempty"`
ClusterConfig *rest.Config `yaml:"-"`
RootLogger logr.Logger `yaml:"-"`
CollectorSelector metav1.LabelSelector `yaml:"collector_selector,omitempty"`
PromConfig *promconfig.Config `yaml:"config"`
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"`
ListenAddr string `yaml:"listen_addr,omitempty"`
KubeConfigFilePath string `yaml:"kube_config_file_path,omitempty"`
ClusterConfig *rest.Config `yaml:"-"`
RootLogger logr.Logger `yaml:"-"`
CollectorSelector *metav1.LabelSelector `yaml:"collector_selector,omitempty"`
PromConfig *promconfig.Config `yaml:"config"`
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"`
}

type PrometheusCRConfig struct {
Expand Down Expand Up @@ -119,7 +119,7 @@ func unmarshal(cfg *Config, configFile string) error {
if err != nil {
return err
}
if err = yaml.UnmarshalStrict(yamlFile, cfg); err != nil {
if err = yaml.Unmarshal(yamlFile, cfg); err != nil {
return fmt.Errorf("error unmarshaling YAML: %w", err)
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions cmd/otel-allocator/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestLoad(t *testing.T) {
file: "./testdata/config_test.yaml",
},
want: Config{
CollectorSelector: metav1.LabelSelector{
CollectorSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"app.kubernetes.io/instance": "default.test",
"app.kubernetes.io/managed-by": "opentelemetry-operator",
Expand Down Expand Up @@ -111,7 +111,7 @@ func TestLoad(t *testing.T) {
file: "./testdata/pod_service_selector_test.yaml",
},
want: Config{
CollectorSelector: metav1.LabelSelector{
CollectorSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"app.kubernetes.io/instance": "default.test",
"app.kubernetes.io/managed-by": "opentelemetry-operator",
Expand Down
2 changes: 1 addition & 1 deletion cmd/otel-allocator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func main() {
})
runGroup.Add(
func() error {
err := collectorWatcher.Watch(ctx, &cfg.CollectorSelector, allocator.SetCollectors)
err := collectorWatcher.Watch(ctx, cfg.CollectorSelector, allocator.SetCollectors)
setupLog.Info("Collector watcher exited")
return err
},
Expand Down
14 changes: 12 additions & 2 deletions controllers/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,11 @@ config:
source_labels:
- __meta_service_name
target_label: instance
label_selector:
app.kubernetes.io/component: opentelemetry-collector
app.kubernetes.io/instance: test.test
app.kubernetes.io/managed-by: opentelemetry-operator
app.kubernetes.io/part-of: opentelemetry
`,
},
},
Expand Down Expand Up @@ -1372,7 +1377,7 @@ config:
"app.kubernetes.io/version": "latest",
},
Annotations: map[string]string{
"opentelemetry-targetallocator-config/hash": "40afbbdb738923bf9cb4a117648cd86030cc5b748601d19120226eb1ee74c91a",
"opentelemetry-targetallocator-config/hash": "20c09760c240d08287ff05bd2375985220b577d938e82efd85467e17174690e0",
},
},
Spec: corev1.PodSpec{
Expand Down Expand Up @@ -1724,6 +1729,11 @@ config:
source_labels:
- __meta_service_name
target_label: instance
label_selector:
app.kubernetes.io/component: opentelemetry-collector
app.kubernetes.io/instance: test.test
app.kubernetes.io/managed-by: opentelemetry-operator
app.kubernetes.io/part-of: opentelemetry
`,
},
},
Expand Down Expand Up @@ -1756,7 +1766,7 @@ config:
"app.kubernetes.io/version": "latest",
},
Annotations: map[string]string{
"opentelemetry-targetallocator-config/hash": "40afbbdb738923bf9cb4a117648cd86030cc5b748601d19120226eb1ee74c91a",
"opentelemetry-targetallocator-config/hash": "20c09760c240d08287ff05bd2375985220b577d938e82efd85467e17174690e0",
},
},
Spec: corev1.PodSpec{
Expand Down
6 changes: 6 additions & 0 deletions controllers/reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,12 @@ func TestOpenTelemetryCollectorReconciler_Reconcile(t *testing.T) {
"app.kubernetes.io/part-of": "opentelemetry",
},
}
taConfig["label_selector"] = map[string]string{
"app.kubernetes.io/instance": "default.test",
"app.kubernetes.io/managed-by": "opentelemetry-operator",
"app.kubernetes.io/component": "opentelemetry-collector",
"app.kubernetes.io/part-of": "opentelemetry",
}
taConfig["config"] = promConfig["config"]
taConfig["allocation_strategy"] = "least-weighted"
taConfig["prometheus_cr"] = map[string]string{
Expand Down
6 changes: 5 additions & 1 deletion internal/manifests/targetallocator/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,13 @@ func ConfigMap(params manifests.Params) (*corev1.ConfigMap, error) {

taConfig := make(map[interface{}]interface{})
prometheusCRConfig := make(map[interface{}]interface{})
collectorSelectorLabels := manifestutils.SelectorLabels(params.OtelCol.ObjectMeta, collector.ComponentOpenTelemetryCollector)
taConfig["collector_selector"] = map[string]any{
"matchlabels": manifestutils.SelectorLabels(params.OtelCol.ObjectMeta, collector.ComponentOpenTelemetryCollector),
"matchlabels": collectorSelectorLabels,
}
// The below instruction is here for compatibility with the previous target allocator version
// TODO: Drop it after 3 more versions
taConfig["label_selector"] = collectorSelectorLabels
// We only take the "config" from the returned object, if it's present
if prometheusConfig, ok := prometheusReceiverConfig["config"]; ok {
taConfig["config"] = prometheusConfig
Expand Down
15 changes: 15 additions & 0 deletions internal/manifests/targetallocator/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ config:
- targets:
- 0.0.0.0:8888
- 0.0.0.0:9999
label_selector:
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
`,
}
instance := collectorInstance()
Expand Down Expand Up @@ -91,6 +96,11 @@ config:
- targets:
- 0.0.0.0:8888
- 0.0.0.0:9999
label_selector:
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
pod_monitor_selector:
release: my-instance
service_monitor_selector:
Expand Down Expand Up @@ -138,6 +148,11 @@ config:
- targets:
- 0.0.0.0:8888
- 0.0.0.0:9999
label_selector:
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
prometheus_cr:
scrape_interval: 30s
`,
Expand Down

0 comments on commit a1b0dd9

Please sign in to comment.