You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
In my Helm template, why is pluck evaluating to a float64? #9362
pluck return (in Go terms) a []interface{}. You can find the code here.
What is curious is why a float64 would be in this anywhere. The YAML parser Helm uses is the same one that Kubernetes provides. We import their package.
Have you tried writing the output of ``{{ pluck .Values.env .Values.environments | first | default .Values.environments.sandbox }}` to see what is written? Or, try using a type function to detect the types along the way.
I worked around the problem by doing this, where I pipe the pluck() into an int. It still begs the question on why, like you said, float64 is there at all.
templates/deployments.yaml
--------------------------
apiVersion: apps/v1
kind: Deployment
metadata:
...
spec:
{{- if not .Values.autoscaling.enabled }}
replicas: {{ index .Values.replicaCount ((pluck .Values.env .Values.environments | first | default .Values.environments.sandbox) | int) }}
@mattfarina I think at the heart of the issue is that Kubernetes uses kubernetes-sigs/yaml to parse YAML, which is a fork of ghodoss/yaml, which was explicitly designed for use marshalling and unmarshalling structs with known, tagged members. To accomplish this goal, it knowingly abandons functionality useful to a general-purpose YAML parser/renderer, because it wasn't meant as a general-purposes parser/renderer. For example, the library has no need to keep track of whether a particular numeric value in a YAML document parsed as a float or an int - it'll just get converted to whatever format is specified in the struct definition, anyway.
This limited, special-purpose YAML parser is perfect for the general needs of Kubernetes; Kubernetes manifests do get marshalled from and unmarshalled to tagged structs representing specific, known types in Go. But in Helm, that's a lot less helpful, at least for when parsing values.yaml, evaluating toYaml and fromYaml, and similar situations. All these cases deal with map[string]interface {} (or if we're being entirely honest, map[interface{}]interface{}), and users generally are going to expect values to get their types from the source document (because there's no where else to get them).
I'm personally of the opinion that either:
Helm should be adjusted to use both kubernetes-sigs/yaml and go-yaml/yaml; the former when dealing with Kubernetes API objects, and the latter when dealing with values, template functions, and the like, OR
If Helm doesn't want to use two different libraries for YAML, then maybe kubernetes-sigs/yaml could add a function, something like yaml.UnmarshalRaw, that delegates to go-yaml/yaml (which kubernetes-sigs/yaml already depends on) then doesn't pass the result through JSON before returning it, OR
If the people who maintain kubernetes-sigs/yaml want to be really fancy, they could fix the existing code to maintain all the unmarshalling-to-struct magic without losing source type information; but I think this would mean no longer delegating to json.Unmarshal, which would probably imply an immense amount of extra work.
EDIT: Here's, I think, a relevant quote from the original announcement of ghodss/yaml:
Note that there are a few other uses for YAML beyond converting them to and from Go structs. gosexy/yaml, for example, uses YAML to store and access configuration files. In those cases, this may not be the right approach, and you may want the full power of YAML. But when serializing and deserializing structs, the above method is more robust and maintainable in the long run.
@mattfarina I can give that a shot, sure. It's not clear to me from the HIP documentation whether this is a "feature" or "informational" proposal. It doesn't really address adding new features (except for a very broad understanding of the word "feature"), but I guess it's a "feature" insofar as it describes "an interoperability standard that will be supported by the Helm project" (that is to say, the YAML specification)?
::sighs:: Alright, when reviewing various YAML libraries in Go in preparation for writing up a HIP about this, I was reminded of the fact that the "de facto" standard library for working with YAML in Go, gopkg.in/yaml, alsointentionally disregards the YAML specification (although in a different way and for different reasons).
Mind, direct use of that library for parsing values files would still give better results than the current library (the bug in go-yaml only affects rendering, not parsing), but I'm a bit leery of recommending replacing one library with incorrect behavior with a different library that also has incorrect behavior (especially if the library maintainer views the incorrect behavior as desirable and not to be fixed).
The only Go library I can find which comes close to getting this correct is github.com/goccy/go-yaml; but that library fails to correctly output any float with more than 7 digits after the decimal point, for some reason.
but I'm a bit leery of recommending replacing one library with incorrect behavior with a different library that also has incorrect behavior (especially if the library maintainer views the incorrect behavior as desirable and not to be fixed).
ding ding ding.
And here lies our predicament: every YAML parser implementation interprets the YAML specification differently, and in many cases, handle certain cases in a way that goes against the spec due to technical limitations. And because the YAML specification is SO HUGE, there is not a single parser out there today to our knowledge that handles each and every edge case in the YAML 1.2 specification (which is 84 pages long, mind you)... Leaving us in a difficult position.
Add on the fact that we have to maintain backwards compatibility so that existing Helm charts do not break during an upgrade, and herein lies our issue. :)
At this point, I'm strongly considering whether we need to switch over to another markup language... We've always joked that we'd switch over to strict JSON support if it supported code comments.
I should note as a side comment, the rendering problem (as opposed to the parsing problem) isn't limited to just YAML; even just calling the toString template function on a float value of 1.0 results in output of "1", because of decisions made in Sprig (as well as in Go at the language level).
And yes, the YAML 1.2 specification is a giant PITA. At the same time, using JSON for values would definitely not solve the problems @ZillaG or I are experiencing, since JSON doesn't support integers anyway.
(For reference, my problem is that I want to use the toToml template function to generate e.g. Telegraf config files from YAML data, and I can't because there's no way to get integers in the output. I can't even use the workaround of passing data through toInt first because there's no way in Helm to recursively iterate over a nested data structure).
This issue has been marked as stale because it has been open for 90 days with no activity. This thread will be automatically closed in 30 days if no further activity occurs.
Activity
mattfarina commentedon Feb 15, 2021
pluckreturn (in Go terms) a[]interface{}. You can find the code here.What is curious is why a float64 would be in this anywhere. The YAML parser Helm uses is the same one that Kubernetes provides. We import their package.
Have you tried writing the output of ``{{ pluck .Values.env .Values.environments | first | default .Values.environments.sandbox }}` to see what is written? Or, try using a type function to detect the types along the way.
ZillaG commentedon Feb 15, 2021
Thanks. I'm new to all this (helm, Kubernetes, Go). I did this, is this what you meant for printing the
pluck()value?I'll look into the type function idea.
I worked around the problem by doing this, where I pipe the
pluck()into anint. It still begs the question on why, like you said,float64is there at all.mattfarina commentedon Feb 15, 2021
I think I found the problem. In the YAML parser that Helm and Kubernetes/Kubectl use there is a bug for this... kubernetes-sigs/yaml#45
philomory commentedon Feb 17, 2021
@mattfarina I think at the heart of the issue is that Kubernetes uses
kubernetes-sigs/yamlto parse YAML, which is a fork ofghodoss/yaml, which was explicitly designed for use marshalling and unmarshalling structs with known, tagged members. To accomplish this goal, it knowingly abandons functionality useful to a general-purpose YAML parser/renderer, because it wasn't meant as a general-purposes parser/renderer. For example, the library has no need to keep track of whether a particular numeric value in a YAML document parsed as a float or an int - it'll just get converted to whatever format is specified in the struct definition, anyway.This limited, special-purpose YAML parser is perfect for the general needs of Kubernetes; Kubernetes manifests do get marshalled from and unmarshalled to tagged structs representing specific, known types in Go. But in Helm, that's a lot less helpful, at least for when parsing
values.yaml, evaluatingtoYamlandfromYaml, and similar situations. All these cases deal withmap[string]interface {}(or if we're being entirely honest,map[interface{}]interface{}), and users generally are going to expect values to get their types from the source document (because there's no where else to get them).I'm personally of the opinion that either:
kubernetes-sigs/yamlandgo-yaml/yaml; the former when dealing with Kubernetes API objects, and the latter when dealing with values, template functions, and the like, ORkubernetes-sigs/yamlcould add a function, something likeyaml.UnmarshalRaw, that delegates togo-yaml/yaml(whichkubernetes-sigs/yamlalready depends on) then doesn't pass the result through JSON before returning it, ORkubernetes-sigs/yamlwant to be really fancy, they could fix the existing code to maintain all the unmarshalling-to-struct magic without losing source type information; but I think this would mean no longer delegating tojson.Unmarshal, which would probably imply an immense amount of extra work.EDIT: Here's, I think, a relevant quote from the original announcement of
ghodss/yaml:mattfarina commentedon Feb 18, 2021
@philomory Thanks for the suggestion on a different way to handle the YAML. A change like this usually requires a Helm Improvement Proposal. Could you submit one? https://github.com/helm/community/tree/master/hips
philomory commentedon Feb 18, 2021
@mattfarina I can give that a shot, sure. It's not clear to me from the HIP documentation whether this is a "feature" or "informational" proposal. It doesn't really address adding new features (except for a very broad understanding of the word "feature"), but I guess it's a "feature" insofar as it describes "an interoperability standard that will be supported by the Helm project" (that is to say, the YAML specification)?
philomory commentedon Feb 24, 2021
::sighs:: Alright, when reviewing various YAML libraries in Go in preparation for writing up a HIP about this, I was reminded of the fact that the "de facto" standard library for working with YAML in Go,
gopkg.in/yaml, also intentionally disregards the YAML specification (although in a different way and for different reasons).Mind, direct use of that library for parsing values files would still give better results than the current library (the bug in go-yaml only affects rendering, not parsing), but I'm a bit leery of recommending replacing one library with incorrect behavior with a different library that also has incorrect behavior (especially if the library maintainer views the incorrect behavior as desirable and not to be fixed).
The only Go library I can find which comes close to getting this correct is github.com/goccy/go-yaml; but that library fails to correctly output any float with more than 7 digits after the decimal point, for some reason.
bacongobbler commentedon Feb 24, 2021
ding ding ding.
And here lies our predicament: every YAML parser implementation interprets the YAML specification differently, and in many cases, handle certain cases in a way that goes against the spec due to technical limitations. And because the YAML specification is SO HUGE, there is not a single parser out there today to our knowledge that handles each and every edge case in the YAML 1.2 specification (which is 84 pages long, mind you)... Leaving us in a difficult position.
Add on the fact that we have to maintain backwards compatibility so that existing Helm charts do not break during an upgrade, and herein lies our issue. :)
At this point, I'm strongly considering whether we need to switch over to another markup language... We've always joked that we'd switch over to strict JSON support if it supported code comments.
philomory commentedon Feb 24, 2021
I should note as a side comment, the rendering problem (as opposed to the parsing problem) isn't limited to just YAML; even just calling the
toStringtemplate function on a float value of1.0results in output of"1", because of decisions made in Sprig (as well as in Go at the language level).And yes, the YAML 1.2 specification is a giant PITA. At the same time, using JSON for values would definitely not solve the problems @ZillaG or I are experiencing, since JSON doesn't support integers anyway.
(For reference, my problem is that I want to use the
toTomltemplate function to generate e.g. Telegraf config files from YAML data, and I can't because there's no way to get integers in the output. I can't even use the workaround of passing data throughtoIntfirst because there's no way in Helm to recursively iterate over a nested data structure).github-actions commentedon May 26, 2021
This issue has been marked as stale because it has been open for 90 days with no activity. This thread will be automatically closed in 30 days if no further activity occurs.