/
app_deploy_plan_reconciliation.go
128 lines (108 loc) · 3.25 KB
/
app_deploy_plan_reconciliation.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
package bosun
import (
"github.com/naveego/bosun/pkg/actions"
"github.com/naveego/bosun/pkg/workspace"
)
type Plan []PlanStep
type PlanStep struct {
Name string
Description string
Action func(ctx BosunContext) error
}
func (a *AppDeploy) PlanReconciliation(ctx BosunContext) (Plan, error) {
ctx = ctx.WithAppDeploy(a)
var steps []PlanStep
actual, desired := a.ActualState, a.DesiredState
log := ctx.Log().WithField("name", a.AppManifest.Name)
log.WithField("state", desired.String()).Debug("Desired state.")
log.WithField("state", actual.String()).Debug("Actual state.")
var (
needsDelete bool
needsInstall bool
needsRollback bool
needsUpgrade bool
)
if desired.Status == workspace.StatusNotFound || desired.Status == workspace.StatusDeleted {
needsDelete = actual.Status != workspace.StatusDeleted && actual.Status != workspace.StatusNotFound
} else {
needsDelete = actual.Status == workspace.StatusFailed
needsDelete = needsDelete || actual.Status == workspace.StatusPendingUpgrade
}
if desired.Status == workspace.StatusDeployed {
if needsDelete {
needsInstall = true
} else {
switch actual.Status {
case workspace.StatusNotFound:
needsInstall = true
case workspace.StatusDeleted:
needsInstall = true
case workspace.StatusPendingUpgrade:
needsInstall = true
default:
needsUpgrade = actual.Status != workspace.StatusDeployed
needsUpgrade = needsUpgrade || actual.Routing != desired.Routing
needsUpgrade = needsUpgrade || actual.Version != desired.Version
needsUpgrade = needsUpgrade || actual.Diff != ""
needsUpgrade = needsUpgrade || desired.Force
}
}
}
if needsDelete {
steps = append(steps, PlanStep{
Name: "Delete",
Description: "Delete release from kubernetes.",
Action: a.Delete,
})
}
if desired.Status == workspace.StatusDeployed {
for i := range a.AppManifest.AppConfig.Actions {
action := a.AppManifest.AppConfig.Actions[i]
if action.When.Contains(actions.ActionBeforeDeploy) && action.WhereFilter.Matches(ctx.GetMatchMapArgs()) {
steps = append(steps, PlanStep{
Name: action.Name,
Description: action.Description,
Action: func(ctx BosunContext) error {
return action.Execute(ctx)
},
})
}
}
}
if needsInstall {
steps = append(steps, PlanStep{
Name: "Install",
Description: "Install chart to kubernetes.",
Action: a.Install,
})
}
if needsRollback {
steps = append(steps, PlanStep{
Name: "Rollback",
Description: "Rollback existing release in kubernetes to allow upgrade.",
Action: a.Rollback,
})
}
if needsUpgrade {
steps = append(steps, PlanStep{
Name: "Upgrade",
Description: "Upgrade existing release in kubernetes.",
Action: a.Upgrade,
})
}
if desired.Status == workspace.StatusDeployed {
for i := range a.AppManifest.AppConfig.Actions {
action := a.AppManifest.AppConfig.Actions[i]
if action.When.Contains(actions.ActionAfterDeploy) && action.WhereFilter.Matches(ctx.GetMatchMapArgs()) {
steps = append(steps, PlanStep{
Name: action.Name,
Description: action.Description,
Action: func(ctx BosunContext) error {
return action.Execute(ctx)
},
})
}
}
}
return steps, nil
}