J release changes
[ric-plt/ric-dep.git] / helm / infrastructure / subcharts / kong / charts / postgresql / charts / common / templates / _utils.tpl
1 {{/* vim: set filetype=mustache: */}}
2 {{/*
3 Print instructions to get a secret value.
4 Usage:
5 {{ include "common.utils.secret.getvalue" (dict "secret" "secret-name" "field" "secret-value-field" "context" $) }}
6 */}}
7 {{- define "common.utils.secret.getvalue" -}}
8 {{- $varname := include "common.utils.fieldToEnvVar" . -}}
9 export {{ $varname }}=$(kubectl get secret --namespace {{ .context.Release.Namespace | quote }} {{ .secret }} -o jsonpath="{.data.{{ .field }}}" | base64 -d)
10 {{- end -}}
11
12 {{/*
13 Build env var name given a field
14 Usage:
15 {{ include "common.utils.fieldToEnvVar" dict "field" "my-password" }}
16 */}}
17 {{- define "common.utils.fieldToEnvVar" -}}
18   {{- $fieldNameSplit := splitList "-" .field -}}
19   {{- $upperCaseFieldNameSplit := list -}}
20
21   {{- range $fieldNameSplit -}}
22     {{- $upperCaseFieldNameSplit = append $upperCaseFieldNameSplit ( upper . ) -}}
23   {{- end -}}
24
25   {{ join "_" $upperCaseFieldNameSplit }}
26 {{- end -}}
27
28 {{/*
29 Gets a value from .Values given
30 Usage:
31 {{ include "common.utils.getValueFromKey" (dict "key" "path.to.key" "context" $) }}
32 */}}
33 {{- define "common.utils.getValueFromKey" -}}
34 {{- $splitKey := splitList "." .key -}}
35 {{- $value := "" -}}
36 {{- $latestObj := $.context.Values -}}
37 {{- range $splitKey -}}
38   {{- if not $latestObj -}}
39     {{- printf "please review the entire path of '%s' exists in values" $.key | fail -}}
40   {{- end -}}
41   {{- $value = ( index $latestObj . ) -}}
42   {{- $latestObj = $value -}}
43 {{- end -}}
44 {{- printf "%v" (default "" $value) -}} 
45 {{- end -}}
46
47 {{/*
48 Returns first .Values key with a defined value or first of the list if all non-defined
49 Usage:
50 {{ include "common.utils.getKeyFromList" (dict "keys" (list "path.to.key1" "path.to.key2") "context" $) }}
51 */}}
52 {{- define "common.utils.getKeyFromList" -}}
53 {{- $key := first .keys -}}
54 {{- $reverseKeys := reverse .keys }}
55 {{- range $reverseKeys }}
56   {{- $value := include "common.utils.getValueFromKey" (dict "key" . "context" $.context ) }}
57   {{- if $value -}}
58     {{- $key = . }}
59   {{- end -}}
60 {{- end -}}
61 {{- printf "%s" $key -}} 
62 {{- end -}}