Skip to content

Commit

Permalink
Check pod recommendation is not nil before iterating
Browse files Browse the repository at this point in the history
  • Loading branch information
domenicbozzuto committed Apr 8, 2024
1 parent ad3ec34 commit 23788cb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
13 changes: 9 additions & 4 deletions vertical-pod-autoscaler/pkg/utils/vpa/allowed_resource_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,19 @@ func (p *allowedResourceFilter) Apply(podRecommendation *vpa_types.RecommendedPo
policy *vpa_types.PodResourcePolicy,
conditions []vpa_types.VerticalPodAutoscalerCondition,
pod *corev1.Pod) (*vpa_types.RecommendedPodResources, ContainerToAnnotationsMap, error) {
recommendation := podRecommendation

if podRecommendation == nil || policy == nil {
// If there is no recommendation or no policies have been defined then no recommendation can be computed.
return podRecommendation, nil, nil
}

accumulatedContainerToAnnotationsMap := ContainerToAnnotationsMap{}

for i, containerRecommendation := range recommendation.ContainerRecommendations {
recommendation.ContainerRecommendations[i] = filterAllowedContainerResources(containerRecommendation, p.allowedResources)
for i, containerRecommendation := range podRecommendation.ContainerRecommendations {
podRecommendation.ContainerRecommendations[i] = filterAllowedContainerResources(containerRecommendation, p.allowedResources)
}

return recommendation, accumulatedContainerToAnnotationsMap, nil
return podRecommendation, accumulatedContainerToAnnotationsMap, nil
}

func filterAllowedContainerResources(recommendation vpa_types.RecommendedContainerResources, allowedResources []corev1.ResourceName) vpa_types.RecommendedContainerResources {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func TestAllowedResourceFilter(t *testing.T) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
processor := NewAllowedResourceFilter(tc.allowedResources)
processedRecommendation, _, err := processor.Apply(&tc.recommendation, nil, nil, nil)
processedRecommendation, _, err := processor.Apply(&tc.recommendation, &vpa_types.PodResourcePolicy{}, nil, nil)
assert.NoError(t, err)
assert.Equal(t, tc.expectedRecommendation, *processedRecommendation)
})
Expand Down

0 comments on commit 23788cb

Please sign in to comment.