adding single quotes to helm value

11/30/2019

In my values.yaml file for helm, I am trying to create a value with quotes but when I run it, it gives a different result

values.yaml

annotation: '"ports": {"88":"sandbox-backendconfig"}}'

{{ .Values.annotation }}

what shows when I do dry run

"ports": {"88":"sandbox-backendconfig"}}

how can I make the single quotes around it show also

-- King
kubernetes
kubernetes-helm
yaml

1 Answer

12/1/2019

When the Helm YAML parser reads in the values.yaml file, it sees that the value of annotation: is a single-quoted string and so it keeps the contents of the value without the outer quotes.

As the YAML spec suggests, you can include single quotes inside a single-quoted string by doubling the quote. It might be more familiar to make this a double-quoted string and use backslash escaping. A third possibility is to make this into a block scalar, which would put the value on a separate line, but wouldn't require any escaping at all.

annotation: '''"ports": {"88":"sandbox-backendconfig"}}'''
annotation: "'\"ports\": {\"88\":\"sandbox-backendconfig\"}}'"
annotation: >-
  '"ports": {"88":"sandbox-backendconfig"}}'

I'm not sure what context you're trying to use this in, but if this is a more structured format, you can use Helm's toYaml or toJson functions to build up the annotation value for you.

# values.yaml
ports:
  '88': sandbox-backendconfig
# templates/some-resource.yaml
annotations: {{ printf "\"ports\": %s" (toJson .Values.ports) | squote }}
-- David Maze
Source: StackOverflow