This repository has been archived by the owner on Nov 7, 2020. It is now read-only.
forked from Praqma/helmsman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecision_maker.go
356 lines (283 loc) · 13.6 KB
/
decision_maker.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
package main
import (
"strings"
)
var outcome plan
var releases string
// makePlan creates a plan of the actions needed to make the desired state come true.
func makePlan(s *state) *plan {
outcome = createPlan()
buildState()
for _, r := range s.Apps {
decide(r)
}
return &outcome
}
// decide makes a decision about what commands (actions) need to be executed
// to make a release section of the desired state come true.
func decide(r *release) {
// check for deletion
if !r.Enabled {
if ok, rs := helmReleaseExists(r, ""); ok {
if !isProtected(r, rs) {
// delete it
deleteRelease(r, rs)
} else {
logDecision("DECISION: release [ "+r.Name+" ] in namespace [ "+r.Namespace+" ] is PROTECTED. Operations are not allowed on this release until "+
"you remove its protection.", r.Priority)
}
} else {
logDecision("DECISION: release [ "+r.Name+" ] is set to be disabled but is not yet deployed. Skipping.", r.Priority)
}
} else { // check for install/upgrade/rollback
if ok, rs := helmReleaseExists(r, "deployed"); ok {
if !isProtected(r, rs) {
inspectUpgradeScenario(r, rs) // upgrade or move
} else {
logDecision("DECISION: release [ "+r.Name+" ] in namespace [ "+r.Namespace+" ] is PROTECTED. Operations are not allowed on this release until "+
"you remove its protection.", r.Priority)
}
} else if ok, rs := helmReleaseExists(r, "deleted"); ok {
if !isProtected(r, rs) {
rollbackRelease(r, rs) // rollback
} else {
logDecision("DECISION: release [ "+r.Name+" ] in namespace [ "+r.Namespace+" ] is PROTECTED. Operations are not allowed on this release until "+
"you remove its protection.", r.Priority)
}
} else if ok, rs := helmReleaseExists(r, "failed"); ok {
if !isProtected(r, rs) {
logDecision("DECISION: release [ "+r.Name+" ] in namespace [ "+r.Namespace+" ] is in FAILED state. I will upgrade it for you. Hope it gets fixed!", r.Priority)
upgradeRelease(r)
} else {
logDecision("DECISION: release [ "+r.Name+" ] in namespace [ "+r.Namespace+" ] is PROTECTED. Operations are not allowed on this release until "+
"you remove its protection.", r.Priority)
}
} else {
installRelease(r) // install a new release
}
}
}
// testRelease creates a Helm command to test a particular release.
func testRelease(r *release) {
cmd := command{
Cmd: "bash",
Args: []string{"-c", "helm test " + r.Name + getDesiredTillerNamespaceFlag(r) + getTLSFlags(r)},
Description: "running tests for release [ " + r.Name + " ]",
}
outcome.addCommand(cmd, r.Priority, r)
logDecision("DECISION: release [ "+r.Name+" ] in namespace [ "+r.Namespace+" ] is required to be tested when installed. Got it!", r.Priority)
}
// installRelease creates a Helm command to install a particular release in a particular namespace using a particular Tiller.
func installRelease(r *release) {
cmd := command{
Cmd: "bash",
Args: []string{"-c", "helm install " + r.Chart + " -n " + r.Name + " --namespace " + r.Namespace + getValuesFiles(r) + " --version " + r.Version + getSetValues(r) + getWait(r) + getDesiredTillerNamespaceFlag(r) + getTLSFlags(r)},
Description: "installing release [ " + r.Name + " ] in namespace [[ " + r.Namespace + " ]] using Tiller in [ " + getDesiredTillerNamespace(r) + " ]",
}
outcome.addCommand(cmd, r.Priority, r)
logDecision("DECISION: release [ "+r.Name+" ] is not installed. Will install it in namespace [[ "+
r.Namespace+" ]] using Tiller in [ "+getDesiredTillerNamespace(r)+" ]", r.Priority)
if r.Test {
testRelease(r)
}
}
// rollbackRelease evaluates if a rollback action needs to be taken for a given release.
// if the release is already deleted but from a different namespace than the one specified in input,
// it purge deletes it and create it in the specified namespace.
func rollbackRelease(r *release, rs releaseState) {
if r.Namespace == rs.Namespace {
cmd := command{
Cmd: "bash",
Args: []string{"-c", "helm rollback " + r.Name + " " + getReleaseRevision(rs) + getWait(r) + getDesiredTillerNamespaceFlag(r) + getTLSFlags(r)},
Description: "rolling back release [ " + r.Name + " ] using Tiller in [ " + getDesiredTillerNamespace(r) + " ]",
}
outcome.addCommand(cmd, r.Priority, r)
upgradeRelease(r) // this is to reflect any changes in values file(s)
logDecision("DECISION: release [ "+r.Name+" ] is currently deleted and is desired to be rolledback to "+
"namespace [[ "+r.Namespace+" ]] . It will also be upgraded in case values have changed.", r.Priority)
} else {
reInstallRelease(r, rs)
logDecision("DECISION: release [ "+r.Name+" ] is deleted BUT from namespace [[ "+rs.Namespace+
" ]]. Will purge delete it from there and install it in namespace [[ "+r.Namespace+" ]]", r.Priority)
logDecision("WARNING: rolling back release [ "+r.Name+" ] from [[ "+rs.Namespace+" ]] to [[ "+r.Namespace+
" ]] might not correctly connect to existing volumes. Check https://github.com/gofunky/helmsman/blob/master/docs/how_to/move_charts_across_namespaces.md"+
" for details if this release uses PV and PVC.", r.Priority)
}
}
// deleteRelease deletes a release from a particular Tiller in a k8s cluster
func deleteRelease(r *release, rs releaseState) {
p := ""
purgeDesc := ""
if r.Purge {
p = "--purge"
purgeDesc = "and purged!"
}
cmd := command{
Cmd: "bash",
Args: []string{"-c", "helm delete " + p + " " + r.Name + getCurrentTillerNamespaceFlag(rs) + getTLSFlags(r)},
Description: "deleting release [ " + r.Name + " ] from namespace [[ " + r.Namespace + " ]] using Tiller in [ " + getDesiredTillerNamespace(r) + " ]",
}
outcome.addCommand(cmd, r.Priority, r)
logDecision("DECISION: release [ "+r.Name+" ] is desired to be deleted "+purgeDesc+". Planing this for you!", r.Priority)
}
// inspectUpgradeScenario evaluates if a release should be upgraded.
// - If the release is already in the same namespace specified in the input,
// it will be upgraded using the values file specified in the release info.
// - If the release is already in the same namespace specified in the input but is using a different chart,
// it will be purge deleted and installed in the same namespace using the new chart.
// - If the release is NOT in the same namespace specified in the input,
// it will be purge deleted and installed in the new namespace.
func inspectUpgradeScenario(r *release, rs releaseState) {
if r.Namespace == rs.Namespace {
if extractChartName(r.Chart) == getReleaseChartName(rs) && r.Version != getReleaseChartVersion(rs) {
// upgrade
upgradeRelease(r)
logDecision("DECISION: release [ "+r.Name+" ] is desired to be upgraded. Planing this for you!", r.Priority)
} else if extractChartName(r.Chart) != getReleaseChartName(rs) {
reInstallRelease(r, rs)
logDecision("DECISION: release [ "+r.Name+" ] is desired to use a new Chart [ "+r.Chart+
" ]. I am planning a purge delete of the current release and will install it with the new chart in namespace [[ "+
r.Namespace+" ]]", r.Priority)
} else {
upgradeRelease(r)
logDecision("DECISION: release [ "+r.Name+" ] is desired to be enabled and is currently enabled."+
"I will upgrade it in case you changed your values.yaml!", r.Priority)
}
} else {
reInstallRelease(r, rs)
logDecision("DECISION: release [ "+r.Name+" ] is desired to be enabled in a new namespace [[ "+r.Namespace+
" ]]. I am planning a purge delete of the current release from namespace [[ "+rs.Namespace+" ]] "+
"and will install it for you in namespace [[ "+r.Namespace+" ]]", r.Priority)
logDecision("WARNING: moving release [ "+r.Name+" ] from [[ "+rs.Namespace+" ]] to [[ "+r.Namespace+
" ]] might not correctly connect to existing volumes. Check https://github.com/gofunky/helmsman/blob/master/docs/how_to/move_charts_across_namespaces.md"+
" for details if this release uses PV and PVC.", r.Priority)
}
}
// upgradeRelease upgrades an existing release with the specified values.yaml
func upgradeRelease(r *release) {
cmd := command{
Cmd: "bash",
Args: []string{"-c", "helm upgrade " + r.Name + " " + r.Chart + getValuesFiles(r) + " --version " + r.Version + " --force " + getSetValues(r) + getWait(r) + getDesiredTillerNamespaceFlag(r) + getTLSFlags(r)},
Description: "upgrading release [ " + r.Name + " ] using Tiller in [ " + getDesiredTillerNamespace(r) + " ]",
}
outcome.addCommand(cmd, r.Priority, r)
}
// reInstallRelease purge deletes a release and reinstalls it.
// This is used when moving a release to another namespace or when changing the chart used for it.
func reInstallRelease(r *release, rs releaseState) {
delCmd := command{
Cmd: "bash",
Args: []string{"-c", "helm delete --purge " + r.Name + getCurrentTillerNamespaceFlag(rs) + getTLSFlags(r)},
Description: "deleting release [ " + r.Name + " ] from namespace [[ " + r.Namespace + " ]] using Tiller in [ " + getDesiredTillerNamespace(r) + " ]",
}
outcome.addCommand(delCmd, r.Priority, r)
installCmd := command{
Cmd: "bash",
Args: []string{"-c", "helm install " + r.Chart + " --version " + r.Version + " -n " + r.Name + " --namespace " + r.Namespace + getValuesFiles(r) + getSetValues(r) + getWait(r) + getDesiredTillerNamespaceFlag(r) + getTLSFlags(r)},
Description: "installing release [ " + r.Name + " ] in namespace [[ " + r.Namespace + " ]] using Tiller in [ " + getDesiredTillerNamespace(r) + " ]",
}
outcome.addCommand(installCmd, r.Priority, r)
}
// logDecision adds the decisions made to the plan.
// Depending on the debug flag being set or not, it will either log the the decision to output or not.
func logDecision(decision string, priority int) {
outcome.addDecision(decision, priority)
}
// extractChartName extracts the Helm chart name from full chart name in the desired state.
// example: it extracts "chartY" from "repoX/chartY"
func extractChartName(releaseChart string) string {
return strings.TrimSpace(strings.Split(releaseChart, "/")[1])
}
// getValuesFiles return partial install/upgrade release command to substitute the -f flag in Helm.
func getValuesFiles(r *release) string {
if r.ValuesFile != "" {
return " -f " + r.ValuesFile
} else if len(r.ValuesFiles) > 0 {
return " -f " + strings.Join(r.ValuesFiles, " -f ")
}
return ""
}
// getSetValues returns --set params to be used with helm install/upgrade commands
func getSetValues(r *release) string {
result := ""
for k, v := range r.Set {
_, value := envVarExists(v)
result = result + " --set " + k + "=\"" + strings.Replace(value, ",", "\\,", -1) + "\""
}
return result
}
// getWait returns a partial helm command containing the helm wait flag (--wait) if the wait flag for the release was set to true
// Otherwise, retruns an empty string
func getWait(r *release) string {
result := ""
if r.Wait {
result = " --wait"
}
return result
}
// getDesiredNamespace returns the namespace of a release
func getDesiredNamespace(r *release) string {
return r.Namespace
}
// getCurrentNamespaceProtection returns the protection state for the namespace where a release is currently installed.
// It returns true if a namespace is defined as protected in the desired state file, false otherwise.
func getCurrentNamespaceProtection(rs releaseState) bool {
return s.Namespaces[rs.Namespace].Protected
}
// isProtected checks if a release is protected or not.
// A protected is release is either: a) deployed in a protected namespace b) flagged as protected in the desired state file
// Any release in a protected namespace is protected by default regardless of its flag
// returns true if a release is protected, false otherwise
func isProtected(r *release, rs releaseState) bool {
// if the release does not exist in the cluster, it is not protected
if ok, _ := helmReleaseExists(r, ""); !ok {
return false
}
if getCurrentNamespaceProtection(rs) {
return true
}
if r.Protected {
return true
}
return false
}
// getDesiredTillerNamespaceFlag returns a tiller-namespace flag with which a release is desired to be maintained
func getDesiredTillerNamespaceFlag(r *release) string {
return " --tiller-namespace " + getDesiredTillerNamespace(r)
}
// getDesiredTillerNamespace returns the Tiller namespace with which a release should be managed
func getDesiredTillerNamespace(r *release) string {
if r.TillerNamespace != "" {
return r.TillerNamespace
} else if v, ok := s.Namespaces[r.Namespace]; ok && v.InstallTiller {
return r.Namespace
}
return "kube-system"
}
// getCurrentTillerNamespaceFlag returns the tiller-namespace with which a release is currently maintained
func getCurrentTillerNamespaceFlag(rs releaseState) string {
if rs.TillerNamespace != "" {
return " --tiller-namespace " + rs.TillerNamespace
}
return ""
}
// getTLSFlags returns TLS flags with which a release is maintained
// If the release where the namespace is to be deployed has Tiller deployed, the TLS flags will use certs/keys for that namespace (if any)
// otherwise, it will be the certs/keys for the kube-system namespace.
func getTLSFlags(r *release) string {
tls := ""
if r.TillerNamespace != "" {
if tillerTLSEnabled(r.TillerNamespace) {
tls = " --tls --tls-ca-cert " + r.TillerNamespace + "-ca.cert --tls-cert " + r.TillerNamespace + "-client.cert --tls-key " + r.TillerNamespace + "-client.key "
}
} else if s.Namespaces[r.Namespace].InstallTiller {
if tillerTLSEnabled(r.Namespace) {
tls = " --tls --tls-ca-cert " + r.Namespace + "-ca.cert --tls-cert " + r.Namespace + "-client.cert --tls-key " + r.Namespace + "-client.key "
}
} else {
if tillerTLSEnabled("kube-system") {
tls = " --tls --tls-ca-cert kube-system-ca.cert --tls-cert kube-system-client.cert --tls-key kube-system-client.key "
}
}
return tls
}