-
Notifications
You must be signed in to change notification settings - Fork 15
/
repair.go
154 lines (130 loc) · 4.53 KB
/
repair.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
package cke
import (
"errors"
"slices"
"time"
)
// RepairStatus is the status of a repair operation
type RepairStatus string
const (
RepairStatusQueued = RepairStatus("queued")
RepairStatusProcessing = RepairStatus("processing")
RepairStatusSucceeded = RepairStatus("succeeded")
RepairStatusFailed = RepairStatus("failed")
)
var repairStatuses = []RepairStatus{RepairStatusQueued, RepairStatusProcessing, RepairStatusSucceeded, RepairStatusFailed}
// RepairStepStatus is the status of the current step in a repair operation
type RepairStepStatus string
const (
RepairStepStatusWaiting = RepairStepStatus("waiting")
RepairStepStatusDraining = RepairStepStatus("draining")
RepairStepStatusWatching = RepairStepStatus("watching")
)
// RepairQueueEntry represents a queue entry of a repair operation
type RepairQueueEntry struct {
Index int64 `json:"index,string"`
Address string `json:"address"`
Nodename string `json:"nodename"`
MachineType string `json:"machine_type"`
Operation string `json:"operation"`
Status RepairStatus `json:"status"`
Step int `json:"step"`
StepStatus RepairStepStatus `json:"step_status"`
Deleted bool `json:"deleted"`
LastTransitionTime time.Time `json:"last_transition_time,omitempty"`
DrainBackOffCount int `json:"drain_backoff_count,omitempty"`
DrainBackOffExpire time.Time `json:"drain_backoff_expire,omitempty"`
}
var (
ErrRepairProcedureNotFound = errors.New("repair procedure not found for repair queue entry")
ErrRepairOperationNotFound = errors.New("repair operation not found for repair queue entry")
ErrRepairStepOutOfRange = errors.New("repair step of repair queue entry is out of range")
)
func NewRepairQueueEntry(operation, machineType, address string) *RepairQueueEntry {
return &RepairQueueEntry{
Operation: operation,
MachineType: machineType,
Address: address,
Status: RepairStatusQueued,
StepStatus: RepairStepStatusWaiting,
}
}
func (entry *RepairQueueEntry) FillNodename(cluster *Cluster) {
for _, node := range cluster.Nodes {
if node.Address == entry.Address {
entry.Nodename = node.Nodename()
return
}
}
entry.Nodename = ""
}
func (entry *RepairQueueEntry) IsInCluster() bool {
return entry.Nodename != ""
}
func (entry *RepairQueueEntry) HasFinished() bool {
return entry.Status == RepairStatusSucceeded || entry.Status == RepairStatusFailed
}
func (entry *RepairQueueEntry) getMatchingRepairProcedure(cluster *Cluster) (*RepairProcedure, error) {
for i, proc := range cluster.Repair.RepairProcedures {
if slices.Contains(proc.MachineTypes, entry.MachineType) {
return &cluster.Repair.RepairProcedures[i], nil
}
}
return nil, ErrRepairProcedureNotFound
}
func (entry *RepairQueueEntry) GetMatchingRepairOperation(cluster *Cluster) (*RepairOperation, error) {
proc, err := entry.getMatchingRepairProcedure(cluster)
if err != nil {
return nil, err
}
for i, op := range proc.RepairOperations {
if op.Operation == entry.Operation {
return &proc.RepairOperations[i], nil
}
}
return nil, ErrRepairOperationNotFound
}
func (entry *RepairQueueEntry) GetCurrentRepairStep(cluster *Cluster) (*RepairStep, error) {
op, err := entry.GetMatchingRepairOperation(cluster)
if err != nil {
return nil, err
}
if entry.Step >= len(op.RepairSteps) {
return nil, ErrRepairStepOutOfRange
}
return &op.RepairSteps[entry.Step], nil
}
func CountRepairQueueEntries(entries []*RepairQueueEntry) map[string]int {
ret := make(map[string]int)
for _, status := range repairStatuses {
// initialize explicitly to provide list of possible statuses
ret[string(status)] = 0
}
for _, entry := range entries {
ret[string(entry.Status)]++
}
return ret
}
func BuildMachineRepairStatus(nodes []*Node, entries []*RepairQueueEntry) map[string]map[string]bool {
ret := make(map[string]map[string]bool)
// (keys of ret) == union of (addresses of nodes) and (addresses of entries)
for _, node := range nodes {
ret[node.Address] = make(map[string]bool)
}
for _, entry := range entries {
if _, ok := ret[entry.Address]; ok {
continue
}
ret[entry.Address] = make(map[string]bool)
}
for address := range ret {
for _, status := range repairStatuses {
// initialize explicitly to provide list of possible statuses
ret[address][string(status)] = false
}
}
for _, entry := range entries {
ret[entry.Address][string(entry.Status)] = true
}
return ret
}