forked from kubernetes/autoscaler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
labels.go
117 lines (102 loc) · 3.27 KB
/
labels.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
/*
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 labels
import (
"reflect"
"sort"
"strings"
apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)
var (
// cpu amount used for account pods that don't specify cpu requests
defaultMinCPU = *resource.NewMilliQuantity(50, resource.DecimalSI)
infrastructureLabels = []string{"kubernetes.io", "cloud.google.com"}
)
type nodeSelectorStats struct {
nodeSelector map[string]string
totalCpu resource.Quantity
}
// BestLabelSet returns a set of labels for nodes that will allow to schedule the pods that
// requested the most cpu.
func BestLabelSet(pods []*apiv1.Pod) map[string]string {
nodeSelectors := calculateNodeSelectorStats(pods)
sortNodeSelectorStats(nodeSelectors)
// Take labels from the selector that covers most of the pods (in terms of requested cpu).
selector := nodeSelectors[0].nodeSelector
// Expand the list of labels so that the other pods can fit as well. However as infrastructure
// related labels might not be compatibile with each other let's skip these selectors that
// require new infrastructure labels (like kubernetes.io/preemptive=true). New generic
// labels that are unlikely to cause problems when mixed are ok. And obviously skip pods that
// require conflicting labels.
statloop:
for _, nss := range nodeSelectors[1:] {
for k, v := range nss.nodeSelector {
currentValue, found := selector[k]
if found && currentValue != v {
continue statloop
}
if !found {
for _, infraLabel := range infrastructureLabels {
if strings.Contains(k, infraLabel) {
continue statloop
}
}
}
}
// All labels are non-infra and/or, can be added.
for k, v := range nss.nodeSelector {
selector[k] = v
}
}
return selector
}
func sortNodeSelectorStats(stats []nodeSelectorStats) {
sort.Slice(stats, func(i, j int) bool {
return stats[i].totalCpu.MilliValue() > stats[j].totalCpu.MilliValue()
})
}
func calculateNodeSelectorStats(pods []*apiv1.Pod) []nodeSelectorStats {
stats := make([]nodeSelectorStats, 0)
for _, pod := range pods {
var podCpu resource.Quantity
for _, container := range pod.Spec.Containers {
if container.Resources.Requests != nil {
containerCpu := container.Resources.Requests[apiv1.ResourceCPU]
podCpu.Add(containerCpu)
}
}
if podCpu.MilliValue() == 0 {
podCpu = defaultMinCPU
}
found := false
nodeSelector := pod.Spec.NodeSelector
if nodeSelector == nil {
nodeSelector = map[string]string{}
}
for i := range stats {
if reflect.DeepEqual(stats[i].nodeSelector, nodeSelector) {
found = true
stats[i].totalCpu.Add(podCpu)
break
}
}
if !found {
stats = append(stats, nodeSelectorStats{
nodeSelector: nodeSelector,
totalCpu: podCpu,
})
}
}
return stats
}