简体   繁体   中英

How can I add a default value to variable in helm chart template

How can I add a default value to shutdownDelay to 50 seconds in my template

        lifecycle:
        
           
            preStop:
{{ toYaml .Values.lifecycleHooks.preStop | indent 14 }}
            {{- else if gt (int .Values.shutdownDelay) 0 }}
            preStop:
              exec:
                command:
                  - sleep
                  - "{{ int .Values.shutdownDelay }}"

I tried few combinations like but nothing is working

Like if shutdownDelay isn't specified in values.yaml use 50 as default value and should appear like

Ordinarily I'd recommend the default function

... (gt (.Values.shutdownDelay | default 50) 0) ...

The problem here is that 0 is an expected value, but it's logical "false" for purposes of default , so you won't be able to explicitly specify no delay. You can't easily tell 0 and nil or "absent" apart with simple conditionals like this.

The "big hammer" here is the ternary function, which acts like the inline condition? true_expr: false_expr condition? true_expr: false_expr conditional expression in C-like languages. You can use this to select one value if the value is absent and another if it's present.

{{- $delay := hasKey .Values "shutdownDelay" | ternary .Values.shutdownDelay 50 }}
{{- if gt $delay 0 }}
...
{{- end }}

If you decide to use a more complex expression inside ternary , remember that it is an ordinary function and is not "short-circuiting"; the condition and both values are always evaluated even though only one will actually be returned, so your expression will need to pass the various type constraints even if the value is absent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM