diff --git a/vertical-pod-autoscaler/pkg/admission-controller/logic/limitrange_checker.go b/vertical-pod-autoscaler/pkg/admission-controller/logic/limitrange_checker.go index 28ff6855501f..c11b0d9e4368 100644 --- a/vertical-pod-autoscaler/pkg/admission-controller/logic/limitrange_checker.go +++ b/vertical-pod-autoscaler/pkg/admission-controller/logic/limitrange_checker.go @@ -25,12 +25,14 @@ import ( v1_listers "k8s.io/client-go/listers/core/v1" ) +// LimitsHints provide hinted limits that respect limit range max ratio type LimitsHints interface { IsNil() bool RequestsExceedsRatio(indexOfContainer int, resourceName v1.ResourceName) bool HintedLimit(indexOfContainer int, resourceName v1.ResourceName) resource.Quantity } +// LimitRangeHints implements LimitsHints interface type LimitRangeHints struct { requestsExceedsRatio []map[v1.ResourceName]bool limitsRespectingRatio []v1.ResourceList @@ -38,14 +40,17 @@ type LimitRangeHints struct { var _ LimitsHints = &LimitRangeHints{} +// LimitsChecker checks for LimitRange and if container needs limits to be set type LimitsChecker interface { NeedsLimits(*v1.Pod, []ContainerResources) LimitsHints } +// IsNil return true if there is no hints to set limits func (lrh *LimitRangeHints) IsNil() bool { return lrh == (*LimitRangeHints)(nil) } +// RequestsExceedsRatio return true if limits have to be set to respect limit range max ratio func (lrh *LimitRangeHints) RequestsExceedsRatio(indexOfContainer int, resourceName v1.ResourceName) bool { if !lrh.IsNil() { yes, ok := lrh.requestsExceedsRatio[indexOfContainer][resourceName] @@ -54,6 +59,7 @@ func (lrh *LimitRangeHints) RequestsExceedsRatio(indexOfContainer int, resourceN return false } +// HintedLimit return the limit Quantity that respect the limit range max ration func (lrh *LimitRangeHints) HintedLimit(indexOfContainer int, resourceName v1.ResourceName) resource.Quantity { if !lrh.IsNil() { limit, ok := lrh.limitsRespectingRatio[indexOfContainer][resourceName] @@ -79,6 +85,7 @@ type limitsChecker struct { var _ LimitsChecker = &limitsChecker{} +// NewLimitsChecker create a LimitsChecker func NewLimitsChecker(i interface{}) LimitsChecker { if f, ok := i.(informers.SharedInformerFactory); ok { limitrangeLister := f.Core().V1().LimitRanges().Lister() @@ -190,7 +197,7 @@ func (lc *limitsChecker) NeedsLimits(pod *v1.Pod, containersResources []Containe lrh.requestsExceedsRatio[i] = make(map[v1.ResourceName]bool) lrh.limitsRespectingRatio[i] = make(v1.ResourceList) for name, value := range cr.Requests { - var ctrLimit *resource.Quantity = nil + var ctrLimit *resource.Quantity if pod.Spec.Containers[i].Resources.Limits != nil { if q, hasLimit := pod.Spec.Containers[i].Resources.Limits[name]; hasLimit { ctrLimit = &q diff --git a/vertical-pod-autoscaler/pkg/admission-controller/logic/limitrange_checker_test.go b/vertical-pod-autoscaler/pkg/admission-controller/logic/limitrange_checker_test.go index b6667d175a4b..da64bf2149e8 100644 --- a/vertical-pod-autoscaler/pkg/admission-controller/logic/limitrange_checker_test.go +++ b/vertical-pod-autoscaler/pkg/admission-controller/logic/limitrange_checker_test.go @@ -88,13 +88,13 @@ func TestUpdateResourceLimits(t *testing.T) { initialized := test.Pod().WithName("test_initialized").AddContainer(test.BuildTestContainer(containerName, "1", "100Mi")).Get() initialized.ObjectMeta.Labels = labels - with_limits := test.Pod().WithName("test_initialized").AddContainer(test.BuildTestContainer(containerName, "1", "100Mi")).Get() - with_limits.ObjectMeta.Labels = labels - with_limits.Spec.Containers[0].Resources.Limits = test.Resources("1500m", "800Mi") + withLimits := test.Pod().WithName("test_initialized").AddContainer(test.BuildTestContainer(containerName, "1", "100Mi")).Get() + withLimits.ObjectMeta.Labels = labels + withLimits.Spec.Containers[0].Resources.Limits = test.Resources("1500m", "800Mi") - with_huge_mem_limits := test.Pod().WithName("test_initialized").AddContainer(test.BuildTestContainer(containerName, "1", "10Gi")).Get() - with_huge_mem_limits.ObjectMeta.Labels = labels - with_huge_mem_limits.Spec.Containers[0].Resources.Limits = test.Resources("1500m", "80Gi") + withHugeMemLimits := test.Pod().WithName("test_initialized").AddContainer(test.BuildTestContainer(containerName, "1", "10Gi")).Get() + withHugeMemLimits.ObjectMeta.Labels = labels + withHugeMemLimits.Spec.Containers[0].Resources.Limits = test.Resources("1500m", "80Gi") vpaBuilder := test.VerticalPodAutoscaler(). WithName(vpaName). @@ -142,7 +142,7 @@ func TestUpdateResourceLimits(t *testing.T) { limitsRespectingRatioCPU: expectedCPU(vpaContainersResources, minRatio), limitsRespectingRatioMemory: expectedMemory(vpaContainersResources, minRatio), }, { - pod: with_limits, + pod: withLimits, containerResources: vpaContainersResources, limitRanges: limitranges, requestsExceedsRatioCPU: true, @@ -150,7 +150,7 @@ func TestUpdateResourceLimits(t *testing.T) { limitsRespectingRatioCPU: expectedCPU(vpaContainersResources, minRatio), limitsRespectingRatioMemory: resource.Quantity{}, }, { - pod: with_huge_mem_limits, + pod: withHugeMemLimits, containerResources: vpaHighMemContainersResources, limitRanges: limitranges, requestsExceedsRatioCPU: false,