Kubernetes Helm, combine two variables with a string in the middle

拜拜、爱过 提交于 2019-12-04 22:57:36

For concatenation just use printf:

{{-  $serviceNamespace := printf "%s-%s" .Values.serviceNamespace .Values.serviceTag -}}

Update

It is now possible in the 1.11 version of golang, see commit:

{{- $serviceNamespace := .Values.serviceNamespace -}}
{{- $serviceTag := .Values.serviceTag -}}
{{- if $serviceTag}}
{{- $serviceNamespace = .Values.serviceNamespace  "-" .Values.serviceTag -}}
{{- end}}

Notice the new = operator in $serviceNamespace = .Values.serviceNamespace "-" .Values.serviceTag

Older golang versions

You cannot currently (in golang 1.9, but available in 1.11, see update above) reassign template variables because if introduces a new scope. Until this is fixed (see issue and proposed fix), you can work around this by writing a function:

{{ define "makeServiceNamespace" }}
    {{- if .Values.serviceTag }}
    {{- printf "%s-%s" .Values.serviceNamespace .Values.serviceTag -}}
    {{- else }}
    {{- print .Values.serviceNamespace }}
    {{- end }}
{{- end }}

Then use it like so:

serviceNamespace: {{ template makeServiceNamespace . }}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!