-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[VPA] check OwnerRef against TargetRef to confirm VPA/Pod association #6460
Changes from 4 commits
3a3b388
2bba2ba
3c47994
a5cae3b
4f9f840
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,10 +19,14 @@ package vpa | |
import ( | ||
"testing" | ||
|
||
appsv1 "k8s.io/api/apps/v1" | ||
v1 "k8s.io/api/autoscaling/v1" | ||
core "k8s.io/api/core/v1" | ||
meta "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
|
||
vpa_types "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1" | ||
controllerfetcher "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/target/controller_fetcher" | ||
target_mock "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/target/mock" | ||
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/test" | ||
|
||
|
@@ -37,8 +41,24 @@ func parseLabelSelector(selector string) labels.Selector { | |
} | ||
|
||
func TestGetMatchingVpa(t *testing.T) { | ||
podBuilder := test.Pod().WithName("test-pod").WithLabels(map[string]string{"app": "test"}). | ||
sts := appsv1.StatefulSet{ | ||
TypeMeta: meta.TypeMeta{ | ||
Kind: "StatefulSet", | ||
APIVersion: "apps/v1", | ||
}, | ||
ObjectMeta: meta.ObjectMeta{ | ||
Name: "sts", | ||
Namespace: "default", | ||
}, | ||
} | ||
targetRef := &v1.CrossVersionObjectReference{ | ||
Kind: sts.Kind, | ||
Name: sts.Name, | ||
APIVersion: sts.APIVersion, | ||
} | ||
podBuilderWithoutCreator := test.Pod().WithName("test-pod").WithLabels(map[string]string{"app": "test"}). | ||
AddContainer(test.Container().WithName("i-am-container").Get()) | ||
podBuilder := podBuilderWithoutCreator.WithCreator(&sts.ObjectMeta, &sts.TypeMeta) | ||
vpaBuilder := test.VerticalPodAutoscaler().WithContainer("i-am-container") | ||
testCases := []struct { | ||
name string | ||
|
@@ -52,33 +72,41 @@ func TestGetMatchingVpa(t *testing.T) { | |
name: "matching selector", | ||
pod: podBuilder.Get(), | ||
vpas: []*vpa_types.VerticalPodAutoscaler{ | ||
vpaBuilder.WithUpdateMode(vpa_types.UpdateModeAuto).WithName("auto-vpa").Get(), | ||
vpaBuilder.WithUpdateMode(vpa_types.UpdateModeAuto).WithName("auto-vpa").WithTargetRef(targetRef).Get(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please correct me if I am wrong. I thought the target ref of vpa is usually "Deployment" kind. E.g., https://github.com/kubernetes/autoscaler/tree/master/vertical-pod-autoscaler#example-vpa-configuration Once the code is submitted, does it still work for vpas using Deployment as target reference? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test is using a So for this test, the only important thing is that the reference set in the pod is the same that the one set in the VPA. We are left with 2 options if we stick to deployments, none reflecting really the reality: In fact there is a 3rd option that we can use in the test and the would be more correct compare to reality: we use Statefulset. Here is a commit that does that change: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to clarify, the functional code does traverse ownerRefs (pod -> replica-set -> deployment)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
}, | ||
labelSelector: "app = test", | ||
expectedFound: true, | ||
expectedVpaName: "auto-vpa", | ||
}, { | ||
name: "matching selector but not match ownerRef (orphan pod)", | ||
pod: podBuilderWithoutCreator.Get(), | ||
vpas: []*vpa_types.VerticalPodAutoscaler{ | ||
vpaBuilder.WithUpdateMode(vpa_types.UpdateModeAuto).WithName("auto-vpa").WithTargetRef(targetRef).Get(), | ||
}, | ||
labelSelector: "app = test", | ||
expectedFound: false, | ||
}, { | ||
name: "not matching selector", | ||
pod: podBuilder.Get(), | ||
vpas: []*vpa_types.VerticalPodAutoscaler{ | ||
vpaBuilder.WithUpdateMode(vpa_types.UpdateModeAuto).WithName("auto-vpa").Get(), | ||
vpaBuilder.WithUpdateMode(vpa_types.UpdateModeAuto).WithName("auto-vpa").WithTargetRef(targetRef).Get(), | ||
}, | ||
labelSelector: "app = differentApp", | ||
expectedFound: false, | ||
}, { | ||
name: "off mode", | ||
pod: podBuilder.Get(), | ||
vpas: []*vpa_types.VerticalPodAutoscaler{ | ||
vpaBuilder.WithUpdateMode(vpa_types.UpdateModeOff).WithName("off-vpa").Get(), | ||
vpaBuilder.WithUpdateMode(vpa_types.UpdateModeOff).WithName("off-vpa").WithTargetRef(targetRef).Get(), | ||
}, | ||
labelSelector: "app = test", | ||
expectedFound: false, | ||
}, { | ||
name: "two vpas one in off mode", | ||
pod: podBuilder.Get(), | ||
vpas: []*vpa_types.VerticalPodAutoscaler{ | ||
vpaBuilder.WithUpdateMode(vpa_types.UpdateModeOff).WithName("off-vpa").Get(), | ||
vpaBuilder.WithUpdateMode(vpa_types.UpdateModeAuto).WithName("auto-vpa").Get(), | ||
vpaBuilder.WithUpdateMode(vpa_types.UpdateModeOff).WithName("off-vpa").WithTargetRef(targetRef).Get(), | ||
vpaBuilder.WithUpdateMode(vpa_types.UpdateModeAuto).WithName("auto-vpa").WithTargetRef(targetRef).Get(), | ||
}, | ||
labelSelector: "app = test", | ||
expectedFound: true, | ||
|
@@ -87,7 +115,7 @@ func TestGetMatchingVpa(t *testing.T) { | |
name: "initial mode", | ||
pod: podBuilder.Get(), | ||
vpas: []*vpa_types.VerticalPodAutoscaler{ | ||
vpaBuilder.WithUpdateMode(vpa_types.UpdateModeInitial).WithName("initial-vpa").Get(), | ||
vpaBuilder.WithUpdateMode(vpa_types.UpdateModeInitial).WithName("initial-vpa").WithTargetRef(targetRef).Get(), | ||
}, | ||
labelSelector: "app = test", | ||
expectedFound: true, | ||
|
@@ -114,7 +142,7 @@ func TestGetMatchingVpa(t *testing.T) { | |
vpaLister.On("VerticalPodAutoscalers", "default").Return(vpaNamespaceLister) | ||
|
||
mockSelectorFetcher.EXPECT().Fetch(gomock.Any()).AnyTimes().Return(parseLabelSelector(tc.labelSelector), nil) | ||
matcher := NewMatcher(vpaLister, mockSelectorFetcher) | ||
matcher := NewMatcher(vpaLister, mockSelectorFetcher, controllerfetcher.FakeControllerFetcher{}) | ||
|
||
vpa := matcher.GetMatchingVPA(tc.pod) | ||
if tc.expectedFound && assert.NotNil(t, vpa) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
Copyright 2019 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package controllerfetcher | ||
|
||
// FakeControllerFetcher should be used in test only. It returns exactly the same controllerKey | ||
type FakeControllerFetcher struct{} | ||
|
||
// FindTopMostWellKnownOrScalable returns the same key for that fake implementation | ||
func (f FakeControllerFetcher) FindTopMostWellKnownOrScalable(controller *ControllerKeyWithAPIVersion) (*ControllerKeyWithAPIVersion, error) { | ||
return controller, nil | ||
} | ||
|
||
var _ ControllerFetcher = &FakeControllerFetcher{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a testing case where VPA does not select a pod when its selector matches but the target ref does not match?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test was missing, I have added a test where selector match but not the ownerRef, here it is:
a5cae3b