diff --git a/.chloggen/fix_load-initial-servicemonitors.yaml b/.chloggen/fix_load-initial-servicemonitors.yaml new file mode 100755 index 0000000000..7466a937e2 --- /dev/null +++ b/.chloggen/fix_load-initial-servicemonitors.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. collector, target allocator, auto-instrumentation, opamp, github action) +component: target allocator + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Fix target allocator readiness check + +# One or more tracking issues related to the change +issues: [2903] + +# (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/cmd/otel-allocator/main.go b/cmd/otel-allocator/main.go index 8da384f88a..a8e57da102 100644 --- a/cmd/otel-allocator/main.go +++ b/cmd/otel-allocator/main.go @@ -111,6 +111,17 @@ func main() { setupLog.Error(err, "Can't start the prometheus watcher") os.Exit(1) } + // apply the initial configuration + promConfig, loadErr := promWatcher.LoadConfig(ctx) + if loadErr != nil { + setupLog.Error(err, "Can't load initial Prometheus configuration from Prometheus CRs") + os.Exit(1) + } + loadErr = targetDiscoverer.ApplyConfig(allocatorWatcher.EventSourcePrometheusCR, promConfig.ScrapeConfigs) + if loadErr != nil { + setupLog.Error(err, "Can't load initial scrape targets from Prometheus CRs") + os.Exit(1) + } runGroup.Add( func() error { promWatcherErr := promWatcher.Watch(eventChan, errChan) @@ -138,7 +149,7 @@ func main() { runGroup.Add( func() error { // Initial loading of the config file's scrape config - if cfg.PromConfig != nil { + if cfg.PromConfig != nil && len(cfg.PromConfig.ScrapeConfigs) > 0 { err = targetDiscoverer.ApplyConfig(allocatorWatcher.EventSourceConfigMap, cfg.PromConfig.ScrapeConfigs) if err != nil { setupLog.Error(err, "Unable to apply initial configuration") diff --git a/internal/manifests/targetallocator/configmap.go b/internal/manifests/targetallocator/configmap.go index db59e37e48..09a3c5f48f 100644 --- a/internal/manifests/targetallocator/configmap.go +++ b/internal/manifests/targetallocator/configmap.go @@ -40,7 +40,7 @@ func ConfigMap(params manifests.Params) (*corev1.ConfigMap, error) { taConfig["collector_selector"] = taSpec.CollectorSelector // Add scrape configs if present - if instance.Spec.ScrapeConfigs != nil { + if instance.Spec.ScrapeConfigs != nil && len(instance.Spec.ScrapeConfigs) > 0 { taConfig["config"] = map[string]interface{}{ "scrape_configs": instance.Spec.ScrapeConfigs, } diff --git a/internal/manifests/targetallocator/configmap_test.go b/internal/manifests/targetallocator/configmap_test.go index 26a743843a..05992196cc 100644 --- a/internal/manifests/targetallocator/configmap_test.go +++ b/internal/manifests/targetallocator/configmap_test.go @@ -23,6 +23,7 @@ import ( "github.com/stretchr/testify/require" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "github.com/open-telemetry/opentelemetry-operator/apis/v1beta1" "github.com/open-telemetry/opentelemetry-operator/internal/config" "github.com/open-telemetry/opentelemetry-operator/internal/manifests" ) @@ -78,6 +79,43 @@ prometheus_cr: assert.Equal(t, expectedLabels, actual.Labels) assert.Equal(t, expectedData[targetAllocatorFilename], actual.Data[targetAllocatorFilename]) + }) + t.Run("should return target allocator config map without scrape configs", func(t *testing.T) { + expectedLabels["app.kubernetes.io/component"] = "opentelemetry-targetallocator" + expectedLabels["app.kubernetes.io/name"] = "my-instance-targetallocator" + + 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: [] +filter_strategy: relabel-config +prometheus_cr: + pod_monitor_selector: null + service_monitor_selector: null +`, + } + collector := collectorInstance() + targetAllocator := targetAllocatorInstance() + targetAllocator.Spec.ScrapeConfigs = []v1beta1.AnyConfig{} + cfg := config.New() + params := manifests.Params{ + OtelCol: collector, + TargetAllocator: targetAllocator, + Config: cfg, + Log: logr.Discard(), + } + actual, err := ConfigMap(params) + require.NoError(t, err) + + assert.Equal(t, "my-instance-targetallocator", actual.Name) + assert.Equal(t, expectedLabels, actual.Labels) + assert.Equal(t, expectedData[targetAllocatorFilename], actual.Data[targetAllocatorFilename]) + }) t.Run("should return expected target allocator config map with label selectors", func(t *testing.T) { expectedLabels["app.kubernetes.io/component"] = "opentelemetry-targetallocator" diff --git a/tests/e2e-targetallocator/targetallocator-prometheuscr/00-install.yaml b/tests/e2e-targetallocator/targetallocator-prometheuscr/00-install.yaml index 8a693e0213..65a9807454 100644 --- a/tests/e2e-targetallocator/targetallocator-prometheuscr/00-install.yaml +++ b/tests/e2e-targetallocator/targetallocator-prometheuscr/00-install.yaml @@ -155,16 +155,5 @@ spec: enabled: true prometheusCR: enabled: true - scrapeInterval: 1s + scrapeInterval: 1s serviceAccount: ta ---- -apiVersion: monitoring.coreos.com/v1 -kind: ServiceMonitor -metadata: - name: prometheus-cr -spec: - endpoints: - - port: monitoring - selector: - matchLabels: - app.kubernetes.io/managed-by: opentelemetry-operator diff --git a/tests/e2e-targetallocator/targetallocator-prometheuscr/01-install.yaml b/tests/e2e-targetallocator/targetallocator-prometheuscr/01-install.yaml index 1d26ae47d2..f2142f1a7a 100644 --- a/tests/e2e-targetallocator/targetallocator-prometheuscr/01-install.yaml +++ b/tests/e2e-targetallocator/targetallocator-prometheuscr/01-install.yaml @@ -1,4 +1,15 @@ --- +apiVersion: monitoring.coreos.com/v1 +kind: ServiceMonitor +metadata: + name: prometheus-cr +spec: + endpoints: + - port: monitoring + selector: + matchLabels: + app.kubernetes.io/managed-by: opentelemetry-operator +--- apiVersion: batch/v1 kind: Job metadata: