Merge pull request #6061 from DataDog/david.benque/post-processing-reduced-dependencies

use vpaObject instead of vpaModel to reduce deps of post-processors
This commit is contained in:
Kubernetes Prow Robot 2023-09-04 05:19:51 -07:00 committed by GitHub
commit a20872b53f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 27 deletions

View File

@ -17,10 +17,10 @@ limitations under the License.
package routines package routines
import ( import (
vpa_types "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1"
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/recommender/model"
vpa_utils "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/vpa"
"k8s.io/klog/v2" "k8s.io/klog/v2"
vpa_types "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1"
vpa_utils "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/vpa"
) )
// CappingPostProcessor ensure that the policy is applied to recommendation // CappingPostProcessor ensure that the policy is applied to recommendation
@ -30,11 +30,11 @@ type CappingPostProcessor struct{}
var _ RecommendationPostProcessor = &CappingPostProcessor{} var _ RecommendationPostProcessor = &CappingPostProcessor{}
// Process apply the capping post-processing to the recommendation. (use to be function getCappedRecommendation) // Process apply the capping post-processing to the recommendation. (use to be function getCappedRecommendation)
func (c CappingPostProcessor) Process(vpa *model.Vpa, recommendation *vpa_types.RecommendedPodResources, policy *vpa_types.PodResourcePolicy) *vpa_types.RecommendedPodResources { func (c CappingPostProcessor) Process(vpa *vpa_types.VerticalPodAutoscaler, recommendation *vpa_types.RecommendedPodResources) *vpa_types.RecommendedPodResources {
// TODO: maybe rename the vpa_utils.ApplyVPAPolicy to something that mention that it is doing capping only // TODO: maybe rename the vpa_utils.ApplyVPAPolicy to something that mention that it is doing capping only
cappedRecommendation, err := vpa_utils.ApplyVPAPolicy(recommendation, policy) cappedRecommendation, err := vpa_utils.ApplyVPAPolicy(recommendation, vpa.Spec.ResourcePolicy)
if err != nil { if err != nil {
klog.Errorf("Failed to apply policy for VPA %v/%v: %v", vpa.ID.Namespace, vpa.ID.VpaName, err) klog.Errorf("Failed to apply policy for VPA %v/%v: %v", vpa.GetNamespace(), vpa.GetName(), err)
return recommendation return recommendation
} }
return cappedRecommendation return cappedRecommendation

View File

@ -17,11 +17,12 @@ limitations under the License.
package routines package routines
import ( import (
"strings"
apiv1 "k8s.io/api/core/v1" apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource" "k8s.io/apimachinery/pkg/api/resource"
vpa_types "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1" vpa_types "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1"
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/recommender/model"
"strings"
) )
// IntegerCPUPostProcessor ensures that the recommendation delivers an integer value for CPU // IntegerCPUPostProcessor ensures that the recommendation delivers an integer value for CPU
@ -40,7 +41,7 @@ var _ RecommendationPostProcessor = &IntegerCPUPostProcessor{}
// Process apply the capping post-processing to the recommendation. // Process apply the capping post-processing to the recommendation.
// For this post processor the CPU value is rounded up to an integer // For this post processor the CPU value is rounded up to an integer
func (p *IntegerCPUPostProcessor) Process(vpa *model.Vpa, recommendation *vpa_types.RecommendedPodResources, policy *vpa_types.PodResourcePolicy) *vpa_types.RecommendedPodResources { func (p *IntegerCPUPostProcessor) Process(vpa *vpa_types.VerticalPodAutoscaler, recommendation *vpa_types.RecommendedPodResources) *vpa_types.RecommendedPodResources {
amendedRecommendation := recommendation.DeepCopy() amendedRecommendation := recommendation.DeepCopy()

View File

@ -17,13 +17,15 @@ limitations under the License.
package routines package routines
import ( import (
"testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource" "k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
vpa_types "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1" vpa_types "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1"
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/recommender/model"
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/test" "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/test"
"testing"
) )
func TestExtractContainerName(t *testing.T) { func TestExtractContainerName(t *testing.T) {
@ -73,15 +75,17 @@ func TestExtractContainerName(t *testing.T) {
func TestIntegerCPUPostProcessor_Process(t *testing.T) { func TestIntegerCPUPostProcessor_Process(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
vpa *model.Vpa vpa *vpa_types.VerticalPodAutoscaler
recommendation *vpa_types.RecommendedPodResources recommendation *vpa_types.RecommendedPodResources
want *vpa_types.RecommendedPodResources want *vpa_types.RecommendedPodResources
}{ }{
{ {
name: "No containers match", name: "No containers match",
vpa: &model.Vpa{Annotations: map[string]string{ vpa: &vpa_types.VerticalPodAutoscaler{
vpaPostProcessorPrefix + "container-other" + vpaPostProcessorIntegerCPUSuffix: vpaPostProcessorIntegerCPUValue, ObjectMeta: metav1.ObjectMeta{
}}, Annotations: map[string]string{
vpaPostProcessorPrefix + "container-other" + vpaPostProcessorIntegerCPUSuffix: vpaPostProcessorIntegerCPUValue,
}}},
recommendation: &vpa_types.RecommendedPodResources{ recommendation: &vpa_types.RecommendedPodResources{
ContainerRecommendations: []vpa_types.RecommendedContainerResources{ ContainerRecommendations: []vpa_types.RecommendedContainerResources{
test.Recommendation().WithContainer("container1").WithTarget("8.6", "200Mi").GetContainerResources(), test.Recommendation().WithContainer("container1").WithTarget("8.6", "200Mi").GetContainerResources(),
@ -97,9 +101,10 @@ func TestIntegerCPUPostProcessor_Process(t *testing.T) {
}, },
{ {
name: "2 containers, 1 matching only", name: "2 containers, 1 matching only",
vpa: &model.Vpa{Annotations: map[string]string{ vpa: &vpa_types.VerticalPodAutoscaler{
vpaPostProcessorPrefix + "container1" + vpaPostProcessorIntegerCPUSuffix: vpaPostProcessorIntegerCPUValue, ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{
}}, vpaPostProcessorPrefix + "container1" + vpaPostProcessorIntegerCPUSuffix: vpaPostProcessorIntegerCPUValue,
}}},
recommendation: &vpa_types.RecommendedPodResources{ recommendation: &vpa_types.RecommendedPodResources{
ContainerRecommendations: []vpa_types.RecommendedContainerResources{ ContainerRecommendations: []vpa_types.RecommendedContainerResources{
test.Recommendation().WithContainer("container1").WithTarget("8.6", "200Mi").GetContainerResources(), test.Recommendation().WithContainer("container1").WithTarget("8.6", "200Mi").GetContainerResources(),
@ -115,10 +120,11 @@ func TestIntegerCPUPostProcessor_Process(t *testing.T) {
}, },
{ {
name: "2 containers, 2 matching", name: "2 containers, 2 matching",
vpa: &model.Vpa{Annotations: map[string]string{ vpa: &vpa_types.VerticalPodAutoscaler{
vpaPostProcessorPrefix + "container1" + vpaPostProcessorIntegerCPUSuffix: vpaPostProcessorIntegerCPUValue, ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{
vpaPostProcessorPrefix + "container2" + vpaPostProcessorIntegerCPUSuffix: vpaPostProcessorIntegerCPUValue, vpaPostProcessorPrefix + "container1" + vpaPostProcessorIntegerCPUSuffix: vpaPostProcessorIntegerCPUValue,
}}, vpaPostProcessorPrefix + "container2" + vpaPostProcessorIntegerCPUSuffix: vpaPostProcessorIntegerCPUValue,
}}},
recommendation: &vpa_types.RecommendedPodResources{ recommendation: &vpa_types.RecommendedPodResources{
ContainerRecommendations: []vpa_types.RecommendedContainerResources{ ContainerRecommendations: []vpa_types.RecommendedContainerResources{
test.Recommendation().WithContainer("container1").WithTarget("8.6", "200Mi").GetContainerResources(), test.Recommendation().WithContainer("container1").WithTarget("8.6", "200Mi").GetContainerResources(),
@ -136,7 +142,7 @@ func TestIntegerCPUPostProcessor_Process(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
c := IntegerCPUPostProcessor{} c := IntegerCPUPostProcessor{}
got := c.Process(tt.vpa, tt.recommendation, nil) got := c.Process(tt.vpa, tt.recommendation)
assert.True(t, equalRecommendedPodResources(tt.want, got), "Process(%v, %v, nil)", tt.vpa, tt.recommendation) assert.True(t, equalRecommendedPodResources(tt.want, got), "Process(%v, %v, nil)", tt.vpa, tt.recommendation)
}) })
} }

View File

@ -18,11 +18,9 @@ package routines
import ( import (
vpa_types "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1" vpa_types "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1"
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/recommender/model"
) )
// RecommendationPostProcessor can amend the recommendation according to the defined policies // RecommendationPostProcessor can amend the recommendation according to the defined policies
type RecommendationPostProcessor interface { type RecommendationPostProcessor interface {
Process(vpa *model.Vpa, recommendation *vpa_types.RecommendedPodResources, Process(vpa *vpa_types.VerticalPodAutoscaler, recommendation *vpa_types.RecommendedPodResources) *vpa_types.RecommendedPodResources
policy *vpa_types.PodResourcePolicy) *vpa_types.RecommendedPodResources
} }

View File

@ -97,7 +97,7 @@ func (r *recommender) UpdateVPAs() {
listOfResourceRecommendation := logic.MapToListOfRecommendedContainerResources(resources) listOfResourceRecommendation := logic.MapToListOfRecommendedContainerResources(resources)
for _, postProcessor := range r.recommendationPostProcessor { for _, postProcessor := range r.recommendationPostProcessor {
listOfResourceRecommendation = postProcessor.Process(vpa, listOfResourceRecommendation, observedVpa.Spec.ResourcePolicy) listOfResourceRecommendation = postProcessor.Process(observedVpa, listOfResourceRecommendation)
} }
vpa.UpdateRecommendation(listOfResourceRecommendation) vpa.UpdateRecommendation(listOfResourceRecommendation)