This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
create.go
71 lines (61 loc) · 2.36 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
package masters
import (
"context"
"fmt"
"github.com/giantswarm/microerror"
"github.com/giantswarm/azure-operator/v4/service/controller/internal/state"
"github.com/giantswarm/azure-operator/v4/service/controller/key"
)
// createStateMachine configures and returns state machine that is driven by
// EnsureCreated.
func (r *Resource) createStateMachine() state.Machine {
sm := state.Machine{
Logger: r.Logger,
ResourceName: Name,
Transitions: state.TransitionMap{
Empty: r.emptyStateTransition,
DeploymentUninitialized: r.deploymentUninitializedTransition,
DeploymentInitialized: r.deploymentInitializedTransition,
ProvisioningSuccessful: r.provisioningSuccessfulTransition,
ClusterUpgradeRequirementCheck: r.clusterUpgradeRequirementCheckTransition,
MasterInstancesUpgrading: r.masterInstancesUpgradingTransition,
WaitForMastersToBecomeReady: r.waitForMastersToBecomeReadyTransition,
DeploymentCompleted: r.deploymentCompletedTransition,
},
}
return sm
}
// This resource applies the ARM template for the master instances, monitors the process and handles upgrades.
func (r *Resource) EnsureCreated(ctx context.Context, obj interface{}) error {
cr, err := key.ToCustomResource(obj)
if err != nil {
return microerror.Mask(err)
}
var newState state.State
var currentState state.State
{
s, err := r.GetResourceStatus(cr, Stage)
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/%s'", Stage, newState))
err = r.SetResourceStatus(cr, Stage, string(newState))
if err != nil {
return microerror.Mask(err)
}
r.Logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("set resource status to '%s/%s'", Stage, newState))
r.Logger.LogCtx(ctx, "level", "debug", "message", "canceling reconciliation")
} else {
r.Logger.LogCtx(ctx, "level", "debug", "message", "no state change")
}
return nil
}