Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions vertical-pod-autoscaler/pkg/updater/logic/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/sets"
kube_client "k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
corescheme "k8s.io/client-go/kubernetes/scheme"
Expand Down Expand Up @@ -166,6 +167,9 @@ func (u *updater) RunOnce(ctx context.Context) {

inPlaceFeatureEnable := features.Enabled(features.InPlaceOrRecreate)

var podsList []*apiv1.Pod
seenPods := sets.New[string]()

for _, vpa := range vpaList {
if slices.Contains(u.ignoredNamespaces, vpa.Namespace) {
klog.V(3).InfoS("Skipping VPA object in ignored namespace", "vpa", klog.KObj(vpa), "namespace", vpa.Namespace)
Expand All @@ -185,6 +189,19 @@ func (u *updater) RunOnce(ctx context.Context) {
klog.V(3).InfoS("Skipping VPA object because we cannot fetch selector", "vpa", klog.KObj(vpa))
continue
}
podsWithSelector, err := u.podLister.List(selector)
if err != nil {
klog.ErrorS(err, "Failed to get pods", "selector", selector)
continue
}

// handle the case of overlapping VPA selectors
for _, pod := range podsWithSelector {
if !seenPods.Has(pod.Name) {
seenPods.Insert(pod.Name)
podsList = append(podsList, pod)
}
}

vpas = append(vpas, &vpa_api_util.VpaWithSelector{
Vpa: vpa,
Expand All @@ -200,11 +217,6 @@ func (u *updater) RunOnce(ctx context.Context) {
return
}

podsList, err := u.podLister.List(labels.Everything())
if err != nil {
klog.ErrorS(err, "Failed to get pods list")
return
}
timer.ObserveStep("ListPods")
allLivePods := filterDeletedPods(podsList)

Expand Down
7 changes: 5 additions & 2 deletions vertical-pod-autoscaler/pkg/updater/logic/updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/fake"
featuregatetesting "k8s.io/component-base/featuregate/testing"

Expand Down Expand Up @@ -232,6 +233,7 @@ func testRunOnceBase(
Get()

pods[i].Labels = labels
pods[i].UID = types.UID(fmt.Sprintf("pod-uid-%d", i))

inplace.On("CanInPlaceUpdate", pods[i]).Return(canInPlaceUpdate)
if shouldInPlaceFail {
Expand All @@ -251,7 +253,7 @@ func testRunOnceBase(
vpaLister := &test.VerticalPodAutoscalerListerMock{}

podLister := &test.PodListerMock{}
podLister.On("List").Return(pods, nil)
podLister.On("List", selector).Return(pods, nil)
targetRef := &v1.CrossVersionObjectReference{
Kind: rc.Kind,
Name: rc.Name,
Expand Down Expand Up @@ -381,6 +383,7 @@ func TestRunOnceIgnoreNamespaceMatchingPods(t *testing.T) {
Get()

pods[i].Labels = labels
pods[i].UID = types.UID(fmt.Sprintf("pod-uid-%d", i))
eviction.On("CanEvict", pods[i]).Return(true)
eviction.On("Evict", pods[i], nil).Return(nil)
}
Expand All @@ -392,7 +395,7 @@ func TestRunOnceIgnoreNamespaceMatchingPods(t *testing.T) {
vpaLister := &test.VerticalPodAutoscalerListerMock{}

podLister := &test.PodListerMock{}
podLister.On("List").Return(pods, nil)
podLister.On("List", selector).Return(pods, nil)
targetRef := &v1.CrossVersionObjectReference{
Kind: rc.Kind,
Name: rc.Name,
Expand Down
2 changes: 1 addition & 1 deletion vertical-pod-autoscaler/pkg/utils/test/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (m *PodListerMock) Pods(namespace string) v1.PodNamespaceLister {

// List is a mock implementation of PodLister.List
func (m *PodListerMock) List(selector labels.Selector) (ret []*apiv1.Pod, err error) {
args := m.Called()
args := m.Called(selector)
var returnArg []*apiv1.Pod
if args.Get(0) != nil {
returnArg = args.Get(0).([]*apiv1.Pod)
Expand Down