Helm
Kubernetes
YAML
template
configuration

Helm generate comma separated list

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

In Helm templates, generating a comma-separated list is usually a matter of joining strings rather than manually managing commas in a loop. The easiest solution is often the join function from the Go template plus Sprig function set that Helm provides. This article shows the clean patterns and explains when loops are still useful.

The Simple Case: Join a List of Strings

If your values file already contains a list of strings, join is the cleanest option.

Example values.yaml:

yaml
1hosts:
2  - api.example.com
3  - admin.example.com
4  - static.example.com

Template usage:

gotemplate
{{ join "," .Values.hosts }}

That renders:

text
api.example.com,admin.example.com,static.example.com

This is clearer than building the commas by hand.

Quote the Result When YAML Needs a String

If the comma-separated output is going into a YAML scalar value, quote it explicitly.

gotemplate
allowedHosts: {{ join "," .Values.hosts | quote }}

That prevents YAML parsing surprises and makes the intent obvious.

Build a List First, Then Join It

Sometimes the source values are not already the exact strings you need. In that case, build the list first and then join it.

gotemplate
1{{- $items := list -}}
2{{- range .Values.ports }}
3  {{- $items = append $items (printf "%d" .) -}}
4{{- end -}}
5{{ join "," $items }}

This is useful when the original values are numbers or more complex objects and need transformation before joining.

Join Values from a List of Objects

Suppose your values contain objects and you only want one field from each object.

Example values.yaml:

yaml
1services:
2  - name: api
3    port: 8080
4  - name: worker
5    port: 9090

Template:

gotemplate
1{{- $names := list -}}
2{{- range .Values.services }}
3  {{- $names = append $names .name -}}
4{{- end -}}
5{{ join "," $names }}

This gives you a comma-separated list of service names.

Why Manual Comma Logic Is Messier

You can also use a loop and conditionally print commas, but it is usually less readable.

gotemplate
1{{- range $i, $host := .Values.hosts -}}
2  {{- if gt $i 0 }},{{ end -}}
3  {{- $host -}}
4{{- end -}}

This works, but join is almost always easier to read and maintain when the data already exists as a list of strings.

Newlines, Spaces, and YAML Formatting

When Helm templates generate comma-separated output inside YAML, whitespace control matters. If you see odd spacing or broken formatting, pay attention to template trimming markers such as {{- and -}}.

For inline scalar output, a compact pattern is often best:

gotemplate
endpoints: {{ join "," .Values.hosts | quote }}

For block YAML, test the rendered output with helm template rather than guessing from the template file alone.

Use helm template to Verify the Rendered Result

Always inspect the rendered manifest.

bash
helm template myrelease ./mychart

That is the fastest way to catch issues such as:

  • unquoted comma-separated output
  • wrong list field extraction
  • extra whitespace or indentation problems

Helm templates often look correct while still rendering invalid YAML.

Common Pitfalls

  • Manually building commas in a loop when join would be simpler.
  • Forgetting to convert numbers or objects into strings before joining.
  • Forgetting to quote comma-separated output when YAML expects a string scalar.
  • Ignoring whitespace trimming and producing oddly formatted manifests.
  • Debugging the template source without checking the actual rendered output from helm template.

Summary

  • Use join for comma-separated lists whenever you already have a list of strings.
  • Build a temporary list first when the source values need transformation.
  • Quote the result when it belongs in a YAML string field.
  • Manual comma logic works, but it is usually less readable than join.
  • Validate the final render with helm template rather than trusting the template by inspection alone.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.