Helmfile/go template questions

I’m implementing some go templates for Helmfile and I’m facing two issues:

  1. {{ $name := Release.name }}
    I need to render $name inside Values
    I’m trying with:
    {{ .Values.$name.something }}
    but it doesn’t work, same for:
    {{ .Values.{{$name}}.something }}

  2. I need to have:
    value: myurlone,myurltwo,myurlthree

Values.elements → one, two, three

Values file:
elements:
one:
website:
url: myurlone
two:
website:
url: myurltwo
three:
website:
url: myurlthree

I’m trying something like:

{{ -range $key, $value := .Values.elements }}
{{ join “,” .Values.$value.website.url }}
But with no success.

Any help would be much appreciated.

defined variable

{{- $url := .Values.url}}
{{ -range $key, $value := .Values.elements }}
{{ $url }}{{ $key }}{{ $value }}
{{- end }}

Which point are you answering here? 1 or 2? And how do you define $url if you don’t have a $value initially?

Thanks, but I don’t see there a way to parse a variable inside the {{ .Values }} scope.

Hi, Stefano, welcome to the community!

  1. By “Helmfile,” do you mean this: GitHub - roboll/helmfile: Deploy Kubernetes Helm Charts?

  2. I’m unclear on what you’re trying to do. It sounds like you’re parsing YAML and you want to render it into a different YAML schema with the text/template package. Is that right?

    If so, why not use a Go package for processing YAML like yaml package - gopkg.in/yaml.v2 - Go Packages?

    If not, can you restate your input and desired output?

P.S.: Also, it’s usually easier for us to read code snippets (especially those where whitespace is important, like YAML) when they are wrapped in backticks ( ` ) for example:

`inline code` becomes inline code

Or for multiple lines, put 3 backticks ( ``` ), then a new line, then your code, then a new line, and then another 3 back ticks. For example:

```
multiple
lines
of
code
```

becomes:

multiple
lines
of
code

This way we can see the actual YAML layout of the input (and output?) YAML.

Hi Skillian,
yes, so the second question is about obtaining:

value: myurlone,myurltwo,myurlthree

From the following YAML file:

elements:
  one:
    website:
      url: myurlone
  two:
    website:
      url: myurltwo
  three:
    website:
      url: myurlthree

So I need to somehow loop into the elements and get a comma separeted list of the URLs

{{ -range $key, $value := .Values.elements }}
{{ -- not working join “,” .Values.$value.website.url }}

Can you tell me why you’re trying to generate YAML with the text/template package instead of using a package specifically for dealing with YAML, such as the one I linked to (yaml package - gopkg.in/yaml.v2 - Go Packages)? How are you parsing the YAML in the first place to use it in the template?

My config is made of YAML files and I’m templating K8s object given these YAMLs.
So the command is

helmfile -f helmfile.yaml -l name=service template > output.yaml
oc apply -f output.yaml

Can you show an example of how you’re deserializing the input YAML? Knowing what form that is in will help answer your question about how to produce the output.