generated from giantswarm/template-operator
/
create.go
93 lines (79 loc) · 3.13 KB
/
create.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
package etcdbackup
import (
"context"
"fmt"
"github.com/giantswarm/microerror"
"github.com/giantswarm/operatorkit/v7/pkg/controller/context/reconciliationcanceledcontext"
"github.com/giantswarm/etcd-backup-operator/v3/service/controller/key"
"github.com/giantswarm/etcd-backup-operator/v3/service/controller/resource/etcdbackup/internal/state"
)
const (
// Global States.
backupStateEmpty = ""
backupStatePending = "Pending"
backupStateRunningV2BackupRunning = "V2BackupRunning"
backupStateRunningV2BackupCompleted = "V2BackupCompleted"
backupStateRunningV3BackupRunning = "V3BackupRunning"
backupStateRunningV3BackupCompleted = "V3BackupCompleted"
backupStateCompleted = "Completed"
backupStateFailed = "Failed"
// Instance States.
instanceBackupStatePending = "Pending"
instanceBackupStateCompleted = "Completed"
instanceBackupStateFailed = "Failed"
instanceBackupStateRunning = "Running"
instanceBackupStateSkipped = "Skipped"
// Various settings.
maxBackupAttempts = int8(3)
// Default values.
crKeepTimeoutSeconds = 7 * 24 * 60 * 60
)
// configureStateMachine configures and returns state machine that is driven by
// EnsureCreated.
func (r *Resource) configureStateMachine() {
sm := state.Machine{
backupStateEmpty: r.backupEmptyTransition,
backupStatePending: r.backupPendingTransition,
backupStateRunningV2BackupRunning: r.backupRunningV2BackupRunningTransition,
backupStateRunningV2BackupCompleted: r.backupRunningV2BackupCompletedTransition,
backupStateRunningV3BackupRunning: r.backupRunningV3BackupRunningTransition,
backupStateRunningV3BackupCompleted: r.backupRunningV3BackupCompletedTransition,
backupStateCompleted: r.backupCompletedTransition,
backupStateFailed: r.backupFailedTransition,
}
r.stateMachine = sm
}
func (r *Resource) EnsureCreated(ctx context.Context, obj interface{}) error {
customObject, err := key.ToCustomObject(obj)
if err != nil {
return microerror.Mask(err)
}
var newState state.State
var currentState state.State
{
s, err := r.getGlobalStatus(customObject)
if err != nil {
return microerror.Mask(err)
}
currentState = state.State(s)
r.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("current state: %s", currentState))
newState, err = r.stateMachine.Execute(ctx, obj, currentState)
if err != nil {
return microerror.Mask(err)
}
}
if newState != currentState {
r.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("new state: %s", newState))
r.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("setting resource status to '%s'", newState))
err = r.setGlobalStatus(ctx, customObject, string(newState))
if err != nil {
return microerror.Mask(err)
}
r.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("set resource status to '%s'", newState))
r.logger.LogCtx(ctx, "level", "debug", "message", "canceling reconciliation")
reconciliationcanceledcontext.SetCanceled(ctx)
} else {
r.logger.LogCtx(ctx, "level", "debug", "message", "no state change")
}
return nil
}