@@ -61,6 +61,8 @@ type StatefulSetController struct {
61
61
control StatefulSetControlInterface
62
62
// podControl is used for patching pods.
63
63
podControl controller.PodControlInterface
64
+ // podIndexer allows looking up pods by ControllerRef UID
65
+ podIndexer cache.Indexer
64
66
// podLister is able to list/get pods from a shared informer's store
65
67
podLister corelisters.PodLister
66
68
// podListerSynced returns true if the pod shared informer has synced at least once
@@ -129,7 +131,8 @@ func NewStatefulSetController(
129
131
})
130
132
ssc .podLister = podInformer .Lister ()
131
133
ssc .podListerSynced = podInformer .Informer ().HasSynced
132
-
134
+ controller .AddPodControllerUIDIndexer (podInformer .Informer ())
135
+ ssc .podIndexer = podInformer .Informer ().GetIndexer ()
133
136
setInformer .Informer ().AddEventHandler (
134
137
cache.ResourceEventHandlerFuncs {
135
138
AddFunc : ssc .enqueueStatefulSet ,
@@ -309,11 +312,24 @@ func (ssc *StatefulSetController) deletePod(logger klog.Logger, obj interface{})
309
312
// NOTE: Returned Pods are pointers to objects from the cache.
310
313
// If you need to modify one, you need to copy it first.
311
314
func (ssc * StatefulSetController ) getPodsForStatefulSet (ctx context.Context , set * apps.StatefulSet , selector labels.Selector ) ([]* v1.Pod , error ) {
312
- // List all pods to include the pods that don't match the selector anymore but
313
- // has a ControllerRef pointing to this StatefulSet.
314
- pods , err := ssc .podLister .Pods (set .Namespace ).List (labels .Everything ())
315
- if err != nil {
316
- return nil , err
315
+ // Iterate over two keys:
316
+ // The UID of the StatefulSet, which identifies Pods that are controlled by the StatefulSet.
317
+ // The OrphanPodIndexKey, which helps identify orphaned Pods that are not currently managed by any controller,
318
+ // but may be adopted later on if they have matching labels with the StatefulSet.
319
+ podsForSts := []* v1.Pod {}
320
+ for _ , key := range []string {string (set .UID ), controller .OrphanPodIndexKey } {
321
+ podObjs , err := ssc .podIndexer .ByIndex (controller .PodControllerUIDIndex , key )
322
+ if err != nil {
323
+ return nil , err
324
+ }
325
+ for _ , obj := range podObjs {
326
+ pod , ok := obj .(* v1.Pod )
327
+ if ! ok {
328
+ utilruntime .HandleError (fmt .Errorf ("unexpected object type in pod indexer: %v" , obj ))
329
+ continue
330
+ }
331
+ podsForSts = append (podsForSts , pod )
332
+ }
317
333
}
318
334
319
335
filter := func (pod * v1.Pod ) bool {
@@ -322,7 +338,7 @@ func (ssc *StatefulSetController) getPodsForStatefulSet(ctx context.Context, set
322
338
}
323
339
324
340
cm := controller .NewPodControllerRefManager (ssc .podControl , set , selector , controllerKind , ssc .canAdoptFunc (ctx , set ))
325
- return cm .ClaimPods (ctx , pods , filter )
341
+ return cm .ClaimPods (ctx , podsForSts , filter )
326
342
}
327
343
328
344
// If any adoptions are attempted, we should first recheck for deletion with
0 commit comments