Skip to content

Commit c005b85

Browse files
Reduce locking duration on cache to fetch data from Cache
1 parent 838f3c0 commit c005b85

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

pkg/controller/statefulset/stateful_set.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ type StatefulSetController struct {
6161
control StatefulSetControlInterface
6262
// podControl is used for patching pods.
6363
podControl controller.PodControlInterface
64+
// podIndexer allows looking up pods by ControllerRef UID
65+
podIndexer cache.Indexer
6466
// podLister is able to list/get pods from a shared informer's store
6567
podLister corelisters.PodLister
6668
// podListerSynced returns true if the pod shared informer has synced at least once
@@ -129,7 +131,8 @@ func NewStatefulSetController(
129131
})
130132
ssc.podLister = podInformer.Lister()
131133
ssc.podListerSynced = podInformer.Informer().HasSynced
132-
134+
controller.AddPodControllerUIDIndexer(podInformer.Informer())
135+
ssc.podIndexer = podInformer.Informer().GetIndexer()
133136
setInformer.Informer().AddEventHandler(
134137
cache.ResourceEventHandlerFuncs{
135138
AddFunc: ssc.enqueueStatefulSet,
@@ -309,11 +312,24 @@ func (ssc *StatefulSetController) deletePod(logger klog.Logger, obj interface{})
309312
// NOTE: Returned Pods are pointers to objects from the cache.
310313
// If you need to modify one, you need to copy it first.
311314
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+
}
317333
}
318334

319335
filter := func(pod *v1.Pod) bool {
@@ -322,7 +338,7 @@ func (ssc *StatefulSetController) getPodsForStatefulSet(ctx context.Context, set
322338
}
323339

324340
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)
326342
}
327343

328344
// If any adoptions are attempted, we should first recheck for deletion with

0 commit comments

Comments
 (0)