-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
320 lines (268 loc) · 9.6 KB
/
main.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"golang.org/x/net/context"
"gopkg.in/yaml.v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
types "k8s.io/apimachinery/pkg/types"
applyconfigurationscorev1 "k8s.io/client-go/applyconfigurations/core/v1"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
_ "k8s.io/client-go/tools/record"
klog "k8s.io/klog/v2"
"os"
"regexp"
"strings"
)
type priorities map[int][]*regexp.Regexp
func (prio priorities) createPriorityIndexIfAbsent(priorityValue int) (created bool) {
if _, ok := prio[priorityValue]; !ok {
prio[priorityValue] = make([]*regexp.Regexp, 0)
created = true
}
return created
}
func (prio priorities) AddNpIfNotExists(np []string, priorityValue int) {
i := 0
prio.createPriorityIndexIfAbsent(priorityValue)
j := len(prio[priorityValue])
prio[priorityValue] = append(prio[priorityValue], make([]*regexp.Regexp, len(np))...)
for _, target := range np {
reg, err := regexp.Compile(".*" + regexp.QuoteMeta(target) + ".*")
if err == nil {
found := false
for k := range prio[priorityValue] {
if prio[priorityValue][k] == nil {
break
}
if prio[priorityValue][k].String() == reg.String() {
found = true
break
}
}
if !found {
prio[priorityValue][i+j] = reg
i++
}
}
}
k := j + i
prio[priorityValue] = prio[priorityValue][0:k]
}
func (prio priorities) RemoveNpIfExists(np []string, priorityValue int) {
j := 0
if !prio.createPriorityIndexIfAbsent(priorityValue) {
toKeepPrs := make([]*regexp.Regexp, len(prio[priorityValue]))
for _, existingRegexp := range prio[priorityValue] {
exists := false
for _, src := range np {
reg, err := regexp.Compile(".*" + regexp.QuoteMeta(src) + ".*")
if err == nil {
if existingRegexp.String() == reg.String() {
exists = true
break
}
}
}
if !exists {
toKeepPrs[j] = existingRegexp
j++
}
}
prio[priorityValue] = toKeepPrs[0:j]
}
}
func (prio priorities) SerializePriorities() serializablePriorities {
var ser serializablePriorities = make(map[int][]string, len(prio))
for priorityLevel, patterns := range prio {
ser[priorityLevel] = make([]string, len(patterns))
for i, pattern := range patterns {
ser[priorityLevel][i] = pattern.String()
}
}
return ser
}
type serializablePriorities map[int][]string
type patchStringValue struct {
Op string `json:"op"`
Path string `json:"path"`
Value bool `json:"value"`
}
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"
)
var (
kubeconfig = flag.String("kubeconfig", "~/.kube/config", "Kubeconfig file absolute path")
prioritizeNp = flag.String("prioritize-np", "test-p2,test-p3", "Nodepools list that will be prioritized, comma separated for several ones")
cordonNp = flag.String("cordon-np", "test-p1", "Nodepools to cordon (mark unschedulable in spec), comma separated for several ones")
nodePoolLabel = flag.String("nodepool-label", "cloud.google.com/gke-nodepool", "Nodepool selector label key")
priorityValue = flag.Int("priority-value", 100, "priority-value defines the priority level to set for np regexps that will be prioritized to receive workloads")
priorityCmNs = flag.String("priority-cm-ns", "kube-system", "The namespace where is living cluster-autoscaler-priority-expander configmap")
undo = flag.Bool("undo", false, "Undo")
config *rest.Config
)
func npInputCleaning(input *string) []string {
var cleanNpList []string
nps := strings.Split(*input, ",")
for _, np := range nps {
if np != "" {
cleanNpList = append(cleanNpList, np)
}
}
return cleanNpList
}
func parsePrioritiesYAMLString(prioritiesYAML string) (priorities, error) {
if prioritiesYAML == "" {
return nil, fmt.Errorf("priority configuration in %s configmap is empty; please provide a 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)
}
}
klog.V(4).Info("Successfully loaded priorities configuration from configmap")
return newPriorities, nil
}
func GetClientset() (cs *kubernetes.Clientset, retErr error) {
var err error
confPath := *kubeconfig
if confPath == "" {
config, err = rest.InClusterConfig()
if err != nil {
retErr = errors.New(err.Error())
}
} else {
if confPath[0] == '~' {
dirname, err := os.UserHomeDir()
if err != nil {
klog.Fatalf("Error trying to retrieve user home dir: %s", err)
}
confPath = dirname + confPath[1:]
}
config, err = clientcmd.BuildConfigFromFlags("", confPath)
if err != nil {
retErr = errors.New(fmt.Sprintf("Error with config file `%s`, %s", confPath, err))
}
}
cs, err = kubernetes.NewForConfig(config)
if err != nil {
retErr = errors.New(fmt.Sprintf("Bad config file `%s`", err))
} else {
}
return cs, retErr
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
klog.InitFlags(nil)
defer klog.Flush()
flag.Parse()
clientset, err := GetClientset()
if err != nil {
klog.Fatalf("Unable to get a proper configuration file to query a kube api server: %s", err)
} else {
klog.V(4).Info("Clientset properly retrieved")
}
var prs priorities
core := clientset.CoreV1()
klog.V(4).Infof("Trying to retrieve `%s` configmap in `%s` namespace", PriorityConfigMapName, *priorityCmNs)
prioExpanderCm, err := core.ConfigMaps(*priorityCmNs).Get(ctx, PriorityConfigMapName, metav1.GetOptions{})
if err == nil {
klog.V(4).Infof("Configmap retrieved! Now trying to parse `%s` key", ConfigMapKey)
prs, err = parsePrioritiesYAMLString(prioExpanderCm.Data[ConfigMapKey])
if err != nil {
klog.Infof("Unable to parse priorities string value at key `%s` in existing `%s` configmap, we will build a new one", ConfigMapKey, PriorityConfigMapName)
prs = make(map[int][]*regexp.Regexp)
}
} else {
klog.Infof("Unable to get `%s` configmap, we will create a new one", PriorityConfigMapName)
prs = make(map[int][]*regexp.Regexp)
}
klog.Infof("Initial `%s` configmap `%s` key state: %s", PriorityConfigMapName, ConfigMapKey, prs)
klog.V(4).Infof("Before cleaning `prioritize-np` input value: %s", *prioritizeNp)
klog.V(4).Infof("Before cleaning `cordon-np` input value: %s", *cordonNp)
klog.V(4).Info("Cleaning `prioritize-np` and `cordon-np` inputs")
prioritizeAr := npInputCleaning(prioritizeNp)
cordonAr := npInputCleaning(cordonNp)
klog.V(4).Infof("Cleaned `prioritize-np` value: %s", prioritizeAr)
klog.V(4).Infof("Cleaned `cordon-np` value: %s", cordonAr)
if *undo {
klog.Infof("About to remove %s from priorities cm", prioritizeAr)
prs.RemoveNpIfExists(prioritizeAr, *priorityValue)
} else {
klog.V(4).Infof("Removing cordon nodepools %s from %s", cordonAr, prs)
prs.RemoveNpIfExists(cordonAr, *priorityValue)
klog.V(4).Infof("Adding prioritize nodepools %s to %s", prioritizeAr, prs)
prs.AddNpIfNotExists(prioritizeAr, *priorityValue)
}
klog.V(4).Infof("Serializing new priorities value: %s", prs)
strPrs := prs.SerializePriorities()
newPrsStr, err := yaml.Marshal(&strPrs)
if err == nil {
klog.Infof("New serialized computed priorities value:\n%s", newPrsStr)
}
cmApplyConf := applyconfigurationscorev1.ConfigMap(PriorityConfigMapName, *priorityCmNs)
cmApplyConf.Data = make(map[string]string, 1)
cmApplyConf.Data[ConfigMapKey] = string(newPrsStr)
klog.Infof("Will apply new computed configmap: %s", cmApplyConf)
_, err = core.ConfigMaps(*priorityCmNs).Apply(ctx, cmApplyConf, metav1.ApplyOptions{FieldManager: "k8s-nodepool-cordon", Force: true})
if err != nil {
klog.Fatalf("Unable apply cm: %s", err)
}
unschedValue := true
if *undo {
klog.Infof("About to mark schedulable all nodes having label `%s` in %s", *nodePoolLabel, cordonAr)
unschedValue = false
} else {
klog.Infof("About to mark unschedulable all nodes having label `%s` in %s", *nodePoolLabel, cordonAr)
}
payload := []patchStringValue{{
Op: "replace",
Path: "/spec/unschedulable",
Value: unschedValue,
}}
klog.V(4).Infof("The patch submitted to apiserver for concerned nodes will look like: %s", payload)
payloadBytes, err := json.Marshal(payload)
if err != nil {
klog.Fatalf("Unable to json marshal path: %s", err)
}
for _, srcNp := range cordonAr {
klog.V(4).Infof("About to list the nodes under %s nodepool", srcNp)
nodesLs, err := core.Nodes().List(ctx, metav1.ListOptions{
LabelSelector: *nodePoolLabel + "=" + srcNp,
})
if err != nil {
klog.Errorf("Skipping %s, we received an error trying to retrieve matching nodes: %s", srcNp, err)
continue
}
if len(nodesLs.Items) == 0 {
klog.Infof("No nodes matching %s nodepool", srcNp)
}
for _, nd := range nodesLs.Items {
klog.Infof("Patching node %s under nodepool %s", nd, srcNp)
_, err := core.Nodes().Patch(ctx, nd.ObjectMeta.Name, types.JSONPatchType, payloadBytes, metav1.PatchOptions{})
if err != nil {
klog.Errorf("Error patching node %s under nodepool %s: %s", nd, srcNp, err)
}
}
}
klog.Info("Task achieved!")
}