@@ -26,6 +26,7 @@ import (
2626 apiv1 "k8s.io/api/core/v1"
2727 "k8s.io/apimachinery/pkg/fields"
2828 "k8s.io/apimachinery/pkg/labels"
29+ "k8s.io/apimachinery/pkg/util/sets"
2930 kube_client "k8s.io/client-go/kubernetes"
3031 "k8s.io/client-go/kubernetes/fake"
3132 corescheme "k8s.io/client-go/kubernetes/scheme"
@@ -166,6 +167,8 @@ func (u *updater) RunOnce(ctx context.Context) {
166167
167168 inPlaceFeatureEnable := features .Enabled (features .InPlaceOrRecreate )
168169
170+ seenPods := sets .New [* apiv1.Pod ]()
171+
169172 for _ , vpa := range vpaList {
170173 if slices .Contains (u .ignoredNamespaces , vpa .Namespace ) {
171174 klog .V (3 ).InfoS ("Skipping VPA object in ignored namespace" , "vpa" , klog .KObj (vpa ), "namespace" , vpa .Namespace )
@@ -185,6 +188,16 @@ func (u *updater) RunOnce(ctx context.Context) {
185188 klog .V (3 ).InfoS ("Skipping VPA object because we cannot fetch selector" , "vpa" , klog .KObj (vpa ))
186189 continue
187190 }
191+ podsWithSelector , err := u .podLister .List (selector )
192+ if err != nil {
193+ klog .ErrorS (err , "Failed to get pods" , "selector" , selector )
194+ continue
195+ }
196+
197+ // handle the case of overlapping VPA selectors
198+ for _ , pod := range podsWithSelector {
199+ seenPods .Insert (pod )
200+ }
188201
189202 vpas = append (vpas , & vpa_api_util.VpaWithSelector {
190203 Vpa : vpa ,
@@ -200,13 +213,8 @@ func (u *updater) RunOnce(ctx context.Context) {
200213 return
201214 }
202215
203- podsList , err := u .podLister .List (labels .Everything ())
204- if err != nil {
205- klog .ErrorS (err , "Failed to get pods list" )
206- return
207- }
208216 timer .ObserveStep ("ListPods" )
209- allLivePods := filterDeletedPods ( podsList )
217+ allLivePods := filterDeletedPodsFromSet ( seenPods )
210218
211219 controlledPods := make (map [* vpa_types.VerticalPodAutoscaler ][]* apiv1.Pod )
212220 for _ , pod := range allLivePods {
@@ -391,10 +399,14 @@ func filterNonEvictablePods(pods []*apiv1.Pod, evictionRestriction restriction.P
391399 return filterPods (pods , evictionRestriction .CanEvict )
392400}
393401
394- func filterDeletedPods (pods []* apiv1.Pod ) []* apiv1.Pod {
395- return filterPods (pods , func (pod * apiv1.Pod ) bool {
396- return pod .DeletionTimestamp == nil
397- })
402+ func filterDeletedPodsFromSet (pods sets.Set [* apiv1.Pod ]) []* apiv1.Pod {
403+ result := make ([]* apiv1.Pod , 0 )
404+ for p := range pods {
405+ if p .DeletionTimestamp == nil {
406+ result = append (result , p )
407+ }
408+ }
409+ return result
398410}
399411
400412func newPodLister (kubeClient kube_client.Interface , namespace string ) v1lister.PodLister {
0 commit comments