Looping Files with range Over .Files.Glob
Generating ConfigMaps from a directory of files.
Looping Files with range Over .Files.Glob is a free Helm Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Helm Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Bundling Files in a Chart
Charts can ship extra files alongside templates. The .Files object gives your templates read access to those bundled files.
Matching Files with Glob
The .Files.Glob method returns the files whose paths match a pattern, like every file under a config directory.
{{ $files := .Files.Glob "configs/*.conf" }}Looping the Matches
Pair Glob with range to iterate over each matched file, giving you its path and its raw bytes inside the loop.
{{ range $path, $bytes := .Files.Glob "configs/*" }}Generating a ConfigMap
A classic use is building one ConfigMap whose data keys are filenames and whose values are the file contents.
The AsConfig Shortcut
For exactly that case, AsConfig turns a glob result straight into a ready-to-use data block for a ConfigMap.
data: {{ (.Files.Glob "configs/*").AsConfig | nindent 2 }}Secrets with AsSecrets
Its sibling, AsSecrets, Base64-encodes each file so a glob result can populate a Secret resource instead.
data: {{ (.Files.Glob "secret/*").AsSecrets | nindent 2 }}Using the Base Name
Inside a loop you often want just the filename as the key, so apply the base function to strip the directory part.
{{ base $path }}: |Reading Content per File
The loop variable is raw bytes, so pipe it through toString or nindent to place the content cleanly under its key.
{{ $content | toString | nindent 4 }}Glob Patterns Are Relative
Glob patterns resolve from the chart root, and dot-files are skipped, so keep bundled assets in clearly named folders.
Keeping Output Valid
Indentation matters: use nindent for each block so the generated keys and bodies nest correctly under data.
Reading a Single File
When you need just one file rather than a set, .Files.Get returns its contents directly without any looping at all.
{{ .Files.Get "configs/app.conf" }}Quick Check
Quick check on file globbing.
Recap
You learned to select bundled files with .Files.Glob, loop them with range, and emit ConfigMaps via AsConfig or AsSecrets. 🎉
Frequently asked questions
Is the “Looping Files with range Over .Files.Glob” lesson free?
Yes — the full text of “Looping Files with range Over .Files.Glob” is free to read here on the web, and the Helm Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Helm Academy course, upgrade to CoddyKit PRO.
What will I learn in “Looping Files with range Over .Files.Glob”?
Generating ConfigMaps from a directory of files. You practise Helm Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Helm Academy?
No prior experience is required. Helm Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Looping Files with range Over .Files.Glob” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Helm Academy lesson?
Yes. Every Helm Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Deep Merge with mergeOverwrite and dict
- tpl: Rendering Strings from Values
- Looping Files with range Over .Files.Glob
- fail and Custom Validation Guards