forked from kubernetes/autoscaler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recommendation_provider.go
136 lines (117 loc) · 5.39 KB
/
recommendation_provider.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
/*
Copyright 2018 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 (
"math"
"github.com/golang/glog"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/labels"
vpa_types "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/poc.autoscaling.k8s.io/v1alpha1"
vpa_lister "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/client/listers/poc.autoscaling.k8s.io/v1alpha1"
vpa_api_util "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/vpa"
)
const (
// MemoryLimitBumpUpRatio specifies how much memory will be available above memory request.
// Limit is set to trigger OOMs.
MemoryLimitBumpUpRatio float64 = 1.2
// MemoryLimitMinBumpUp specifies minimal amount of memory above memory request.
// Limit is set to trigger OOMs.
MemoryLimitMinBumpUp float64 = 100 * 1024 * 1024 // 100MB
)
// ContainerResources holds request and limit resources for container
type ContainerResources struct {
Requests, Limits v1.ResourceList
}
func newContainerResources() ContainerResources {
return ContainerResources{Requests: v1.ResourceList{}, Limits: v1.ResourceList{}}
}
// RecommendationProvider gets current recommendation and limits for the given pod.
type RecommendationProvider interface {
GetContainersResourcesForPod(pod *v1.Pod) ([]ContainerResources, string, error)
}
type recommendationProvider struct {
vpaLister vpa_lister.VerticalPodAutoscalerLister
recommendationProcessor vpa_api_util.RecommendationProcessor
}
// NewRecommendationProvider constructs the recommendation provider that list VPAs and can be used to determine recommendations for pods.
func NewRecommendationProvider(vpaLister vpa_lister.VerticalPodAutoscalerLister, recommendationProcessor vpa_api_util.RecommendationProcessor) *recommendationProvider {
return &recommendationProvider{vpaLister: vpaLister,
recommendationProcessor: recommendationProcessor}
}
// getMemoryLimit returns a limit that is proportionally (with min step) higher than the request. Limit is set to trigger OOMs.
func getMemoryLimit(resources v1.ResourceList) *resource.Quantity {
memory, found := resources[v1.ResourceMemory]
if !found {
return nil
}
limit := float64(memory.Value())
limit = math.Max(limit+MemoryLimitMinBumpUp, limit*MemoryLimitBumpUpRatio)
return resource.NewQuantity(int64(limit), memory.Format)
}
// getContainersResources returns the recommended resources and limits for each container in the given pod in the same order they are specified in the pod.Spec.
func getContainersResources(pod *v1.Pod, podRecommendation vpa_types.RecommendedPodResources, policy *vpa_types.PodResourcePolicy) []ContainerResources {
resources := make([]ContainerResources, len(pod.Spec.Containers))
for i, container := range pod.Spec.Containers {
resources[i] = newContainerResources()
setLimit := func(r v1.ResourceList) {
memoryLimit := getMemoryLimit(r)
if memoryLimit != nil {
resources[i].Limits[v1.ResourceMemory] = *memoryLimit
vpa_api_util.ApplyVPAContainerPolicy(resources[i].Limits, container, policy)
}
}
recommendation := vpa_api_util.GetRecommendationForContainer(container.Name, &podRecommendation)
if recommendation == nil {
glog.V(2).Infof("no matching recommendation found for container %s", container.Name)
setLimit(pod.Spec.Containers[i].Resources.Requests)
continue
}
resources[i].Requests = recommendation.Target
setLimit(resources[i].Requests)
}
return resources
}
func (p *recommendationProvider) getMatchingVPA(pod *v1.Pod) *vpa_types.VerticalPodAutoscaler {
configs, err := p.vpaLister.VerticalPodAutoscalers(pod.Namespace).List(labels.Everything())
if err != nil {
glog.Error("failed to get vpa configs: %v", err)
return nil
}
onConfigs := make([]*vpa_types.VerticalPodAutoscaler, 0)
for _, vpaConfig := range configs {
if vpa_api_util.GetUpdateMode(vpaConfig) == vpa_types.UpdateModeOff {
continue
}
onConfigs = append(onConfigs, vpaConfig)
}
glog.Infof("Let's choose from %d configs", len(onConfigs))
return vpa_api_util.GetControllingVPAForPod(pod, onConfigs)
}
// GetContainersResourcesForPod returns recommended request and limits for a given pod and name of controlling VPA.
// The returned slice corresponds 1-1 to containers in the Pod.
func (p *recommendationProvider) GetContainersResourcesForPod(pod *v1.Pod) ([]ContainerResources, string, error) {
glog.V(2).Infof("updating requirements for pod %s.", pod.Name)
vpaConfig := p.getMatchingVPA(pod)
if vpaConfig == nil {
glog.V(2).Infof("no matching VPA found for pod %s", pod.Name)
return nil, "", nil
}
recommendedPodResources, err := p.recommendationProcessor.Apply(vpaConfig.Status.Recommendation, vpaConfig.Spec.ResourcePolicy, pod)
if err != nil {
glog.V(2).Infof("cannot process recommendation for pod %s", pod.Name)
return nil, "", err
}
containerResources := getContainersResources(pod, *recommendedPodResources, vpaConfig.Spec.ResourcePolicy)
return containerResources, vpaConfig.Name, nil
}