Merge "[AAI] Add model-loader tracing config"
[oom.git] / kubernetes / common / mongodb / templates / _helpers.tpl
1 {{/*
2 Copyright VMware, Inc.
3 SPDX-License-Identifier: APACHE-2.0
4 */}}
5
6 {{/* vim: set filetype=mustache: */}}
7 {{/*
8 Expand the name of the chart.
9 */}}
10 {{- define "mongodb.name" -}}
11 {{- include "common.names.name" . -}}
12 {{- end -}}
13
14 {{/*
15 Create a default fully qualified app name.
16 We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
17 If release name contains chart name it will be used as a full name.
18 */}}
19 {{- define "mongodb.fullname" -}}
20 {{- include "common.names.fullname" . -}}
21 {{- end -}}
22
23 {{/*
24 Create a default mongo service name which can be overridden.
25 */}}
26 {{- define "mongodb.service.nameOverride" -}}
27     {{- if and .Values.service .Values.service.nameOverride -}}
28         {{- print .Values.service.nameOverride -}}
29     {{- else -}}
30         {{- if eq .Values.architecture "replicaset" -}}
31             {{- printf "%s-headless" (include "mongodb.fullname" .) -}}
32         {{- else -}}
33             {{- printf "%s" (include "mongodb.fullname" .) -}}
34         {{- end -}}
35     {{- end -}}
36 {{- end -}}
37
38 {{/*
39 Create a default mongo arbiter service name which can be overridden.
40 */}}
41 {{- define "mongodb.arbiter.service.nameOverride" -}}
42     {{- if and .Values.arbiter.service .Values.arbiter.service.nameOverride -}}
43         {{- print .Values.arbiter.service.nameOverride -}}
44     {{- else -}}
45         {{- printf "%s-arbiter-headless" (include "mongodb.fullname" .) -}}
46     {{- end }}
47 {{- end }}
48
49 {{/*
50 Return the proper MongoDB® image name
51 */}}
52 {{- define "mongodb.image" -}}
53 {{- include "common.images.image" (dict "imageRoot" .Values.image "global" .Values.global) -}}
54 {{- end -}}
55
56 {{/*
57 Return the proper image name (for the metrics image)
58 */}}
59 {{- define "mongodb.metrics.image" -}}
60 {{- include "common.images.image" (dict "imageRoot" .Values.metrics.image "global" .Values.global) -}}
61 {{- end -}}
62
63 {{/*
64 Return the proper image name (for the init container volume-permissions image)
65 */}}
66 {{- define "mongodb.volumePermissions.image" -}}
67 {{- include "common.images.image" (dict "imageRoot" .Values.volumePermissions.image "global" .Values.global) -}}
68 {{- end -}}
69
70 {{/*
71 Return the proper image name (for the init container auto-discovery image)
72 */}}
73 {{- define "mongodb.externalAccess.autoDiscovery.image" -}}
74 {{- include "common.images.image" (dict "imageRoot" .Values.externalAccess.autoDiscovery.image "global" .Values.global) -}}
75 {{- end -}}
76
77 {{/*
78 Return the proper image name (for the TLS Certs image)
79 */}}
80 {{- define "mongodb.tls.image" -}}
81 {{- include "common.images.image" (dict "imageRoot" .Values.tls.image "global" .Values.global) -}}
82 {{- end -}}
83
84 {{/*
85 Return the proper Docker Image Registry Secret Names
86 */}}
87 {{- define "mongodb.imagePullSecrets" -}}
88 {{- include "common.images.renderPullSecrets" (dict "images" (list .Values.image .Values.metrics.image .Values.volumePermissions.image .Values.tls.image) "context" $) -}}
89 {{- end -}}
90
91 {{/*
92 Allow the release namespace to be overridden for multi-namespace deployments in combined charts.
93 */}}
94 {{- define "mongodb.namespace" -}}
95     {{- if and .Values.global .Values.global.namespaceOverride -}}
96         {{- print .Values.global.namespaceOverride -}}
97     {{- else -}}
98         {{- print .Release.Namespace -}}
99     {{- end }}
100 {{- end -}}
101 {{- define "mongodb.serviceMonitor.namespace" -}}
102     {{- if .Values.metrics.serviceMonitor.namespace -}}
103         {{- print .Values.metrics.serviceMonitor.namespace -}}
104     {{- else -}}
105         {{- include "mongodb.namespace" . -}}
106     {{- end }}
107 {{- end -}}
108 {{- define "mongodb.prometheusRule.namespace" -}}
109     {{- if .Values.metrics.prometheusRule.namespace -}}
110         {{- print .Values.metrics.prometheusRule.namespace -}}
111     {{- else -}}
112         {{- include "mongodb.namespace" . -}}
113     {{- end }}
114 {{- end -}}
115
116 {{/*
117 Returns the proper service account name depending if an explicit service account name is set
118 in the values file. If the name is not set it will default to either mongodb.fullname if serviceAccount.create
119 is true or default otherwise.
120 */}}
121 {{- define "mongodb.serviceAccountName" -}}
122     {{- if .Values.serviceAccount.create -}}
123         {{- default (include "mongodb.fullname" .) (print .Values.serviceAccount.name) -}}
124     {{- else -}}
125         {{- default "default" (print .Values.serviceAccount.name) -}}
126     {{- end -}}
127 {{- end -}}
128
129 {{/*
130 Return the list of custom users to create during the initialization (string format)
131 */}}
132 {{- define "mongodb.customUsers" -}}
133     {{- $customUsers := list -}}
134     {{- if .Values.auth.username -}}
135         {{- $customUsers = append $customUsers .Values.auth.username }}
136     {{- end }}
137     {{- range .Values.auth.usernames }}
138         {{- $customUsers = append $customUsers . }}
139     {{- end }}
140     {{- printf "%s" (default "" (join "," $customUsers)) -}}
141 {{- end -}}
142
143 {{/*
144 Return the list of passwords for the custom users (string format)
145 */}}
146 {{- define "mongodb.customPasswords" -}}
147     {{- $customPasswords := list -}}
148     {{- if .Values.auth.password -}}
149         {{- $customPasswords = append $customPasswords .Values.auth.password }}
150     {{- end }}
151     {{- range .Values.auth.passwords }}
152         {{- $customPasswords = append $customPasswords . }}
153     {{- end }}
154     {{- printf "%s" (default "" (join "," $customPasswords)) -}}
155 {{- end -}}
156
157 {{/*
158 Return the list of custom databases to create during the initialization (string format)
159 */}}
160 {{- define "mongodb.customDatabases" -}}
161     {{- $customDatabases := list -}}
162     {{- if .Values.auth.database -}}
163         {{- $customDatabases = append $customDatabases .Values.auth.database }}
164     {{- end }}
165     {{- range .Values.auth.databases }}
166         {{- $customDatabases = append $customDatabases . }}
167     {{- end }}
168     {{- printf "%s" (default "" (join "," $customDatabases)) -}}
169 {{- end -}}
170
171 {{/*
172 Return the configmap with the MongoDB® configuration
173 */}}
174 {{- define "mongodb.configmapName" -}}
175 {{- if .Values.existingConfigmap -}}
176     {{- printf "%s" (tpl .Values.existingConfigmap $) -}}
177 {{- else -}}
178     {{- printf "%s" (include "mongodb.fullname" .) -}}
179 {{- end -}}
180 {{- end -}}
181
182 {{/*
183 Return true if a configmap object should be created for MongoDB®
184 */}}
185 {{- define "mongodb.createConfigmap" -}}
186 {{- if and .Values.configuration (not .Values.existingConfigmap) }}
187     {{- true -}}
188 {{- else -}}
189 {{- end -}}
190 {{- end -}}
191
192 {{/*
193 Return the secret with MongoDB® credentials
194 */}}
195 {{- define "mongodb.secretName" -}}
196     {{- if .Values.auth.existingSecret -}}
197         {{- printf "%s" (tpl .Values.auth.existingSecret $) -}}
198     {{- else -}}
199         {{- printf "%s" (include "mongodb.fullname" .) -}}
200     {{- end -}}
201 {{- end -}}
202
203 {{/*
204 Return true if a secret object should be created for MongoDB®
205 */}}
206 {{- define "mongodb.createSecret" -}}
207 {{- if and .Values.auth.enabled (not .Values.auth.existingSecret) }}
208     {{- true -}}
209 {{- else -}}
210 {{- end -}}
211 {{- end -}}
212
213 {{/*
214 Get the initialization scripts ConfigMap name.
215 */}}
216 {{- define "mongodb.initdbScriptsCM" -}}
217 {{- if .Values.initdbScriptsConfigMap -}}
218 {{- printf "%s" .Values.initdbScriptsConfigMap -}}
219 {{- else -}}
220 {{- printf "%s-init-scripts" (include "mongodb.fullname" .) -}}
221 {{- end -}}
222 {{- end -}}
223
224 {{/*
225 Return true if the Arbiter should be deployed
226 */}}
227 {{- define "mongodb.arbiter.enabled" -}}
228 {{- if and (eq .Values.architecture "replicaset") .Values.arbiter.enabled }}
229     {{- true -}}
230 {{- else -}}
231 {{- end -}}
232 {{- end -}}
233
234 {{/*
235 Return the configmap with the MongoDB® configuration for the Arbiter
236 */}}
237 {{- define "mongodb.arbiter.configmapName" -}}
238 {{- if .Values.arbiter.existingConfigmap -}}
239     {{- printf "%s" (tpl .Values.arbiter.existingConfigmap $) -}}
240 {{- else -}}
241     {{- printf "%s-arbiter" (include "mongodb.fullname" .) -}}
242 {{- end -}}
243 {{- end -}}
244
245 {{/*
246 Return true if a configmap object should be created for MongoDB® Arbiter
247 */}}
248 {{- define "mongodb.arbiter.createConfigmap" -}}
249 {{- if and (eq .Values.architecture "replicaset") .Values.arbiter.enabled .Values.arbiter.configuration (not .Values.arbiter.existingConfigmap) }}
250     {{- true -}}
251 {{- else -}}
252 {{- end -}}
253 {{- end -}}
254
255 {{/*
256 Return true if the Hidden should be deployed
257 */}}
258 {{- define "mongodb.hidden.enabled" -}}
259 {{- if and (eq .Values.architecture "replicaset") .Values.hidden.enabled }}
260     {{- true -}}
261 {{- end -}}
262 {{- end -}}
263
264 {{/*
265 Return the configmap with the MongoDB® configuration for the Hidden
266 */}}
267 {{- define "mongodb.hidden.configmapName" -}}
268 {{- if .Values.hidden.existingConfigmap -}}
269     {{- printf "%s" (tpl .Values.hidden.existingConfigmap $) -}}
270 {{- else -}}
271     {{- printf "%s-hidden" (include "mongodb.fullname" .) -}}
272 {{- end -}}
273 {{- end -}}
274
275 {{/*
276 Return true if a configmap object should be created for MongoDB® Hidden
277 */}}
278 {{- define "mongodb.hidden.createConfigmap" -}}
279 {{- if and  (include "mongodb.hidden.enabled" .) .Values.hidden.enabled .Values.hidden.configuration (not .Values.hidden.existingConfigmap) }}
280     {{- true -}}
281 {{- end -}}
282 {{- end -}}
283
284 {{/*
285 Compile all warnings into a single message, and call fail.
286 */}}
287 {{- define "mongodb.validateValues" -}}
288 {{- $messages := list -}}
289 {{- $messages := append $messages (include "mongodb.validateValues.pspAndRBAC" .) -}}
290 {{- $messages := append $messages (include "mongodb.validateValues.architecture" .) -}}
291 {{- $messages := append $messages (include "mongodb.validateValues.customUsersDBs" .) -}}
292 {{- $messages := append $messages (include "mongodb.validateValues.customUsersDBsLength" .) -}}
293 {{- $messages := append $messages (include "mongodb.validateValues.externalAccessServiceType" .) -}}
294 {{- $messages := append $messages (include "mongodb.validateValues.loadBalancerIPsListLength" .) -}}
295 {{- $messages := append $messages (include "mongodb.validateValues.nodePortListLength" .) -}}
296 {{- $messages := append $messages (include "mongodb.validateValues.externalAccessAutoDiscoveryRBAC" .) -}}
297 {{- $messages := append $messages (include "mongodb.validateValues.replicaset.existingSecrets" .) -}}
298 {{- $messages := append $messages (include "mongodb.validateValues.hidden.existingSecrets" .) -}}
299 {{- $messages := without $messages "" -}}
300 {{- $message := join "\n" $messages -}}
301
302 {{- if $message -}}
303 {{-   printf "\nVALUES VALIDATION:\n%s" $message | fail -}}
304 {{- end -}}
305 {{- end -}}
306
307 {{/* Validate RBAC is created when using PSP */}}
308 {{- define "mongodb.validateValues.pspAndRBAC" -}}
309 {{- if and (.Values.podSecurityPolicy.create) (not .Values.rbac.create) -}}
310 mongodb: podSecurityPolicy.create, rbac.create
311     Both podSecurityPolicy.create and rbac.create must be true, if you want
312     to create podSecurityPolicy
313 {{- end -}}
314 {{- end -}}
315
316 {{/* Validate values of MongoDB® - must provide a valid architecture */}}
317 {{- define "mongodb.validateValues.architecture" -}}
318 {{- if and (ne .Values.architecture "standalone") (ne .Values.architecture "replicaset") -}}
319 mongodb: architecture
320     Invalid architecture selected. Valid values are "standalone" and
321     "replicaset". Please set a valid architecture (--set mongodb.architecture="xxxx")
322 {{- end -}}
323 {{- end -}}
324
325 {{/*
326 Validate values of MongoDB® - both auth.usernames and auth.databases are necessary
327 to create a custom user and database during 1st initialization
328 */}}
329 {{- define "mongodb.validateValues.customUsersDBs" -}}
330 {{- $customUsers := include "mongodb.customUsers" . -}}
331 {{- $customDatabases := include "mongodb.customDatabases" . -}}
332 {{- if or (and (empty $customUsers) (not (empty $customDatabases))) (and (not (empty $customUsers)) (empty $customDatabases)) }}
333 mongodb: auth.usernames, auth.databases
334     Both auth.usernames and auth.databases must be provided to create
335     custom users and databases during 1st initialization.
336     Please set both of them (--set auth.usernames[0]="xxxx",auth.databases[0]="yyyy")
337 {{- end -}}
338 {{- end -}}
339
340 {{/*
341 Validate values of MongoDB® - both auth.usernames and auth.databases arrays should have the same length
342 to create a custom user and database during 1st initialization
343 */}}
344 {{- define "mongodb.validateValues.customUsersDBsLength" -}}
345 {{- if ne (len .Values.auth.usernames) (len .Values.auth.databases) }}
346 mongodb: auth.usernames, auth.databases
347     Both auth.usernames and auth.databases arrays should have the same length
348 {{- end -}}
349 {{- end -}}
350
351 {{/*
352 Validate values of MongoDB® - service type for external access
353 */}}
354 {{- define "mongodb.validateValues.externalAccessServiceType" -}}
355 {{- if and (eq .Values.architecture "replicaset") (not (eq .Values.externalAccess.service.type "NodePort")) (not (eq .Values.externalAccess.service.type "LoadBalancer")) (not (eq .Values.externalAccess.service.type "ClusterIP")) -}}
356 mongodb: externalAccess.service.type
357     Available service type for external access are NodePort, LoadBalancer or ClusterIP.
358 {{- end -}}
359 {{- end -}}
360
361 {{/*
362 Validate values of MongoDB® - number of replicas must be the same than LoadBalancer IPs list
363 */}}
364 {{- define "mongodb.validateValues.loadBalancerIPsListLength" -}}
365 {{- $replicaCount := int .Values.replicaCount }}
366 {{- $loadBalancerListLength := len .Values.externalAccess.service.loadBalancerIPs }}
367 {{- if and (eq .Values.architecture "replicaset") .Values.externalAccess.enabled (not .Values.externalAccess.autoDiscovery.enabled ) (eq .Values.externalAccess.service.type "LoadBalancer") (not (eq $replicaCount $loadBalancerListLength )) -}}
368 mongodb: .Values.externalAccess.service.loadBalancerIPs
369     Number of replicas and loadBalancerIPs array length must be the same.
370 {{- end -}}
371 {{- end -}}
372
373 {{/*
374 Validate values of MongoDB® - number of replicas must be the same than NodePort list
375 */}}
376 {{- define "mongodb.validateValues.nodePortListLength" -}}
377 {{- $replicaCount := int .Values.replicaCount }}
378 {{- $nodePortListLength := len .Values.externalAccess.service.nodePorts }}
379 {{- if and (eq .Values.architecture "replicaset") .Values.externalAccess.enabled (eq .Values.externalAccess.service.type "NodePort") (not (eq $replicaCount $nodePortListLength )) -}}
380 mongodb: .Values.externalAccess.service.nodePorts
381     Number of replicas and nodePorts array length must be the same.
382 {{- end -}}
383 {{- end -}}
384
385 {{/*
386 Validate values of MongoDB® - RBAC should be enabled when autoDiscovery is enabled
387 */}}
388 {{- define "mongodb.validateValues.externalAccessAutoDiscoveryRBAC" -}}
389 {{- if and (eq .Values.architecture "replicaset") .Values.externalAccess.enabled .Values.externalAccess.autoDiscovery.enabled (not .Values.rbac.create ) }}
390 mongodb: rbac.create
391     By specifying "externalAccess.enabled=true" and "externalAccess.autoDiscovery.enabled=true"
392     an initContainer will be used to autodetect the external IPs/ports by querying the
393     K8s API. Please note this initContainer requires specific RBAC resources. You can create them
394     by specifying "--set rbac.create=true".
395 {{- end -}}
396 {{- end -}}
397
398 {{/*
399 Validate values of MongoDB® - Number of replicaset secrets must be the same than number of replicaset nodes.
400 */}}
401 {{- define "mongodb.validateValues.replicaset.existingSecrets" -}}
402 {{- if and .Values.tls.enabled (eq .Values.architecture "replicaset") (not (empty .Values.tls.replicaset.existingSecrets)) }}
403 {{- $nbSecrets := len .Values.tls.replicaset.existingSecrets -}}
404 {{- if not (eq $nbSecrets (int .Values.replicaCount)) }}
405 mongodb: tls.replicaset.existingSecrets
406     tls.replicaset.existingSecrets Number of secrets and number of replicaset nodes must be the same.
407 {{- end -}}
408 {{- end -}}
409 {{- end -}}
410
411 {{/*
412 Validate values of MongoDB® - Number of hidden secrets must be the same than number of hidden nodes.
413 */}}
414 {{- define "mongodb.validateValues.hidden.existingSecrets" -}}
415 {{- if and .Values.tls.enabled (include "mongodb.hidden.enabled" .) (not (empty .Values.tls.hidden.existingSecrets)) }}
416 {{- $nbSecrets := len .Values.tls.hidden.existingSecrets -}}
417 {{- if not (eq $nbSecrets (int .Values.hidden.replicaCount)) }}
418 mongodb: tls.hidden.existingSecrets
419     tls.hidden.existingSecrets Number of secrets and number of hidden nodes must be the same.
420 {{- end -}}
421 {{- end -}}
422 {{- end -}}
423
424 {{/*
425 Validate values of MongoDB® exporter URI string - auth.enabled and/or tls.enabled must be enabled or it defaults
426 */}}
427 {{- define "mongodb.mongodb_exporter.uri" -}}
428     {{- $tlsEnabled := .Values.tls.enabled -}}
429     {{- $mTlsEnabled := and $tlsEnabled .Values.tls.mTLS.enabled -}}
430     {{- $tlsArgs := "" -}}
431     {{- if $tlsEnabled -}}
432         {{- $tlsCertKeyFile := ternary "&tlsCertificateKeyFile=/certs/mongodb.pem" "" $mTlsEnabled -}}
433         {{- $tlsArgs = printf "tls=true%s&tlsCAFile=/certs/mongodb-ca-cert" $tlsCertKeyFile -}}
434     {{- end -}}
435     {{- if .Values.metrics.username -}}
436         {{- $uriAuth := ternary "$(echo $MONGODB_METRICS_USERNAME | sed -r \"s/@/%40/g;s/:/%3A/g\"):$(echo $MONGODB_METRICS_PASSWORD | sed -r \"s/@/%40/g;s/:/%3A/g\")@" "" .Values.auth.enabled -}}
437         {{- printf "mongodb://%slocalhost:%d/admin?%s" $uriAuth (int .Values.containerPorts.mongodb) $tlsArgs -}}
438     {{- else -}}
439         {{- $uriAuth := ternary "$MONGODB_ROOT_USER:$(echo $MONGODB_ROOT_PASSWORD | sed -r \"s/@/%40/g;s/:/%3A/g\")@" "" .Values.auth.enabled -}}
440         {{- printf "mongodb://%slocalhost:%d/admin?%s" $uriAuth (int .Values.containerPorts.mongodb) $tlsArgs -}}
441     {{- end -}}
442 {{- end -}}
443
444 {{/*
445 Return the appropriate apiGroup for PodSecurityPolicy.
446 */}}
447 {{- define "podSecurityPolicy.apiGroup" -}}
448 {{- if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}}
449 {{- print "policy" -}}
450 {{- else -}}
451 {{- print "extensions" -}}
452 {{- end -}}
453 {{- end -}}
454
455 {{/*
456 Return true if a TLS secret object should be created
457 */}}
458 {{- define "mongodb.createTlsSecret" -}}
459 {{- if and .Values.tls.enabled (not .Values.tls.existingSecret) (include "mongodb.autoGenerateCerts" .) }}
460     {{- true -}}
461 {{- end -}}
462 {{- end -}}
463
464 {{/*
465 Return the secret containing MongoDB® TLS certificates
466 */}}
467 {{- define "mongodb.tlsSecretName" -}}
468 {{- $secretName := .Values.tls.existingSecret -}}
469 {{- if $secretName -}}
470     {{- printf "%s" (tpl $secretName $) -}}
471 {{- else -}}
472     {{- printf "%s-ca" (include "mongodb.fullname" .) -}}
473 {{- end -}}
474 {{- end -}}
475
476 {{/*
477 Return true if certificates must be auto generated
478 */}}
479 {{- define "mongodb.autoGenerateCerts" -}}
480 {{- $standalone := (eq .Values.architecture "standalone") | ternary (not .Values.tls.standalone.existingSecret) true -}}
481 {{- $replicaset := (eq .Values.architecture "replicaset") | ternary (empty .Values.tls.replicaset.existingSecrets) true -}}
482 {{- $arbiter := (eq (include "mongodb.arbiter.enabled" .) "true") | ternary (not .Values.tls.arbiter.existingSecret) true -}}
483 {{- $hidden := (eq (include "mongodb.hidden.enabled" .) "true") | ternary (empty .Values.tls.hidden.existingSecrets) true -}}
484 {{- if and $standalone $replicaset $arbiter $hidden -}}
485     {{- true -}}
486 {{- end -}}
487 {{- end -}}
488
489 {{/*
490 Generate argument list for mongodb-exporter
491 reference: https://github.com/percona/mongodb_exporter/blob/main/REFERENCE.md
492 */}}
493 {{- define "mongodb.exporterArgs" -}}
494 {{- with .Values.metrics.collector -}}
495 {{- ternary " --collect-all" "" .all -}}
496 {{- ternary " --collector.diagnosticdata" "" .diagnosticdata -}}
497 {{- ternary " --collector.replicasetstatus" "" .replicasetstatus -}}
498 {{- ternary " --collector.dbstats" "" .dbstats -}}
499 {{- ternary " --collector.topmetrics" "" .topmetrics -}}
500 {{- ternary " --collector.indexstats" "" .indexstats -}}
501 {{- ternary " --collector.collstats" "" .collstats -}}
502 {{- if .collstatsColls -}}
503 {{- " --mongodb.collstats-colls=" -}}
504 {{- join "," .collstatsColls -}}
505 {{- end -}}
506 {{- if .indexstatsColls -}}
507 {{- " --mongodb.indexstats-colls=" -}}
508 {{- join "," .indexstatsColls -}}
509 {{- end -}}
510 {{- $limitArg := print " --collector.collstats-limit=" .collstatsLimit -}}
511 {{- ne (print .collstatsLimit) "0" | ternary $limitArg "" -}}
512 {{- end -}}
513 {{- ternary " --compatible-mode" "" .Values.metrics.compatibleMode -}}
514 {{- end -}}