Files, ConfigMaps & Secrets
Configuration that is more than a few key/value pairs (a config file, a SQL migration, a dashboard JSON) is easier to keep as a real file in the chart than as a YAML string in values.yaml. The .Files object reads any non-template file in the chart.
.Files
| Call | Returns |
|---|---|
.Files.Get "config/app.toml" |
the file's content as a string |
.Files.GetBytes "x" |
the content as bytes |
.Files.Glob "config/*" |
the matching files, as an object you can range over or convert |
(.Files.Glob "config/*").AsConfig |
YAML filename: content pairs, ready for a ConfigMap's data |
(.Files.Glob "certs/*").AsSecrets |
the same, base64-encoded, for a Secret's data |
.Files.Lines "x" |
the file as a list of lines |
Paths are relative to the chart root. Files under templates/ and files excluded by .helmignore are not accessible.
mkdir -p linkly/templates linkly/config && cd linkly
printf 'apiVersion: v2\nname: linkly\nversion: 0.5.0\n' > Chart.yaml
echo "{}" > values.yaml
cat > config/blocklist.txt <<'EOF'
phishing.example
malware.example
EOF
cat > config/limits.json <<'EOF'
{"maxLinksPerMinute": 30, "maxUrlLength": 2048}
EOF
cat > templates/configmap.yaml <<'EOF'
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-files
data:
{{- (.Files.Glob "config/*").AsConfig | nindent 2 }}
blocked-count: {{ len (.Files.Lines "config/blocklist.txt" | compact) | quote }}
EOF
helm template demo . | tail -n +3apiVersion: v1
kind: ConfigMap
metadata:
name: demo-files
data:
blocklist.txt: |
phishing.example
malware.example
limits.json: |
{"maxLinksPerMinute": 30, "maxUrlLength": 2048}
blocked-count: "2"
(compact removes the empty string that the file's final newline produces.)
Rolling Pods when config changes
A Deployment does not restart its Pods when a ConfigMap it reads changes; the Pod template didn't change, so Kubernetes sees nothing to roll out. The standard Helm fix is a checksum annotation on the Pod template: hash the rendered ConfigMap, and any config change changes the hash, which changes the template, which triggers a rolling update.
mkdir -p linkly/templates && cd linkly
printf 'apiVersion: v2\nname: linkly\nversion: 0.5.0\n' > Chart.yaml
printf 'logLevel: info\n' > values.yaml
cat > templates/configmap.yaml <<'EOF'
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-config
data:
LOG_LEVEL: {{ .Values.logLevel | quote }}
EOF
cat > templates/deployment.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Release.Name }}
spec:
selector: {matchLabels: {app: linkly}}
template:
metadata:
labels: {app: linkly}
annotations:
checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum | trunc 16 }}
spec:
containers:
- name: linkly
image: ghcr.io/acme/linkly:1.4.0
envFrom: [{configMapRef: {name: {{ .Release.Name }}-config}}]
EOF
helm template demo . -s templates/deployment.yaml | grep checksum
helm template demo . -s templates/deployment.yaml | grep checksum
helm template demo . -s templates/deployment.yaml --set logLevel=debug | grep checksum checksum/config: cb01908a908df0d8
checksum/config: cb01908a908df0d8
checksum/config: 59f11f9ee3f41d74
Same values, same hash; a different log level, a different hash. include (print $.Template.BasePath "/configmap.yaml") . renders another template file by its path. Kustomize solves the same problem differently, by putting a hash in the ConfigMap's name (ConfigMap & Secret Generators).
Secrets: what a chart should and shouldn't do
A chart can template a Secret, but the secret value has to come from somewhere, and values.yaml in Git is the wrong place. Options, from weakest to strongest:
| Approach | How | Trade-off |
|---|---|---|
| value passed at install | --set-file db.url=… or --set from the CI secret store |
the value ends up in the release record (a Secret in the cluster) and in CI logs if you're careless |
| reference an existing Secret | values hold only the Secret's name; someone or something else creates it | the chart never sees the secret; the usual choice |
| an operator writes the Secret | External Secrets Operator, Sealed Secrets, the CSI secrets driver | secrets live in a vault; Git holds only references or encrypted blobs |
linkly's chart takes the second route with a fallback for development:
mkdir -p linkly/templates && cd linkly
printf 'apiVersion: v2\nname: linkly\nversion: 0.5.0\n' > Chart.yaml
cat > values.yaml <<'EOF'
database:
existingSecret: "" # name of a Secret with key "url"; preferred
url: "" # development only: the chart creates the Secret
EOF
cat > templates/secret.yaml <<'EOF'
{{- if and (not .Values.database.existingSecret) .Values.database.url }}
apiVersion: v1
kind: Secret
metadata:
name: {{ .Release.Name }}-db
type: Opaque
stringData:
url: {{ .Values.database.url | quote }}
{{- end }}
EOF
cat > templates/env.yaml <<'EOF'
{{- if not (or .Values.database.existingSecret .Values.database.url) }}
{{- fail "set database.existingSecret (or database.url for development)" }}
{{- end }}
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-secret-ref
data:
secretName: {{ .Values.database.existingSecret | default (printf "%s-db" .Release.Name) | quote }}
EOF
echo "--- production"; helm template prod . --set database.existingSecret=linkly-db-prod | grep -E 'kind|secretName'
echo "--- development"; helm template dev . --set database.url=postgres://dev:dev@db:5432/linkly | grep -E 'kind|url|secretName'
echo "--- neither"; helm template x . 2>&1 | head -1--- production
kind: ConfigMap
secretName: "linkly-db-prod"
--- development
kind: Secret
url: "postgres://dev:dev@db:5432/linkly"
kind: ConfigMap
secretName: "dev-db"
--- neither
Error: execution error at (linkly/templates/env.yaml:2:4): set database.existingSecret (or database.url for development)
stringData lets you write the value in clear text and the API server encodes it; with data you would pipe through b64enc yourself.
lookup and random values
Two functions behave very differently under helm template than under helm install:
lookupreads a live object from the cluster (lookup "v1" "Secret" "ns" "name"). With no cluster connection, as inhelm templateor--dry-run=client, it returns an empty map, so templates must handle "not found".randAlphaNum 24produces a different value on every render, so a generated password changes on everyhelm upgradeand breaks whatever used the old one. Charts combine it withlookup("keep the existing Secret's value if there is one"), which only works on install/upgrade against a real cluster, never inhelm templateor GitOps tools that render offline.
mkdir -p c/templates && cd c
printf 'apiVersion: v2\nname: c\nversion: 0.1.0\n' > Chart.yaml
cat > templates/s.yaml <<'EOF'
{{- $existing := lookup "v1" "Secret" .Release.Namespace "c-password" }}
found: {{ ternary "yes" "no" (not (empty $existing)) }}
length: {{ randAlphaNum 24 | len }}
EOF
helm template d . | tail -2found: no
length: 24
The session prints only the length: the value itself is different every time. For GitOps-friendly charts, prefer an existing Secret over generated ones.
Test yourself
Why add a checksum/config annotation to the Pod template?
- Kubernetes requires it
- So a config change alters the Pod template and triggers a rolling update
- To encrypt the ConfigMap
- To speed up rendering
What does lookup return during helm template?
- The live object
- An error
- An empty map
- A random value
What's the usual way for a chart to get a database password?
- Hard-code it in values.yaml
- Generate it with randAlphaNum on every upgrade
- Reference an existing Secret by name, created by someone or something else
- Put it in Chart.yaml