-
Notifications
You must be signed in to change notification settings - Fork 254
/
host_state_machine.go
600 lines (533 loc) · 20.2 KB
/
host_state_machine.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
599
600
package controllers
import (
"encoding/json"
"fmt"
metal3api "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
"github.com/metal3-io/baremetal-operator/pkg/provisioner"
"github.com/go-logr/logr"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// hostStateMachine is a finite state machine that manages transitions between
// the states of a BareMetalHost.
type hostStateMachine struct {
Host *metal3api.BareMetalHost
NextState metal3api.ProvisioningState
Reconciler *BareMetalHostReconciler
Provisioner provisioner.Provisioner
haveCreds bool
}
func newHostStateMachine(host *metal3api.BareMetalHost,
reconciler *BareMetalHostReconciler,
provisioner provisioner.Provisioner,
haveCreds bool) *hostStateMachine {
currentState := host.Status.Provisioning.State
r := hostStateMachine{
Host: host,
NextState: currentState, // Remain in current state by default
Reconciler: reconciler,
Provisioner: provisioner,
haveCreds: haveCreds,
}
return &r
}
type stateHandler func(*reconcileInfo) actionResult
func (hsm *hostStateMachine) handlers() map[metal3api.ProvisioningState]stateHandler {
return map[metal3api.ProvisioningState]stateHandler{
metal3api.StateNone: hsm.handleNone,
metal3api.StateUnmanaged: hsm.handleUnmanaged,
metal3api.StateRegistering: hsm.handleRegistering,
metal3api.StateInspecting: hsm.handleInspecting,
metal3api.StateExternallyProvisioned: hsm.handleExternallyProvisioned,
metal3api.StateMatchProfile: hsm.handleMatchProfile, // Backward compatibility, remove eventually
metal3api.StatePreparing: hsm.handlePreparing,
metal3api.StateAvailable: hsm.handleAvailable,
metal3api.StateReady: hsm.handleAvailable,
metal3api.StateProvisioning: hsm.handleProvisioning,
metal3api.StateProvisioned: hsm.handleProvisioned,
metal3api.StateDeprovisioning: hsm.handleDeprovisioning,
metal3api.StatePoweringOffBeforeDelete: hsm.handlePoweringOffBeforeDelete,
metal3api.StateDeleting: hsm.handleDeleting,
}
}
func recordStateBegin(host *metal3api.BareMetalHost, state metal3api.ProvisioningState, time metav1.Time) {
if nextMetric := host.OperationMetricForState(state); nextMetric != nil {
if nextMetric.Start.IsZero() || !nextMetric.End.IsZero() {
*nextMetric = metal3api.OperationMetric{
Start: time,
}
}
}
}
func recordStateEnd(info *reconcileInfo, host *metal3api.BareMetalHost, state metal3api.ProvisioningState, time metav1.Time) (changed bool) {
if prevMetric := host.OperationMetricForState(state); prevMetric != nil {
if !prevMetric.Start.IsZero() && prevMetric.End.IsZero() {
prevMetric.End = time
info.postSaveCallbacks = append(info.postSaveCallbacks, func() {
observer := stateTime[state].With(hostMetricLabels(info.request))
observer.Observe(prevMetric.Duration().Seconds())
})
changed = true
}
}
return
}
func (hsm *hostStateMachine) ensureCapacity(info *reconcileInfo, state metal3api.ProvisioningState) actionResult {
hasCapacity, err := hsm.Provisioner.HasCapacity()
if err != nil {
return actionError{errors.Wrap(err, "failed to determine current provisioner capacity")}
}
if !hasCapacity {
return recordActionDelayed(info, state)
}
return nil
}
func (hsm *hostStateMachine) updateHostStateFrom(initialState metal3api.ProvisioningState,
info *reconcileInfo) actionResult {
if hsm.NextState != initialState {
// Check if there is a free slot available when trying to
// (de)provision an host - if not the action will be delayed.
// The check is limited to only the (de)provisioning states to
// avoid putting an excessive pressure on the provisioner
switch hsm.NextState {
case metal3api.StateInspecting, metal3api.StateProvisioning,
metal3api.StateDeprovisioning:
if actionRes := hsm.ensureCapacity(info, hsm.NextState); actionRes != nil {
return actionRes
}
}
info.log.Info("changing provisioning state",
"old", initialState,
"new", hsm.NextState)
now := metav1.Now()
recordStateEnd(info, hsm.Host, initialState, now)
recordStateBegin(hsm.Host, hsm.NextState, now)
info.postSaveCallbacks = append(info.postSaveCallbacks, func() {
stateChanges.With(stateChangeMetricLabels(initialState, hsm.NextState)).Inc()
})
hsm.Host.Status.Provisioning.State = hsm.NextState
// Here we assume that if we're being asked to change the
// state, the return value of ReconcileState (our caller) is
// set up to ensure the change in the host is written back to
// the API. That means we can safely update any status fields
// along with the state.
switch hsm.NextState {
case metal3api.StateRegistering,
metal3api.StateInspecting,
metal3api.StateProvisioning:
// TODO: When the user-selectable profile field is
// removed, move saveHostProvisioningSettings() from the
// controller to this point. We can't move it yet because
// it needs error handling logic that we can't support in
// this function.
if updateBootModeStatus(hsm.Host) {
info.log.Info("saving boot mode",
"new mode", hsm.Host.Status.Provisioning.BootMode)
}
}
}
return nil
}
func (hsm *hostStateMachine) checkDelayedHost(info *reconcileInfo) actionResult {
// Check if there's a free slot for hosts that have been previously delayed
if info.host.Status.OperationalStatus == metal3api.OperationalStatusDelayed {
if actionRes := hsm.ensureCapacity(info, info.host.Status.Provisioning.State); actionRes != nil {
return actionRes
}
// A slot is available, let's cleanup the status and retry
clearError(info.host)
return actionUpdate{}
}
// Make sure the check is re-applied when provisioning an
// host not yet tracked by the provisioner
switch info.host.Status.Provisioning.State {
case metal3api.StateInspecting, metal3api.StateProvisioning,
metal3api.StateDeprovisioning:
if actionRes := hsm.ensureCapacity(info, info.host.Status.Provisioning.State); actionRes != nil {
return actionRes
}
}
return nil
}
func (hsm *hostStateMachine) ReconcileState(info *reconcileInfo) (actionRes actionResult) {
initialState := hsm.Host.Status.Provisioning.State
defer func() {
if overrideAction := hsm.updateHostStateFrom(initialState, info); overrideAction != nil {
actionRes = overrideAction
}
}()
if delayedResult := hsm.checkDelayedHost(info); delayedResult != nil {
return delayedResult
}
if hsm.checkInitiateDelete(info.log) {
info.log.Info("Initiating host deletion")
return actionComplete{}
}
if detachedResult := hsm.checkDetachedHost(info); detachedResult != nil {
return detachedResult
}
if registerResult := hsm.ensureRegistered(info); registerResult != nil {
hostRegistrationRequired.Inc()
return registerResult
}
if stateHandler, found := hsm.handlers()[initialState]; found {
return stateHandler(info)
}
info.log.Info("No handler found for state", "state", initialState)
return actionError{fmt.Errorf("No handler found for state \"%s\"", initialState)}
}
func updateBootModeStatus(host *metal3api.BareMetalHost) bool {
// Make sure we have saved the current boot mode value.
bootMode := host.BootMode()
if bootMode == host.Status.Provisioning.BootMode {
return false
}
host.Status.Provisioning.BootMode = bootMode
return true
}
func (hsm *hostStateMachine) checkInitiateDelete(log logr.Logger) bool {
if hsm.Host.DeletionTimestamp.IsZero() {
// Delete not requested
return false
}
switch hsm.NextState {
default:
hsm.NextState = metal3api.StatePoweringOffBeforeDelete
case metal3api.StateProvisioning, metal3api.StateProvisioned:
if hsm.Host.OperationalStatus() == metal3api.OperationalStatusDetached {
if delayDeleteForDetachedHost(hsm.Host) {
log.Info("Delaying detached host deletion")
deleteDelayedForDetached.Inc()
return false
}
// We cannot power off a detached host. Skip to delete.
hsm.NextState = metal3api.StateDeleting
} else {
hsm.NextState = metal3api.StateDeprovisioning
}
case metal3api.StateDeprovisioning:
// Allow state machine to run to continue deprovisioning.
return false
case metal3api.StateDeleting:
// Already in deleting state. Allow state machine to run.
return false
case metal3api.StatePoweringOffBeforeDelete:
// Already in powering off state. Allow state machine to run.
return false
}
return true
}
// hasDetachedAnnotation checks for existence of baremetalhost.metal3.io/detached
func hasDetachedAnnotation(host *metal3api.BareMetalHost) bool {
annotations := host.GetAnnotations()
if annotations != nil {
if _, ok := annotations[metal3api.DetachedAnnotation]; ok {
return true
}
}
return false
}
func delayDeleteForDetachedHost(host *metal3api.BareMetalHost) bool {
annotations := host.GetAnnotations()
args := metal3api.DetachedAnnotationArguments{}
val, present := annotations[metal3api.DetachedAnnotation]
// if the host is detached, but missing the annotation, also delay delete
// to allow for the host to be re-attached
if !present && host.OperationalStatus() == metal3api.OperationalStatusDetached {
return true
}
if present {
if err := json.Unmarshal([]byte(val), &args); err != nil {
// default behavior if these are missing or not json is to not delay
return false
}
}
return args.DeleteAction == metal3api.DetachedDeleteActionDelay
}
func (hsm *hostStateMachine) checkDetachedHost(info *reconcileInfo) (result actionResult) {
// If the detached annotation is set we remove any host from the
// provisioner and take no further action
// Note this doesn't change the current state, only the OperationalStatus
if hasDetachedAnnotation(hsm.Host) {
// Only allow detaching hosts in Provisioned/ExternallyProvisioned/Ready/Available states
switch info.host.Status.Provisioning.State {
case metal3api.StateProvisioned, metal3api.StateExternallyProvisioned, metal3api.StateReady, metal3api.StateAvailable:
return hsm.Reconciler.detachHost(hsm.Provisioner, info)
}
}
if info.host.Status.ErrorType == metal3api.DetachError {
clearError(info.host)
hsm.Host.Status.ErrorCount = 0
info.log.Info("removed detach error")
return actionUpdate{}
}
if info.host.OperationalStatus() == metal3api.OperationalStatusDetached {
newStatus := metal3api.OperationalStatusOK
if info.host.Status.ErrorType != "" {
newStatus = metal3api.OperationalStatusError
}
info.host.SetOperationalStatus(newStatus)
info.log.Info("removed detached status")
return actionUpdate{}
}
return nil
}
func (hsm *hostStateMachine) ensureRegistered(info *reconcileInfo) (result actionResult) {
if !hsm.haveCreds {
// If we are in the process of deletion (which may start with
// deprovisioning) and we have been unable to obtain any credentials,
// don't attempt to re-register the Host as this will always fail.
return
}
switch hsm.NextState {
case metal3api.StateNone, metal3api.StateUnmanaged:
// We haven't yet reached the Registration state, so don't attempt
// to register the Host.
return
case metal3api.StateMatchProfile:
// Backward compatibility, remove eventually
return
case metal3api.StateDeleting, metal3api.StatePoweringOffBeforeDelete:
// In the deleting state the whole idea is to de-register the host
return
case metal3api.StateRegistering:
default:
if hsm.Host.Status.ErrorType == metal3api.RegistrationError ||
!hsm.Host.Status.GoodCredentials.Match(*info.bmcCredsSecret) {
info.log.Info("Retrying registration")
recordStateBegin(hsm.Host, metal3api.StateRegistering, metav1.Now())
}
}
result = hsm.Reconciler.registerHost(hsm.Provisioner, info)
_, complete := result.(actionComplete)
if (result == nil || complete) &&
hsm.NextState != metal3api.StateRegistering {
if recordStateEnd(info, hsm.Host, metal3api.StateRegistering, metav1.Now()) {
result = actionUpdate{}
}
}
if complete {
result = actionUpdate{}
}
return
}
func (hsm *hostStateMachine) handleNone(info *reconcileInfo) actionResult {
// No state is set, so immediately move to either Registering or Unmanaged
if hsm.Host.HasBMCDetails() {
hsm.NextState = metal3api.StateRegistering
} else {
info.publishEvent("Discovered", "Discovered host with no BMC details")
hsm.Host.SetOperationalStatus(metal3api.OperationalStatusDiscovered)
hsm.NextState = metal3api.StateUnmanaged
hostUnmanaged.Inc()
}
return actionComplete{}
}
func (hsm *hostStateMachine) handleUnmanaged(info *reconcileInfo) actionResult {
actResult := hsm.Reconciler.actionUnmanaged(hsm.Provisioner, info)
if _, complete := actResult.(actionComplete); complete {
hsm.NextState = metal3api.StateRegistering
}
return actResult
}
func (hsm *hostStateMachine) handleRegistering(_ *reconcileInfo) actionResult {
// Getting to the state handler at all means we have successfully
// registered using the current BMC credentials, so we can move to the
// next state. We will not return to the Registering state, even
// if the credentials change and the Host must be re-registered.
if hsm.Host.Spec.ExternallyProvisioned {
hsm.NextState = metal3api.StateExternallyProvisioned
} else if inspectionDisabled(hsm.Host) {
hsm.NextState = metal3api.StatePreparing
} else {
hsm.NextState = metal3api.StateInspecting
}
hsm.Host.Status.ErrorCount = 0
return actionComplete{}
}
func (hsm *hostStateMachine) handleInspecting(info *reconcileInfo) actionResult {
actResult := hsm.Reconciler.actionInspecting(hsm.Provisioner, info)
if _, complete := actResult.(actionComplete); complete {
hsm.NextState = metal3api.StatePreparing
hsm.Host.Status.ErrorCount = 0
}
return actResult
}
func (hsm *hostStateMachine) handleMatchProfile(_ *reconcileInfo) actionResult {
// Backward compatibility, remove eventually
hsm.NextState = metal3api.StatePreparing
hsm.Host.Status.ErrorCount = 0
return actionComplete{}
}
func (hsm *hostStateMachine) handleExternallyProvisioned(info *reconcileInfo) actionResult {
if hsm.Host.Spec.ExternallyProvisioned {
// ErrorCount is cleared when appropriate inside actionManageSteadyState
return hsm.Reconciler.actionManageSteadyState(hsm.Provisioner, info)
}
// TODO(dtantsur): move this logic inside NeedsHardwareInspection?
if hsm.Host.NeedsHardwareInspection() && !inspectionDisabled(hsm.Host) {
hsm.NextState = metal3api.StateInspecting
} else {
hsm.NextState = metal3api.StatePreparing
}
return actionComplete{}
}
func (hsm *hostStateMachine) handlePreparing(info *reconcileInfo) actionResult {
actResult := hsm.Reconciler.actionPreparing(hsm.Provisioner, info)
if _, complete := actResult.(actionComplete); complete {
hsm.Host.Status.ErrorCount = 0
hsm.NextState = metal3api.StateAvailable
}
return actResult
}
func (hsm *hostStateMachine) handleAvailable(info *reconcileInfo) actionResult {
if hsm.Host.Spec.ExternallyProvisioned {
hsm.NextState = metal3api.StateExternallyProvisioned
clearHostProvisioningSettings(info.host)
return actionComplete{}
}
if hasInspectAnnotation(hsm.Host) {
hsm.NextState = metal3api.StateInspecting
return actionComplete{}
}
if dirty, _, err := getHostProvisioningSettings(info.host, info); err != nil {
return actionError{err}
} else if dirty {
hsm.NextState = metal3api.StatePreparing
return actionComplete{}
}
// Check if hostFirmwareSettings have changed
if dirty, _, err := hsm.Reconciler.getHostFirmwareSettings(info); err != nil {
return actionError{err}
} else if dirty {
hsm.NextState = metal3api.StatePreparing
return actionComplete{}
}
// ErrorCount is cleared when appropriate inside actionManageAvailable
actResult := hsm.Reconciler.actionManageAvailable(hsm.Provisioner, info)
if _, complete := actResult.(actionComplete); complete {
hsm.NextState = metal3api.StateProvisioning
}
return actResult
}
func (hsm *hostStateMachine) provisioningCancelled() bool {
if hsm.Host.Spec.CustomDeploy != nil && hsm.Host.Spec.CustomDeploy.Method != "" {
if hsm.Host.Status.Provisioning.CustomDeploy != nil && hsm.Host.Status.Provisioning.CustomDeploy.Method != "" &&
hsm.Host.Spec.CustomDeploy.Method != hsm.Host.Status.Provisioning.CustomDeploy.Method {
return true
}
// At this point spec.CustomDeploy value didn't change (and it's not empty). Only a discrepancy in the Image
// could require a deprovisioning, but if it isn't set or empty then is fine
if hsm.Host.Status.Provisioning.Image.URL != "" &&
(hsm.Host.Spec.Image == nil ||
hsm.Host.Spec.Image.URL != hsm.Host.Status.Provisioning.Image.URL) {
return true
}
return false
} else if hsm.Host.Status.Provisioning.CustomDeploy != nil && hsm.Host.Status.Provisioning.CustomDeploy.Method != "" {
return true
}
return hsm.imageProvisioningCancelled()
}
func (hsm *hostStateMachine) imageProvisioningCancelled() bool {
if hsm.Host.Spec.Image == nil {
return true
}
if hsm.Host.Spec.Image.URL == "" {
return true
}
if hsm.Host.Status.Provisioning.Image.URL == "" {
return false
}
if hsm.Host.Spec.Image.URL != hsm.Host.Status.Provisioning.Image.URL {
return true
}
return false
}
func (hsm *hostStateMachine) handleProvisioning(info *reconcileInfo) actionResult {
if hsm.Host.Status.ErrorType != "" || hsm.provisioningCancelled() {
hsm.NextState = metal3api.StateDeprovisioning
return actionComplete{}
}
actResult := hsm.Reconciler.actionProvisioning(hsm.Provisioner, info)
if _, complete := actResult.(actionComplete); complete {
hsm.NextState = metal3api.StateProvisioned
hsm.Host.Status.ErrorCount = 0
}
return actResult
}
func (hsm *hostStateMachine) handleProvisioned(info *reconcileInfo) actionResult {
if hsm.provisioningCancelled() {
hsm.NextState = metal3api.StateDeprovisioning
return actionComplete{}
}
// ErrorCount is cleared when appropriate inside actionManageSteadyState
return hsm.Reconciler.actionManageSteadyState(hsm.Provisioner, info)
}
func (hsm *hostStateMachine) handleDeprovisioning(info *reconcileInfo) actionResult {
actResult := hsm.Reconciler.actionDeprovisioning(hsm.Provisioner, info)
if hsm.Host.DeletionTimestamp.IsZero() {
if _, complete := actResult.(actionComplete); complete {
hsm.NextState = metal3api.StateAvailable
hsm.Host.Status.ErrorCount = 0
}
} else {
switch r := actResult.(type) {
case actionComplete:
hsm.NextState = metal3api.StatePoweringOffBeforeDelete
hsm.Host.Status.ErrorCount = 0
case actionFailed:
// If the provisioner gives up deprovisioning and
// deletion has been requested, continue to delete.
if hsm.Host.Status.ErrorCount > 3 {
info.log.Info("Giving up on host clean up after 3 attempts. The host may still be operational " +
"and cause issues in your clusters. You should clean it up manually now.")
hsm.NextState = metal3api.StatePoweringOffBeforeDelete
info.postSaveCallbacks = append(info.postSaveCallbacks, deleteWithoutDeprov.Inc)
return actionComplete{}
}
case actionError:
if r.NeedsRegistration() && !hsm.haveCreds {
// If the host is not registered as a node in Ironic and we
// lack the credentials to deprovision it, just continue to
// delete.
hsm.NextState = metal3api.StateDeleting
info.postSaveCallbacks = append(info.postSaveCallbacks, deleteWithoutPowerOff.Inc)
return actionComplete{}
}
}
}
return actResult
}
func (hsm *hostStateMachine) handlePoweringOffBeforeDelete(info *reconcileInfo) actionResult {
actResult := hsm.Reconciler.actionPowerOffBeforeDeleting(hsm.Provisioner, info)
skipToDelete := func() actionResult {
hsm.NextState = metal3api.StateDeleting
info.postSaveCallbacks = append(info.postSaveCallbacks, deleteWithoutPowerOff.Inc)
return actionComplete{}
}
switch r := actResult.(type) {
case actionComplete:
hsm.NextState = metal3api.StateDeleting
hsm.Host.Status.ErrorCount = 0
hsm.Host.Status.PoweredOn = false
case actionFailed:
// If the provisioner gives up deprovisioning and
// deletion has been requested, continue to delete.
if hsm.Host.Status.ErrorCount > 3 {
info.log.Info("Giving up on host power off after 3 attempts.")
return skipToDelete()
}
case actionError:
if r.NeedsRegistration() && !hsm.haveCreds {
// If the host is not registered as a node in Ironic and we
// lack the credentials to power it off, just continue to
// delete.
return skipToDelete()
}
}
return actResult
}
func (hsm *hostStateMachine) handleDeleting(info *reconcileInfo) actionResult {
return hsm.Reconciler.actionDeleting(hsm.Provisioner, info)
}