Skip to content

Commit

Permalink
Add method for getting pod limit range
Browse files Browse the repository at this point in the history
  • Loading branch information
jbartosik committed May 31, 2019
1 parent 8bb4cb1 commit 9ce20d6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -315,8 +321,8 @@ func TestUpdateResourceRequests(t *testing.T) {
recommendationProcessor: vpa_api_util.NewCappingRecommendationProcessor(limitrange.NewNoopLimitsCalculator()),
selectorFetcher: mockSelectorFetcher,
limitsRangeCalculator: &fakeLimitRangeCalculator{
tc.limitRange,
tc.limitRangeCalcErr,
containerLimitRange: tc.limitRange,
containerErr: tc.limitRangeCalcErr,
},
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
11 changes: 8 additions & 3 deletions vertical-pod-autoscaler/pkg/utils/vpa/capping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -276,7 +281,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)
Expand Down

0 comments on commit 9ce20d6

Please sign in to comment.