diff --git a/controllers/opentelemetrycollector_controller.go b/controllers/opentelemetrycollector_controller.go index 9f6d063f8b..b447c193a9 100644 --- a/controllers/opentelemetrycollector_controller.go +++ b/controllers/opentelemetrycollector_controller.go @@ -113,7 +113,6 @@ func (r *OpenTelemetryCollectorReconciler) findOtelOwnedObjects(ctx context.Cont default: } } - // at this point we don't know if the most recent ConfigMap will still be the most recent after reconciliation, or // if a new one will be created. We keep one additional ConfigMap to account for this. The next reconciliation that // doesn't spawn a new ConfigMap will delete the extra one we kept here. diff --git a/controllers/reconcile_test.go b/controllers/reconcile_test.go index b944d094bf..d110edb6dc 100644 --- a/controllers/reconcile_test.go +++ b/controllers/reconcile_test.go @@ -978,7 +978,8 @@ func TestOpenTelemetryCollectorReconciler_VersionedConfigMaps(t *testing.T) { assert.EventuallyWithT(t, func(collect *assert.CollectT) { configMaps := &v1.ConfigMapList{} - listErr := k8sClient.List(clientCtx, configMaps, opts...) + // use the reconciler client here to ensure it sees the new ConfigMap, before running the next reconciliation + listErr := reconciler.Client.List(clientCtx, configMaps, opts...) assert.NoError(collect, listErr) assert.NotEmpty(collect, configMaps) assert.Len(collect, configMaps.Items, 4) @@ -992,8 +993,12 @@ func TestOpenTelemetryCollectorReconciler_VersionedConfigMaps(t *testing.T) { listErr := k8sClient.List(clientCtx, configMaps, opts...) assert.NoError(collect, listErr) assert.NotEmpty(collect, configMaps) - assert.Len(collect, configMaps.Items, 3) - }, time.Second*5, time.Millisecond) + // actual deletion can happen with a delay in a K8s cluster, check the timestamp instead to speed things up + items := slices.DeleteFunc(configMaps.Items, func(item v1.ConfigMap) bool { + return item.DeletionTimestamp != nil + }) + assert.Len(collect, items, 3) + }, time.Second*30, time.Second) // not sure why this can take so long to bubble up } func TestOpAMPBridgeReconciler_Reconcile(t *testing.T) { diff --git a/controllers/suite_test.go b/controllers/suite_test.go index c32b101280..fa7329b9f3 100644 --- a/controllers/suite_test.go +++ b/controllers/suite_test.go @@ -48,6 +48,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/envtest" logf "sigs.k8s.io/controller-runtime/pkg/log" + "sigs.k8s.io/controller-runtime/pkg/log/zap" metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" "sigs.k8s.io/controller-runtime/pkg/webhook" "sigs.k8s.io/yaml" @@ -139,6 +140,9 @@ func TestMain(m *testing.M) { ctx, cancel = context.WithCancel(context.TODO()) defer cancel() + // logging is useful for these tests + logf.SetLogger(zap.New()) + // +kubebuilder:scaffold:scheme utilruntime.Must(monitoringv1.AddToScheme(testScheme)) utilruntime.Must(networkingv1.AddToScheme(testScheme))