Skip to content

In my Helm template, why is pluck evaluating to a float64? #9362

Closed

Description

@ZillaG

Output of helm version:
version.BuildInfo{Version:"v3.5.2", GitCommit:"167aac70832d3a384f65f9745335e9fb40169dc2", GitTreeState:"dirty", GoVersion:"go1.15.7"}

Output of kubectl version:

Client Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.2", GitCommit:"faecb196815e248d3ecfb03c680a4507229c2a56", GitTreeState:"clean", BuildDate:"2021-01-14T05:14:17Z", GoVersion:"go1.15.6", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.2", GitCommit:"faecb196815e248d3ecfb03c680a4507229c2a56", GitTreeState:"clean", BuildDate:"2021-01-13T13:20:00Z", GoVersion:"go1.15.5", Compiler:"gc", Platform:"linux/amd64"}

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

I'm trying to set values in my templates based on a passed-in env variable, so I'm doing this.

values.yaml
-----------
env: sandbox
environments:
  sandbox: 0
  staging: 1
  production: 2
replicaCount:
  - 1
  - 2
  - 4

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) }}

But I get

$ helm template . --dry-run
Error: template: guestbook/templates/deployment.yaml:10:15: executing "guestbook/templates/deployment.yaml" at <index .Values.replicaCount (pluck .Values.env .Values.environments | first | default .Values.environments.sandbox)>: error calling index: cannot index slice/array with type float64

Why is pluck returning a float64 instead of an integer, which I expect since my environments dictionary values are integers?

Activity

  1. mattfarina commented on Feb 15, 2021

    @mattfarina
    Collaborator

    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.

  2. ZillaG commented on Feb 15, 2021

    @ZillaG
    Author

    Thanks. I'm new to all this (helm, Kubernetes, Go). I did this, is this what you meant for printing the pluck() value?

    {{- printf "%s:" (pluck .Values.env .Values.environments | first | default .Values.environments.sandbox) }}
    spec:%!s(float64=0):
    

    I'll look into the type function idea.

    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) }}
    
  3. mattfarina commented on Feb 15, 2021

    @mattfarina
    Collaborator

    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

  4. philomory commented on Feb 17, 2021

    @philomory

    @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.

  5. mattfarina commented on Feb 18, 2021

    @mattfarina
    Collaborator

    @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

  6. philomory commented on Feb 18, 2021

    @philomory

    @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)?

  7. philomory commented on Feb 24, 2021

    @philomory

    ::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.

  8. bacongobbler commented on Feb 24, 2021

    @bacongobbler
    Member

    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.

  9. philomory commented on Feb 24, 2021

    @philomory

    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).

  10. github-actions commented on May 26, 2021

    @github-actions

    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.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions