blob: 37ad98aa1d84ea3914d5e0b3a630e083044943c9 [file] [log] [blame]
gio07eb1082024-10-25 14:35:56 +04001{{/*
2Copyright Broadcom, Inc. All Rights Reserved.
3SPDX-License-Identifier: APACHE-2.0
4*/}}
5
6{{/* vim: set filetype=mustache: */}}
7{{/*
8Expand the name of the chart.
9*/}}
10{{- define "mongodb.name" -}}
11{{- include "common.names.name" . -}}
12{{- end -}}
13
14{{/*
15Create a default fully qualified app name.
16We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
17If 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{{/*
24Create 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{{/*
39Create 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{{/*
50Return 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{{/*
57Return 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{{/*
64Return 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{{/*
71Return 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{{/*
78Return the proper image name (for the init container dns-check image)
79*/}}
80{{- define "mongodb.externalAccess.dnsCheck.image" -}}
81{{- include "common.images.image" (dict "imageRoot" .Values.externalAccess.dnsCheck.image "global" .Values.global) -}}
82{{- end -}}
83
84{{/*
85Return the proper image name (for the TLS Certs image)
86*/}}
87{{- define "mongodb.tls.image" -}}
88{{- include "common.images.image" (dict "imageRoot" .Values.tls.image "global" .Values.global) -}}
89{{- end -}}
90
91{{/*
92Return the proper Docker Image Registry Secret Names
93*/}}
94{{- define "mongodb.imagePullSecrets" -}}
95{{- include "common.images.renderPullSecrets" (dict "images" (list .Values.image .Values.metrics.image .Values.volumePermissions.image .Values.tls.image .Values.externalAccess.dnsCheck.image .Values.externalAccess.autoDiscovery.image) "context" $) -}}
96{{- end -}}
97
98{{/*
99Allow the release namespace to be overridden for multi-namespace deployments in combined charts.
100*/}}
101{{- define "mongodb.namespace" -}}
102 {{- if and .Values.global .Values.global.namespaceOverride -}}
103 {{- print .Values.global.namespaceOverride -}}
104 {{- else -}}
105 {{- print .Release.Namespace -}}
106 {{- end }}
107{{- end -}}
108{{- define "mongodb.serviceMonitor.namespace" -}}
109 {{- if .Values.metrics.serviceMonitor.namespace -}}
110 {{- print .Values.metrics.serviceMonitor.namespace -}}
111 {{- else -}}
112 {{- include "mongodb.namespace" . -}}
113 {{- end }}
114{{- end -}}
115{{- define "mongodb.prometheusRule.namespace" -}}
116 {{- if .Values.metrics.prometheusRule.namespace -}}
117 {{- print .Values.metrics.prometheusRule.namespace -}}
118 {{- else -}}
119 {{- include "mongodb.namespace" . -}}
120 {{- end }}
121{{- end -}}
122
123{{/*
124Returns the proper service account name depending if an explicit service account name is set
125in the values file. If the name is not set it will default to either mongodb.fullname if serviceAccount.create
126is true or default otherwise.
127*/}}
128{{- define "mongodb.serviceAccountName" -}}
129 {{- if .Values.serviceAccount.create -}}
130 {{- default (include "mongodb.fullname" .) (print .Values.serviceAccount.name) -}}
131 {{- else -}}
132 {{- default "default" (print .Values.serviceAccount.name) -}}
133 {{- end -}}
134{{- end -}}
135
136{{/*
137Return the list of custom users to create during the initialization (string format)
138*/}}
139{{- define "mongodb.customUsers" -}}
140 {{- $customUsers := list -}}
141 {{- if .Values.auth.username -}}
142 {{- $customUsers = append $customUsers .Values.auth.username }}
143 {{- end }}
144 {{- range .Values.auth.usernames }}
145 {{- $customUsers = append $customUsers . }}
146 {{- end }}
147 {{- printf "%s" (default "" (join "," $customUsers)) -}}
148{{- end -}}
149
150{{/*
151Return the list of passwords for the custom users (string format)
152*/}}
153{{- define "mongodb.customPasswords" -}}
154 {{- $customPasswords := list -}}
155 {{- if .Values.auth.password -}}
156 {{- $customPasswords = append $customPasswords .Values.auth.password }}
157 {{- end }}
158 {{- range .Values.auth.passwords }}
159 {{- $customPasswords = append $customPasswords . }}
160 {{- end }}
161 {{- printf "%s" (default "" (join "," $customPasswords)) -}}
162{{- end -}}
163
164{{/*
165Return the list of custom databases to create during the initialization (string format)
166*/}}
167{{- define "mongodb.customDatabases" -}}
168 {{- $customDatabases := list -}}
169 {{- if .Values.auth.database -}}
170 {{- $customDatabases = append $customDatabases .Values.auth.database }}
171 {{- end }}
172 {{- range .Values.auth.databases }}
173 {{- $customDatabases = append $customDatabases . }}
174 {{- end }}
175 {{- printf "%s" (default "" (join "," $customDatabases)) -}}
176{{- end -}}
177
178{{/*
179Return the configmap with the MongoDB® configuration
180*/}}
181{{- define "mongodb.configmapName" -}}
182{{- if .Values.existingConfigmap -}}
183 {{- printf "%s" (tpl .Values.existingConfigmap $) -}}
184{{- else -}}
185 {{- printf "%s" (include "mongodb.fullname" .) -}}
186{{- end -}}
187{{- end -}}
188
189{{/*
190Return true if a configmap object should be created for MongoDB®
191*/}}
192{{- define "mongodb.createConfigmap" -}}
193{{- if and .Values.configuration (not .Values.existingConfigmap) }}
194 {{- true -}}
195{{- else -}}
196{{- end -}}
197{{- end -}}
198
199{{/*
200Return the secret with MongoDB® credentials
201*/}}
202{{- define "mongodb.secretName" -}}
203 {{- if .Values.auth.existingSecret -}}
204 {{- printf "%s" (tpl .Values.auth.existingSecret $) -}}
205 {{- else -}}
206 {{- printf "%s" (include "mongodb.fullname" .) -}}
207 {{- end -}}
208{{- end -}}
209
210{{/*
211Return true if a secret object should be created for MongoDB®
212*/}}
213{{- define "mongodb.createSecret" -}}
214{{- if and .Values.auth.enabled (not .Values.auth.existingSecret) }}
215 {{- true -}}
216{{- else -}}
217{{- end -}}
218{{- end -}}
219
220{{/*
221Get the initialization scripts ConfigMap name.
222*/}}
223{{- define "mongodb.initdbScriptsCM" -}}
224{{- if .Values.initdbScriptsConfigMap -}}
225{{- printf "%s" (tpl .Values.initdbScriptsConfigMap $) -}}
226{{- else -}}
227{{- printf "%s-init-scripts" (include "mongodb.fullname" .) -}}
228{{- end -}}
229{{- end -}}
230
231{{/*
232Get initial primary host to configure MongoDB cluster.
233*/}}
234{{- define "mongodb.initialPrimaryHost" -}}
235{{ ternary ( printf "%s-0.$(K8S_SERVICE_NAME).$(MY_POD_NAMESPACE).svc.%s" (include "mongodb.fullname" .) .Values.clusterDomain ) ( first .Values.externalAccess.service.publicNames ) ( empty .Values.externalAccess.service.publicNames ) }}
236{{- end -}}
237
238{{/*
239Init container definition to change/establish volume permissions.
240*/}}
241{{- define "mongodb.initContainer.volumePermissions" }}
242- name: volume-permissions
243 image: {{ include "mongodb.volumePermissions.image" . }}
244 imagePullPolicy: {{ .Values.volumePermissions.image.pullPolicy | quote }}
245 command:
246 - /bin/bash
247 args:
248 - -ec
249 - |
250 mkdir -p {{ printf "%s/%s" .Values.persistence.mountPath (default "" .Values.persistence.subPath) }}
251 chown {{ .Values.containerSecurityContext.runAsUser }}:{{ .Values.podSecurityContext.fsGroup }} {{ printf "%s/%s" .Values.persistence.mountPath (default "" .Values.persistence.subPath) }}
252 find {{ printf "%s/%s" .Values.persistence.mountPath (default "" .Values.persistence.subPath) }} -mindepth 1 -maxdepth 1 -not -name ".snapshot" -not -name "lost+found" | xargs -r chown -R {{ .Values.containerSecurityContext.runAsUser }}:{{ .Values.podSecurityContext.fsGroup }}
253 {{- if eq ( toString ( .Values.volumePermissions.securityContext.runAsUser )) "auto" }}
254 securityContext: {{- omit .Values.volumePermissions.securityContext "runAsUser" | toYaml | nindent 12 }}
255 {{- else }}
256 securityContext: {{- .Values.volumePermissions.securityContext | toYaml | nindent 12 }}
257 {{- end }}
258 {{- if .Values.volumePermissions.resources }}
259 resources: {{- toYaml .Values.volumePermissions.resources | nindent 12 }}
260 {{- else if ne .Values.volumePermissions.resourcesPreset "none" }}
261 resources: {{- include "common.resources.preset" (dict "type" .Values.volumePermissions.resourcesPreset) | nindent 12 }}
262 {{- end }}
263 volumeMounts:
264 - name: empty-dir
265 mountPath: /tmp
266 subPath: tmp-dir
267 - name: {{ .Values.persistence.name | default "datadir" }}
268 mountPath: {{ .Values.persistence.mountPath }}
269{{- end -}}
270
271{{/*
272Init container definition to recover log dir.
273*/}}
274{{- define "mongodb.initContainer.prepareLogDir" }}
275- name: log-dir
276 image: {{ include "mongodb.image" . }}
277 imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
278 command:
279 - /bin/bash
280 args:
281 - -ec
282 - |
283 ln -sf /dev/stdout "/opt/bitnami/mongodb/logs/mongodb.log"
284 {{- if .Values.containerSecurityContext.enabled }}
285 securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.containerSecurityContext "context" $) | nindent 12 }}
286 {{- end }}
287 {{- if .Values.resources }}
288 resources: {{- toYaml .Values.resources | nindent 12 }}
289 {{- else if ne .Values.resourcesPreset "none" }}
290 resources: {{- include "common.resources.preset" (dict "type" .Values.resourcesPreset) | nindent 12 }}
291 {{- end }}
292 volumeMounts:
293 - name: empty-dir
294 mountPath: /opt/bitnami/mongodb/logs
295 subPath: app-logs-dir
296{{- end -}}
297
298{{/*
299Init container definition to get external IP addresses.
300*/}}
301{{- define "mongodb.initContainers.autoDiscovery" -}}
302- name: auto-discovery
303 image: {{ include "mongodb.externalAccess.autoDiscovery.image" . }}
304 imagePullPolicy: {{ .Values.externalAccess.autoDiscovery.image.pullPolicy | quote }}
305 # We need the service account token for contacting the k8s API
306 command:
307 - /scripts/auto-discovery.sh
308 env:
309 - name: MY_POD_NAME
310 valueFrom:
311 fieldRef:
312 fieldPath: metadata.name
313 - name: SHARED_FILE
314 value: "/shared/info.txt"
315 {{- if .Values.externalAccess.autoDiscovery.resources }}
316 resources: {{- toYaml .Values.externalAccess.autoDiscovery.resources | nindent 12 }}
317 {{- else if ne .Values.externalAccess.autoDiscovery.resourcesPreset "none" }}
318 resources: {{- include "common.resources.preset" (dict "type" .Values.externalAccess.autoDiscovery.resourcesPreset) | nindent 12 }}
319 {{- end }}
320 volumeMounts:
321 - name: shared
322 mountPath: /shared
323 - name: scripts
324 mountPath: /scripts/auto-discovery.sh
325 subPath: auto-discovery.sh
326 - name: empty-dir
327 mountPath: /tmp
328 subPath: tmp-dir
329{{- end -}}
330
331{{/*
332Init container definition to wait external DNS names.
333*/}}
334{{- define "mongodb.initContainers.dnsCheck" -}}
335- name: dns-check
336 image: {{ include "mongodb.externalAccess.dnsCheck.image" . }}
337 imagePullPolicy: {{ .Values.externalAccess.dnsCheck.image.pullPolicy | quote }}
338 command:
339 - /bin/bash
340 args:
341 - -ec
342 - |
343 # MONGODB_INITIAL_PRIMARY_HOST should be resolvable
344 while ! (getent ahosts "{{ include "mongodb.initialPrimaryHost" . }}" | grep STREAM); do
345 sleep 10
346 done
347 {{- if .Values.containerSecurityContext.enabled }}
348 securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.containerSecurityContext "context" $) | nindent 12 }}
349 {{- end }}
350 {{- if .Values.externalAccess.dnsCheck.resources }}
351 resources: {{- toYaml .Values.externalAccess.dnsCheck.resources | nindent 12 }}
352 {{- else if ne .Values.externalAccess.dnsCheck.resourcesPreset "none" }}
353 resources: {{- include "common.resources.preset" (dict "type" .Values.externalAccess.dnsCheck.resourcesPreset) | nindent 12 }}
354 {{- end }}
355{{- end -}}
356
357{{/*
358Return true if the Arbiter should be deployed
359*/}}
360{{- define "mongodb.arbiter.enabled" -}}
361{{- if and (eq .Values.architecture "replicaset") .Values.arbiter.enabled }}
362 {{- true -}}
363{{- else -}}
364{{- end -}}
365{{- end -}}
366
367{{/*
368Return the configmap with the MongoDB® configuration for the Arbiter
369*/}}
370{{- define "mongodb.arbiter.configmapName" -}}
371{{- if .Values.arbiter.existingConfigmap -}}
372 {{- printf "%s" (tpl .Values.arbiter.existingConfigmap $) -}}
373{{- else -}}
374 {{- printf "%s-arbiter" (include "mongodb.fullname" .) -}}
375{{- end -}}
376{{- end -}}
377
378{{/*
379Return true if a configmap object should be created for MongoDB® Arbiter
380*/}}
381{{- define "mongodb.arbiter.createConfigmap" -}}
382{{- if and (eq .Values.architecture "replicaset") .Values.arbiter.enabled .Values.arbiter.configuration (not .Values.arbiter.existingConfigmap) }}
383 {{- true -}}
384{{- else -}}
385{{- end -}}
386{{- end -}}
387
388{{/*
389Return true if the Hidden should be deployed
390*/}}
391{{- define "mongodb.hidden.enabled" -}}
392{{- if and (eq .Values.architecture "replicaset") .Values.hidden.enabled }}
393 {{- true -}}
394{{- end -}}
395{{- end -}}
396
397{{/*
398Return the configmap with the MongoDB® configuration for the Hidden
399*/}}
400{{- define "mongodb.hidden.configmapName" -}}
401{{- if .Values.hidden.existingConfigmap -}}
402 {{- printf "%s" (tpl .Values.hidden.existingConfigmap $) -}}
403{{- else -}}
404 {{- printf "%s-hidden" (include "mongodb.fullname" .) -}}
405{{- end -}}
406{{- end -}}
407
408{{/*
409Return true if a configmap object should be created for MongoDB® Hidden
410*/}}
411{{- define "mongodb.hidden.createConfigmap" -}}
412{{- if and (include "mongodb.hidden.enabled" .) .Values.hidden.enabled .Values.hidden.configuration (not .Values.hidden.existingConfigmap) }}
413 {{- true -}}
414{{- end -}}
415{{- end -}}
416
417{{/*
418Compile all warnings into a single message, and call fail.
419*/}}
420{{- define "mongodb.validateValues" -}}
421{{- $messages := list -}}
422{{- $messages := append $messages (include "mongodb.validateValues.pspAndRBAC" .) -}}
423{{- $messages := append $messages (include "mongodb.validateValues.architecture" .) -}}
424{{- $messages := append $messages (include "mongodb.validateValues.customUsersDBs" .) -}}
425{{- $messages := append $messages (include "mongodb.validateValues.customUsersDBsLength" .) -}}
426{{- $messages := append $messages (include "mongodb.validateValues.externalAccessServiceType" .) -}}
427{{- $messages := append $messages (include "mongodb.validateValues.loadBalancerIPsListLength" .) -}}
428{{- $messages := append $messages (include "mongodb.validateValues.nodePortListLength" .) -}}
429{{- $messages := append $messages (include "mongodb.validateValues.externalAccessAutoDiscoveryRBAC" .) -}}
430{{- $messages := append $messages (include "mongodb.validateValues.externalAccessAutoDiscoverySA" .) -}}
431{{- $messages := append $messages (include "mongodb.validateValues.replicaset.existingSecrets" .) -}}
432{{- $messages := append $messages (include "mongodb.validateValues.hidden.existingSecrets" .) -}}
433{{- $messages := without $messages "" -}}
434{{- $message := join "\n" $messages -}}
435
436{{- if $message -}}
437{{- printf "\nVALUES VALIDATION:\n%s" $message | fail -}}
438{{- end -}}
439{{- end -}}
440
441{{/* Validate RBAC is created when using PSP */}}
442{{- define "mongodb.validateValues.pspAndRBAC" -}}
443{{- if and (.Values.podSecurityPolicy.create) (not .Values.rbac.create) -}}
444mongodb: podSecurityPolicy.create, rbac.create
445 Both podSecurityPolicy.create and rbac.create must be true, if you want
446 to create podSecurityPolicy
447{{- end -}}
448{{- end -}}
449
450{{/* Validate values of MongoDB® - must provide a valid architecture */}}
451{{- define "mongodb.validateValues.architecture" -}}
452{{- if and (ne .Values.architecture "standalone") (ne .Values.architecture "replicaset") -}}
453mongodb: architecture
454 Invalid architecture selected. Valid values are "standalone" and
455 "replicaset". Please set a valid architecture (--set mongodb.architecture="xxxx")
456{{- end -}}
457{{- end -}}
458
459{{/*
460Validate values of MongoDB® - both auth.usernames and auth.databases are necessary
461to create a custom user and database during 1st initialization
462*/}}
463{{- define "mongodb.validateValues.customUsersDBs" -}}
464{{- $customUsers := include "mongodb.customUsers" . -}}
465{{- $customDatabases := include "mongodb.customDatabases" . -}}
466{{- if or (and (empty $customUsers) (not (empty $customDatabases))) (and (not (empty $customUsers)) (empty $customDatabases)) }}
467mongodb: auth.usernames, auth.databases
468 Both auth.usernames and auth.databases must be provided to create
469 custom users and databases during 1st initialization.
470 Please set both of them (--set auth.usernames[0]="xxxx",auth.databases[0]="yyyy")
471{{- end -}}
472{{- end -}}
473
474{{/*
475Validate values of MongoDB® - both auth.usernames and auth.databases arrays should have the same length
476to create a custom user and database during 1st initialization
477*/}}
478{{- define "mongodb.validateValues.customUsersDBsLength" -}}
479{{- if ne (len .Values.auth.usernames) (len .Values.auth.databases) }}
480mongodb: auth.usernames, auth.databases
481 Both auth.usernames and auth.databases arrays should have the same length
482{{- end -}}
483{{- end -}}
484
485{{/*
486Validate values of MongoDB® - service type for external access
487*/}}
488{{- define "mongodb.validateValues.externalAccessServiceType" -}}
489{{- 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")) -}}
490mongodb: externalAccess.service.type
491 Available service type for external access are NodePort, LoadBalancer or ClusterIP.
492{{- end -}}
493{{- end -}}
494
495{{/*
496Validate values of MongoDB® - number of replicas must be the same than LoadBalancer IPs list
497*/}}
498{{- define "mongodb.validateValues.loadBalancerIPsListLength" -}}
499{{- $replicaCount := int .Values.replicaCount }}
500{{- $loadBalancerListLength := len .Values.externalAccess.service.loadBalancerIPs }}
501{{- $publicNamesListLength := len .Values.externalAccess.service.publicNames }}
502{{- if and (eq .Values.architecture "replicaset") .Values.externalAccess.enabled (eq .Values.externalAccess.service.type "LoadBalancer") -}}
503{{- if and (not .Values.externalAccess.autoDiscovery.enabled) (eq $loadBalancerListLength 0) (eq $publicNamesListLength 0) -}}
504mongodb: .Values.externalAccess.service.loadBalancerIPs, .Values.externalAccess.service.publicNames
505 externalAccess.service.loadBalancerIPs, externalAccess.service.publicNames or externalAccess.autoDiscovery.enabled are required when externalAccess is enabled.
506{{- else if and (not .Values.externalAccess.autoDiscovery.enabled) (not (eq $replicaCount $loadBalancerListLength )) (not (eq $loadBalancerListLength 0)) -}}
507mongodb: .Values.externalAccess.service.loadBalancerIPs
508 Number of replicas ({{ $replicaCount }}) and loadBalancerIPs array length ({{ $loadBalancerListLength }}) must be the same.
509{{- else if and (not .Values.externalAccess.autoDiscovery.enabled) (not (eq $replicaCount $publicNamesListLength )) (not (eq $publicNamesListLength 0)) -}}
510mongodb: .Values.externalAccess.service.publicNames
511 Number of replicas ({{ $replicaCount }}) and publicNames array length ({{ $publicNamesListLength }}) must be the same.
512{{- end -}}
513{{- end -}}
514{{- end -}}
515
516{{/*
517Validate values of MongoDB® - number of replicas must be the same than NodePort list
518*/}}
519{{- define "mongodb.validateValues.nodePortListLength" -}}
520{{- $replicaCount := int .Values.replicaCount }}
521{{- $nodePortListLength := len .Values.externalAccess.service.nodePorts }}
522{{- if and (eq .Values.architecture "replicaset") .Values.externalAccess.enabled (eq .Values.externalAccess.service.type "NodePort") -}}
523{{- if and (not .Values.externalAccess.autoDiscovery.enabled) (eq $nodePortListLength 0) -}}
524mongodb: .Values.externalAccess.service.nodePorts
525 externalAccess.service.nodePorts or externalAccess.autoDiscovery.enabled are required when externalAccess is enabled.
526{{- else if and (not .Values.externalAccess.autoDiscovery.enabled) (not (eq $replicaCount $nodePortListLength )) -}}
527mongodb: .Values.externalAccess.service.nodePorts
528 Number of replicas ({{ $replicaCount }}) and nodePorts ({{ $nodePortListLength }}) array length must be the same.
529{{- end -}}
530{{- end -}}
531{{- end -}}
532
533{{/*
534Validate values of MongoDB® - RBAC should be enabled when autoDiscovery is enabled
535*/}}
536{{- define "mongodb.validateValues.externalAccessAutoDiscoveryRBAC" -}}
537{{- if and (eq .Values.architecture "replicaset") .Values.externalAccess.enabled .Values.externalAccess.autoDiscovery.enabled (not .Values.rbac.create ) }}
538mongodb: rbac.create
539 By specifying "externalAccess.enabled=true" and "externalAccess.autoDiscovery.enabled=true"
540 an initContainer will be used to autodetect the external IPs/ports by querying the
541 K8s API. Please note this initContainer requires specific RBAC resources. You can create them
542 by specifying "--set rbac.create=true".
543{{- end -}}
544{{- end -}}
545
546{{/*
547Validate values of MongoDB® - automountServiceAccountToken should be enabled when autoDiscovery is enabled
548*/}}
549{{- define "mongodb.validateValues.externalAccessAutoDiscoverySA" -}}
550{{- if and (eq .Values.architecture "replicaset") .Values.externalAccess.enabled .Values.externalAccess.autoDiscovery.enabled (not .Values.automountServiceAccountToken ) }}
551mongodb: automountServiceAccountToken
552 By specifying "externalAccess.enabled=true" and "externalAccess.autoDiscovery.enabled=true"
553 an initContainer will be used to autodetect the external IPs/ports by querying the
554 K8s API. Please note this initContainer requires a service account to access K8S API.
555 You can attach it to the pod by specifying "--set automountServiceAccountToken=true".
556{{- end -}}
557{{- end -}}
558
559{{/*
560Validate values of MongoDB® - Number of replicaset secrets must be the same than number of replicaset nodes.
561*/}}
562{{- define "mongodb.validateValues.replicaset.existingSecrets" -}}
563{{- if and .Values.tls.enabled (eq .Values.architecture "replicaset") (not (empty .Values.tls.replicaset.existingSecrets)) }}
564{{- $nbSecrets := len .Values.tls.replicaset.existingSecrets -}}
565{{- if not (eq $nbSecrets (int .Values.replicaCount)) }}
566mongodb: tls.replicaset.existingSecrets
567 tls.replicaset.existingSecrets Number of secrets and number of replicaset nodes must be the same.
568{{- end -}}
569{{- end -}}
570{{- end -}}
571
572{{/*
573Validate values of MongoDB® - Number of hidden secrets must be the same than number of hidden nodes.
574*/}}
575{{- define "mongodb.validateValues.hidden.existingSecrets" -}}
576{{- if and .Values.tls.enabled (include "mongodb.hidden.enabled" .) (not (empty .Values.tls.hidden.existingSecrets)) }}
577{{- $nbSecrets := len .Values.tls.hidden.existingSecrets -}}
578{{- if not (eq $nbSecrets (int .Values.hidden.replicaCount)) }}
579mongodb: tls.hidden.existingSecrets
580 tls.hidden.existingSecrets Number of secrets and number of hidden nodes must be the same.
581{{- end -}}
582{{- end -}}
583{{- end -}}
584
585{{/*
586Validate values of MongoDB® exporter URI string - auth.enabled and/or tls.enabled must be enabled or it defaults
587*/}}
588{{- define "mongodb.mongodb_exporter.uri" -}}
589 {{- $tlsEnabled := .Values.tls.enabled -}}
590 {{- $mTlsEnabled := and $tlsEnabled .Values.tls.mTLS.enabled -}}
591 {{- $tlsArgs := "" -}}
592 {{- if $tlsEnabled -}}
593 {{- $tlsCertKeyFile := ternary "&tlsCertificateKeyFile=/certs/mongodb.pem" "" $mTlsEnabled -}}
594 {{- $tlsArgs = printf "tls=true%s&tlsCAFile=/certs/mongodb-ca-cert" $tlsCertKeyFile -}}
595 {{- end -}}
596 {{- if .Values.metrics.username -}}
597 {{- $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 -}}
598 {{- printf "mongodb://%slocalhost:%d/admin?%s" $uriAuth (int .Values.containerPorts.mongodb) $tlsArgs -}}
599 {{- else -}}
600 {{- $uriAuth := ternary "$MONGODB_ROOT_USER:$(echo $MONGODB_ROOT_PASSWORD | sed -r \"s/@/%40/g;s/:/%3A/g\")@" "" .Values.auth.enabled -}}
601 {{- printf "mongodb://%slocalhost:%d/admin?%s" $uriAuth (int .Values.containerPorts.mongodb) $tlsArgs -}}
602 {{- end -}}
603{{- end -}}
604
605{{/*
606Return the appropriate apiGroup for PodSecurityPolicy.
607*/}}
608{{- define "podSecurityPolicy.apiGroup" -}}
609{{- if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}}
610{{- print "policy" -}}
611{{- else -}}
612{{- print "extensions" -}}
613{{- end -}}
614{{- end -}}
615
616{{/*
617Return true if a TLS secret object should be created
618*/}}
619{{- define "mongodb.createTlsSecret" -}}
620{{- if and .Values.tls.enabled (not .Values.tls.existingSecret) (include "mongodb.autoGenerateCerts" .) }}
621 {{- true -}}
622{{- end -}}
623{{- end -}}
624
625{{/*
626Return the secret containing MongoDB® TLS certificates
627*/}}
628{{- define "mongodb.tlsSecretName" -}}
629{{- $secretName := .Values.tls.existingSecret -}}
630{{- if $secretName -}}
631 {{- printf "%s" (tpl $secretName $) -}}
632{{- else -}}
633 {{- printf "%s-ca" (include "mongodb.fullname" .) -}}
634{{- end -}}
635{{- end -}}
636
637{{/*
638Return true if certificates must be auto generated
639*/}}
640{{- define "mongodb.autoGenerateCerts" -}}
641{{- $standalone := (eq .Values.architecture "standalone") | ternary (not .Values.tls.standalone.existingSecret) true -}}
642{{- $replicaset := (eq .Values.architecture "replicaset") | ternary (empty .Values.tls.replicaset.existingSecrets) true -}}
643{{- $arbiter := (eq (include "mongodb.arbiter.enabled" .) "true") | ternary (not .Values.tls.arbiter.existingSecret) true -}}
644{{- $hidden := (eq (include "mongodb.hidden.enabled" .) "true") | ternary (empty .Values.tls.hidden.existingSecrets) true -}}
645{{- if and $standalone $replicaset $arbiter $hidden -}}
646 {{- true -}}
647{{- end -}}
648{{- end -}}
649
650{{/*
651Generate argument list for mongodb-exporter
652reference: https://github.com/percona/mongodb_exporter/blob/main/REFERENCE.md
653*/}}
654{{- define "mongodb.exporterArgs" -}}
655{{- with .Values.metrics.collector -}}
656{{- ternary " --collect-all" "" .all -}}
657{{- ternary " --collector.diagnosticdata" "" .diagnosticdata -}}
658{{- ternary " --collector.replicasetstatus" "" .replicasetstatus -}}
659{{- ternary " --collector.dbstats" "" .dbstats -}}
660{{- ternary " --collector.topmetrics" "" .topmetrics -}}
661{{- ternary " --collector.indexstats" "" .indexstats -}}
662{{- ternary " --collector.collstats" "" .collstats -}}
663{{- if .collstatsColls -}}
664{{- " --mongodb.collstats-colls=" -}}
665{{- join "," .collstatsColls -}}
666{{- end -}}
667{{- if .indexstatsColls -}}
668{{- " --mongodb.indexstats-colls=" -}}
669{{- join "," .indexstatsColls -}}
670{{- end -}}
671{{- $limitArg := print " --collector.collstats-limit=" .collstatsLimit -}}
672{{- ne (print .collstatsLimit) "0" | ternary $limitArg "" -}}
673{{- end -}}
674{{- ternary " --compatible-mode" "" .Values.metrics.compatibleMode -}}
675{{- end -}}