diff --git a/vertical-pod-autoscaler/pkg/utils/vpa/allowed_resource_filter.go b/vertical-pod-autoscaler/pkg/utils/vpa/allowed_resource_filter.go index 4460c1822983..42473134bcb1 100644 --- a/vertical-pod-autoscaler/pkg/utils/vpa/allowed_resource_filter.go +++ b/vertical-pod-autoscaler/pkg/utils/vpa/allowed_resource_filter.go @@ -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 { diff --git a/vertical-pod-autoscaler/pkg/utils/vpa/allowed_resource_filter_test.go b/vertical-pod-autoscaler/pkg/utils/vpa/allowed_resource_filter_test.go index 7b5fadf4e1a5..e4a756a71536 100644 --- a/vertical-pod-autoscaler/pkg/utils/vpa/allowed_resource_filter_test.go +++ b/vertical-pod-autoscaler/pkg/utils/vpa/allowed_resource_filter_test.go @@ -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) })