-
Notifications
You must be signed in to change notification settings - Fork 4k
/
priority.go
175 lines (147 loc) · 5.01 KB
/
priority.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
/*
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 priority
import (
"errors"
"fmt"
"regexp"
"gopkg.in/yaml.v2"
"k8s.io/autoscaler/cluster-autoscaler/expander"
apiv1 "k8s.io/api/core/v1"
"k8s.io/autoscaler/cluster-autoscaler/simulator/framework"
v1lister "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/tools/record"
klog "k8s.io/klog/v2"
)
const (
// PriorityConfigMapName defines a name of the ConfigMap used to store priority expander configuration
PriorityConfigMapName = "cluster-autoscaler-priority-expander"
// ConfigMapKey defines the key used in the ConfigMap to configure priorities
ConfigMapKey = "priorities"
)
type priorities map[int][]*regexp.Regexp
type priority struct {
logRecorder record.EventRecorder
okConfigUpdates int
badConfigUpdates int
configMapLister v1lister.ConfigMapNamespaceLister
}
// NewFilter returns an expansion filter that picks node groups based on user-defined priorities
func NewFilter(configMapLister v1lister.ConfigMapNamespaceLister,
logRecorder record.EventRecorder) expander.Filter {
res := &priority{
logRecorder: logRecorder,
configMapLister: configMapLister,
}
return res
}
func (p *priority) reloadConfigMap() (priorities, *apiv1.ConfigMap, error) {
cm, err := p.configMapLister.Get(PriorityConfigMapName)
if err != nil {
return nil, nil, fmt.Errorf("Priority expander config map %s not found: %v", PriorityConfigMapName, err)
}
prioString, found := cm.Data[ConfigMapKey]
if !found {
msg := fmt.Sprintf("Wrong configmap for priority expander, doesn't contain %s key. Ignoring update.",
ConfigMapKey)
p.logConfigWarning(cm, "PriorityConfigMapInvalid", msg)
return nil, cm, errors.New(msg)
}
newPriorities, err := p.parsePrioritiesYAMLString(prioString)
if err != nil {
msg := fmt.Sprintf("Wrong configuration for priority expander: %v. Ignoring update.", err)
p.logConfigWarning(cm, "PriorityConfigMapInvalid", msg)
return nil, cm, err
}
return newPriorities, cm, nil
}
func (p *priority) logConfigWarning(cm *apiv1.ConfigMap, reason, msg string) {
p.logRecorder.Event(cm, apiv1.EventTypeWarning, reason, msg)
klog.Warning(msg)
p.badConfigUpdates++
}
func (p *priority) parsePrioritiesYAMLString(prioritiesYAML string) (priorities, error) {
if prioritiesYAML == "" {
return nil, fmt.Errorf("priority configuration in %s configmap is empty; please provide valid configuration",
PriorityConfigMapName)
}
var config map[int][]string
if err := yaml.Unmarshal([]byte(prioritiesYAML), &config); err != nil {
return nil, fmt.Errorf("Can't parse YAML with priorities in the configmap: %v", err)
}
newPriorities := make(map[int][]*regexp.Regexp)
for prio, reList := range config {
for _, re := range reList {
regexp, err := regexp.Compile(re)
if err != nil {
return nil, fmt.Errorf("Can't compile regexp rule for priority %d and rule %s: %v", prio, re, err)
}
newPriorities[prio] = append(newPriorities[prio], regexp)
}
}
p.okConfigUpdates++
msg := "Successfully loaded priority configuration from configmap."
klog.V(4).Info(msg)
return newPriorities, nil
}
func (p *priority) BestOptions(expansionOptions []expander.Option, nodeInfo map[string]*framework.NodeInfo) []expander.Option {
if len(expansionOptions) <= 0 {
return nil
}
priorities, cm, err := p.reloadConfigMap()
if err != nil {
return expansionOptions
}
maxPrio := -1
best := []expander.Option{}
for _, option := range expansionOptions {
id := option.NodeGroup.Id()
found := false
for prio, nameRegexpList := range priorities {
if !p.groupIDMatchesList(id, nameRegexpList) {
continue
}
found = true
if prio < maxPrio {
continue
}
if prio > maxPrio {
maxPrio = prio
best = nil
}
best = append(best, option)
}
if !found {
msg := fmt.Sprintf("Priority expander: node group %s not found in priority expander configuration. "+
"The group won't be used.", id)
p.logConfigWarning(cm, "PriorityConfigMapNotMatchedGroup", msg)
}
}
if len(best) == 0 {
msg := "Priority expander: no priorities info found for any of the expansion options. No options filtered."
p.logConfigWarning(cm, "PriorityConfigMapNoGroupMatched", msg)
return expansionOptions
}
for _, opt := range best {
klog.V(2).Infof("priority expander: %s chosen as the highest available", opt.NodeGroup.Id())
}
return best
}
func (p *priority) groupIDMatchesList(id string, nameRegexpList []*regexp.Regexp) bool {
for _, re := range nameRegexpList {
if re.FindStringIndex(id) != nil {
return true
}
}
return false
}