Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions internal/controller/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ func addForDiscovery(c *k8stesting.Fake, resources []schema.GroupVersionResource
}

func initializeControllerForReconciliationTests(t *testing.T, items []ResourceAction, discoverResources []schema.GroupVersionResource) *Controller {
// Clear any collectors left registered by a prior test (see getTestController) so the
// NewController call below cannot hit a "duplicate metrics collector registration" panic.
deregisterMetrics()

createKindMap = map[string]int{}
// add schemes for various client sets
smeScheme.AddToScheme(scheme.Scheme)
Expand Down
9 changes: 9 additions & 0 deletions internal/controller/reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,15 @@ func addRuntimeObjects(objects *[]runtime.Object, object runtime.Object) {
}

func getTestController(resources testResources) *Controller {
// NewController calls initializeMetrics, which MustRegisters the collectors on the
// process-global Prometheus registry. Controller.Start deregisters them asynchronously
// on context cancellation, so a prior test's cleanup goroutine may not have run yet when
// the next controller is built here, causing a "duplicate metrics collector registration"
// panic. Clearing the registry synchronously before NewController guarantees a clean slate
// regardless of outstanding async deregisters. (In production NewController runs once, so
// this situation never arises.)
deregisterMetrics()

crdObjects := []runtime.Object{}
coreObjects := []runtime.Object{}
istioObjects := []runtime.Object{}
Expand Down
17 changes: 16 additions & 1 deletion internal/controller/rollout-manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sfake "k8s.io/client-go/kubernetes/fake"
k8stesting "k8s.io/client-go/testing"
"k8s.io/client-go/util/workqueue"
)

// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -1149,6 +1150,19 @@ func TestStartWorker_ErrorPathRestoresSecretsAndRequeues(t *testing.T) {
// ... but the credential secret is intentionally absent so generateVCAPEnv errors.

m := newFastRolloutManager(c)
// Replace the queue's rate limiter with a long, fixed backoff. On the error
// path the worker calls restoreSecrets(ns) and then AddRateLimited(ns); the
// default rate limiter re-adds the item after ~5ms, and the ensuing retry
// drainSecrets() removes the secret again within a sub-millisecond window.
// A single-shot assertion against pendingSecrets then races those retries and
// fails intermittently on loaded CI runners. A multi-second backoff keeps
// NumRequeues observable (incremented immediately by AddRateLimited) while
// ensuring no second drain fires during the test window, so the restored
// state is stable to assert.
m.queue = workqueue.NewTypedRateLimitingQueueWithConfig(
workqueue.NewTypedItemExponentialFailureRateLimiter[string](30*time.Second, 30*time.Second),
workqueue.TypedRateLimitingQueueConfig[string]{Name: "rollout-manager-test"},
)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand All @@ -1163,7 +1177,8 @@ func TestStartWorker_ErrorPathRestoresSecretsAndRequeues(t *testing.T) {
t.Fatal("item was not rate-limited after processing error")
}

// Secret must have been restored into pendingSecrets for the retry.
// Secret must have been restored into pendingSecrets for the retry. The
// long backoff above guarantees the retry has not re-drained it yet.
m.mu.Lock()
_, restored := m.pendingSecrets[metav1.NamespaceDefault]["cap-cap-01-uaa-bind-cf"]
m.mu.Unlock()
Expand Down