-
Notifications
You must be signed in to change notification settings - Fork 2
/
deployments.helmfile.yaml
411 lines (346 loc) · 19.6 KB
/
deployments.helmfile.yaml
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
values:
- statevalues/000-globals.yaml
- statevalues/001-clusters.yaml
- {{ requiredEnv "HELMFILE_DEPLOY_STATE_VALUES_DIR" }}/*
---
bases:
- {{ requiredEnv "HELMFILE_DEPLOY_ENVIRONMENTS_DIR" }}/index.yaml
---
bases:
- helmDefaults.yaml
---
releases:
{{- /*
---------------------------------------------------------
Basic values declarations
---------------------------------------------------------
*/ -}}
{{- $stateVals := .Values }}
{{- $envVals := .Environment.Values -}}
{{- $namespace := .Namespace -}}
{{- /*
The target "cluster"
Note: simpler syntax should just be: https://github.com/Masterminds/sprig/issues/151
*/ -}}
{{- $targetCluster := (pick $stateVals.clusters $stateVals.targetCluster) | values | first }}
{{- /*
---------------------------------------------------------
These two values are fetched from the helmfile "state values"
and are typically set via invocation:
"--state-values-set bootstrapSecret=x,creatorId=y"
---------------------------------------------------------
*/ -}}
{{ $bootstrapSecretValue := $stateVals | getOrNil "bootstrapSecret" }}
{{ $creatorId := $stateVals | getOrNil "creatorId" }}
{{- /*
---------------------------------------------------------
For each [helmfile-environment].environments.[environment] loaded/found
in yaml files under environments/[helmfile-environment]/* we loop over
each environment's context(s) declared "services", one by one
and generate a "appdeploy" helmfile "release"
NOTE! since the environment's context yaml file can potentially
list the same service.version across multiple service elements
we keep track of this as to not create duplicate releases.
Why can a "version" be declared across multiple "services"?
Because "services" are just named things we can route traffic
to and the conduits.helmfile.yaml template also processes the same
file. In this case the "version" just nets diff selectors
on conduit services. In any case, it is a legit case and permitted
---------------------------------------------------------
*/ -}}
{{- range $currEnvironmentName,$currEnvironment := $envVals.environments }}
{{- range $currContextName,$currContext := $currEnvironment.contexts }}
{{ $versionsReleased := list }}
{{- range $currService := $currContext.services }}
{{- /* version check */ -}}
{{ if not (has $currService.version $versionsReleased) }}
{{ $versionsReleased = append $versionsReleased $currService.version }}
{{- /*
Used by `tpl` variable parsing below. The caller can specify tpl parsed values i.e.
"{{ .helmfile.[path] }}" If ".helmfile" is NOT present the "{{bracketed}}" will
be left alone and assumed to be consumable by the chart in the release
*/ -}}
{{- $helmfileVarsDict := dict "helmfile" (dict "clusters" $stateVals.clusters "targetCluster" $targetCluster "envVals" $envVals "stateVals" $stateVals "namespace" $namespace "service" $currService "context" $currContext "environment" $currEnvironment) }}
{{- /*
---------------------------------------------------------
"chartConfigs"
---------------------------------------------------------
Here we grab the "chartConfigs" from the following possible locations in order of priority:
1. The current "[helmfile-environment].environments.[environment].contexts.[context].services.[service]" we are iterating over
i.e. this would be declared in environments/[helmfile-environment]/[any-env-file.yaml]
2. The current "[helmfile-environment].environments.[environment].contexts.[context]" we are iterating over
i.e. this would be declared in environments/[helmfile-environment]/[any-env-file.yaml]
3. The current "[helmfile-environment].environments.[environment]" we are iterating over
i.e. this would be declared in environments/[helmfile-environment]/[any-env-file.yaml]
4. The top level helmfile-environment's "chartConfigs"
i.e. this would be declared in environments/[helmfile-environment]/chartconfigs.yaml
5. The targetCluster's "chartConfigs" (clusters.[targetCluster].chartConfigs)
i.e. this would be declared in: statevalues/001-clusters.yaml or any other HELMFILE_DEPLOY_STATE_VALUES_DIR yaml
6. The global state values "chartConfigs"
i.e. this would be declared in: statevalues/000-globals.yaml or any other HELMFILE_DEPLOY_STATE_VALUES_DIR yaml
7. Note with the sprig "merge" function: see oddities: https://github.com/Masterminds/sprig/issues/120
*/ -}}
{{ $chartConfigs := dict }}
{{ $chartConfigs := hasKey $stateVals "chartConfigs" | ternary (deepCopy ((hasKey $stateVals "chartConfigs") | ternary ($stateVals | getOrNil "chartConfigs") dict) | mergeOverwrite $chartConfigs) $chartConfigs }}
{{ $chartConfigs := hasKey $targetCluster "chartConfigs" | ternary (deepCopy ((hasKey $targetCluster "chartConfigs") | ternary ($targetCluster | getOrNil "chartConfigs") dict) | mergeOverwrite $chartConfigs) $chartConfigs }}
{{ $chartConfigs := hasKey $envVals "chartConfigs" | ternary (deepCopy ((hasKey $envVals "chartConfigs") | ternary ($envVals | getOrNil "chartConfigs") dict) | mergeOverwrite $chartConfigs) $chartConfigs }}
{{ $chartConfigs := hasKey $currEnvironment "chartConfigs" | ternary (deepCopy ((hasKey $currEnvironment "chartConfigs") | ternary ($currEnvironment | getOrNil "chartConfigs") dict) | mergeOverwrite $chartConfigs) $chartConfigs }}
{{ $chartConfigs := hasKey $currContext "chartConfigs" | ternary (deepCopy ((hasKey $currContext "chartConfigs") | ternary ($currContext | getOrNil "chartConfigs") dict) | mergeOverwrite $chartConfigs) $chartConfigs }}
{{ $chartConfigs := hasKey $currService "chartConfigs" | ternary (deepCopy ((hasKey $currService "chartConfigs") | ternary ($currService | getOrNil "chartConfigs") dict) | mergeOverwrite $chartConfigs) $chartConfigs }}
{{- /*
---------------------------------------------------------
Generate a "releaseIdentifier"
[envVals.appname]-[environment.context]-[currEnvironment.service.version]-[envVals.classifier]
---------------------------------------------------------
*/ -}}
{{- $releaseIdentifier := printf "%s-%s-%s-%s" (hasKey $envVals "releaseBaseName" | ternary ($envVals | getOrNil "releaseBaseName") $envVals.appname) $currContextName ($currService.version | replace "." "-") (hasKey $envVals "classifier" | ternary ($envVals | getOrNil "classifier") "NOCLASSIFIER") }}
{{- $releaseIdentifier := $releaseIdentifier | replace "-NOCLASSIFIER" "" -}}
{{- $releaseIdentifier := $releaseIdentifier | replace "NOCLASSIFIER" "" -}}
{{- $releaseIdentifier := $releaseIdentifier | replace "-%!s(<nil>)" "" }}
{{- /*
---------------------------------------------------------
Build BASE values file references -> $valuesFiles
The "chartConfigs.appdeploy" chart configuration can declare one or more named
"availableBaseValueSets" which define sets of supplemental "values.yaml" files.
Here we inspect the "baseValuesSets" list of referenced "set" names to determine
the actual list of "values.yaml" files to apply to the chart which are ultimately
located under the configured "chartConfigs.appdeploy.chartValues.baseValuesRootDir" path
@see statevalues/000-globals.yaml for more info
---------------------------------------------------------
*/ -}}
{{ $valuesFiles := dict "files" list }}
{{ $valueBaseParseVars := dict "appname" $envVals.appname "classifier" ($envVals | getOrNil "classifier") "environmentName" $currEnvironmentName "contextName" $currContextName "service" $currService }}
{{- /* Get list of all named "sets" we actually want to use */ -}}
{{ range $baseValueSetName := $chartConfigs.appdeploy.chartValues.baseValueSets }}
{{- /* Load the actual referenced set by name from 'availableBaseValueSets' */ -}}
{{ $baseValues := (pick $chartConfigs.appdeploy.chartValues.availableBaseValueSets $baseValueSetName) | values | first }}
{{- /* For each one, lets add it to the list of overall values files we will reference below */ -}}
{{ range $valueBase := $baseValues }}
{{ $parsedValueBase := tpl $valueBase $valueBaseParseVars }}
{{ $_ := append $valuesFiles.files (printf "%s/values/%s/values.yaml" $chartConfigs.appdeploy.chartValues.baseValuesRootDir $parsedValueBase) | set $valuesFiles "files" }}
{{ end }}
{{ end }}
{{- /*
Each "service" can declare a "hooks" object, each top-level "key" of it
designates the name of the hook and it's values folder within the
"chartConfigs.appdeploy.chartValues.baseValuesRootDir" location
for supplemental values particular to that hook.
*/ -}}
{{ if hasKey $currService "hooks"}}
{{ range $currHookName,$currHook := $currService.hooks }}
{{ $_ := append $valuesFiles.files (printf "%s/values/hooks/%s/values.yaml" $chartConfigs.appdeploy.chartValues.baseValuesRootDir $currHookName) | set $valuesFiles "files" }}
{{ end }}
{{ end }}
{{- /*
---------------------------------------------------------
Helmfile RELEASE generation:
Generate an actual helmfile release of the "appdeploy" chart
for the currentEnvironment.currentContext.currService
with its desired state expressed in helm values
---------------------------------------------------------
*/ -}}
- name: {{ $releaseIdentifier }}
labels:
app: {{ $envVals.appname }}
context: {{ $currContextName }}
version: {{ ($currService.version | replace "." "-") }}
name: {{ $currService.name }}
namespace: {{ $namespace }}
chart: {{ $chartConfigs.appdeploy.chart }}
{{ if hasKey $chartConfigs.appdeploy "version" }}
version: {{ $chartConfigs.appdeploy.version }}
{{ end }}
{{ if $currService.installed }}
installed: true
{{ else }}
installed: false
{{ end }}
{{- /* Permit forced override of the default helm tillerNamespace (defined in helmDefaults.yaml)
(set this via helmfile --state-values-set forceHelmTillerNamespace=[namespace]) */ -}}
{{ if hasKey $stateVals "forceHelmTillerNamespace" }}
tillerNamespace: {{ $stateVals.forceHelmTillerNamespace }}
{{ end }}
{{- /* Permit forced override of the default helm timeout (defined in helmDefaults.yaml)
(set this via helmfile --state-values-set forceHelmTimeout=[N seconds]) */ -}}
{{ if hasKey $stateVals "forceHelmTimeout" }}
timeout: {{ $stateVals.forceHelmTimeout }}
{{ end }}
{{- /* Permit forced override of the default helm 3.2+ create-namespace behavior (defined in helmDefaults.yaml)
(set this via helmfile --state-values-set forceHelmCreateNamespace=[true|false]) */ -}}
{{ if hasKey $stateVals "forceHelmCreateNamespace" }}
createNamespace: {{ $stateVals.forceHelmCreateNamespace }}
{{ end }}
values:
{{- /*
---------------------------------------------------------
RENDER: BASE values file references
note: "exec" hack can go away when
https://github.com/roboll/helmfile/issues/766 is addressed
---------------------------------------------------------
*/ -}}
{{- range $valueFile := $valuesFiles.files }}
{{ if eq (exec "./fileexists.sh" (list $valueFile)) "true" }}
- {{$valueFile}}
{{ end }}
{{ end }}
{{/*
---------------------------------------------------------
Inline custom values
These values, combined with the "bases" above yield
the overall set of "values" customizations passed to helm
---------------------------------------------------------
*/ -}}
- app:
name: {{ $envVals.appname }}
context: {{ $currContextName }}
environment: {{ $currEnvironmentName }}
{{ if $envVals | getOrNil "classifier" }}
classifier: {{ $envVals.classifier }}
{{ end }}
creatorId: {{ $creatorId }}
replicaCount: {{ $chartConfigs.appdeploy.chartValues.values.replicaCount }}
image:
{{ if hasKey $chartConfigs.appdeploy.chartValues.values "image" }}
{{ if hasKey $chartConfigs.appdeploy.chartValues.values.image "repository" }}
repository: {{ $chartConfigs.appdeploy.chartValues.values.image.repository }}
{{ end }}
{{ end }}
tag: "{{ $currService.version }}"
{{ if hasKey $chartConfigs.appdeploy.chartValues.values "containerPorts" }}
containerPorts:
{{ toYaml $chartConfigs.appdeploy.chartValues.values.containerPorts | indent 12 }}
{{ end }}
{{ if hasKey $chartConfigs.appdeploy.chartValues.values "ingress" }}
ingress:
{{ if hasKey $chartConfigs.appdeploy.chartValues.values.ingress "dns" }}
dns:
{{ toYaml $chartConfigs.appdeploy.chartValues.values.ingress.dns | indent 0 }}
{{ end }}
{{ if hasKey $chartConfigs.appdeploy.chartValues.values.ingress "tls" }}
tls:
{{ toYaml $chartConfigs.appdeploy.chartValues.values.ingress.tls | indent 0 }}
{{ end }}
{{ if hasKey $chartConfigs.appdeploy.chartValues.values.ingress "metadata" }}
metadata:
{{ if hasKey $chartConfigs.appdeploy.chartValues.values.ingress.metadata "labels" }}
labels:
{{ range $labelName,$labelValue := $chartConfigs.appdeploy.chartValues.values.ingress.metadata.labels }}
"{{$labelName}}": "{{ contains ".helmfile" $labelValue | ternary (tpl $labelValue $helmfileVarsDict) $labelValue }}"
{{ end }}
{{ end }}
{{ if hasKey $chartConfigs.appdeploy.chartValues.values.ingress.metadata "annotations" }}
annotations:
{{ range $annotationName,$annotationValue := $chartConfigs.appdeploy.chartValues.values.ingress.metadata.annotations }}
"{{$annotationName}}": "{{ contains ".helmfile" $annotationValue | ternary (tpl $annotationValue $helmfileVarsDict) $annotationValue }}"
{{ end }}
{{ end }}
{{ end }}
{{ end }}
{{ if hasKey $chartConfigs.appdeploy.chartValues.values "healthcheck" }}
healthcheck:
{{ toYaml $chartConfigs.appdeploy.chartValues.values.healthcheck | indent 12 }}
{{ end }}
{{ if hasKey $chartConfigs.appdeploy.chartValues.values "resources" }}
resources:
{{ toYaml $chartConfigs.appdeploy.chartValues.values.resources | indent 12 }}
{{ end }}
{{ if hasKey $chartConfigs.appdeploy.chartValues.values "autoscaling" }}
autoscaling:
{{ toYaml $chartConfigs.appdeploy.chartValues.values.autoscaling | indent 12 }}
{{ end }}
{{ if hasKey $chartConfigs.appdeploy.chartValues.values "disruptionBudget" }}
disruptionBudget:
{{ toYaml $chartConfigs.appdeploy.chartValues.values.disruptionBudget | indent 12 }}
{{ end }}
bootstrapSecret:
k8Secret:
secretValue: "{{ $bootstrapSecretValue }}"
{{- if $chartConfigs.appdeploy.chartValues.values | getOrNil "env" }}
env:
{{- range $key,$val := $chartConfigs.appdeploy.chartValues.values.env }}
{{- if and (hasKey $val "value") $val.value }}
{{ $key }}:
{{ if contains ".helmfile" $val.value }}
value: {{ tpl $val.value $helmfileVarsDict }}
{{ else }}
value: {{ $val.value }}
{{ end }}
{{ else }}
{{ $key }}:
value: ""
{{ end }}
{{ end }}
{{ end }}
{{ if hasKey $chartConfigs.appdeploy.chartValues.values "hooks" }}
hooks:
{{- /*
DEFAULT hooks (hooks.default.[n])
Dump out all non-custom appdeploy "hooks.default" defined in "chartConfigs" blocks
*/ -}}
{{ if hasKey $chartConfigs.appdeploy.chartValues.values.hooks "default" }}
{{ toYaml $chartConfigs.appdeploy.chartValues.values.hooks.default | indent 12 }}
{{ end }}
{{- /*
CUSTOM Hooks: (hooks.custom.[n])
Here we manually rewrite it all as w support variable
tpl parsing. This is not entirely complete as it does
not cover re-rendering all possible vars as the use
case at this point is pretty limited whereby the majority
of 'custom' entries only configure the options below
while the majority of other configuration for them is
sourced from baseValues valuesFiles above. Below we
only support enabled, variables and the bootstrapSecret
*/ -}}
{{ if hasKey $chartConfigs.appdeploy.chartValues.values.hooks "custom" }}
custom:
{{ range $currHookName,$currHook := $chartConfigs.appdeploy.chartValues.values.hooks.custom }}
{{ $currHookName }}:
{{ if not (empty $currHook) }}
{{ if hasKey $currHook "values" }}
{{ if hasKey $currHook.values "enabled" }}
enabled: {{ $currHook.values.enabled }}
{{ end }}
{{ if hasKey $currHook.values "variables" }}
variables:
{{ range $name,$value := $currHook.values.variables }}
{{ if contains ".helmfile" $value }}
{{ $name }}: {{ tpl $value $helmfileVarsDict }}
{{ else }}
{{ $name }}: {{ $value }}
{{ end }}
{{ end }}
{{ end }}
{{ end }} {{- /* END: if not (empty $currHook) */ -}}
{{ end }} {{- /* END: if hasKey $currHook "values" */ -}}
{{- /*
---------------------------------------------------------
Each custom hook's optional "bootstrapSecret" value
can be specified on helmfile invocation like:
"--state-values-set hooks.custom.[hookname].bootstrapSecret=x"
Note that "x" will be passed for 'tpl' parsing so it can
reference another variable in the $helmfileVarsDict
---------------------------------------------------------
*/}}
bootstrapSecret:
k8Secret:
{{- if ($stateVals | getOrNil "hooks.custom") }}
{{- $stateValsCurrHook := $stateVals.hooks.custom | getOrNil $currHookName }}
{{ if $stateValsCurrHook }}
{{ if contains ".helmfile" $stateValsCurrHook.bootstrapSecret }}
secretValue: {{ tpl $stateValsCurrHook.bootstrapSecret $helmfileVarsDict }}
{{ else }}
secretValue: {{ $stateValsCurrHook.bootstrapSecret }}
{{ end }}
{{ else }}
secretValue: "NO_SECRET_VALUE1"
{{ end }}
{{ else }}
secretValue: "NO_SECRET_VALUE2"
{{ end }}
{{ end }} {{- /* END: range $currHookName,$currHook := ..... */ -}}
{{ end }} {{- /* END: if hasKey $chartConfigs.appdeploy.chartValues.values.hooks "custom"... */ -}}
{{ end }} {{- /* END: if hasKey $chartConfigs.appdeploy.chartValues.values "hooks" */ -}}
{{ end }} {{- /* END: range $currService := $currContext.services */ -}}
{{ end }} {{- /* END: if not has $currService.version $versionsReleased */ -}}
{{ end }} {{- /* END: range $currContextName,$currContext := $currEnvironment.contexts */ -}}
{{ end }} {{- /* END: range $currEnvironmentName,$currEnvironment := $envVals.environments */ -}}