-
Notifications
You must be signed in to change notification settings - Fork 15
/
reboot.go
99 lines (84 loc) · 2.61 KB
/
reboot.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
package cke
import (
"time"
)
// RebootStatus is status of reboot operation
type RebootStatus string
// Reboot statuses
const (
RebootStatusQueued = RebootStatus("queued")
RebootStatusDraining = RebootStatus("draining")
RebootStatusRebooting = RebootStatus("rebooting")
RebootStatusCancelled = RebootStatus("cancelled")
)
var rebootStatuses = []RebootStatus{RebootStatusQueued, RebootStatusDraining, RebootStatusRebooting, RebootStatusCancelled}
// RebootQueueEntry represents a queue entry of reboot operation
type RebootQueueEntry struct {
Index int64 `json:"index,string"`
Node string `json:"node"`
Status RebootStatus `json:"status"`
LastTransitionTime time.Time `json:"last_transition_time,omitempty"`
DrainBackOffCount int `json:"drain_backoff_count,omitempty"`
DrainBackOffExpire time.Time `json:"drain_backoff_expire,omitempty"`
}
// NewRebootQueueEntry creates new `RebootQueueEntry`.
// `Index` will be supplied in registration.
func NewRebootQueueEntry(node string) *RebootQueueEntry {
return &RebootQueueEntry{
Node: node,
Status: RebootStatusQueued,
}
}
// ClusterMember returns whether the node in this entry is a cluster member.
func (entry *RebootQueueEntry) ClusterMember(c *Cluster) bool {
for _, clusterNode := range c.Nodes {
if entry.Node == clusterNode.Address {
return true
}
}
return false
}
func DedupRebootQueueEntries(entries []*RebootQueueEntry) []*RebootQueueEntry {
var ret []*RebootQueueEntry
nodes := map[string]bool{}
for _, entry := range entries {
if !nodes[entry.Node] {
nodes[entry.Node] = true
ret = append(ret, entry)
}
}
return ret
}
func CountRebootQueueEntries(entries []*RebootQueueEntry) map[string]int {
ret := map[string]int{}
for _, status := range rebootStatuses {
// initialize explicitly to provide list of possible statuses
ret[string(status)] = 0
}
for _, entry := range entries {
ret[string(entry.Status)]++
}
return ret
}
func BuildNodeRebootStatus(nodes []*Node, entries []*RebootQueueEntry) map[string]map[string]bool {
ret := make(map[string]map[string]bool)
addr2name := make(map[string]string)
for _, node := range nodes {
name := node.Nodename()
ret[name] = make(map[string]bool)
for _, status := range rebootStatuses {
// initialize explicitly to provide list of possible statuses
ret[name][string(status)] = false
}
addr2name[node.Address] = name
}
for _, entry := range entries {
name, ok := addr2name[entry.Node]
if !ok {
// removed from K8s cluster after queued
continue
}
ret[name][string(entry.Status)] = true
}
return ret
}