-
-
Notifications
You must be signed in to change notification settings - Fork 107
/
_helpers.tpl
261 lines (246 loc) · 8.83 KB
/
_helpers.tpl
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
# Expand the name of the chart.
{{- define "stackstorm-ha.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
{{- end -}}
# Generate Docker image repository: Public Docker Hub 'stackstorm' for FOSS version
{{- define "imageRepository" -}}
{{- if .Values.image.repository -}}
{{ .Values.image.repository }}
{{- else -}}
stackstorm
{{- end -}}
{{- end -}}
{{/*
Create the name of the stackstorm-ha service account to use
*/}}
{{- define "stackstorm-ha.serviceAccountName" -}}
{{- default .Chart.Name .Values.serviceAccount.serviceAccountName -}}
{{- end -}}
# Generate '-' prefix only when the variable is defined
{{- define "hyphenPrefix" -}}
{{ if . }}-{{ . }}{{end}}
{{- end -}}
# Allow calling helpers from nested sub-chart
# https://stackoverflow.com/a/52024583/4533625
# https://github.com/helm/helm/issues/4535#issuecomment-477778391
# Usage: "{{ include "nested" (list . "mongodb" "mongodb.fullname") }}"
{{- define "nested" }}
{{- $dot := index . 0 }}
{{- $subchart := index . 1 | splitList "." }}
{{- $template := index . 2 }}
{{- $values := $dot.Values }}
{{- range $subchart }}
{{- $values = index $values . }}
{{- end }}
{{- include $template (dict "Chart" (dict "Name" (last $subchart)) "Values" $values "Release" $dot.Release "Capabilities" $dot.Capabilities) }}
{{- end }}
# Generate comma-separated list of nodes for MongoDB-HA connection string, based on number of replicas and service name
{{- define "mongodb-nodes" -}}
{{- $replicas := (int (index .Values "mongodb" "replicaCount")) }}
{{- $architecture := (index .Values "mongodb" "architecture" ) }}
{{- $mongo_fullname := include "nested" (list $ "mongodb" "mongodb.fullname") }}
{{- range $index0 := until $replicas -}}
{{- $index1 := $index0 | add1 -}}
{{- if eq $architecture "replicaset" }}
{{- $mongo_fullname }}-{{ $index0 }}.{{ $mongo_fullname }}-headless{{ if ne $index1 $replicas }},{{ end }}
{{- else }}
{{- $mongo_fullname }}-{{ $index0 }}.{{ $mongo_fullname }}{{ if ne $index1 $replicas }},{{ end }}
{{- end -}}
{{- end -}}
{{- end -}}
# Generate list of nodes for Redis with Sentinel connection string, based on number of replicas and service name
{{- define "redis-nodes" -}}
{{- if not .Values.redis.sentinel.enabled }}
{{- fail "value for redis.sentinel.enabled MUST be true" }}
{{- end }}
{{- $replicas := (int (index .Values "redis" "cluster" "slaveCount")) }}
{{- $master_name := (index .Values "redis" "sentinel" "masterSet") }}
{{- $sentinel_port := (index .Values "redis" "sentinel" "port") }}
{{- range $index0 := until $replicas -}}
{{- if eq $index0 0 -}}
{{ $.Release.Name }}-redis-node-{{ $index0 }}.{{ $.Release.Name }}-redis-headless:{{ $sentinel_port }}?sentinel={{ $master_name }}
{{- else -}}
&sentinel_fallback={{ $.Release.Name }}-redis-node-{{ $index0 }}.{{ $.Release.Name }}-redis-headless:{{ $sentinel_port }}
{{- end -}}
{{- end -}}
{{- end -}}
# Reduce duplication of the st2.*.conf volume details
{{- define "st2-config-volume-mounts" -}}
- name: st2-config-vol
mountPath: /etc/st2/st2.docker.conf
subPath: st2.docker.conf
- name: st2-config-vol
mountPath: /etc/st2/st2.user.conf
subPath: st2.user.conf
{{- end -}}
{{- define "st2-config-volume" -}}
- name: st2-config-vol
configMap:
name: {{ $.Release.Name }}-st2-config
{{- end -}}
{{- define "init-containers-wait-for-db" -}}
{{- if index .Values "mongodb" "enabled" }}
{{- $mongodb_port := (int (index .Values "mongodb" "service" "port")) }}
- name: wait-for-db
image: busybox:1.28
command:
- 'sh'
- '-c'
- >
until nc -z -w 2 {{ $.Release.Name }}-mongodb-headless {{ $mongodb_port }} && echo mongodb ok;
do
echo 'Waiting for MongoDB Connection...'
sleep 2;
done
{{- end }}
{{- end -}}
{{- define "init-containers-wait-for-mq" -}}
{{- if index .Values "rabbitmq" "enabled" }}
{{- $rabbitmq_port := (int (index .Values "rabbitmq" "service" "port")) }}
- name: wait-for-queue
image: busybox:1.28
command:
- 'sh'
- '-c'
- >
until nc -z -w 2 {{ $.Release.Name }}-rabbitmq {{ $rabbitmq_port }} && echo rabbitmq ok;
do
echo 'Waiting for RabbitMQ Connection...'
sleep 2;
done
{{- end }}
{{- end -}}
# consolidate pack-configs-volumes definitions
{{- define "pack-configs-volume" -}}
{{- if and .Values.st2.packs.volumes.enabled .Values.st2.packs.volumes.configs }}
- name: st2-pack-configs-vol
{{- toYaml .Values.st2.packs.volumes.configs | nindent 2 }}
{{- if .Values.st2.packs.configs }}
- name: st2-pack-configs-from-helm-vol
configMap:
name: {{ .Release.Name }}-st2-pack-configs
{{- end }}
{{- else }}
- name: st2-pack-configs-vol
configMap:
name: {{ .Release.Name }}-st2-pack-configs
{{- end }}
{{- end -}}
{{- define "pack-configs-volume-mount" -}}
- name: st2-pack-configs-vol
mountPath: /opt/stackstorm/configs/
{{- if and .Values.st2.packs.volumes.enabled .Values.st2.packs.volumes.configs .Values.st2.packs.configs }}
- name: st2-pack-configs-from-helm-vol
mountPath: /opt/stackstorm/configs-helm/
{{- end }}
{{- end -}}
# For custom st2packs-Container reduce duplicity by defining it here once
{{- define "packs-volumes" -}}
{{- if .Values.st2.packs.volumes.enabled }}
- name: st2-packs-vol
{{- toYaml .Values.st2.packs.volumes.packs | nindent 2 }}
- name: st2-virtualenvs-vol
{{- toYaml .Values.st2.packs.volumes.virtualenvs | nindent 2 }}
{{- else if .Values.st2.packs.images }}
- name: st2-packs-vol
emptyDir: {}
- name: st2-virtualenvs-vol
emptyDir: {}
{{- end }}
{{- end -}}
{{- define "packs-volume-mounts" -}}
{{- if .Values.st2.packs.volumes.enabled }}
- name: st2-packs-vol
mountPath: /opt/stackstorm/packs
- name: st2-virtualenvs-vol
mountPath: /opt/stackstorm/virtualenvs
{{- else if .Values.st2.packs.images }}
- name: st2-packs-vol
mountPath: /opt/stackstorm/packs
readOnly: true
- name: st2-virtualenvs-vol
mountPath: /opt/stackstorm/virtualenvs
readOnly: true
{{- end }}
{{- end -}}
# define this here as well to simplify comparison with packs-volume-mounts
{{- define "packs-volume-mounts-for-register-job" -}}
{{- if or .Values.st2.packs.images .Values.st2.packs.volumes.enabled }}
- name: st2-packs-vol
mountPath: /opt/stackstorm/packs
- name: st2-virtualenvs-vol
mountPath: /opt/stackstorm/virtualenvs
{{- end }}
{{- end -}}
# For custom st2packs-initContainers reduce duplicity by defining them here once
# Merge packs and virtualenvs from st2 with those from st2packs images
{{- define "packs-initContainers" -}}
{{- if $.Values.st2.packs.images }}
{{- range $.Values.st2.packs.images }}
- name: 'st2-custom-pack-{{ printf "%s-%s-%s" .repository .name .tag | sha1sum }}'
image: "{{ .repository }}/{{ .name }}:{{ .tag }}"
imagePullPolicy: {{ .pullPolicy | quote }}
volumeMounts:
- name: st2-packs-vol
mountPath: /opt/stackstorm/packs-shared
- name: st2-virtualenvs-vol
mountPath: /opt/stackstorm/virtualenvs-shared
command:
- 'sh'
- '-ec'
- |
/bin/cp -aR /opt/stackstorm/packs/. /opt/stackstorm/packs-shared &&
/bin/cp -aR /opt/stackstorm/virtualenvs/. /opt/stackstorm/virtualenvs-shared
{{- end }}
{{- end }}
{{- if or $.Values.st2.packs.images $.Values.st2.packs.volumes.enabled }}
# System packs
- name: st2-system-packs
image: '{{ template "imageRepository" . }}/st2actionrunner:{{ tpl (.Values.st2actionrunner.image.tag | default .Values.image.tag) . }}'
imagePullPolicy: {{ .Values.image.pullPolicy }}
volumeMounts:
- name: st2-packs-vol
mountPath: /opt/stackstorm/packs-shared
- name: st2-virtualenvs-vol
mountPath: /opt/stackstorm/virtualenvs-shared
command:
- 'sh'
- '-ec'
- |
/bin/cp -aR /opt/stackstorm/packs/. /opt/stackstorm/packs-shared &&
/bin/cp -aR /opt/stackstorm/virtualenvs/. /opt/stackstorm/virtualenvs-shared
{{- end }}
{{- if and $.Values.st2.packs.configs $.Values.st2.packs.volumes.enabled }}
# Pack configs defined in helm values
- name: st2-pack-configs-from-helm
image: '{{ template "imageRepository" . }}/st2actionrunner:{{ tpl (.Values.st2actionrunner.image.tag | default .Values.image.tag) . }}'
imagePullPolicy: {{ .Values.image.pullPolicy }}
volumeMounts:
- name: st2-pack-configs-vol
mountPath: /opt/stackstorm/configs-shared
- name: st2-pack-configs-from-helm-vol
mountPath: /opt/stackstorm/configs
command:
- 'sh'
- '-ec'
- |
/bin/cp -aR /opt/stackstorm/configs/. /opt/stackstorm/configs-shared
{{- end }}
{{- end -}}
# For custom st2packs-pullSecrets reduce duplicity by defining them here once
{{- define "packs-pullSecrets" -}}
{{- range $.Values.st2.packs.images }}
{{- if .pullSecret }}
- name: {{ .pullSecret }}
{{- end }}
{{- end }}
{{- end -}}
{{/*
Create the custom env list for each deployment
*/}}
{{- define "stackstorm-ha.customEnv" -}}
{{- range $env, $value := .env }}
- name: {{ $env | quote }}
value: {{ $value | quote }}
{{- end }}
{{- end -}}