kubernetes-helm

What is the difference between the quote function vs using quotation marks?


Why should I prefer this:

args: [{{ Values.somevalue | quote }}]

to this

args: ["{{ Values.somevalue }}"]

I feel like I read about the difference somewhere but I can find anything about what "" do in the docs, they only mention | quote. But if the two lines I wrote above are equivalent, why would they bother creating the quote function in the first place?


Solution

  • It looks like quote's actual interesting property is that it can take arbitrarily many parameters, quotes each of them, and puts a single space between each

    {{ $v := "foo" }}
    {{ quote $v "bar" "baz" }}
    {{/* "foo" "bar" "baz" */}}
    

    If you had a need to include it in an extended pipeline, the function form could be convenient.

    {{ list (quote $x) | toJson }}
    {{/* ["\"foo\""] */}}
    

    But otherwise it doesn't seem to do anything special, and in particular doesn't do any quote escaping.

    {{ $x := "foo" -}}
    {{/* All produce "foo": */}}
    "{{ $x }}"
    {{ $x | quote }}
    {{ $x | printf "\"%s\"" }}
    
    {{/* Not what you hoped for: */}}
    {{ $y := "bar\", \"baz" }}
    {{ $y | quote }}
    {{/* "bar", "baz" */}}
    

    There is a toJson extension function that converts an arbitrary object to JSON. If you know you have a string, this will double-quote it, and also do any other escaping that's necessary for either JSON or YAML format. This may be more robust than quote in practice.

    {{ "foo" | toJson }}
    {{/* "foo" */}}
    
    {{ "bar\", \"baz" | toJson }}
    {{/* "bar\", \"baz" */}}
    

    (FWIW I almost always {{ ... | quote }}; but I think when I started learning Helm I also expected this to actually do escaping and it doesn't.)