Skip to content

toYaml removes all quotes and yaml types breaks #4262

Closed

Description

@shal

Output of helm version:

Client: &version.Version{SemVer:"v2.9.1", GitCommit:"20adb27c7c5868466912eebdf6664e7390ebe710", GitTreeState:"clean"}
Server: &version.Version{SemVer:"v2.9.1", GitCommit:"20adb27c7c5868466912eebdf6664e7390ebe710", GitTreeState:"clean"}

Output of kubectl version:

Client Version: version.Info{Major:"1", Minor:"9", GitVersion:"v1.9.7", GitCommit:"dd5e1a2978fd0b97d9b78e1564398aeea7e7fe92", GitTreeState:"clean", BuildDate:"2018-04-19T00:05:56Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"8+", GitVersion:"v1.8.10-gke.0", GitCommit:"16ebd0de8e0ab2d1ef86d5b16ab1899b624a77cd", GitTreeState:"clean", BuildDate:"2018-03-20T20:21:01Z", GoVersion:"go1.8.3b4", Compiler:"gc", Platform:"linux/amd64"}

Cloud Provider/Platform (AKS, GKE, Minikube etc.): GKE

I got following problem:

File: values.yml

info:
  address: "0x1ddf249430c343f72f8b12848aa3f932538e3336"

File: configmap.yml

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "fullname" . }}
  labels:
    app: {{ template "fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
data:
  file.yml: |-
{{ toYaml .Values.info | indent 4 }}

After toYaml address will be not a string.

Activity

  1. bacongobbler commented on Jun 29, 2018

    @bacongobbler
    Member

    @ashanaakh can you provide the output of helm template with this sample?

  2. shal commented on Jun 29, 2018

    @shal
    Author

    @bacongobbler Sorry, I can't provide all output, because it is confidential information.

    I see, that function changed type of 0x1ddf249430c343f72f8b19868ea4f932500e0000.

    In values.yml it was in quotes, like

    address: "0x1ddf249430c343f72f8b19868ea4f932500e0000"

    After toYaml, I see

    address: 0x1ddf249430c343f72f8b19868ea4f932500e0000

    so configmap looks like this

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: {{ template "fullname" . }}
      labels:
        app: {{ template "fullname" . }}
        chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
        release: "{{ .Release.Name }}"
        heritage: "{{ .Release.Service }}"
    data:
      file.yml: |-
        address: 0x1ddf249430c343f72f8b19868ea4f932500e0000
  3. bacongobbler commented on Jun 29, 2018

    @bacongobbler
    Member

    oh I see what you mean. However, both are considered strings though according to the YAML spec though, right? That should still work as a string as single, double and unquoted values are accepted as strings according to the spec.

  4. shal commented on Jun 29, 2018

    @shal
    Author

    @bacongobbler Unfortunately, 0x1ddf249430c343f72f8b19868ea4f932500e0000 is hexadecimal literal in YAML.

    So it is kind of integer.

    Please, take a look on YAML specification here.

    Thanks!

  5. shal commented on Jul 1, 2018

    @shal
    Author

    @bacongobbler

    Here I see, that toYaml function just use dependency for marshaling.

    https://github.com/kubernetes/helm/blob/aa98e7e3dd2356bce72e8e367e8c87e8085c692b/pkg/strvals/parser.go#L33-L40

    I reproduced this bug using helm dependency github.com/ghodss/yaml.

    package main
    
    import (
    	"fmt"
    	"github.com/ghodss/yaml"
    )
    
    func main() {
    	p := map[string]string{"key": "0x1ddf249430c343f72f8b19868ea4f932500e0000"}
    	d, _ := yaml.Marshal(p)
    	fmt.Print(string(d))
    }

    Output will be:

    key: 0x1ddf249430c343f72f8b19868ea4f932500e0000
    

    Actually this dependency has one more dependency, which is github.com/go-yaml/yaml, where this bug occurs.

  6. hmcmanus commented on Jul 13, 2018

    @hmcmanus

    It might be a bit late now but I had the same problem but was able to get around it by copying what these guys did here:

    https://github.com/kubernetes/charts/blob/master/incubator/elasticsearch-curator/values.yaml

  7. amir-hadi commented on Apr 8, 2020

    @amir-hadi

    @shal You closed this issue, did the problem disappear for you? I see a similar behavior with toYaml which is caused problems for me right now.

  8. shal commented on Apr 8, 2020

    @shal
    Author

    Hey @amir-hadi, I temporary resolved this issue by just copying all YAML files as config maps into the helm chart 😄

  9. jocko-wowza commented on Jun 18, 2020

    @jocko-wowza

    So I'm trying to us toYaml to dynamically inject custom Environment variables along side required environment variables in my container: definition in my Helm chart.

    The problem is that regardless of whether the value is a numer or string, the requirement is that the environment value needs to be quoted. Because toYaml is unquoting it my helm chart is failing.

    Anyone know how I can prevent this?

  10. jocko-wowza commented on Jun 18, 2020

    @jocko-wowza

    I solved it by not using toYaml ... That's a seemingly direct way to get data from a values file to append to my env: key, but this solution allows me to make sure all of the values are quoted:

    container:
      env:
      - name: SOME_ENV_VARIABLE
         value: "foo"
      - name: SOME_OTHER_VARIABLE
         value: "bar"
    {{- range .Values.some_value.env }}
      - name: {{ .name }}
        value: {{ .value | quote }}
    {{- end }}
    
  11. rdxmb commented on Jun 19, 2020

    @rdxmb

    In my opinion this is a bug and not an question/support like it is actually labeled.

  12. rdxmb commented on Jun 19, 2020

    @rdxmb

    Can this issue be opened again?

  13. reopened this on Jun 19, 2020
  14. 50 remaining items

  15. andykcfung commented on Nov 11, 2022

    @andykcfung

    After trying for an hour to stop toYaml from removing the quotes that I wanted to keep, I found a solution for my specific usage:

    To begin, I have this entry in my values.yaml (block 1)

    global:
      properties:
         mad:
           person: 'My id: 1234'
    

    where "1234" should actually come from another entry in my values.yaml (note that I hard coded the id "1234" above but this will be addressed by the solution later). The following entry is also in my values.yaml:

    person:
      id: 1234
    

    Now I needed to create a file named "mad.yaml" with the following content using toYaml (note the single code around the value) (target i):

    mad:
      person: 'My id: 1234'
    

    The code that I will use to create the file "mad.yaml" is like this (code A):

    mad.yaml: |-
    {{ tpl (toYaml .Values.global.properties) . | indent 4}}
    

    The solution: I modified my original entry (i.e., block 1) to this:

    global:
      properties:
         mad:
           person: '{{ printf "%s %d" "My id:" (.Values.person.id | int) }}'
    

    and my code (code A) was able to produce what I wanted (i.e., target i).

    Hope this helps. It took me much longer to write this up than to find the solution I needed.

  16. joejulian commented on Nov 11, 2022

    @joejulian
    Contributor

    If your string has to have the single quotes, you could also use the squote filter

    With a value of:

    mystring: My id: 1234
    

    this template:

    {{ .Values.mystring | squote }}
    

    produces:

    'My id: 1234'
    
  17. RiccardoManzan commented on May 26, 2023

    @RiccardoManzan

    I needed to put a yaml defined in a values file as a string with yaml format, and in order to do that i used toYaml.
    Unfortunately in that yaml portion i had a string which contained some special characters like : and }
    I solved by using the backslash \ before special characters.

    In this way i was able to keep my escapes.

  18. almson commented on Jun 1, 2023

    @almson

    Not being able to keep the type of the original yaml is a very surprising behavior. One expects the string "1" to stay a string and not turn into a yaml integer 1: The issue seems to be in "sigs.k8s.io/yaml" package in the Unmarshal function as the following program demonstrates:

    package main
    
    import (
    	"fmt"
    
    	"sigs.k8s.io/yaml"
    )
    
    var data = `
    a: Easy!
    b:
      c: 2
      d: [3, 4]
      e: "5"
    `
    
    func main() {
    
    	// Unmarshal the YAML back into a Person struct.
    	m := map[string]interface{}{}
    
    	err = yaml.Unmarshal([]byte(data), &m)
    	if err != nil {
    		fmt.Printf("err: %v\n", err)
    		return
    	}
    	fmt.Println(m)
    }
    
    /*
    age: 30
    name: John
    
    map[a:Easy! b:map[c:2 d:[3 4] e:5]]
    */
    

    The reference go-yaml v2 & v3 program (with the "e" addition) keeps the quotes just fine.

    That's pretty garbage. Could also explain why people keep needing quotes even though the yaml spec doesn't require them. Eg. why does CronJob schedule need to be quoted? If Kubernetes won't fix its problems, then Helm should not insist "we're confirming to YAML, don't look at us!" Otherwise that's garbage behavior from both Helm and Kubernetes. What a crappy ecosystem.

    The fix is pretty "simple." Stop using these parsers, and write (fork) your own that tracks the quote status of strings on Unmarshal and reproduces them on Marshal. Or is that too difficult for you?

  19. baracudda commented on Mar 13, 2025

    @baracudda

    Having just run into this bug nearly 7 years after it was reported, disappointed is an understatement. However, I found a simple workaround for those who find this thread after me. Using the original definition as an example, just place the variable on the next line after a pipe-dash and without quotes, then toYaml() keeps the var as a string.

    info:
      address: |-
        0x1ddf249430c343f72f8b12848aa3f932538e3336
    

    which results in

    ...
    data:
      file.yml: |-
        info:
          address: |-
            0x1ddf249430c343f72f8b12848aa3f932538e3336
    

    If you use a pipe-dash and double quote the string, the quotes will be a part of the resulting variable value.

  20. grassehh commented on Mar 26, 2025

    @grassehh

    Having just run into this bug nearly 7 years after it was reported, disappointed is an understatement. However, I found a simple workaround for those who find this thread after me. Using the original definition as an example, just place the variable on the next line after a pipe-dash and without quotes, then toYaml() keeps the var as a string.

    info:
      address: |-
        0x1ddf249430c343f72f8b12848aa3f932538e3336
    

    which results in

    ...
    data:
      file.yml: |-
        info:
          address: |-
            0x1ddf249430c343f72f8b12848aa3f932538e3336
    

    If you use a pipe-dash and double quote the string, the quotes will be a part of the resulting variable value.

    When I use double quote, the resulting variable has additionnal surrounding simple quotes. Exemple

    property: |-
        "value"

    produces

    property: '"value"'

    And if I try with simple quote, it ends with 3 quotes:

    property: |-
        'value'

    produces

    property: '''value'''
  21. baracudda commented on Mar 26, 2025

    @baracudda

    I eventually abandoned trying to use toYaml. My "simple fix" worked in OP's case, but not mine (a string with spaces in it). You may have luck with just the pipe char, e.g.

    property: |
        value
    

    as long as the value is a simple non-whitespaced string.

  22. grassehh commented on Nov 6, 2025

    @grassehh

    Hello, why is this issue closed ? The problem still remains

  23. dlerps commented on May 7, 2026

    @dlerps

    This is a bug which is unresolved and should not be closed!

  24. almson commented on May 13, 2026

    @almson

    helm and the Go/Kubernetes ecosystem sucks. for f's sake just ask Claude to write a better yaml library, if you can't be bothered to do it yourselves. it can use Java Jackson as a reference, since in Java people do things correctly.

  25. cwegener commented on May 13, 2026

    @cwegener

    helm and the Go/Kubernetes ecosystem sucks. for f's sake just ask Claude to write a better yaml library, if you can't be bothered to do it yourselves. it can use Java Jackson as a reference, since in Java people do things correctly.

    seriously? https://matrix.yaml.info/valid.html

  26. almson commented on May 14, 2026

    @almson

    helm and the Go/Kubernetes ecosystem sucks. for f's sake just ask Claude to write a better yaml library, if you can't be bothered to do it yourselves. it can use Java Jackson as a reference, since in Java people do things correctly.

    seriously? https://matrix.yaml.info/valid.html

    If it's so great, why can't it be used to preserve type information?

    P.S. go-yaml has 49 errors in that table, but that's not the issue at the root of this bug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    blockedbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions