Merge "[AAI] Add model-loader tracing config"
[oom.git] / kubernetes / common / mongodb / common / templates / _utils.tpl
1 {{/*
2 Copyright VMware, Inc.
3 SPDX-License-Identifier: APACHE-2.0
4 */}}
5
6 {{/* vim: set filetype=mustache: */}}
7 {{/*
8 Print instructions to get a secret value.
9 Usage:
10 {{ include "common.utils.secret.getvalue" (dict "secret" "secret-name" "field" "secret-value-field" "context" $) }}
11 */}}
12 {{- define "common.utils.secret.getvalue" -}}
13 {{- $varname := include "common.utils.fieldToEnvVar" . -}}
14 export {{ $varname }}=$(kubectl get secret --namespace {{ include "common.names.namespace" .context | quote }} {{ .secret }} -o jsonpath="{.data.{{ .field }}}" | base64 -d)
15 {{- end -}}
16
17 {{/*
18 Build env var name given a field
19 Usage:
20 {{ include "common.utils.fieldToEnvVar" dict "field" "my-password" }}
21 */}}
22 {{- define "common.utils.fieldToEnvVar" -}}
23   {{- $fieldNameSplit := splitList "-" .field -}}
24   {{- $upperCaseFieldNameSplit := list -}}
25
26   {{- range $fieldNameSplit -}}
27     {{- $upperCaseFieldNameSplit = append $upperCaseFieldNameSplit ( upper . ) -}}
28   {{- end -}}
29
30   {{ join "_" $upperCaseFieldNameSplit }}
31 {{- end -}}
32
33 {{/*
34 Gets a value from .Values given
35 Usage:
36 {{ include "common.utils.getValueFromKey" (dict "key" "path.to.key" "context" $) }}
37 */}}
38 {{- define "common.utils.getValueFromKey" -}}
39 {{- $splitKey := splitList "." .key -}}
40 {{- $value := "" -}}
41 {{- $latestObj := $.context.Values -}}
42 {{- range $splitKey -}}
43   {{- if not $latestObj -}}
44     {{- printf "please review the entire path of '%s' exists in values" $.key | fail -}}
45   {{- end -}}
46   {{- $value = ( index $latestObj . ) -}}
47   {{- $latestObj = $value -}}
48 {{- end -}}
49 {{- printf "%v" (default "" $value) -}}
50 {{- end -}}
51
52 {{/*
53 Returns first .Values key with a defined value or first of the list if all non-defined
54 Usage:
55 {{ include "common.utils.getKeyFromList" (dict "keys" (list "path.to.key1" "path.to.key2") "context" $) }}
56 */}}
57 {{- define "common.utils.getKeyFromList" -}}
58 {{- $key := first .keys -}}
59 {{- $reverseKeys := reverse .keys }}
60 {{- range $reverseKeys }}
61   {{- $value := include "common.utils.getValueFromKey" (dict "key" . "context" $.context ) }}
62   {{- if $value -}}
63     {{- $key = . }}
64   {{- end -}}
65 {{- end -}}
66 {{- printf "%s" $key -}}
67 {{- end -}}
68
69 {{/*
70 Checksum a template at "path" containing a *single* resource (ConfigMap,Secret) for use in pod annotations, excluding the metadata (see #18376).
71 Usage:
72 {{ include "common.utils.checksumTemplate" (dict "path" "/configmap.yaml" "context" $) }}
73 */}}
74 {{- define "common.utils.checksumTemplate" -}}
75 {{- $obj := include (print .context.Template.BasePath .path) .context | fromYaml -}}
76 {{ omit $obj "apiVersion" "kind" "metadata" | toYaml | sha256sum }}
77 {{- end -}}