-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathrecommender.go
179 lines (154 loc) · 8.04 KB
/
recommender.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
Copyright 2017 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 logic
import (
"flag"
"sort"
vpa_types "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1"
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/recommender/model"
)
var (
safetyMarginFraction = flag.Float64("recommendation-margin-fraction", 0.15, `Fraction of usage added as the safety margin to the recommended request`)
podMinCPUMillicores = flag.Float64("pod-recommendation-min-cpu-millicores", 25, `Minimum CPU recommendation for a pod`)
podMinMemoryMb = flag.Float64("pod-recommendation-min-memory-mb", 250, `Minimum memory recommendation for a pod`)
targetCPUPercentile = flag.Float64("target-cpu-percentile", 0.9, "CPU usage percentile that will be used as a base for CPU target recommendation. Doesn't affect CPU lower bound, CPU upper bound nor memory recommendations.")
)
// PodResourceRecommender computes resource recommendation for a Vpa object.
type PodResourceRecommender interface {
GetRecommendedPodResources(containerNameToAggregateStateMap model.ContainerNameToAggregateStateMap) RecommendedPodResources
}
// RecommendedPodResources is a Map from container name to recommended resources.
type RecommendedPodResources map[string]RecommendedContainerResources
// RecommendedContainerResources is the recommendation of resources for a
// container.
type RecommendedContainerResources struct {
// Recommended optimal amount of resources.
Target model.Resources
// Recommended minimum amount of resources.
LowerBound model.Resources
// Recommended maximum amount of resources.
UpperBound model.Resources
}
type podResourceRecommender struct {
targetEstimator ResourceEstimator
lowerBoundEstimator ResourceEstimator
upperBoundEstimator ResourceEstimator
}
func (r *podResourceRecommender) GetRecommendedPodResources(containerNameToAggregateStateMap model.ContainerNameToAggregateStateMap) RecommendedPodResources {
var recommendation = make(RecommendedPodResources)
if len(containerNameToAggregateStateMap) == 0 {
return recommendation
}
fraction := 1.0 / float64(len(containerNameToAggregateStateMap))
minResources := model.Resources{
model.ResourceCPU: model.ScaleResource(model.CPUAmountFromCores(*podMinCPUMillicores*0.001), fraction),
model.ResourceMemory: model.ScaleResource(model.MemoryAmountFromBytes(*podMinMemoryMb*1024*1024), fraction),
}
recommender := &podResourceRecommender{
WithMinResources(minResources, r.targetEstimator),
WithMinResources(minResources, r.lowerBoundEstimator),
WithMinResources(minResources, r.upperBoundEstimator),
}
for containerName, aggregatedContainerState := range containerNameToAggregateStateMap {
recommendation[containerName] = recommender.estimateContainerResources(aggregatedContainerState)
}
return recommendation
}
// Takes AggregateContainerState and returns a container recommendation.
func (r *podResourceRecommender) estimateContainerResources(s *model.AggregateContainerState) RecommendedContainerResources {
return RecommendedContainerResources{
FilterControlledResources(r.targetEstimator.GetResourceEstimation(s), s.GetControlledResources()),
FilterControlledResources(r.lowerBoundEstimator.GetResourceEstimation(s), s.GetControlledResources()),
FilterControlledResources(r.upperBoundEstimator.GetResourceEstimation(s), s.GetControlledResources()),
}
}
// FilterControlledResources returns estimations from 'estimation' only for resources present in 'controlledResources'.
func FilterControlledResources(estimation model.Resources, controlledResources []model.ResourceName) model.Resources {
result := make(model.Resources)
for _, resource := range controlledResources {
if value, ok := estimation[resource]; ok {
result[resource] = value
}
}
return result
}
// CreatePodResourceRecommender returns the primary recommender.
func CreatePodResourceRecommender() PodResourceRecommender {
lowerBoundCPUPercentile := 0.5
upperBoundCPUPercentile := 0.95
targetMemoryPeaksPercentile := 0.9
lowerBoundMemoryPeaksPercentile := 0.5
upperBoundMemoryPeaksPercentile := 0.95
targetEstimator := NewPercentileEstimator(*targetCPUPercentile, targetMemoryPeaksPercentile)
lowerBoundEstimator := NewPercentileEstimator(lowerBoundCPUPercentile, lowerBoundMemoryPeaksPercentile)
upperBoundEstimator := NewPercentileEstimator(upperBoundCPUPercentile, upperBoundMemoryPeaksPercentile)
targetEstimator = WithMargin(*safetyMarginFraction, targetEstimator)
lowerBoundEstimator = WithMargin(*safetyMarginFraction, lowerBoundEstimator)
upperBoundEstimator = WithMargin(*safetyMarginFraction, upperBoundEstimator)
// Apply confidence multiplier to the upper bound estimator. This means
// that the updater will be less eager to evict pods with short history
// in order to reclaim unused resources.
// Using the confidence multiplier 1 with exponent +1 means that
// the upper bound is multiplied by (1 + 1/history-length-in-days).
// See estimator.go to see how the history length and the confidence
// multiplier are determined. The formula yields the following multipliers:
// No history : *INF (do not force pod eviction)
// 12h history : *3 (force pod eviction if the request is > 3 * upper bound)
// 24h history : *2
// 1 week history : *1.14
upperBoundEstimator = WithConfidenceMultiplier(1.0, 1.0, upperBoundEstimator)
// Apply confidence multiplier to the lower bound estimator. This means
// that the updater will be less eager to evict pods with short history
// in order to provision them with more resources.
// Using the confidence multiplier 0.001 with exponent -2 means that
// the lower bound is multiplied by the factor (1 + 0.001/history-length-in-days)^-2
// (which is very rapidly converging to 1.0).
// See estimator.go to see how the history length and the confidence
// multiplier are determined. The formula yields the following multipliers:
// No history : *0 (do not force pod eviction)
// 5m history : *0.6 (force pod eviction if the request is < 0.6 * lower bound)
// 30m history : *0.9
// 60m history : *0.95
lowerBoundEstimator = WithConfidenceMultiplier(0.001, -2.0, lowerBoundEstimator)
return &podResourceRecommender{
targetEstimator,
lowerBoundEstimator,
upperBoundEstimator}
}
// MapToListOfRecommendedContainerResources converts the map of RecommendedContainerResources into a stable sorted list
// This can be used to get a stable sequence while ranging on the data
func MapToListOfRecommendedContainerResources(resources RecommendedPodResources) *vpa_types.RecommendedPodResources {
containerResources := make([]vpa_types.RecommendedContainerResources, 0, len(resources))
// Sort the container names from the map. This is because maps are an
// unordered data structure, and iterating through the map will return
// a different order on every call.
containerNames := make([]string, 0, len(resources))
for containerName := range resources {
containerNames = append(containerNames, containerName)
}
sort.Strings(containerNames)
// Create the list of recommendations for each container.
for _, name := range containerNames {
containerResources = append(containerResources, vpa_types.RecommendedContainerResources{
ContainerName: name,
Target: model.ResourcesAsResourceList(resources[name].Target),
LowerBound: model.ResourcesAsResourceList(resources[name].LowerBound),
UpperBound: model.ResourcesAsResourceList(resources[name].UpperBound),
UncappedTarget: model.ResourcesAsResourceList(resources[name].Target),
})
}
recommendation := &vpa_types.RecommendedPodResources{
ContainerRecommendations: containerResources,
}
return recommendation
}