-
Notifications
You must be signed in to change notification settings - Fork 14
/
node_filter.go
598 lines (539 loc) · 14.8 KB
/
node_filter.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
package server
import (
"strings"
"github.com/cybozu-go/cke"
"github.com/cybozu-go/cke/op/etcd"
"github.com/cybozu-go/cke/op/k8s"
"github.com/cybozu-go/log"
corev1 "k8s.io/api/core/v1"
)
// NodeFilter filters nodes to
type NodeFilter struct {
cluster *cke.Cluster
status *cke.ClusterStatus
nodeMap map[string]*cke.Node
cp []*cke.Node
}
// NewNodeFilter creates and initializes NodeFilter.
func NewNodeFilter(cluster *cke.Cluster, status *cke.ClusterStatus) *NodeFilter {
nodeMap := make(map[string]*cke.Node)
cp := make([]*cke.Node, 0, 5)
for _, n := range cluster.Nodes {
nodeMap[n.Address] = n
if n.ControlPlane {
cp = append(cp, n)
}
}
return &NodeFilter{
cluster: cluster,
status: status,
nodeMap: nodeMap,
cp: cp,
}
}
func (nf *NodeFilter) nodeStatus(n *cke.Node) *cke.NodeStatus {
return nf.status.NodeStatuses[n.Address]
}
// InCluster returns true if a node having address is defined in cluster YAML.
func (nf *NodeFilter) InCluster(address string) bool {
_, ok := nf.nodeMap[address]
return ok
}
// ControlPlane returns control plane nodes.
func (nf *NodeFilter) ControlPlane() []*cke.Node {
return nf.cp
}
// RiversStoppedNodes returns nodes that are not running rivers.
func (nf *NodeFilter) RiversStoppedNodes() (nodes []*cke.Node) {
for _, n := range nf.cluster.Nodes {
if !nf.nodeStatus(n).Rivers.Running {
nodes = append(nodes, n)
}
}
return nodes
}
// RiversOutdatedNodes returns nodes that are running rivers with outdated image or params.
func (nf *NodeFilter) RiversOutdatedNodes() (nodes []*cke.Node) {
currentBuiltIn := k8s.RiversParams(nf.cp)
currentExtra := nf.cluster.Options.Rivers
for _, n := range nf.cluster.Nodes {
st := nf.nodeStatus(n).Rivers
switch {
case !st.Running:
// stopped nodes are excluded
case cke.ToolsImage.Name() != st.Image:
fallthrough
case !currentBuiltIn.Equal(st.BuiltInParams):
fallthrough
case !currentExtra.Equal(st.ExtraParams):
nodes = append(nodes, n)
}
}
return nodes
}
// EtcdBootstrapped returns true if etcd cluster has been bootstrapped.
func (nf *NodeFilter) EtcdBootstrapped() bool {
for _, n := range nf.cp {
if nf.nodeStatus(n).Etcd.HasData {
return true
}
}
return false
}
// EtcdIsGood returns true if etcd cluster is responding and all members are in sync.
func (nf *NodeFilter) EtcdIsGood() bool {
st := nf.status.Etcd
if !st.IsHealthy {
return false
}
return len(st.Members) == len(st.InSyncMembers)
}
// EtcdStoppedMembers returns control plane nodes that are not running etcd.
func (nf *NodeFilter) EtcdStoppedMembers() (nodes []*cke.Node) {
for _, n := range nf.cp {
if _, ok := nf.status.Etcd.Members[n.Address]; !ok && nf.status.Etcd.IsHealthy {
continue
}
st := nf.nodeStatus(n).Etcd
if st.Running {
continue
}
if !st.HasData {
continue
}
nodes = append(nodes, n)
}
return nodes
}
// EtcdNonClusterMemberIDs returns IDs of etcd members not defined in cluster YAML.
func (nf *NodeFilter) EtcdNonClusterMemberIDs(healthy bool) (ids []uint64) {
st := nf.status.Etcd
for k, v := range st.Members {
if nf.InCluster(k) {
continue
}
if st.InSyncMembers[k] != healthy {
continue
}
ids = append(ids, v.ID)
}
return ids
}
// EtcdNonCPMembers returns nodes and IDs of etcd members running on
// non control plane nodes. The order of ids matches the order of nodes.
func (nf *NodeFilter) EtcdNonCPMembers(healthy bool) (nodes []*cke.Node, ids []uint64) {
st := nf.status.Etcd
for k, v := range st.Members {
n, ok := nf.nodeMap[k]
if !ok {
continue
}
if n.ControlPlane {
continue
}
if st.InSyncMembers[k] != healthy {
continue
}
nodes = append(nodes, n)
ids = append(ids, v.ID)
}
return nodes, ids
}
// EtcdUnstartedMembers returns nodes that are added to members but not really
// joined to the etcd cluster. Such members need to be re-added.
func (nf *NodeFilter) EtcdUnstartedMembers() (nodes []*cke.Node) {
st := nf.status.Etcd
for k, v := range st.Members {
n, ok := nf.nodeMap[k]
if !ok {
continue
}
if !n.ControlPlane {
continue
}
if len(v.Name) > 0 {
continue
}
nodes = append(nodes, n)
}
return nodes
}
// EtcdNewMembers returns control plane nodes to be added to the etcd cluster.
func (nf *NodeFilter) EtcdNewMembers() (nodes []*cke.Node) {
members := nf.status.Etcd.Members
for _, n := range nf.cp {
if _, ok := members[n.Address]; ok {
continue
}
nodes = append(nodes, n)
}
return nodes
}
func etcdEqualParams(running, current cke.ServiceParams) bool {
// NOTE ignore parameters starting with "--initial-" prefix.
// There options are used only on starting etcd process at first time.
var rarg, carg []string
for _, s := range running.ExtraArguments {
if !strings.HasPrefix(s, "--initial-") {
rarg = append(rarg, s)
}
}
for _, s := range current.ExtraArguments {
if !strings.HasPrefix(s, "--initial-") {
carg = append(carg, s)
}
}
rparams := cke.ServiceParams{
ExtraArguments: rarg,
ExtraBinds: running.ExtraBinds,
ExtraEnvvar: running.ExtraEnvvar,
}
cparams := cke.ServiceParams{
ExtraArguments: carg,
ExtraBinds: current.ExtraBinds,
ExtraEnvvar: current.ExtraEnvvar,
}
return rparams.Equal(cparams)
}
// EtcdOutdatedMembers returns nodes that are running etcd with outdated image or params.
func (nf *NodeFilter) EtcdOutdatedMembers() (nodes []*cke.Node) {
currentExtra := nf.cluster.Options.Etcd.ServiceParams
for _, n := range nf.cp {
st := nf.nodeStatus(n).Etcd
if !st.Running {
continue
}
currentBuiltIn := etcd.BuiltInParams(n, []string{}, "new")
switch {
case cke.EtcdImage.Name() != st.Image:
fallthrough
case !etcdEqualParams(st.BuiltInParams, currentBuiltIn):
fallthrough
case !etcdEqualParams(st.ExtraParams, currentExtra):
nodes = append(nodes, n)
}
}
return nodes
}
// APIServerStoppedNodes returns control plane nodes that are not running API server.
func (nf *NodeFilter) APIServerStoppedNodes() (nodes []*cke.Node) {
for _, n := range nf.cp {
if !nf.nodeStatus(n).APIServer.Running {
nodes = append(nodes, n)
}
}
return nodes
}
// APIServerOutdatedNodes returns nodes that are running API server with outdated image or params.
func (nf *NodeFilter) APIServerOutdatedNodes() (nodes []*cke.Node) {
currentExtra := nf.cluster.Options.APIServer
for _, n := range nf.cp {
st := nf.nodeStatus(n).APIServer
currentBuiltIn := k8s.APIServerParams(nf.ControlPlane(), n.Address, nf.cluster.ServiceSubnet)
switch {
case !st.Running:
// stopped nodes are excluded
case cke.HyperkubeImage.Name() != st.Image:
fallthrough
case !currentBuiltIn.Equal(st.BuiltInParams):
fallthrough
case !currentExtra.Equal(st.ExtraParams):
nodes = append(nodes, n)
}
}
return nodes
}
// ControllerManagerStoppedNodes returns control plane nodes that are not running controller manager.
func (nf *NodeFilter) ControllerManagerStoppedNodes() (nodes []*cke.Node) {
for _, n := range nf.cp {
if !nf.nodeStatus(n).ControllerManager.Running {
nodes = append(nodes, n)
}
}
return nodes
}
// ControllerManagerOutdatedNodes returns nodes that are running controller manager with outdated image or params.
func (nf *NodeFilter) ControllerManagerOutdatedNodes() (nodes []*cke.Node) {
currentBuiltIn := k8s.ControllerManagerParams(nf.cluster.Name, nf.cluster.ServiceSubnet)
currentExtra := nf.cluster.Options.ControllerManager
for _, n := range nf.cp {
st := nf.nodeStatus(n).ControllerManager
switch {
case !st.Running:
// stopped nodes are excluded
case cke.HyperkubeImage.Name() != st.Image:
fallthrough
case !currentBuiltIn.Equal(st.BuiltInParams):
fallthrough
case !currentExtra.Equal(st.ExtraParams):
nodes = append(nodes, n)
}
}
return nodes
}
// SchedulerStoppedNodes returns control plane nodes that are not running kube-scheduler.
func (nf *NodeFilter) SchedulerStoppedNodes() (nodes []*cke.Node) {
for _, n := range nf.cp {
if !nf.nodeStatus(n).Scheduler.Running {
nodes = append(nodes, n)
}
}
return nodes
}
// SchedulerOutdatedNodes returns nodes that are running kube-scheduler with outdated image or params.
func (nf *NodeFilter) SchedulerOutdatedNodes() (nodes []*cke.Node) {
currentBuiltIn := k8s.SchedulerParams()
currentExtra := nf.cluster.Options.Scheduler
for _, n := range nf.cp {
st := nf.nodeStatus(n).Scheduler
switch {
case !st.Running:
// stopped nodes are excluded
case cke.HyperkubeImage.Name() != st.Image:
fallthrough
case !currentBuiltIn.Equal(st.BuiltInParams):
fallthrough
case !currentExtra.Equal(st.ExtraParams):
nodes = append(nodes, n)
}
}
return nodes
}
// KubeletStoppedNodes returns nodes that are not running kubelet.
func (nf *NodeFilter) KubeletStoppedNodes() (nodes []*cke.Node) {
for _, n := range nf.cluster.Nodes {
if !nf.nodeStatus(n).Kubelet.Running {
nodes = append(nodes, n)
}
}
return nodes
}
// KubeletStoppedRegisteredNodes returns nodes that are not running kubelet and are registered on Kubernetes.
func (nf *NodeFilter) KubeletStoppedRegisteredNodes() (nodes []*cke.Node) {
registered := make(map[string]bool)
for _, kn := range nf.status.Kubernetes.Nodes {
registered[kn.Name] = true
}
for _, n := range nf.KubeletStoppedNodes() {
if registered[n.Nodename()] {
nodes = append(nodes, n)
}
}
return nodes
}
// KubeletOutdatedNodes returns nodes that are running kubelet with outdated image or params.
func (nf *NodeFilter) KubeletOutdatedNodes() (nodes []*cke.Node) {
currentOpts := nf.cluster.Options.Kubelet
currentExtra := nf.cluster.Options.Kubelet.ServiceParams
for _, n := range nf.cluster.Nodes {
st := nf.nodeStatus(n).Kubelet
currentBuiltIn := k8s.KubeletServiceParams(n)
switch {
case !st.Running:
// stopped nodes are excluded
case cke.HyperkubeImage.Name() != st.Image:
fallthrough
case currentOpts.Domain != st.Domain:
fallthrough
case currentOpts.AllowSwap != st.AllowSwap:
fallthrough
case !kubeletEqualParams(st.BuiltInParams, currentBuiltIn):
fallthrough
case !currentExtra.Equal(st.ExtraParams):
nodes = append(nodes, n)
}
}
return nodes
}
// NonClusterNodes returns nodes not defined in cluster YAML.
func (nf *NodeFilter) NonClusterNodes() (nodes []*corev1.Node) {
members := nf.status.Kubernetes.Nodes
for _, member := range members {
if nf.InCluster(member.Name) {
continue
}
member := member
nodes = append(nodes, &member)
}
return nodes
}
func kubeletEqualParams(running, current cke.ServiceParams) bool {
// NOTE ignore parameter "--register-with-taints".
// This option is used only when kubelet registers the node first time.
var rarg []string
for _, s := range running.ExtraArguments {
if !strings.HasPrefix(s, "--register-with-taints") {
rarg = append(rarg, s)
}
}
running.ExtraArguments = rarg
return running.Equal(current)
}
// ProxyStoppedNodes returns nodes that are not running kube-proxy.
func (nf *NodeFilter) ProxyStoppedNodes() (nodes []*cke.Node) {
for _, n := range nf.cluster.Nodes {
if !nf.nodeStatus(n).Proxy.Running {
nodes = append(nodes, n)
}
}
return nodes
}
// ProxyOutdatedNodes returns nodes that are running kube-proxy with outdated image or params.
func (nf *NodeFilter) ProxyOutdatedNodes() (nodes []*cke.Node) {
currentBuiltIn := k8s.ProxyParams()
currentExtra := nf.cluster.Options.Proxy
for _, n := range nf.cluster.Nodes {
st := nf.nodeStatus(n).Proxy
switch {
case !st.Running:
// stopped nodes are excluded
case cke.HyperkubeImage.Name() != st.Image:
fallthrough
case !currentBuiltIn.Equal(st.BuiltInParams):
fallthrough
case !currentExtra.Equal(st.ExtraParams):
nodes = append(nodes, n)
}
}
return nodes
}
// HealthyAPIServer returns a control plane node running healthy API server.
// If there is no healthy API server, it returns the first control plane node.
func (nf *NodeFilter) HealthyAPIServer() *cke.Node {
var node *cke.Node
for _, n := range nf.ControlPlane() {
node = n
if nf.nodeStatus(n).APIServer.IsHealthy {
break
}
}
return node
}
func isInternal(name string) bool {
if strings.HasPrefix(name, "cke.cybozu.com/") {
return true
}
if strings.Contains(name, ".cke.cybozu.com/") {
return true
}
return false
}
// OutdatedAttrsNodes returns nodes that have outdated set of labels,
// attributes, and/or taints.
func (nf *NodeFilter) OutdatedAttrsNodes() (nodes []*corev1.Node) {
curNodes := make(map[string]*corev1.Node)
for _, cn := range nf.status.Kubernetes.Nodes {
curNodes[cn.Name] = cn.DeepCopy()
}
for _, n := range nf.cluster.Nodes {
current, ok := curNodes[n.Nodename()]
if !ok {
log.Warn("missing Kubernetes Node resource", map[string]interface{}{
"name": n.Nodename(),
"address": n.Address,
})
continue
}
if nodeIsOutdated(n, current) {
labels := make(map[string]string)
for k, v := range current.Labels {
if isInternal(k) {
continue
}
labels[k] = v
}
for k, v := range n.Labels {
labels[k] = v
}
current.Labels = labels
annotations := make(map[string]string)
for k, v := range current.Annotations {
if isInternal(k) {
continue
}
annotations[k] = v
}
for k, v := range n.Annotations {
annotations[k] = v
}
current.Annotations = annotations
nTaints := make(map[string]bool)
for _, taint := range n.Taints {
nTaints[taint.Key] = true
}
taints := make([]corev1.Taint, len(n.Taints))
copy(taints, n.Taints)
for _, taint := range current.Spec.Taints {
if isInternal(taint.Key) || nTaints[taint.Key] {
continue
}
taints = append(taints, taint)
}
current.Spec.Taints = taints
nodes = append(nodes, current)
}
}
return nodes
}
func nodeIsOutdated(n *cke.Node, current *corev1.Node) bool {
for k, v := range n.Labels {
cv, ok := current.Labels[k]
if !ok || v != cv {
return true
}
}
// Labels for CKE internal use need to be synchronized.
for k := range current.Labels {
if !isInternal(k) {
continue
}
if _, ok := n.Labels[k]; !ok {
return true
}
}
for k, v := range n.Annotations {
cv, ok := current.Annotations[k]
if !ok || v != cv {
return true
}
}
// Annotations for CKE internal use need to be synchronized.
for k := range current.Annotations {
if !isInternal(k) {
continue
}
if _, ok := n.Annotations[k]; !ok {
return true
}
}
curTaints := make(map[string]corev1.Taint)
for _, taint := range current.Spec.Taints {
curTaints[taint.Key] = taint
}
for _, taint := range n.Taints {
cv, ok := curTaints[taint.Key]
if !ok {
return true
}
if taint.Value != cv.Value {
return true
}
if taint.Effect != cv.Effect {
return true
}
}
// Taints for CKE internal use need to be synchronized.
nTaints := make(map[string]corev1.Taint)
for _, taint := range n.Taints {
nTaints[taint.Key] = taint
}
for _, taint := range current.Spec.Taints {
if !isInternal(taint.Key) {
continue
}
if _, ok := nTaints[taint.Key]; !ok {
return true
}
}
return false
}