From fcad64b91c344be6babc6ea027e19277f33a6625 Mon Sep 17 00:00:00 2001 From: xigang Date: Thu, 19 Jun 2025 16:32:20 +0800 Subject: [PATCH] Add namespace-aware orphan pod indexing Signed-off-by: xigang --- pkg/controller/controller_utils.go | 9 +++++++-- pkg/controller/daemon/daemon_controller.go | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/pkg/controller/controller_utils.go b/pkg/controller/controller_utils.go index 2dc46a46ae7..4ae1e1369e1 100644 --- a/pkg/controller/controller_utils.go +++ b/pkg/controller/controller_utils.go @@ -1083,6 +1083,11 @@ func AddPodNodeNameIndexer(podInformer cache.SharedIndexInformer) error { }) } +// OrphanPodIndexKeyForNamespace returns the orphan pod index key for a specific namespace. +func OrphanPodIndexKeyForNamespace(namespace string) string { + return OrphanPodIndexKey + "/" + namespace +} + // AddPodControllerUIDIndexer adds an indexer for Pod's controllerRef.UID to the given PodInformer. // This indexer is used to efficiently look up pods by their ControllerRef.UID func AddPodControllerUIDIndexer(podInformer cache.SharedIndexInformer) error { @@ -1100,9 +1105,9 @@ func AddPodControllerUIDIndexer(podInformer cache.SharedIndexInformer) error { if ref := metav1.GetControllerOf(pod); ref != nil { return []string{string(ref.UID)}, nil } - // If the Pod has no controller (i.e., it's orphaned), index it with the OrphanPodIndexKey + // If the Pod has no controller (i.e., it's orphaned), index it with the OrphanPodIndexKeyForNamespace // This helps identify orphan pods for reconciliation and adoption by controllers - return []string{OrphanPodIndexKey}, nil + return []string{OrphanPodIndexKeyForNamespace(pod.Namespace)}, nil }, }) } diff --git a/pkg/controller/daemon/daemon_controller.go b/pkg/controller/daemon/daemon_controller.go index 3ccf0b63b38..a7d419fca99 100644 --- a/pkg/controller/daemon/daemon_controller.go +++ b/pkg/controller/daemon/daemon_controller.go @@ -696,7 +696,7 @@ func (dsc *DaemonSetsController) getDaemonPodsFromCache(ds *apps.DaemonSet) ([]* // The OrphanPodIndexKey, which helps identify orphaned Pods that are not currently managed by any controller, // but may be adopted later on if they have matching labels with the Daemonset. podsForDS := []*v1.Pod{} - for _, key := range []string{string(ds.UID), controller.OrphanPodIndexKey} { + for _, key := range []string{string(ds.UID), controller.OrphanPodIndexKeyForNamespace(ds.Namespace)} { podObjs, err := dsc.podIndexer.ByIndex(controller.PodControllerUIDIndex, key) if err != nil { return nil, err