-
Notifications
You must be signed in to change notification settings - Fork 8
/
machines.go
250 lines (216 loc) · 6.34 KB
/
machines.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
package sabakan
import (
"errors"
"fmt"
"regexp"
"time"
version "github.com/hashicorp/go-version"
)
// MachineState represents a machine's state.
type MachineState string
// String implements fmt.Stringer interface.
func (ms MachineState) String() string {
return string(ms)
}
// IsValid returns true only if the MachineState is pre-defined.
func (ms MachineState) IsValid() bool {
switch ms {
case StateUninitialized:
return true
case StateHealthy:
return true
case StateUnhealthy:
return true
case StateUnreachable:
return true
case StateUpdating:
return true
case StateRetiring:
return true
case StateRetired:
return true
}
return false
}
// GQLEnum returns graphql defined enum string.
func (ms MachineState) GQLEnum() string {
switch ms {
case StateUninitialized:
return "UNINITIALIZED"
case StateHealthy:
return "HEALTHY"
case StateUnhealthy:
return "UNHEALTHY"
case StateUnreachable:
return "UNREACHABLE"
case StateUpdating:
return "UPDATING"
case StateRetiring:
return "RETIRING"
case StateRetired:
return "RETIRED"
}
return ""
}
// Machine state definitions.
const (
StateUninitialized = MachineState("uninitialized")
StateHealthy = MachineState("healthy")
StateUnhealthy = MachineState("unhealthy")
StateUnreachable = MachineState("unreachable")
StateUpdating = MachineState("updating")
StateRetiring = MachineState("retiring")
StateRetired = MachineState("retired")
SetStateErrorFormat = "transition from [ %s ] to [ %s ] is forbidden"
)
var (
reValidBmcType = regexp.MustCompile(`^[a-z0-9A-Z-_/.]+$`)
reValidLabelName = regexp.MustCompile(`^[a-z0-9A-Z]([a-z0-9A-Z_.-]{0,61}[a-z0-9A-Z])?$`)
reValidLabelVal = regexp.MustCompile(`^[a-z0-9A-Z]([a-z0-9A-Z_.-]{0,61}[a-z0-9A-Z])?$`)
permittedTransitions = map[MachineState][]MachineState{
StateUninitialized: {StateHealthy, StateRetiring},
StateHealthy: {StateUnhealthy, StateUnreachable, StateUpdating, StateRetiring},
StateUnhealthy: {StateHealthy, StateUnreachable, StateUpdating, StateRetiring},
StateUnreachable: {StateHealthy, StateUnhealthy, StateUpdating, StateRetiring},
StateUpdating: {StateUninitialized},
StateRetiring: {StateRetired},
StateRetired: {StateUninitialized},
}
// StateList is the list of possible machine states
StateList = []MachineState{
StateUninitialized,
StateHealthy,
StateUnhealthy,
StateUnreachable,
StateUpdating,
StateRetiring,
StateRetired,
}
)
// IsValidRole returns true if role is valid as machine role
func IsValidRole(role string) bool {
return reValidLabelVal.MatchString(role)
}
// IsValidIgnitionID returns true if id is valid as ignition ID
func IsValidIgnitionID(id string) bool {
_, err := version.NewVersion(id)
return err == nil
}
// IsValidBmcType returns true if role is valid as BMC type
func IsValidBmcType(bmcType string) bool {
return reValidBmcType.MatchString(bmcType)
}
// IsValidLabelName returns true if label name is valid
// This is the same as the validation for Kubernetes label names.
func IsValidLabelName(name string) bool {
return reValidLabelName.MatchString(name)
}
// IsValidLabelValue returns true if label value is valid
// This is the same as the validation for Kubernetes label values.
func IsValidLabelValue(value string) bool {
if value == "" {
return true
}
return reValidLabelVal.MatchString(value)
}
// MachineBMC is a bmc interface struct for Machine
type MachineBMC struct {
IPv4 string `json:"ipv4"`
IPv6 string `json:"ipv6"`
Type string `json:"type"`
}
// MachineSpec is a set of attributes to define a machine.
type MachineSpec struct {
Serial string `json:"serial"`
Labels map[string]string `json:"labels"`
Rack uint `json:"rack"`
IndexInRack uint `json:"index-in-rack"`
Role string `json:"role"`
IPv4 []string `json:"ipv4"`
IPv6 []string `json:"ipv6"`
RegisterDate time.Time `json:"register-date"`
RetireDate time.Time `json:"retire-date"`
BMC MachineBMC `json:"bmc"`
}
// MachineStatus represents the status of a machine.
type MachineStatus struct {
Timestamp time.Time `json:"timestamp"`
Duration float64 `json:"duration"`
State MachineState `json:"state"`
}
// NetworkInfo represents NIC configurations.
type NetworkInfo struct {
IPv4 []NICConfig `json:"ipv4"`
}
// BMCInfo represents BMC NIC configuration information.
type BMCInfo struct {
IPv4 NICConfig `json:"ipv4"`
}
// NICConfig represents NIC configuration information.
type NICConfig struct {
Address string `json:"address"`
Netmask string `json:"netmask"`
MaskBits int `json:"maskbits"`
Gateway string `json:"gateway"`
}
// MachineInfo is a set of associated information of a Machine.
type MachineInfo struct {
Network NetworkInfo `json:"network"`
BMC BMCInfo `json:"bmc"`
}
// Machine represents a server hardware.
type Machine struct {
Spec MachineSpec `json:"spec"`
Status MachineStatus `json:"status"`
Info MachineInfo `json:"info"`
}
// NewMachine creates a new machine instance.
func NewMachine(spec MachineSpec) *Machine {
return &Machine{
Spec: spec,
Status: MachineStatus{
Timestamp: time.Now().UTC(),
State: StateUninitialized,
},
}
}
func (m *Machine) isPermittedTransition(to MachineState) bool {
for _, v := range permittedTransitions[m.Status.State] {
if v == to {
return true
}
}
return false
}
// SetState sets the state of the machine.
func (m *Machine) SetState(ms MachineState) error {
if m.Status.State == ms {
return nil
}
_, ok := permittedTransitions[m.Status.State]
if !ok {
return errors.New(m.Status.State.String() + " has no permitted states")
}
if !m.isPermittedTransition(ms) {
return fmt.Errorf(SetStateErrorFormat, m.Status.State.String(), ms.String())
}
m.Status.State = ms
m.Status.Timestamp = time.Now().UTC()
return nil
}
// PutLabel adds a label to Machine if no label with the same name exists, or replaces a label.
func (m *Machine) PutLabel(label, value string) {
if m.Spec.Labels == nil {
m.Spec.Labels = make(map[string]string)
}
m.Spec.Labels[label] = value
}
// DeleteLabel deletes label from Machine.
func (m *Machine) DeleteLabel(label string) error {
_, ok := m.Spec.Labels[label]
if !ok {
return ErrNotFound
}
delete(m.Spec.Labels, label)
return nil
}