Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recommendation post processor for integer CPU #5313

Merged
merged 5 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions vertical-pod-autoscaler/pkg/recommender/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ var (
cpuHistogramDecayHalfLife = flag.Duration("cpu-histogram-decay-half-life", model.DefaultCPUHistogramDecayHalfLife, `The amount of time it takes a historical CPU usage sample to lose half of its weight.`)
)

// Post processors flags
var (
// CPU as integer to benefit for CPU management Static Policy ( https://kubernetes.io/docs/tasks/administer-cluster/cpu-management-policies/#static-policy )
postProcessorCPUasInteger = flag.Bool("cpu-integer-post-processor-enabled", false, "Enable the cpu-integer recommendation post processor. Only containers that specify it can have their CPU recommendation rounded up to an integer value.")
dbenque marked this conversation as resolved.
Show resolved Hide resolved
)

func main() {
klog.InitFlags(nil)
kube_flag.InitFlags()
Expand All @@ -86,6 +92,9 @@ func main() {
postProcessors := []routines.RecommendationPostProcessor{
&routines.CappingPostProcessor{},
}
if *postProcessorCPUasInteger {
dbenque marked this conversation as resolved.
Show resolved Hide resolved
postProcessors = append(postProcessors, &routines.IntegerCPUPostProcessor{})
}
recommender := routines.NewRecommender(config, *checkpointsGCInterval, useCheckpoints, *vpaObjectNamespace, *recommenderName, postProcessors)

promQueryTimeout, err := time.ParseDuration(*queryTimeout)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
Copyright 2022 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package routines

import (
apiv1 "k8s.io/api/core/v1"
vpa_types "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1"
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/recommender/model"
"math"
"strings"
)

// IntegerCPUPostProcessor ensures that the recommendation delivers an integer value for CPU
// This is need for users who want to use CPU Management with static policy: https://kubernetes.io/docs/tasks/administer-cluster/cpu-management-policies/#static-policy
type IntegerCPUPostProcessor struct{}

const (
// The user interface for that post processor is an annotation on the VPA object with the following format:
// vpa-post-processor.kubernetes.io/{containerName}_integerCPU=true
vpaPostProcessorPrefix = "vpa-post-processor.kubernetes.io/"
vpaPostProcessorIntegerCPUSuffix = "_integerCPU"
vpaPostProcessorIntegerCPUValue = "true"
)

var _ RecommendationPostProcessor = &IntegerCPUPostProcessor{}
dbenque marked this conversation as resolved.
Show resolved Hide resolved

// Process apply the capping post-processing to the recommendation.
// 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 {
jbartosik marked this conversation as resolved.
Show resolved Hide resolved

amendedRecommendation := recommendation.DeepCopy()

process := func(recommendation apiv1.ResourceList) {
jbartosik marked this conversation as resolved.
Show resolved Hide resolved
for resourceName, recommended := range recommendation {
if resourceName != apiv1.ResourceCPU {
continue
}
v := float64(recommended.MilliValue()) / 1000
r := int64(math.Ceil(v))
dbenque marked this conversation as resolved.
Show resolved Hide resolved
recommended.Set(r)
recommendation[resourceName] = recommended
}
}

for key, value := range vpa.Annotations {
containerName := extractContainerName(key, vpaPostProcessorPrefix, vpaPostProcessorIntegerCPUSuffix)
if containerName == "" || value != vpaPostProcessorIntegerCPUValue {
continue
}

for _, r := range amendedRecommendation.ContainerRecommendations {
if r.ContainerName != containerName {
continue
}
process(r.Target)
process(r.LowerBound)
process(r.UpperBound)
process(r.UncappedTarget)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the contract about UncappedTarget? Probably post-processors should leave it as-is, like limit capping does?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it make sense that the UncappedTarget is not touched by the capping post-processor, but I would say that the same does not apply to other post-processors than capping.
I don't think we need to keep a UnXYZTarget for each XYZ post-processor

}
}
return amendedRecommendation
}

// extractContainerName return the container name for the feature based on annotation key
// if the returned value is empty that means that the key does not match
func extractContainerName(key, prefix, suffix string) string {
jbartosik marked this conversation as resolved.
Show resolved Hide resolved
if !strings.HasPrefix(key, prefix) {
return ""
}
if !strings.HasSuffix(key, suffix) {
return ""
}

return key[len(prefix) : len(key)-len(suffix)]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
Copyright 2022 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package routines

import (
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/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"
"testing"
)

func Test_extractContainerName(t *testing.T) {
dbenque marked this conversation as resolved.
Show resolved Hide resolved
tests := []struct {
name string
key string
prefix string
suffix string
want string
}{
{
name: "empty",
key: "",
prefix: "",
suffix: "",
want: "",
},
{
name: "no match",
key: "abc",
prefix: "z",
suffix: "x",
want: "",
},
{
name: "match",
key: "abc",
prefix: "a",
suffix: "c",
want: "b",
},
{
name: "real",
key: vpaPostProcessorPrefix + "kafka" + vpaPostProcessorIntegerCPUSuffix,
prefix: vpaPostProcessorPrefix,
suffix: vpaPostProcessorIntegerCPUSuffix,
want: "kafka",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, extractContainerName(tt.key, tt.prefix, tt.suffix), "extractContainerName(%v, %v, %v)", tt.key, tt.prefix, tt.suffix)
})
}
}

func Test_integerCPUPostProcessor_Process(t *testing.T) {
tests := []struct {
name string
vpa *model.Vpa
recommendation *vpa_types.RecommendedPodResources
want *vpa_types.RecommendedPodResources
}{
{
name: "No containers match",
vpa: &model.Vpa{Annotations: map[string]string{
vpaPostProcessorPrefix + "container-other" + vpaPostProcessorIntegerCPUSuffix: "true",
dbenque marked this conversation as resolved.
Show resolved Hide resolved
}},
recommendation: &vpa_types.RecommendedPodResources{
ContainerRecommendations: []vpa_types.RecommendedContainerResources{
test.Recommendation().WithContainer("container1").WithTarget("8.6", "200Mi").GetContainerResources(),
test.Recommendation().WithContainer("container2").WithTarget("8.2", "300Mi").GetContainerResources(),
},
},
want: &vpa_types.RecommendedPodResources{
ContainerRecommendations: []vpa_types.RecommendedContainerResources{
test.Recommendation().WithContainer("container1").WithTarget("8.6", "200Mi").GetContainerResources(),
test.Recommendation().WithContainer("container2").WithTarget("8.2", "300Mi").GetContainerResources(),
},
},
},
{
name: "2 containers, 1 matching only",
vpa: &model.Vpa{Annotations: map[string]string{
vpaPostProcessorPrefix + "container1" + vpaPostProcessorIntegerCPUSuffix: "true",
}},
recommendation: &vpa_types.RecommendedPodResources{
ContainerRecommendations: []vpa_types.RecommendedContainerResources{
test.Recommendation().WithContainer("container1").WithTarget("8.6", "200Mi").GetContainerResources(),
test.Recommendation().WithContainer("container2").WithTarget("8.2", "300Mi").GetContainerResources(),
},
},
want: &vpa_types.RecommendedPodResources{
ContainerRecommendations: []vpa_types.RecommendedContainerResources{
test.Recommendation().WithContainer("container1").WithTarget("9", "200Mi").GetContainerResources(),
test.Recommendation().WithContainer("container2").WithTarget("8.2", "300Mi").GetContainerResources(),
},
},
},
{
name: "2 containers, 2 matching",
vpa: &model.Vpa{Annotations: map[string]string{
vpaPostProcessorPrefix + "container1" + vpaPostProcessorIntegerCPUSuffix: "true",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The annotation can also be turned off by setting it to false, so can you please also add some tests for the case where
vpaPostProcessorPrefix + "container1" + vpaPostProcessorIntegerCPUSuffix: "false"

While writing this, I'm not sure if we need this additional case? How is this different from the annotation not being present for a container? As an alternative, the annotation could list the containers for which this post-processor is turned on?

Copy link
Contributor Author

@dbenque dbenque Nov 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"false" and annotation not being present are equivalent indeed.

We already have the support for multiple container with the current code (list of annotation, one per container), adding another way of expressing the same thing may introduce confusion and complexity.

vpaPostProcessorPrefix + "container2" + vpaPostProcessorIntegerCPUSuffix: "true",
}},
recommendation: &vpa_types.RecommendedPodResources{
ContainerRecommendations: []vpa_types.RecommendedContainerResources{
test.Recommendation().WithContainer("container1").WithTarget("8.6", "200Mi").GetContainerResources(),
test.Recommendation().WithContainer("container2").WithTarget("5.2", "300Mi").GetContainerResources(),
},
},
want: &vpa_types.RecommendedPodResources{
ContainerRecommendations: []vpa_types.RecommendedContainerResources{
test.Recommendation().WithContainer("container1").WithTarget("9", "200Mi").GetContainerResources(),
test.Recommendation().WithContainer("container2").WithTarget("6", "300Mi").GetContainerResources(),
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := IntegerCPUPostProcessor{}
got := c.Process(tt.vpa, tt.recommendation, nil)
assert.Equalf(t, reScale3(tt.want), reScale3(got), "Process(%v, %v, nil)", tt.vpa, tt.recommendation)
dbenque marked this conversation as resolved.
Show resolved Hide resolved
})
}
}

func reScale3(recommended *vpa_types.RecommendedPodResources) *vpa_types.RecommendedPodResources {

scale3 := func(rl v1.ResourceList) {
for k, v := range rl {
v.SetMilli(v.MilliValue())
rl[k] = v
}
}

for _, r := range recommended.ContainerRecommendations {
scale3(r.LowerBound)
scale3(r.Target)
scale3(r.UncappedTarget)
scale3(r.UpperBound)
}
return recommended
}