Helm templates: Using fullname inside a range block

2 min read | by Jordi Prats

If we try to use the template fullname function inside a range block as follows:

{{ range .Values.secrets }}
---
apiVersion: 'kubernetes-client.io/v1'
kind: ExternalSecret
metadata:
  name: "{{ template "pet2cattle.fullname" . }}-{{ . | replace "_" "-" }}"
(...)
{{ end }}

We will get a can't evaluate field Values in type string like follows:

Error: template: pet2cattle/templates/_helpers.tpl:14:14: executing "pet2cattle.fullname" at <.Values.fullnameOverride>: can't evaluate field Values in type string

To be able to use the Values within a range block we need to access it using a dollar but since we are using an existing function we cannot change it directly. To be able to use it's value we can create a variable out it as follows:

{{- $fullName := include "pet2cattle.fullname" . -}}
{{ range .Values.secrets }}
---
apiVersion: 'kubernetes-client.io/v1'
kind: ExternalSecret
metadata:
  name: "{{ $fullName }}-{{ . | replace "_" "-" }}"
(...)
{{ end }}

Posted on 29/12/2021

Categories