{{/* 返回获取 Secret 值的命令。 调用: {{ include "common.utils.secret.getvalue" (dict "secret" "secret-name" "field" "secret-value-field" "context" $) }} */}} {{- define "common.utils.secret.getvalue" -}} {{- $varname := include "common.utils.fieldToEnvVar" . -}} export {{ $varname }}=$(kubectl get secret --namespace {{ include "common.names.namespace" .context | quote }} {{ .secret }} -o jsonpath="{.data.{{ .field }}}" | base64 -d) {{- end -}} {{/* 将给定字段(横线分割)转换为容器的 ENV 变量(全大写) 调用: {{ include "common.utils.fieldToEnvVar" dict "field" "my-password" }} */}} {{- define "common.utils.fieldToEnvVar" -}} {{- $fieldNameSplit := splitList "-" .field -}} {{- $upperCaseFieldNameSplit := list -}} {{- range $fieldNameSplit -}} {{- $upperCaseFieldNameSplit = append $upperCaseFieldNameSplit ( upper . ) -}} {{- end -}} {{ join "_" $upperCaseFieldNameSplit }} {{- end -}} {{/* 从给出的 .Values 中获取一个值。 调用: {{ include "common.utils.getValueFromKey" (dict "key" "path.to.key" "context" $) }} */}} {{- define "common.utils.getValueFromKey" -}} {{- $splitKey := splitList "." .key -}} {{- $value := "" -}} {{- $latestObj := $.context.Values -}} {{- range $splitKey -}} {{- if not $latestObj -}} {{- printf "please review the entire path of '%s' exists in values" $.key | fail -}} {{- end -}} {{- $value = ( index $latestObj . ) -}} {{- $latestObj = $value -}} {{- end -}} {{- printf "%v" (default "" $value) -}} {{- end -}} {{/* 返回 .Values 中第一个有定义值的键,如果所有键都未定义,则返回列表中的第一个键。 调用: {{ include "common.utils.getKeyFromList" (dict "keys" (list "path.to.key1" "path.to.key2") "context" $) }} */}} {{- define "common.utils.getKeyFromList" -}} {{- $key := first .keys -}} {{- $reverseKeys := reverse .keys }} {{- range $reverseKeys }} {{- $value := include "common.utils.getValueFromKey" (dict "key" . "context" $.context ) }} {{- if $value -}} {{- $key = . }} {{- end -}} {{- end -}} {{- printf "%s" $key -}} {{- end -}} {{/* 对`path` 中包含的*单个*资源(ConfigMap、Secret)的模板进行*校验和*计算,以便用于*Pod*注解;校验和计算排除元数据。 调用: {{ include "common.utils.checksumTemplate" (dict "path" "/configmap.yaml" "context" $) }} */}} {{- define "common.utils.checksumTemplate" -}} {{- $obj := include (print .context.Template.BasePath .path) .context | fromYaml -}} {{ omit $obj "apiVersion" "kind" "metadata" | toYaml | sha256sum }} {{- end -}}