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 4 commits
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
14 changes: 12 additions & 2 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. The post processor will round up CPU recommendations to a whole CPU for pods which were opted in by setting an appropriate label on VPA object (experimental)")
)

func main() {
klog.InitFlags(nil)
kube_flag.InitFlags()
Expand All @@ -83,9 +89,13 @@ func main() {

useCheckpoints := *storage != "prometheus"

postProcessors := []routines.RecommendationPostProcessor{
&routines.CappingPostProcessor{},
var postProcessors []routines.RecommendationPostProcessor
if *postProcessorCPUasInteger {
dbenque marked this conversation as resolved.
Show resolved Hide resolved
postProcessors = append(postProcessors, &routines.IntegerCPUPostProcessor{})
}
// CappingPostProcessor, should always come in the last position for post-processing
postProcessors = append(postProcessors, &routines.CappingPostProcessor{})

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,87 @@
/*
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"
"k8s.io/apimachinery/pkg/api/resource"
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
// 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()

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
}
setIntegerCPURecommendation(r.Target)
setIntegerCPURecommendation(r.LowerBound)
setIntegerCPURecommendation(r.UpperBound)
setIntegerCPURecommendation(r.UncappedTarget)
}
}
return amendedRecommendation
}

func setIntegerCPURecommendation(recommendation apiv1.ResourceList) {
for resourceName, recommended := range recommendation {
if resourceName != apiv1.ResourceCPU {
continue
}
recommended.RoundUp(resource.Scale(0))
recommendation[resourceName] = recommended
}
}

// 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,232 @@
/*
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"
"k8s.io/apimachinery/pkg/api/resource"
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 TestExtractContainerName(t *testing.T) {
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 TestIntegerCPUPostProcessor_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: vpaPostProcessorIntegerCPUValue,
}},
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: vpaPostProcessorIntegerCPUValue,
}},
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: vpaPostProcessorIntegerCPUValue,
vpaPostProcessorPrefix + "container2" + vpaPostProcessorIntegerCPUSuffix: vpaPostProcessorIntegerCPUValue,
}},
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.True(t, equalRecommendedPodResources(tt.want, got), "Process(%v, %v, nil)", tt.vpa, tt.recommendation)
})
}
}

func equalRecommendedPodResources(a, b *vpa_types.RecommendedPodResources) bool {
if len(a.ContainerRecommendations) != len(b.ContainerRecommendations) {
return false
}

for i := range a.ContainerRecommendations {
if !equalResourceList(a.ContainerRecommendations[i].LowerBound, b.ContainerRecommendations[i].LowerBound) {
return false
}
if !equalResourceList(a.ContainerRecommendations[i].Target, b.ContainerRecommendations[i].Target) {
return false
}
if !equalResourceList(a.ContainerRecommendations[i].UncappedTarget, b.ContainerRecommendations[i].UncappedTarget) {
return false
}
if !equalResourceList(a.ContainerRecommendations[i].UpperBound, b.ContainerRecommendations[i].UpperBound) {
return false
}
}
return true
}

func equalResourceList(rla, rlb v1.ResourceList) bool {
if len(rla) != len(rlb) {
return false
}
for k := range rla {
q := rla[k]
if q.Cmp(rlb[k]) != 0 {
return false
}
}
for k := range rlb {
q := rlb[k]
if q.Cmp(rla[k]) != 0 {
return false
}
}
return true
}

func TestSetIntegerCPURecommendation(t *testing.T) {
tests := []struct {
name string
recommendation v1.ResourceList
expectedRecommendation v1.ResourceList
}{
{
name: "unchanged",
recommendation: map[v1.ResourceName]resource.Quantity{
v1.ResourceCPU: resource.MustParse("8"),
v1.ResourceMemory: resource.MustParse("6Gi"),
},
expectedRecommendation: map[v1.ResourceName]resource.Quantity{
v1.ResourceCPU: resource.MustParse("8"),
v1.ResourceMemory: resource.MustParse("6Gi"),
},
},
{
name: "round up from 0.1",
recommendation: map[v1.ResourceName]resource.Quantity{
v1.ResourceCPU: resource.MustParse("8.1"),
v1.ResourceMemory: resource.MustParse("6Gi"),
},
expectedRecommendation: map[v1.ResourceName]resource.Quantity{
v1.ResourceCPU: resource.MustParse("9"),
v1.ResourceMemory: resource.MustParse("6Gi"),
},
},
{
name: "round up from 0.9",
recommendation: map[v1.ResourceName]resource.Quantity{
v1.ResourceCPU: resource.MustParse("8.9"),
v1.ResourceMemory: resource.MustParse("6Gi"),
},
expectedRecommendation: map[v1.ResourceName]resource.Quantity{
v1.ResourceCPU: resource.MustParse("9"),
v1.ResourceMemory: resource.MustParse("6Gi"),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
setIntegerCPURecommendation(tt.recommendation)
assert.True(t, equalResourceList(tt.recommendation, tt.expectedRecommendation))
})
}
}