Sub topic 8
Shaping the Secret with templates
This chapter assumes you can write an ExternalSecret with data and dataFrom, as covered in the previous chapters. Here you will use target.template to control the shape of the Kubernetes Secret that ESO writes.
Why templates
Remote secrets rarely match what an application expects. A secret manager might hold username and password as separate JSON properties. The application may want one DATABASE_URL string, or a Secret of type kubernetes.io/dockerconfigjson. A template lets ESO build that shape during sync, so you do not need a sidecar or an init script.
Building one value from several properties
Each property fetched through data or dataFrom becomes a variable in the template. For data entries the variable name is the secretKey. For dataFrom with extract, it is the property name.
apiVersion: external-secrets.io/v1
kind: ExternalSecret
metadata:
name: db-url
namespace: app
spec:
refreshInterval: 1h
secretStoreRef:
name: aws-store
kind: SecretStore
target:
name: db-url
template:
data:
DATABASE_URL: "postgres://{{ .username }}:{{ .password }}@db.app.svc:5432/app"
dataFrom:
- extract:
key: prod/dbThe target Secret db-url now contains one key, DATABASE_URL. The username and password keys do not appear in it.
What ends up in the Secret
By default the template replaces the fetched data. Only the keys you define under template.data are written. To keep the fetched keys as well, set mergePolicy: Merge:
target:
name: db-url
template:
mergePolicy: Merge
data:
DATABASE_URL: "postgres://{{ .username }}:{{ .password }}@db.app.svc:5432/app"With Merge, the Secret holds username, password and DATABASE_URL. If a template key has the same name as a fetched key, the template value wins.
Secret type, metadata and functions
The template can also set the Secret's type, labels and annotations:
target:
name: registry-auth
template:
type: kubernetes.io/dockerconfigjson
metadata:
labels:
app: registry
data:
.dockerconfigjson: "{{ .config }}"Templates use Go template syntax with a library of helper functions. These are the ones you will use most:
| Function | Use |
|---|---|
| `b64enc`, `b64dec` | Encode or decode base64 values. |
| `upper`, `lower` | Change case. |
| `toString` | Convert a value to a string. |
| `index` | Read a key whose name contains characters such as `-`. |
For a property named api-key, {{ .api-key }} fails to parse. Use {{ index . "api-key" }} instead.
Common errors
- A key is missing from the Secret. The default
mergePolicyisReplace, so fetched keys are dropped unless the template also names them. Add them totemplate.dataor useMerge. - The template fails to render. The
SecretSyncedcondition goes false, andkubectl describe externalsecretshows an error such asmap has no entry for key. The variable name does not match the remote property orsecretKey. Check the spelling and case. - An
ExternalSecretthat usesdataFromwith severalextractentries behaves unexpectedly. If two remote secrets share a property name, one overwrites the other in the template variables. Usedatawith distinctsecretKeynames when you need to tell them apart.
Exercise
A remote secret prod/db has the JSON properties username and password. You want the target Secret to contain username, password and a DATABASE_URL built from them. Which template setting do you need besides the DATABASE_URL entry in template.data?
Show answer
Set mergePolicy: Merge under target.template. The default Replace writes only the keys defined in template.data, so username and password would be left out of the Secret.