This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
/
create.go
69 lines (59 loc) · 2.05 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
package masters
import (
"context"
"github.com/giantswarm/microerror"
"github.com/giantswarm/azure-operator/v6/pkg/handler/nodes/state"
"github.com/giantswarm/azure-operator/v6/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,
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(ctx, cr, Stage)
if err != nil {
return microerror.Mask(err)
}
currentState = state.State(s)
r.Logger.Debugf(ctx, "current state: %s", currentState)
newState, err = r.StateMachine.Execute(ctx, obj, currentState)
if err != nil {
return microerror.Mask(err)
}
}
if newState != currentState {
r.Logger.Debugf(ctx, "new state: %s", newState)
r.Logger.Debugf(ctx, "setting resource status to '%s/%s'", Stage, newState)
err = r.SetResourceStatus(ctx, cr, Stage, string(newState))
if err != nil {
return microerror.Mask(err)
}
r.Logger.Debugf(ctx, "set resource status to '%s/%s'", Stage, newState)
r.Logger.Debugf(ctx, "canceling reconciliation")
} else {
r.Logger.Debugf(ctx, "no state change")
}
return nil
}