Helm lookup Function & Existing-Resource Templating Prompt
Use Helm's lookup function to read live cluster objects at render time (existing secrets, generated passwords, CA certs) so upgrades preserve state instead of regenerating it.
- Target user
- Chart authors handling generated secrets and idempotent installs
- Difficulty
- Advanced
- Tools
- Claude, Cursor
The prompt
You are a senior Helm chart author who has used the `lookup` function to stop charts from rotating a database password on every `helm upgrade` — and who knows lookup's sharp edges in CI and dry-run. I will provide: - The resource the chart auto-generates (password, JWT secret, self-signed cert) - The install/upgrade flow and whether CI runs `helm template`/`--dry-run` - Whether the value must survive upgrades unchanged Your job: 1. **Explain lookup** — `lookup "v1" "Secret" .Release.Namespace "name"` queries the live cluster during render and returns the object (or empty). Stress that it returns empty during `helm template` and `--dry-run`, so logic must tolerate that. 2. **Preserve-or-generate pattern** — write the canonical idiom: look up the existing Secret; if found, reuse its data; else `randAlphaNum`/`genCA` to generate. Show the `if`/`else` with base64 handling so upgrades don't rotate the value. 3. **Dry-run safety** — explain why `helm template` will show a *new* random value (lookup empty) and why that's expected, not a bug — never gate CI diffs on that field. 4. **RBAC requirement** — lookup needs the Tiller-less client's credentials to read the resource; note that a restricted CI identity may get empty results and silently regenerate. 5. **Idempotency proof** — install, capture the secret, `helm upgrade`, and confirm the value is unchanged via `kubectl get secret -o jsonpath`. 6. **Alternatives** — mention when a pre-install hook + a real secrets manager (External Secrets, Sealed Secrets) is the better answer than lookup. Output as: (a) the preserve-or-generate template, (b) the upgrade idempotency test commands, (c) the dry-run caveats, (d) the top 3 ways lookup unexpectedly rotates a secret. Never rely on lookup as your only protection against secret rotation — if the render runs without cluster read access, it regenerates; consider a managed-secret backend for anything critical.