@@ -113,7 +113,7 @@ type DaemonSetsController struct {
113
113
historyStoreSynced cache.InformerSynced
114
114
// podLister get list/get pods from the shared informers's store
115
115
podLister corelisters.PodLister
116
- // podIndexer allows looking up pods by node name.
116
+ // podIndexer allows looking up pods by node name or by ControllerRef UID
117
117
podIndexer cache.Indexer
118
118
// podStoreSynced returns true if the pod store has been synced at least once.
119
119
// Added as a member to the struct to allow injection for testing.
@@ -217,6 +217,7 @@ func NewDaemonSetsController(
217
217
dsc .podLister = podInformer .Lister ()
218
218
dsc .podStoreSynced = podInformer .Informer ().HasSynced
219
219
controller .AddPodNodeNameIndexer (podInformer .Informer ())
220
+ controller .AddPodControllerUIDIndexer (podInformer .Informer ())
220
221
dsc .podIndexer = podInformer .Informer ().GetIndexer ()
221
222
222
223
nodeInformer .Informer ().AddEventHandler (cache.ResourceEventHandlerFuncs {
@@ -688,6 +689,30 @@ func (dsc *DaemonSetsController) updateNode(logger klog.Logger, old, cur interfa
688
689
dsc .nodeUpdateQueue .Add (curNode .Name )
689
690
}
690
691
692
+ // getPodsFromCache returns the Pods that a given DS should manage.
693
+ func (dsc * DaemonSetsController ) getDaemonPodsFromCache (ds * apps.DaemonSet ) ([]* v1.Pod , error ) {
694
+ // Iterate over two keys:
695
+ // The UID of the Daemonset, which identifies Pods that are controlled by the Daemonset.
696
+ // The OrphanPodIndexKey, which helps identify orphaned Pods that are not currently managed by any controller,
697
+ // but may be adopted later on if they have matching labels with the Daemonset.
698
+ podsForDS := []* v1.Pod {}
699
+ for _ , key := range []string {string (ds .UID ), controller .OrphanPodIndexKey } {
700
+ podObjs , err := dsc .podIndexer .ByIndex (controller .PodControllerUIDIndex , key )
701
+ if err != nil {
702
+ return nil , err
703
+ }
704
+ for _ , obj := range podObjs {
705
+ pod , ok := obj .(* v1.Pod )
706
+ if ! ok {
707
+ utilruntime .HandleError (fmt .Errorf ("unexpected object type in pod indexer: %v" , obj ))
708
+ continue
709
+ }
710
+ podsForDS = append (podsForDS , pod )
711
+ }
712
+ }
713
+ return podsForDS , nil
714
+ }
715
+
691
716
// getDaemonPods returns daemon pods owned by the given ds.
692
717
// This also reconciles ControllerRef by adopting/orphaning.
693
718
// Note that returned Pods are pointers to objects in the cache.
@@ -697,10 +722,8 @@ func (dsc *DaemonSetsController) getDaemonPods(ctx context.Context, ds *apps.Dae
697
722
if err != nil {
698
723
return nil , err
699
724
}
700
-
701
- // List all pods to include those that don't match the selector anymore but
702
- // have a ControllerRef pointing to this controller.
703
- pods , err := dsc .podLister .Pods (ds .Namespace ).List (labels .Everything ())
725
+ // List all pods indexed to DS UID and Orphan pods
726
+ pods , err := dsc .getDaemonPodsFromCache (ds )
704
727
if err != nil {
705
728
return nil , err
706
729
}
0 commit comments