forked from kubernetes/autoscaler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
waste.go
96 lines (77 loc) · 3.16 KB
/
waste.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
/*
Copyright 2016 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 waste
import (
"github.com/golang/glog"
apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/autoscaler/cluster-autoscaler/expander"
"k8s.io/autoscaler/cluster-autoscaler/expander/random"
"k8s.io/kubernetes/plugin/pkg/scheduler/schedulercache"
)
type leastwaste struct {
fallbackStrategy expander.Strategy
}
// NewStrategy returns a strategy that selects the best scale up option based on which node group returns the least waste
func NewStrategy() expander.Strategy {
return &leastwaste{random.NewStrategy()}
}
// BestOption Finds the option that wastes the least fraction of CPU and Memory
func (l *leastwaste) BestOption(expansionOptions []expander.Option, nodeInfo map[string]*schedulercache.NodeInfo) *expander.Option {
var leastWastedScore float64
var leastWastedOptions []expander.Option
for _, option := range expansionOptions {
requestedCPU, requestedMemory := resourcesForPods(option.Pods)
node, found := nodeInfo[option.NodeGroup.Id()]
if !found {
glog.Errorf("No node info for: %s", option.NodeGroup.Id())
continue
}
nodeCPU, nodeMemory := resourcesForNode(node.Node())
availCPU := nodeCPU.MilliValue() * int64(option.NodeCount)
availMemory := nodeMemory.Value() * int64(option.NodeCount)
wastedCPU := float64(availCPU-requestedCPU.MilliValue()) / float64(availCPU)
wastedMemory := float64(availMemory-requestedMemory.Value()) / float64(availMemory)
wastedScore := wastedCPU + wastedMemory
glog.V(1).Infof("Expanding Node Group %s would waste %0.2f%% CPU, %0.2f%% Memory, %0.2f%% Blended\n", option.NodeGroup.Id(), wastedCPU*100.0, wastedMemory*100.0, wastedScore*50.0)
if wastedScore == leastWastedScore {
leastWastedOptions = append(leastWastedOptions, option)
}
if leastWastedOptions == nil || wastedScore < leastWastedScore {
leastWastedScore = wastedScore
leastWastedOptions = []expander.Option{option}
}
}
if len(leastWastedOptions) == 0 {
return nil
}
return l.fallbackStrategy.BestOption(leastWastedOptions, nodeInfo)
}
func resourcesForPods(pods []*apiv1.Pod) (cpu resource.Quantity, memory resource.Quantity) {
for _, pod := range pods {
for _, container := range pod.Spec.Containers {
if request, ok := container.Resources.Requests[apiv1.ResourceCPU]; ok {
cpu.Add(request)
}
if request, ok := container.Resources.Requests[apiv1.ResourceMemory]; ok {
memory.Add(request)
}
}
}
return cpu, memory
}
func resourcesForNode(node *apiv1.Node) (cpu resource.Quantity, memory resource.Quantity) {
cpu = node.Status.Capacity[apiv1.ResourceCPU]
memory = node.Status.Capacity[apiv1.ResourceMemory]
return cpu, memory
}