Kubernetes Helm 把两个变量和中间的字符串结合起来

我试图改变一个变量的值,如果它设置的另一个变量通过组合两个在中间的破折号,我不知道这样做的语法,我想的是这样的东西:

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

这是正确的吗? 如果 serviceNamespacehelloserviceTag1.0.0我会结束与 serviceNamespacehello-1.0.0

100753 次浏览

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 = print .Values.serviceNamespace  "-" .Values.serviceTag -}}
\{\{- end}}

Notice the new = operator in $serviceNamespace = print .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 . }}

You can simply do it like this , with string ":" in middle

"\{\{ $values.image.repository }}:\{\{ $values.image.tag }}"