Rafał W.
Rafał W.

Reputation: 129

Helm3: Problem with including template inside template

I'm having a problem with a simple template-in-template case and I cannot get a working solution. Here's the roundup:

This is a standard template created by Helm itself:

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "app.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

Later I'd like to re-use app.fullname in this template:

{{- define "app.userSessionSelector" -}}
workload.user.cattle.io/workloadselector: deployment-{{ .Release.Namespace }}-{{ include "app.fullname" . }}-usersession
{{- end }}

When I test this configuration I get:

Error: Failed to render chart: exit status 1: install.go:178: [debug] Original chart version: ""
install.go:195: [debug] CHART PATH: /home/user/helm/app

Error: template: app/templates/_helpers.tpl:70:81: executing "app.userSessionSelector" at <include "app.fullname" .>: error calling include: template: app/templates/_helpers.tpl:18:16: executing "app.fullname" at <$name>: invalid value; expected string
helm.go:84: [debug] template: app/templates/_helpers.tpl:70:81: executing "app.userSessionSelector" at <include "app.fullname" .>: error calling include: template: app/templates/_helpers.tpl:18:16: executing "app.fullname" at <$name>: invalid value; expected string

What puzzles me is why there's a problem with $name while app.fullname template evaluates and is used in various places in the project. I bet this is explained in the documentation somewhere [or it's a bug] but I was unable to find the cause of this.

BTW: I'm using Helm v3.9.2.

Upvotes: 3

Views: 2451

Answers (1)

z.x
z.x

Reputation: 2527

It may be caused by the scope.

$ represents the root of values.

In the range loop (or with etc), you should use . to represents the current element.

{{- define "app.userSessionSelector" -}}
workload.user.cattle.io/workloadselector: deployment-{{ .Release.Namespace }}-{{ include "app.fullname" $ }}-usersession
{{- end }}

Upvotes: 4

Related Questions