Size list with helm

4/18/2019

Simple question is it possible to get size list with helm and sprig function ?

My list :

list:
 - a
 - b
 - c

I tried like this :

{{ .Values.list | len }}
{{ .Values.list | size }}
{{ .Values.list | length }}
-- M.Hol
go
go-templates
kubernetes-helm
sprig-template-functions

1 Answer

4/18/2019

See this How to compare the length of a list in html/template in golang?.

While we talk about the “Helm template language” as if it is Helm-specific, it is actually a combination of the Go template language, some extra functions, and a variety of wrappers to expose certain objects to the templates. Many resources on Go templates may be helpful as you learn about templating.

ref: https://helm.sh/docs/chart_template_guide/#template-functions-and-pipelines

So you can use len function from go-template (text/template package).

Example:

values.yaml contents:

list:
  - a
  - b
  - c

template/list.yaml contents:

kind: List
spec:
  len: {{ len .Values.list }}
  items:
{{- range $item := .Values.list }}
    - {{ $item }}
{{- end }}
-- Shudipta Sharma
Source: StackOverflow