-
Notifications
You must be signed in to change notification settings - Fork 217
/
statemachine.go
647 lines (602 loc) · 27.3 KB
/
statemachine.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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
package cluster
import (
"errors"
"fmt"
"github.com/filanov/stateswitch"
"github.com/openshift/assisted-service/models"
)
const (
TransitionTypeCancelInstallation = "CancelInstallation"
TransitionTypeResetCluster = "ResetCluster"
TransitionTypePrepareForInstallation = "PrepareForInstallation"
TransitionTypeRefreshStatus = "RefreshStatus"
)
func SequentialPostTransitions(postTransitions ...stateswitch.PostTransition) stateswitch.PostTransition {
return func(stateSwitch stateswitch.StateSwitch, args stateswitch.TransitionArgs) error {
var ret error
for _, postTransition := range postTransitions {
err := postTransition(stateSwitch, args) // Run everything anyway ?
if err != nil {
ret = errors.Join(ret, err)
}
}
return ret
}
}
func NewClusterStateMachine(th TransitionHandler) stateswitch.StateMachine {
sm := stateswitch.NewStateMachine()
documentStates(sm)
documentTransitionTypes(sm)
sm.AddTransitionRule(stateswitch.TransitionRule{
TransitionType: TransitionTypeCancelInstallation,
SourceStates: []stateswitch.State{
stateswitch.State(models.ClusterStatusInstalling),
stateswitch.State(models.ClusterStatusInstallingPendingUserAction),
stateswitch.State(models.ClusterStatusError),
stateswitch.State(models.ClusterStatusFinalizing),
},
DestinationState: stateswitch.State(models.ClusterStatusCancelled),
PostTransition: th.PostCancelInstallation,
Documentation: stateswitch.TransitionRuleDoc{
Name: "Cancel installation of installing cluster",
Description: "Move cluster to the cancelled state when user cancels installation",
},
})
sm.AddTransitionRule(stateswitch.TransitionRule{
TransitionType: TransitionTypeCancelInstallation,
SourceStates: []stateswitch.State{
stateswitch.State(models.ClusterStatusPreparingForInstallation),
},
DestinationState: stateswitch.State(models.ClusterStatusReady),
PostTransition: th.PostCancelInstallation,
Documentation: stateswitch.TransitionRuleDoc{
Name: "Cancel installation of preparing cluster",
Description: "Cancelling a cluster during preparation simply cancels the preparation and moves it back to the ready, rather than putting the cluster in the cancelled state",
},
})
sm.AddTransitionRule(stateswitch.TransitionRule{
TransitionType: TransitionTypeResetCluster,
SourceStates: []stateswitch.State{
stateswitch.State(models.ClusterStatusPreparingForInstallation),
stateswitch.State(models.ClusterStatusInstalling),
stateswitch.State(models.ClusterStatusInstallingPendingUserAction),
stateswitch.State(models.ClusterStatusError),
stateswitch.State(models.ClusterStatusCancelled),
stateswitch.State(models.ClusterStatusFinalizing),
},
DestinationState: stateswitch.State(models.ClusterStatusInsufficient),
PostTransition: th.PostResetCluster,
Documentation: stateswitch.TransitionRuleDoc{
Name: "Reset installation",
Description: "Reset the cluster, allowing it to be installed again",
},
})
sm.AddTransitionRule(stateswitch.TransitionRule{
TransitionType: TransitionTypePrepareForInstallation,
SourceStates: []stateswitch.State{
stateswitch.State(models.ClusterStatusReady),
},
DestinationState: stateswitch.State(models.ClusterStatusPreparingForInstallation),
PostTransition: th.PostPrepareForInstallation,
Documentation: stateswitch.TransitionRuleDoc{
Name: "Start installation",
Description: "Begins preparing the cluster for installation",
},
})
var pendingConditions = stateswitch.And(
If(IsMachineCidrDefined),
If(isClusterCidrDefined),
If(isServiceCidrDefined),
If(IsDNSDomainDefined),
If(IsPullSecretSet),
)
var vipsDefinedConditions = stateswitch.And(
If(AreIngressVipsDefined),
If(AreApiVipsDefined),
)
var requiredForInstall = stateswitch.And(
If(IsMachineCidrEqualsToCalculatedCidr),
If(AreApiVipsValid),
If(AreIngressVipsValid),
If(AllHostsAreReadyToInstall),
If(SufficientMastersCount),
If(networkPrefixValid),
If(noCidrOverlapping),
If(IsNtpServerConfigured),
If(IsOdfRequirementsSatisfied),
If(IsLsoRequirementsSatisfied),
If(IsCnvRequirementsSatisfied),
If(IsLvmRequirementsSatisfied),
If(IsMceRequirementsSatisfied),
If(IsMtvRequirementsSatisfied),
If(IsOscRequirementsSatisfied),
If(isNetworkTypeValid),
If(NetworksSameAddressFamilies),
If(IsNodeFeatureDiscoveryRequirementsSatisfied),
If(IsNvidiaGPURequirementsSatisfied),
If(IsPipelinesRequirementsSatisfied),
If(IsServiceMeshRequirementsSatisfied),
If(IsServerLessRequirementsSatisfied),
If(IsOpenShiftAIRequirementsSatisfied),
If(IsAuthorinoRequirementsSatisfied),
)
// Refresh cluster status conditions - Non DHCP
var requiredInputFieldsExistNonDhcp = stateswitch.And(vipsDefinedConditions, pendingConditions)
// Refresh cluster status conditions - DHCP
var isSufficientForInstallDhcp = stateswitch.And(requiredForInstall, vipsDefinedConditions)
var allRefreshStatusConditions = stateswitch.And(pendingConditions, vipsDefinedConditions, requiredForInstall)
// Non DHCP transitions
sm.AddTransitionRule(stateswitch.TransitionRule{
TransitionType: TransitionTypeRefreshStatus,
SourceStates: []stateswitch.State{
stateswitch.State(models.ClusterStatusPendingForInput),
stateswitch.State(models.ClusterStatusReady),
stateswitch.State(models.ClusterStatusInsufficient),
},
Condition: stateswitch.And(stateswitch.Not(If(VipDhcpAllocationSet)), stateswitch.Not(requiredInputFieldsExistNonDhcp)),
DestinationState: stateswitch.State(models.ClusterStatusPendingForInput),
PostTransition: th.PostRefreshCluster(statusInfoPendingForInput),
Documentation: stateswitch.TransitionRuleDoc{
Name: "Refresh discovering cluster - detect required input",
Description: "In order for this transition to be fired at least one of the validations in requiredInputFieldsExistNonDhcp must fail. This transition handles the case that there is missing input that has to be provided from a user or other external means",
},
})
sm.AddTransitionRule(stateswitch.TransitionRule{
TransitionType: TransitionTypeRefreshStatus,
SourceStates: []stateswitch.State{
stateswitch.State(models.ClusterStatusPendingForInput),
stateswitch.State(models.ClusterStatusReady),
stateswitch.State(models.ClusterStatusInsufficient),
},
Condition: stateswitch.And(
stateswitch.Not(If(VipDhcpAllocationSet)),
requiredInputFieldsExistNonDhcp,
stateswitch.Not(requiredForInstall),
),
DestinationState: stateswitch.State(models.ClusterStatusInsufficient),
PostTransition: th.PostRefreshCluster(StatusInfoInsufficient),
Documentation: stateswitch.TransitionRuleDoc{
Name: "Refresh discovering cluster - detect insufficient",
Description: "In order for this transition to be fired at least one of the validations in isSufficientForInstallNonDhcp must fail. This transition handles the case that one of the required validations that are required in order for the cluster to be in ready state has failed",
},
})
// DHCP transitions
sm.AddTransitionRule(stateswitch.TransitionRule{
TransitionType: TransitionTypeRefreshStatus,
SourceStates: []stateswitch.State{
stateswitch.State(models.ClusterStatusPendingForInput),
stateswitch.State(models.ClusterStatusReady),
stateswitch.State(models.ClusterStatusInsufficient),
},
Condition: stateswitch.And(If(VipDhcpAllocationSet), stateswitch.Not(pendingConditions)),
DestinationState: stateswitch.State(models.ClusterStatusPendingForInput),
PostTransition: th.PostRefreshCluster(statusInfoPendingForInput),
Documentation: stateswitch.TransitionRuleDoc{
Name: "TODO: Name this transition",
Description: "In order for this transition to be fired at least one of the validation IsMachineCidrDefined must fail. This transition handles the case that there is missing input that has to be provided from a user or other external means",
},
})
sm.AddTransitionRule(stateswitch.TransitionRule{
TransitionType: TransitionTypeRefreshStatus,
SourceStates: []stateswitch.State{
stateswitch.State(models.ClusterStatusPendingForInput),
stateswitch.State(models.ClusterStatusReady),
stateswitch.State(models.ClusterStatusInsufficient),
},
Condition: stateswitch.And(If(VipDhcpAllocationSet), pendingConditions, stateswitch.Not(isSufficientForInstallDhcp)),
DestinationState: stateswitch.State(models.ClusterStatusInsufficient),
PostTransition: th.PostRefreshCluster(StatusInfoInsufficient),
Documentation: stateswitch.TransitionRuleDoc{
Name: "TODO: Name this transition",
Description: "In order for this transition to be fired at least one of the validations in isSufficientForInstallDhcp must fail. This transition handles the case that one of the required validations that are required in order for the host to be in known state (ready for installation) has failed",
},
})
sm.AddTransitionRule(stateswitch.TransitionRule{
TransitionType: TransitionTypeRefreshStatus,
SourceStates: []stateswitch.State{
stateswitch.State(models.ClusterStatusPendingForInput),
stateswitch.State(models.ClusterStatusReady),
stateswitch.State(models.ClusterStatusInsufficient),
},
Condition: allRefreshStatusConditions,
DestinationState: stateswitch.State(models.ClusterStatusReady),
PostTransition: th.PostRefreshCluster(StatusInfoReady),
Documentation: stateswitch.TransitionRuleDoc{
Name: "Refresh discovering cluster - detect ready",
Description: "This transition is fired when all validations pass",
},
})
sm.AddTransitionRule(stateswitch.TransitionRule{
TransitionType: TransitionTypeRefreshStatus,
SourceStates: []stateswitch.State{stateswitch.State(models.ClusterStatusPreparingForInstallation)},
Condition: stateswitch.And(th.IsPreparingTimedOut, stateswitch.Not(If(FailedPreparingtHostsExist))),
DestinationState: stateswitch.State(models.ClusterStatusReady),
PostTransition: th.PostPreparingTimedOut,
Documentation: stateswitch.TransitionRuleDoc{
Name: "Refresh preparing cluster - detect timeout",
Description: "This transition is fired when the preparing installation reach the timeout",
},
})
sm.AddTransitionRule(stateswitch.TransitionRule{
TransitionType: TransitionTypeRefreshStatus,
SourceStates: []stateswitch.State{stateswitch.State(models.ClusterStatusPreparingForInstallation)},
Condition: stateswitch.And(If(AllHostsPreparedSuccessfully), If(ClusterPreparationSucceeded)),
DestinationState: stateswitch.State(models.ClusterStatusInstalling),
Transition: th.InstallCluster,
PostTransition: th.PostRefreshCluster(statusInfoInstalling),
Documentation: stateswitch.TransitionRuleDoc{
Name: "Refresh preparing cluster - done preparing",
Description: "This transition is fired when cluster installation preperation is complete and all hosts within the cluster have also finished preparing",
},
})
sm.AddTransitionRule(stateswitch.TransitionRule{
TransitionType: TransitionTypeRefreshStatus,
SourceStates: []stateswitch.State{stateswitch.State(models.ClusterStatusPreparingForInstallation)},
Condition: If(UnPreparingtHostsExist),
DestinationState: stateswitch.State(models.ClusterStatusInsufficient),
PostTransition: th.PostRefreshCluster(statusInfoUnpreparingHostExists),
Documentation: stateswitch.TransitionRuleDoc{
Name: "Refresh preparing cluster - insufficient",
Description: "TODO: Document this transition",
},
})
sm.AddTransitionRule(stateswitch.TransitionRule{
TransitionType: TransitionTypeRefreshStatus,
SourceStates: []stateswitch.State{stateswitch.State(models.ClusterStatusPreparingForInstallation)},
Condition: stateswitch.Or(
If(FailedPreparingtHostsExist),
stateswitch.And(
stateswitch.Not(If(UnPreparingtHostsExist)),
If(ClusterPreparationFailed),
),
),
DestinationState: stateswitch.State(models.ClusterStatusReady),
PostTransition: th.PostRefreshCluster(statusInfoClusterFailedToPrepare),
Documentation: stateswitch.TransitionRuleDoc{
Name: "Refresh preparing cluster - failed",
Description: "TODO: Document this transition",
},
})
sm.AddTransitionRule(stateswitch.TransitionRule{
TransitionType: TransitionTypeRefreshStatus,
SourceStates: []stateswitch.State{
stateswitch.State(models.ClusterStatusInstallingPendingUserAction),
},
Condition: stateswitch.Not(
stateswitch.Or(
th.IsInstalling,
th.IsFinalizing,
),
),
DestinationState: stateswitch.State(models.ClusterStatusError),
PostTransition: SequentialPostTransitions(th.PostRefreshCluster(statusInfoError), th.SendClusterInstallationFailedEvent(statusInfoError)),
Documentation: stateswitch.TransitionRuleDoc{
Name: "TODO: Name this transition",
Description: "TODO: Document this transition",
},
})
sm.AddTransitionRule(stateswitch.TransitionRule{
TransitionType: TransitionTypeRefreshStatus,
SourceStates: []stateswitch.State{
stateswitch.State(models.ClusterStatusInstallingPendingUserAction),
},
Condition: th.IsInstallationTimedOut,
DestinationState: stateswitch.State(models.ClusterStatusError),
PostTransition: SequentialPostTransitions(th.PostRefreshCluster(statusInfoTimeout), th.SendClusterInstallationFailedEvent(statusInfoTimeout)),
Documentation: stateswitch.TransitionRuleDoc{
Name: "Timed out while waiting for user",
Description: "User was asked to take action and did not do so in time, give up and display appropriate error",
},
})
sm.AddTransitionRule(stateswitch.TransitionRule{
TransitionType: TransitionTypeRefreshStatus,
SourceStates: []stateswitch.State{
stateswitch.State(models.ClusterStatusInstalling),
stateswitch.State(models.ClusterStatusFinalizing),
},
Condition: stateswitch.And(th.IsInstallationTimedOut, th.SoftTimeoutsEnabled),
DestinationState: stateswitch.State(models.ClusterStatusError),
PostTransition: SequentialPostTransitions(
th.PostRefreshCluster(statusInfoInstallationTimeout, th.InstallationTimeoutMinutes),
th.SendClusterInstallationFailedEvent(statusInfoInstallationTimeout, th.InstallationTimeoutMinutes),
),
Documentation: stateswitch.TransitionRuleDoc{
Name: "Timed out while installing",
Description: "Cluster installation is taking too long, give up and display appropriate error",
},
})
sm.AddTransitionRule(stateswitch.TransitionRule{
TransitionType: TransitionTypeRefreshStatus,
SourceStates: []stateswitch.State{
stateswitch.State(models.ClusterStatusFinalizing),
},
Condition: stateswitch.And(th.IsFinalizingTimedOut,
stateswitch.Not(th.SoftTimeoutsEnabled)),
DestinationState: stateswitch.State(models.ClusterStatusError),
PostTransition: SequentialPostTransitions(th.PostRefreshCluster(statusInfoFinalizingTimeout), th.SendClusterInstallationFailedEvent(statusInfoFinalizingTimeout)),
Documentation: stateswitch.TransitionRuleDoc{
Name: "Timed out while finalizing",
Description: "Cluster finalization took too long, display appropriate error",
},
})
sm.AddTransitionRule(stateswitch.TransitionRule{
TransitionType: TransitionTypeRefreshStatus,
SourceStates: []stateswitch.State{
stateswitch.State(models.ClusterStatusFinalizing),
},
Condition: stateswitch.And(stateswitch.Not(th.SoftTimeoutsEnabled),
th.IsFinalizingStageTimedOut,
stateswitch.Not(isInFinalizingStages(nonFailingFinalizingStages...))),
DestinationState: stateswitch.State(models.ClusterStatusError),
PostTransition: SequentialPostTransitions(
th.PostRefreshCluster(statusInfoFinalizingStageTimeout, FinalizingStage, th.FinalizingStageTimeoutMinutes),
th.SendClusterInstallationFailedEvent(statusInfoFinalizingStageTimeout, FinalizingStage, th.FinalizingStageTimeoutMinutes),
),
Documentation: stateswitch.TransitionRuleDoc{
Name: "Finalizing stage timed out. Move to error",
Description: "Cluster finalization stage took too long, display appropriate error",
},
})
sm.AddTransitionRule(stateswitch.TransitionRule{
TransitionType: TransitionTypeRefreshStatus,
SourceStates: []stateswitch.State{
stateswitch.State(models.ClusterStatusFinalizing),
},
Condition: stateswitch.And(
stateswitch.Not(finalizingStageTimeoutTriggered),
stateswitch.Or(th.SoftTimeoutsEnabled,
isInFinalizingStages(nonFailingFinalizingStages...)),
th.IsFinalizingStageTimedOut),
DestinationState: stateswitch.State(models.ClusterStatusFinalizing),
PostTransition: th.PostRefreshFinalizingStageSoftTimedOut,
Documentation: stateswitch.TransitionRuleDoc{
Name: "Finalizing stage is taking too long. Emit an appropriate event",
Description: "Cluster finalization stage is taking too long, emit a warning event and continue installation",
},
})
sm.AddTransitionRule(stateswitch.TransitionRule{
TransitionType: TransitionTypeRefreshStatus,
SourceStates: []stateswitch.State{
stateswitch.State(models.ClusterStatusInstallingPendingUserAction),
},
Condition: stateswitch.And(
th.IsInstallingPendingUserAction,
stateswitch.Or(
th.IsInstalling,
th.IsFinalizing)),
DestinationState: stateswitch.State(models.ClusterStatusInstallingPendingUserAction),
Documentation: stateswitch.TransitionRuleDoc{
Name: "TODO: Name this transition",
Description: "TODO: Document this transition",
},
})
sm.AddTransitionRule(stateswitch.TransitionRule{
TransitionType: TransitionTypeRefreshStatus,
SourceStates: []stateswitch.State{
stateswitch.State(models.ClusterStatusInstallingPendingUserAction),
},
Condition: stateswitch.And(
stateswitch.Not(th.IsInstallingPendingUserAction),
th.IsInstalling,
stateswitch.Not(th.IsFinalizing),
),
DestinationState: stateswitch.State(models.ClusterStatusInstalling),
PostTransition: th.PostRefreshCluster(statusInfoInstalling),
Documentation: stateswitch.TransitionRuleDoc{
Name: "TODO: Name this transition",
Description: "TODO: Document this transition",
},
})
sm.AddTransitionRule(stateswitch.TransitionRule{
TransitionType: TransitionTypeRefreshStatus,
SourceStates: []stateswitch.State{
stateswitch.State(models.ClusterStatusInstallingPendingUserAction),
},
Condition: stateswitch.And(
stateswitch.Not(th.IsInstallingPendingUserAction),
th.IsFinalizing,
),
DestinationState: stateswitch.State(models.ClusterStatusFinalizing),
PostTransition: th.PostRefreshCluster(statusInfoFinalizing),
Documentation: stateswitch.TransitionRuleDoc{
Name: "TODO: Name this transition",
Description: "TODO: Document this transition",
},
})
sm.AddTransitionRule(stateswitch.TransitionRule{
TransitionType: TransitionTypeRefreshStatus,
SourceStates: []stateswitch.State{
stateswitch.State(models.ClusterStatusInstalling),
stateswitch.State(models.ClusterStatusFinalizing),
},
Condition: stateswitch.And(
stateswitch.Or(
th.IsInstalling,
th.IsFinalizing,
),
th.IsInstallingPendingUserAction,
),
DestinationState: stateswitch.State(models.ClusterStatusInstallingPendingUserAction),
PostTransition: th.PostRefreshCluster(statusInfoInstallingPendingUserAction),
Documentation: stateswitch.TransitionRuleDoc{
Name: "TODO: Name this transition",
Description: "TODO: Document this transition",
},
})
sm.AddTransitionRule(stateswitch.TransitionRule{
TransitionType: TransitionTypeRefreshStatus,
SourceStates: []stateswitch.State{
stateswitch.State(models.ClusterStatusInstalling),
},
Condition: stateswitch.And(
th.IsFinalizing,
stateswitch.Not(th.IsInstallingPendingUserAction),
),
DestinationState: stateswitch.State(models.ClusterStatusFinalizing),
PostTransition: th.PostRefreshCluster(statusInfoFinalizing),
Documentation: stateswitch.TransitionRuleDoc{
Name: "Move to finalizing",
Description: "This transition is fired when the cluster is in installing and should move to finalizing",
},
})
sm.AddTransitionRule(stateswitch.TransitionRule{
TransitionType: TransitionTypeRefreshStatus,
SourceStates: []stateswitch.State{
stateswitch.State(models.ClusterStatusInstalling),
},
Condition: stateswitch.And(
stateswitch.Not(th.IsFinalizing),
stateswitch.Not(th.IsInstallingPendingUserAction),
th.IsInstalling,
),
DestinationState: stateswitch.State(models.ClusterStatusInstalling),
PostTransition: th.PostRefreshCluster(statusInfoInstalling),
Documentation: stateswitch.TransitionRuleDoc{
Name: "Stay in installing",
Description: "Installing cluster should stay in installing",
},
})
sm.AddTransitionRule(stateswitch.TransitionRule{
TransitionType: TransitionTypeRefreshStatus,
SourceStates: []stateswitch.State{
stateswitch.State(models.ClusterStatusFinalizing),
},
DestinationState: stateswitch.State(models.ClusterStatusFinalizing),
Condition: th.WithAMSSubscriptions,
PostTransition: th.PostUpdateFinalizingAMSConsoleUrl,
Documentation: stateswitch.TransitionRuleDoc{
Name: "Update AMS subscription",
Description: "Update AMS subscription with console URL",
},
})
sm.AddTransitionRule(stateswitch.TransitionRule{
TransitionType: TransitionTypeRefreshStatus,
Condition: stateswitch.And(
th.areAllHostsDone,
th.hasClusterCompleteInstallation,
),
SourceStates: []stateswitch.State{stateswitch.State(models.ClusterStatusFinalizing)},
DestinationState: stateswitch.State(models.ClusterStatusInstalled),
PostTransition: th.PostCompleteInstallation,
Documentation: stateswitch.TransitionRuleDoc{
Name: "Finalizing complete",
Description: "The cluster has completed finalizing",
},
})
sm.AddTransitionRule(stateswitch.TransitionRule{
TransitionType: TransitionTypeRefreshStatus,
SourceStates: []stateswitch.State{
stateswitch.State(models.ClusterStatusInstalling),
},
Condition: stateswitch.And(
stateswitch.Not(th.IsFinalizing),
stateswitch.Not(th.IsInstalling),
),
DestinationState: stateswitch.State(models.ClusterStatusError),
PostTransition: SequentialPostTransitions(th.PostRefreshCluster(statusInfoError), th.SendClusterInstallationFailedEvent(statusInfoError)),
Documentation: stateswitch.TransitionRuleDoc{
Name: "Installation error",
Description: "This transition is fired when the cluster is in installing and should move to error",
},
})
for _, state := range []stateswitch.State{
stateswitch.State(models.ClusterStatusError),
stateswitch.State(models.ClusterStatusCancelled),
} {
sm.AddTransitionRule(stateswitch.TransitionRule{
TransitionType: TransitionTypeRefreshStatus,
SourceStates: []stateswitch.State{state},
DestinationState: state,
Condition: th.IsLogCollectionTimedOut,
PostTransition: th.PostRefreshLogsProgress(string(models.LogsStateTimeout)),
Documentation: stateswitch.TransitionRuleDoc{
Name: fmt.Sprintf("Log collection timeout during %s", state),
Description: fmt.Sprintf("Stay in %s state and update logs progress to timeout", state),
},
})
}
// Noop transitions
for _, state := range []stateswitch.State{
stateswitch.State(models.ClusterStatusPreparingForInstallation),
stateswitch.State(models.ClusterStatusFinalizing),
stateswitch.State(models.ClusterStatusInstalled),
stateswitch.State(models.ClusterStatusError),
stateswitch.State(models.ClusterStatusCancelled),
stateswitch.State(models.ClusterStatusAddingHosts),
} {
sm.AddTransitionRule(stateswitch.TransitionRule{
TransitionType: TransitionTypeRefreshStatus,
SourceStates: []stateswitch.State{state},
DestinationState: state,
Documentation: stateswitch.TransitionRuleDoc{
Name: fmt.Sprintf("Maintain %s state", state),
Description: fmt.Sprintf("Stay in %s state", state),
},
})
}
return sm
}
func documentStates(sm stateswitch.StateMachine) {
sm.DescribeState(stateswitch.State(models.ClusterStatusInsufficient), stateswitch.StateDoc{
Name: "Insufficient",
Description: "This is the initial state for regular, non-imported clusters",
})
sm.DescribeState(stateswitch.State(models.ClusterStatusReady), stateswitch.StateDoc{
Name: "Ready",
Description: "The cluster is ready to begin installation",
})
sm.DescribeState(stateswitch.State(models.ClusterStatusError), stateswitch.StateDoc{
Name: "Error",
Description: "The cluster has encountered an error during installation and cannot proceed. Usually due to a timeout",
})
sm.DescribeState(stateswitch.State(models.ClusterStatusPreparingForInstallation), stateswitch.StateDoc{
Name: "Preparing For Installation",
Description: "A transient state between Ready and Installing, cluster hosts are performing pre-installation validations",
})
sm.DescribeState(stateswitch.State(models.ClusterStatusPendingForInput), stateswitch.StateDoc{
Name: "Pending For Input",
Description: "The cluster is not ready for installation because it needs more information from the user",
})
sm.DescribeState(stateswitch.State(models.ClusterStatusInstalling), stateswitch.StateDoc{
Name: "Installing",
Description: "The cluster installation is in progress",
})
sm.DescribeState(stateswitch.State(models.ClusterStatusFinalizing), stateswitch.StateDoc{
Name: "Finalizing",
Description: "The cluster has sufficient ready control-plane and worker nodes, but OCP is still finalizing the installation",
})
sm.DescribeState(stateswitch.State(models.ClusterStatusInstalled), stateswitch.StateDoc{
Name: "Installed",
Description: "The cluster installation is considered complete, all operators are healthy and the cluster is ready to use",
})
sm.DescribeState(stateswitch.State(models.ClusterStatusAddingHosts), stateswitch.StateDoc{
Name: "AddingHosts",
Description: "The cluster is fully installed and is ready to accept new hosts. Installed clusters usually transition to this state automatically when installation is complete, depending on the configuration of the service. This is the initial state for imported clusters, as they are already installed",
})
sm.DescribeState(stateswitch.State(models.ClusterStatusCancelled), stateswitch.StateDoc{
Name: "Cancelled",
Description: "The cluster installation was cancelled by the user. Cluster must be reset to be able to install again",
})
sm.DescribeState(stateswitch.State(models.ClusterStatusInstallingPendingUserAction), stateswitch.StateDoc{
Name: "Installing, Pending User Action",
Description: "Installation is in progress, but is blocked and cannot continue until the user takes action",
})
}
func documentTransitionTypes(sm stateswitch.StateMachine) {
sm.DescribeTransitionType(TransitionTypeCancelInstallation, stateswitch.TransitionTypeDoc{
Name: "Cancel Installation",
Description: "Triggered when the user cancels the installation",
})
sm.DescribeTransitionType(TransitionTypeResetCluster, stateswitch.TransitionTypeDoc{
Name: "Reset Cluster",
Description: "Triggered when the user resets the cluster",
})
sm.DescribeTransitionType(TransitionTypePrepareForInstallation, stateswitch.TransitionTypeDoc{
Name: "PrepareForInstallation",
Description: "Triggered when the user starts the installation",
})
sm.DescribeTransitionType(TransitionTypeRefreshStatus, stateswitch.TransitionTypeDoc{
Name: "RefreshStatus",
Description: "Triggered on some clusters periodically by the background cluster monitor goroutine that runs on the leader instance of the Assisted Service. Responsible for driving transitions between states that require re-evaluation of all the validation results and potential timeout conditions",
})
}