Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to merge named templates in Helm

Suppose we have the following in, say, _helpers.tpl:

{{- define "app.selectorLabels" -}}
app.kubernetes.io/name: app
app.kubernetes.io/instance: instance
{{- end }}

{{- define "app.component.selectorLabels" -}}
{{ include "app.selectorLabels" . }}
app.kubernetes.io/component: component
{{- end }}

{{- define "app.labels" -}}
foo: bar
{{ include "app.selectorLabels" . }}
{{- end }}

{{- define "app.component.labels" -}}
{{ include "app.labels" . }}
{{ include "app.component.selectorLabels" . }}
{{- end }}

Now the app.component.labels named template – used , say, with include somewhere in svc.yaml or whatever – will contain the content of app.selectorLabels twice and will render as the following:

foo: bar
app.kubernetes.io/name: app
app.kubernetes.io/instance: instance
app.kubernetes.io/name: app
app.kubernetes.io/instance: instance
app.kubernetes.io/component: component

Is there any way to merge the two includes in app.component.labels definition so that the duplicates will merge?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You cannot merge templates they output just text, it’s not actual structured data for go. But you can do some tricks with dicts.

{{- define "app.component.labels" -}}
{{- $appLabels := fromYaml (include "app.labels" .) -}}
{{- $selectorLabels := fromYaml (include "app.component.selectorLabels" .) -}}
{{- $labels := merge $appLabels $selectorLabels -}}
{{ toYaml $labels }}
{{- end -}}

The key here is fromYaml, toYamland themergefunction. There are a few variations formerge` and the order matters, you need to decide which way you want precisely. You can check the docs http://masterminds.github.io/sprig/dicts.html


That said, it may be better to simply not include the selector labels in app labels in order to sidestep the problem altogether.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading