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

前端 未结 3 763
無奈伤痛
無奈伤痛 2020-12-30 18:58

I’m trying to change the value of a variable if another variable it set by combining the two with a dash in the middle, I’m not sure of the syntax to do this, I’m thinking o

相关标签:
3条回答
  • 2020-12-30 19:38

    For concatenation just use printf:

    {{-  $serviceNamespace := printf "%s-%s" .Values.serviceNamespace .Values.serviceTag -}}
    
    0 讨论(0)
  • You can simply do it like this , with string ":" in middle

    "{{ $values.image.repository }}:{{ $values.image.tag }}"
    
    0 讨论(0)
  • 2020-12-30 19:54

    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 . }}
    
    0 讨论(0)
提交回复
热议问题