Skip to content

Commit

Permalink
Merge pull request #3546 from kgolab/kg-fix-missing-containers
Browse files Browse the repository at this point in the history
Extend GetContainersResources with an option to emit all containers.
  • Loading branch information
k8s-ci-robot authored Sep 28, 2020
2 parents 4679102 + 203f258 commit 6e6dbbb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,29 @@ func NewProvider(calculator limitrange.LimitRangeCalculator,
}

// GetContainersResources returns the recommended resources for each container in the given pod in the same order they are specified in the pod.Spec.
// If addAll is set to true, containers w/o a recommendation are also added to the list, otherwise they're skipped (default behaviour).
func GetContainersResources(pod *core.Pod, vpaResourcePolicy *vpa_types.PodResourcePolicy, podRecommendation vpa_types.RecommendedPodResources, limitRange *core.LimitRangeItem,
annotations vpa_api_util.ContainerToAnnotationsMap) []vpa_api_util.ContainerResources {
addAll bool, annotations vpa_api_util.ContainerToAnnotationsMap) []vpa_api_util.ContainerResources {
resources := make([]vpa_api_util.ContainerResources, len(pod.Spec.Containers))
for i, container := range pod.Spec.Containers {
recommendation := vpa_api_util.GetRecommendationForContainer(container.Name, &podRecommendation)
if recommendation == nil {
klog.V(2).Infof("no matching recommendation found for container %s", container.Name)
continue
if !addAll {
klog.V(2).Infof("no matching recommendation found for container %s, skipping", container.Name)
continue
}
klog.V(2).Infof("no matching recommendation found for container %s, using Pod request", container.Name)
resources[i].Requests = container.Resources.Requests
} else {
resources[i].Requests = recommendation.Target
}
resources[i].Requests = recommendation.Target
defaultLimit := core.ResourceList{}
if limitRange != nil {
defaultLimit = limitRange.Default
}
containerControlledValues := vpa_api_util.GetContainerControlledValues(container.Name, vpaResourcePolicy)
if containerControlledValues == vpa_types.ContainerControlledValuesRequestsAndLimits {
proportionalLimits, limitAnnotations := vpa_api_util.GetProportionalLimit(container.Resources.Limits, container.Resources.Requests, recommendation.Target, defaultLimit)
proportionalLimits, limitAnnotations := vpa_api_util.GetProportionalLimit(container.Resources.Limits, container.Resources.Requests, resources[i].Requests, defaultLimit)
if proportionalLimits != nil {
resources[i].Limits = proportionalLimits
if len(limitAnnotations) > 0 {
Expand Down Expand Up @@ -102,6 +108,6 @@ func (p *recommendationProvider) GetContainersResourcesForPod(pod *core.Pod, vpa
if vpa.Spec.UpdatePolicy == nil || vpa.Spec.UpdatePolicy.UpdateMode == nil || *vpa.Spec.UpdatePolicy.UpdateMode != vpa_types.UpdateModeOff {
resourcePolicy = vpa.Spec.ResourcePolicy
}
containerResources := GetContainersResources(pod, resourcePolicy, *recommendedPodResources, containerLimitRange, annotations)
containerResources := GetContainersResources(pod, resourcePolicy, *recommendedPodResources, containerLimitRange, false, annotations)
return containerResources, annotations, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestUpdateResourceRequests(t *testing.T) {
WithLabels(labels).Get()

initializedContainer := test.Container().WithName(containerName).
WithCPURequest(resource.MustParse("1")).WithCPURequest(resource.MustParse("2")).WithMemRequest(resource.MustParse("100Mi")).Get()
WithCPURequest(resource.MustParse("2")).WithMemRequest(resource.MustParse("100Mi")).Get()
initialized := test.Pod().WithName("test_initialized").
AddContainer(initializedContainer).WithLabels(labels).Get()

Expand Down Expand Up @@ -179,6 +179,7 @@ func TestUpdateResourceRequests(t *testing.T) {
expectedCPU: resource.MustParse("0"),
},
{
name: "nil recommendation",
pod: initialized,
vpa: vpaWithNilRecommendation,
expectedAction: true,
Expand Down

0 comments on commit 6e6dbbb

Please sign in to comment.