Add namespace-aware orphan pod indexing

Signed-off-by: xigang <wangxigang2014@gmail.com>
This commit is contained in:
xigang
2025-06-19 16:32:20 +08:00
committed by Jordan Liggitt
parent de4a5c1099
commit fcad64b91c
2 changed files with 8 additions and 3 deletions

View File

@@ -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
},
})
}

View File

@@ -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