helmfile icon indicating copy to clipboard operation
helmfile copied to clipboard

Add 'include' function identical to helm's

Open sirianni opened this issue 5 years ago • 13 comments

It would be nice to have an include function like helm. The use case is to be able to render a template to a string and then further transform it in a pipeline.

Without this it is impossible to have a common YAML snippet defined in a template and then indent it contextually based on where it is used. For example:

{{ define "my-template" }}
some-yaml:
  foo:
{{ end }}

bar:
  {{- include "my-template" | nindent 2 }}

baz:
  qux:
    {{- include "my-template" | nindent 4 }}

sirianni avatar Jul 10 '20 15:07 sirianni

@sirianni Hey!

I thought the Helmfile way for that has been {{ readFile "path/to/gotmpl/file" }} and {{ tpl (readFile "path/to/gotmpl/file") .Values }}.

Just curious, but do you think we'd still need include?

mumoshu avatar Aug 04 '20 23:08 mumoshu

You're right, the tpl function can just be used directly. I'm not sure why helm defines both tpl and include then.... :thinking:

{{ define "my-template" }}
some-yaml:
  foo:
{{ end }}

bar:
  {{- tpl "my-template" . | nindent 2 }}

baz:
  qux:
    {{- tpl "my-template" . | nindent 4 }}

sirianni avatar Aug 05 '20 14:08 sirianni

I think there is still a use for having an include function. The use of template is limited in YAML because it can't easily do proper indentation and other processing, but tpl requires all templates in separate files. This is somewhat inconvenient if you have small repetitive bits such as a random common toleration setting. Furthermore, tpl requires you to hardcode the file path everywhere, while this is not the case for template or include.

It also makes it easier for end-users to use the functions they are already familiar with in Helm, directly in Helmfile.

FWest98 avatar Aug 28 '20 15:08 FWest98

@FWest98 Thanks. Yeah I believe this should be super useful if added. The only thing that makes me underprioritize this is that I thought it wasn't trivial to fully make it Helm-compatible. Especially, Helmfile has a bit different semantics in that it doesn't support automatically importing .tpl files.

So, do we also need to add some convention or configuration to allow automatically importing .tpl files so that defined snippets can be included without importing all the .tpl files?

Or can we just add define + include and defer the .tpl file loading to another feature request? In this case all you could do with it would be to just define and include within a same helmfile template file.

WDYT?

mumoshu avatar Aug 29 '20 23:08 mumoshu

I believe Helm has the functionality to automatically include all files with an underscore (see here). I think a similar behaviour where helmfile would load all files with underscores in a given directory would be useful. For example, load all files with underscore in helmfile.d, or some other folder to be specified by the user.

FWest98 avatar Aug 29 '20 23:08 FWest98

Sounds good!

Probably automatic loading of _*.tpl fiiles under the same directory as the target helmfile.yaml, like foo for helmfile -f foo/helmfile.yaml apply, would be enough for the first implementation.

mumoshu avatar Aug 29 '20 23:08 mumoshu

@FWest98 Would you mind submitting a PR for this?

mumoshu avatar Aug 29 '20 23:08 mumoshu

That idea sounds good as well! Unfortunately, I don't have any experience with Go, so I don't think I would be the person to start implementing this.

FWest98 avatar Aug 30 '20 20:08 FWest98

@mumoshu Any desire to see this added still? Happy to take a look in the coming days and submit a PR.

naphta avatar Sep 28 '21 10:09 naphta

@naphta Hey. Yeah, I still think this is worth trying!

mumoshu avatar Jan 15 '22 03:01 mumoshu

This would be an incredibly valuable feature - it would help to reduce complexity greatly, and it would add significant utility to multiple different workflows I - and my colleagues - have designed around helmfile

I'd pay for this feature. :)

armenr avatar Feb 16 '23 08:02 armenr

@armenr please post a feature request in helmfile/helmfile.

yxxhero avatar Feb 16 '23 08:02 yxxhero

@mumoshu @armenr hi. I will post a PR in helmfile/helmfile. thanks so much.

yxxhero avatar Nov 29 '23 05:11 yxxhero