Skip to content

fix: honor environment kubeContext over helmDefaults.kubeContext - #2841

Merged
yxxhero merged 1 commit into
helmfile:mainfrom
Cayan:fix-env-kubecontext-over-helmdefaults
Oct 23, 2026
Merged

yxxhero merged 1 commit into
helmfile:mainfrom
Cayan:fix-env-kubecontext-over-helmdefaults

Conversation

@Cayan

@Cayan Cayan commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

When both helmDefaults.kubeContext and an environment's kubeContext are set, releases that set no kubeContext of their own run against the helmDefaults context, not the environment's. getKubeContext documents the order release, then environment, then helmDefaults (added for #2309), but ExecuteTemplates copies helmDefaults.kubeContext into every release without one, so getKubeContext returns it at the first step and never reaches the environment.

Example:

environments:
  prod:
    kubeContext: prod-ctx
---
helmDefaults:
  kubeContext: default-ctx
releases:
- name: app
  namespace: ns-{{ .Release.KubeContext }}
  chart: ./chart

helmfile -e prod sync with a stub helm that logs its arguments:

  • before: --kube-context default-ctx upgrade --install app chart ... --kube-context default-ctx --namespace ns-default-ctx ...
  • after: --kube-context default-ctx upgrade --install app chart ... --kube-context prod-ctx --namespace ns-prod-ctx ...

diff and apply change the same way. The leading --kube-context default-ctx is the global flag helmfile already passes from helmDefaults; a release with its own kubeContext gets the same pair of flags today, and helm uses the last one.

The fix: where ExecuteTemplates used to copy helmDefaults.kubeContext, it now stores the release's effective context from getKubeContext, so the environment's kubeContext wins and .Release.KubeContext, hooks and release IDs name the context helm uses. A release's own kubeContext still wins over both. The same holds when only the environment sets kubeContext: helm already got the environment's context there, but the release field stayed empty, so .Release.KubeContext rendered "" and a needs: entry with the full ID (prod-ctx/ns/app) failed with depend(s) on an undefined release. Both now use the environment's context. needs: [app] and needs: [ns/app] keep matching, with or without a stored context.

--kube-context keeps winning. The loader writes the flag's value into helmDefaults.kubeContext as well as OverrideKubeContext, so the new code alone would have let the environment's kubeContext beat the flag while templates render (helm would still get the flag later, from ApplyOverrides). getKubeContext now returns OverrideKubeContext first, the order ApplyOverrides already applies to every release, and its comment says so. With -e prod --kube-context cli-ctx, sync, diff and template pass --kube-context cli-ctx and render ns-cli-ctx, as on main. A top-level kubeContext: is the same override, so with it and helmDefaults.kubeContext both set, .Release.KubeContext and release IDs now name the top-level context, which helm already received.

The helmDefaults and environments sections of docs/configuration.md now state the precedence. Releases in a sub-helmfile still do not get the parent environment's kubeContext (getKubeContext reads st.Environments, while the parent's value lands only in st.Env.KubeContext); that is the same on main and is #1213, so I left it out of this change.

Behavior change on upgrade:

  • A setup with different values in helmDefaults.kubeContext and an environment's kubeContext deploys that environment's releases to the environment's context, not the helmDefaults one, which is the fix. helmfile build output shows the same change. Release IDs in that setup change the same way, so a needs: entry that spells out the helmDefaults context (default-ctx/ns/app) has to name the environment's context (prod-ctx/ns/app). Releases already installed in the helmDefaults context stay there: the next sync installs them in the environment's context, and destroy no longer looks in the old one, so the old copies have to be removed by hand (helm uninstall app --kube-context default-ctx --namespace ns).
  • With only an environment kubeContext, templates that read .Release.KubeContext now render that context instead of an empty string. The cluster helm targets does not change. The default helmfile fetch output path uses .Release.KubeContext, so it gains the context segment (out/ns/a/... becomes out/ns/prod-ctx/a/...), and with both values set the segment changes from the helmDefaults context to the environment's. The go-getter chart cache directory is built the same way, so such a chart is downloaded once more. A needs: entry with an explicitly empty context (/ns/app) no longer matches a release whose context comes from the environment; app, ns/app and prod-ctx/ns/app do.
  • Two releases with the same name and namespace, one with kubeContext: prod-ctx and one that only inherits prod-ctx from the environment, now fail with found 2 duplicate releases with ID "prod-ctx/ns/a". Before, both were upgraded to the same cluster.

Tests:

  • TestHelmState_executeTemplatesKubeContext, a table over helmDefaults only, environment over helmDefaults, release over both, environment only, no context anywhere, the top-level kubeContext over helmDefaults, --kube-context over the environment, and --kube-context with a release's own kubeContext (templates still render the release's context, as on main). It checks the effective context, the stored field and a templated namespace. On main four cases fail: environment over helmDefaults (expected "env-context", actual "default-context"), environment only (actual ""), the top-level kubeContext (actual "default-context"), and --kube-context with a release's own kubeContext, where getKubeContext returns the release's context instead of the flag's.
  • TestHelmState_getKubeContext gains a row where --kube-context beats the release and environment contexts.
  • TestListKubeContextPrecedence goes through the desired state loader (which writes --kube-context into helmDefaults.kubeContext too) and reads the context back from a templated namespace: environment over helmDefaults, environment only, and --kube-context over the environment. On main the first two fail (actual "ns-default-context" and actual "ns-"); without the override check in getKubeContext the third fails (actual "ns-env-context").
  • go test ./pkg/state/ ./pkg/app/ passes; gofmt and go vet ./pkg/state/ ./pkg/app/ are clean.
  • TestTemplateNeedsWithEnvironmentKubeContext templates a release with needs: [prod-ctx/ns/a] where only the environment sets the context; on main it fails with the undefined-release error.
  • With a stub helm, sync, diff and template give the before/after above; with --kube-context the arguments match main; with only an environment kubeContext, needs: [prod-ctx/ns/a] fails on main and passes here.

- ExecuteTemplates copied helmDefaults.kubeContext into every release without its own context, so
  getKubeContext returned it before reaching the environment's kubeContext. Each such release now
  stores its effective context, so helm, .Release.KubeContext, hooks, release IDs and needs name
  the context it deploys to. With only an environment kubeContext the field used to stay empty:
  templates rendered "" and a needs entry with the full ID failed as an undefined release.
- getKubeContext returns OverrideKubeContext (--kube-context or the top-level kubeContext) first,
  the order ApplyOverrides already applies. The loader also writes --kube-context into
  helmDefaults.kubeContext, so without this the environment would beat the flag in templates.
- Tests cover the precedence table through ExecuteTemplates and through the desired state loader,
  a needs entry that spells out the environment's context, and a release with its own kubeContext
  under --kube-context, whose templates keep rendering the release's context.
- docs/configuration.md states the precedence in the helmDefaults and environments sections.

Signed-off-by: Cayan <[email protected]>
@yxxhero
yxxhero merged commit 1eb95f5 into helmfile:main Oct 23, 2026
17 checks passed

@yxxhero yxxhero left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix! I verified the root cause: pre-patch, ExecuteTemplates (pkg/state/state_exec_tmpl.go:112) copied helmDefaults.kubeContext into every release without its own context, so getKubeContext returned it at the first step and the environment branch added in #2363 was effectively dead code whenever helmDefaults.kubeContext was set. Storing st.getKubeContext(release) instead restores the documented precedence (--kube-context/top-level > release > environment > helmDefaults) consistently across sync/apply/diff/destroy/delete/status/lint/template/write-values and the kubedog/chartify paths.

The new tests cover env-over-defaults, env-only, top-level-over-defaults, and needs-with-env-context, and fail on the old code. Nothing blocking from my side.

A few non-blocking notes for possible follow-ups (all pre-existing, not introduced by this PR):

  • Sub-helmfiles using inherits: [environments] receive the parent env's kubeContext only via st.Env.KubeContext (pkg/state/create.go:444-446), never in the child's Environments map, so getKubeContext still misses it there (gap from #2363).
  • When both an inherited (parent) env and the child's own env set kubeContext, st.Env.KubeContext (parent's) and st.Environments[name].KubeContext (child's) disagree; worth a deliberate precedence decision.
  • getHelm (pkg/app/app.go:1029) keys its global helm instance on st.HelmDefaults.KubeContext and doesn't consider the env kubeContext (per-release invocations pass --kube-context explicitly, so impact is limited to env-secrets decryption etc.).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants