diff --git a/vertical-pod-autoscaler/pkg/admission-controller/logic/recommendation_provider_test.go b/vertical-pod-autoscaler/pkg/admission-controller/logic/recommendation_provider_test.go index 218a42fdea87..ef9c8537d62a 100644 --- a/vertical-pod-autoscaler/pkg/admission-controller/logic/recommendation_provider_test.go +++ b/vertical-pod-autoscaler/pkg/admission-controller/logic/recommendation_provider_test.go @@ -47,12 +47,18 @@ func mustParseResourcePointer(val string) *resource.Quantity { } type fakeLimitRangeCalculator struct { - limitRange *apiv1.LimitRangeItem - err error + containerLimitRange *apiv1.LimitRangeItem + containerErr error + podLimitRange *apiv1.LimitRangeItem + podErr error } func (nlrc *fakeLimitRangeCalculator) GetContainerLimitRangeItem(namespace string) (*apiv1.LimitRangeItem, error) { - return nlrc.limitRange, nlrc.err + return nlrc.containerLimitRange, nlrc.containerErr +} + +func (nlrc *fakeLimitRangeCalculator) GetPodLimitRangeItem(namespace string) (*apiv1.LimitRangeItem, error) { + return nlrc.podLimitRange, nlrc.podErr } func TestUpdateResourceRequests(t *testing.T) { @@ -333,8 +339,8 @@ func TestUpdateResourceRequests(t *testing.T) { recommendationProcessor: api.NewCappingRecommendationProcessor(limitrange.NewNoopLimitsCalculator()), selectorFetcher: mockSelectorFetcher, limitsRangeCalculator: &fakeLimitRangeCalculator{ - tc.limitRange, - tc.limitRangeCalcErr, + containerLimitRange: tc.limitRange, + containerErr: tc.limitRangeCalcErr, }, } diff --git a/vertical-pod-autoscaler/pkg/utils/limitrange/limit_range_calculator.go b/vertical-pod-autoscaler/pkg/utils/limitrange/limit_range_calculator.go index 7b666c83e7ff..77dc394979cc 100644 --- a/vertical-pod-autoscaler/pkg/utils/limitrange/limit_range_calculator.go +++ b/vertical-pod-autoscaler/pkg/utils/limitrange/limit_range_calculator.go @@ -30,6 +30,8 @@ import ( type LimitRangeCalculator interface { // GetContainerLimitRangeItem returns LimitRangeItem that describes limitation on container limits in the given namespace. GetContainerLimitRangeItem(namespace string) (*core.LimitRangeItem, error) + // GetPodLimitRangeItem returns LimitRangeItem that describes limitation on pod limits in the given namespace. + GetPodLimitRangeItem(namespace string) (*core.LimitRangeItem, error) } type noopLimitsRangeCalculator struct{} @@ -38,6 +40,10 @@ func (lc *noopLimitsRangeCalculator) GetContainerLimitRangeItem(namespace string return nil, nil } +func (lc *noopLimitsRangeCalculator) GetPodLimitRangeItem(namespace string) (*core.LimitRangeItem, error) { + return nil, nil +} + type limitsChecker struct { limitRangeLister listers.LimitRangeLister } @@ -66,6 +72,14 @@ func NewNoopLimitsCalculator() *noopLimitsRangeCalculator { } func (lc *limitsChecker) GetContainerLimitRangeItem(namespace string) (*core.LimitRangeItem, error) { + return lc.getLimitRangeItem(namespace, core.LimitTypeContainer) +} + +func (lc *limitsChecker) GetPodLimitRangeItem(namespace string) (*core.LimitRangeItem, error) { + return lc.getLimitRangeItem(namespace, core.LimitTypePod) +} + +func (lc *limitsChecker) getLimitRangeItem(namespace string, limitType core.LimitType) (*core.LimitRangeItem, error) { limitRanges, err := lc.limitRangeLister.LimitRanges(namespace).List(labels.Everything()) if err != nil { return nil, fmt.Errorf("error loading limit ranges: %s", err) @@ -102,10 +116,10 @@ func (lc *limitsChecker) GetContainerLimitRangeItem(namespace string) (*core.Lim return q2 } - result := &core.LimitRangeItem{Type: core.LimitTypeContainer} + result := &core.LimitRangeItem{Type: limitType} for _, lr := range limitRanges { for _, lri := range lr.Spec.Limits { - if lri.Type == core.LimitTypeContainer && (lri.Max != nil || lri.Default != nil || lri.Min != nil) { + if lri.Type == limitType && (lri.Max != nil || lri.Default != nil || lri.Min != nil) { if lri.Default != nil { result.Default = lri.Default } diff --git a/vertical-pod-autoscaler/pkg/utils/vpa/capping_test.go b/vertical-pod-autoscaler/pkg/utils/vpa/capping_test.go index 6fa52d1f6257..49974099bd1e 100644 --- a/vertical-pod-autoscaler/pkg/utils/vpa/capping_test.go +++ b/vertical-pod-autoscaler/pkg/utils/vpa/capping_test.go @@ -217,11 +217,16 @@ func TestApplyVpa(t *testing.T) { } type fakeLimitRangeCalculator struct { - limitRange apiv1.LimitRangeItem + containerLimitRange apiv1.LimitRangeItem + podLimitRange apiv1.LimitRangeItem } func (nlrc *fakeLimitRangeCalculator) GetContainerLimitRangeItem(namespace string) (*apiv1.LimitRangeItem, error) { - return &nlrc.limitRange, nil + return &nlrc.containerLimitRange, nil +} + +func (nlrc *fakeLimitRangeCalculator) GetPodLimitRangeItem(namespace string) (*apiv1.LimitRangeItem, error) { + return &nlrc.podLimitRange, nil } func TestApplyCapsToLimitRange(t *testing.T) { @@ -282,7 +287,7 @@ func TestApplyCapsToLimitRange(t *testing.T) { }, } - calculator := fakeLimitRangeCalculator{limitRange} + calculator := fakeLimitRangeCalculator{containerLimitRange: limitRange} processor := NewCappingRecommendationProcessor(&calculator) processedRecommendation, annotations, err := processor.Apply(&recommendation, nil, nil, &pod) assert.NoError(t, err)