From 7829f8b6aa7f49a4a379420f2cf5ea8e48a070eb Mon Sep 17 00:00:00 2001 From: dlotterman Date: Wed, 18 Nov 2026 20:47:51 +0000 Subject: [PATCH] fix: Reconcile terminating objects that lost the replicator label selector gcSecretSyncLabels removes the gateway-sync label from Secrets once no SecurityPolicy references them, but the replicator finalizer stays on the Secret. The upstream watch filters events by the label selector, so a later delete of that Secret never reaches finalizeResource and the Secret stays Terminating forever. Also enqueue objects that are terminating and still hold the replicator finalizer, regardless of the label selector. --- .../gateway_resource_replicator_controller.go | 6 ++++- ...way_resource_replicator_controller_test.go | 25 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/internal/controller/gateway_resource_replicator_controller.go b/internal/controller/gateway_resource_replicator_controller.go index b25b1f12..d9a16426 100644 --- a/internal/controller/gateway_resource_replicator_controller.go +++ b/internal/controller/gateway_resource_replicator_controller.go @@ -1123,7 +1123,7 @@ func typedEnqueueRequestForGVK( ) mchandler.TypedEventHandlerFunc[client.Object, GVKRequest] { return func(clusterName multicluster.ClusterName, cl cluster.Cluster) handler.TypedEventHandler[client.Object, GVKRequest] { return handler.TypedEnqueueRequestsFromMapFunc(func(ctx context.Context, obj client.Object) []GVKRequest { - if selector != nil && !selector.Matches(labels.Set(obj.GetLabels())) { + if selector != nil && !selector.Matches(labels.Set(obj.GetLabels())) && !isTerminatingWithReplicatorFinalizer(obj) { return nil } @@ -1145,6 +1145,10 @@ func typedEnqueueRequestForGVK( } } +func isTerminatingWithReplicatorFinalizer(obj client.Object) bool { + return !obj.GetDeletionTimestamp().IsZero() && controllerutil.ContainsFinalizer(obj, gatewayResourceReplicatorFinalizer) +} + func gvkKey(gvk schema.GroupVersionKind) string { return gvk.String() } diff --git a/internal/controller/gateway_resource_replicator_controller_test.go b/internal/controller/gateway_resource_replicator_controller_test.go index acabe718..2b222dec 100644 --- a/internal/controller/gateway_resource_replicator_controller_test.go +++ b/internal/controller/gateway_resource_replicator_controller_test.go @@ -261,6 +261,31 @@ func TestTypedEnqueueRequestForGVKFiltersBySelector(t *testing.T) { assert.Equal(t, testGVK, item.GVK, "unexpected GVK enqueued") } +func TestTypedEnqueueRequestForGVKEnqueuesTerminatingObjectWithFinalizer(t *testing.T) { + selector := labels.SelectorFromSet(map[string]string{"replicate": "true"}) + h := typedEnqueueRequestForGVK(testGVK, selector)("upstream", nil) + queue := workqueue.NewTypedRateLimitingQueue(workqueue.DefaultTypedControllerRateLimiter[GVKRequest]()) + t.Cleanup(queue.ShutDown) + + ctx := context.Background() + now := metav1.Now() + + terminating := newUnstructuredObject("workloads", "example", nil, nil) + terminating.SetDeletionTimestamp(&now) + terminating.SetFinalizers([]string{"other.example.com/finalizer"}) + h.Update(ctx, event.TypedUpdateEvent[client.Object]{ObjectOld: terminating, ObjectNew: terminating}, queue) + assert.Equal(t, 0, queue.Len(), "terminating object without replicator finalizer must stay filtered") + + terminating.SetFinalizers([]string{gatewayResourceReplicatorFinalizer}) + h.Update(ctx, event.TypedUpdateEvent[client.Object]{ObjectOld: terminating, ObjectNew: terminating}, queue) + assert.Equal(t, 1, queue.Len(), "terminating object holding the replicator finalizer must be enqueued despite lost label") + + live := newUnstructuredObject("workloads", "other", nil, nil) + live.SetFinalizers([]string{gatewayResourceReplicatorFinalizer}) + h.Create(ctx, event.TypedCreateEvent[client.Object]{Object: live}, queue) + assert.Equal(t, 1, queue.Len(), "non-terminating unlabeled object must stay filtered") +} + func TestReplicatorCleansDownstreamOnDelete(t *testing.T) { scheme := runtime.NewScheme() err := corev1.AddToScheme(scheme)