Repository navigation
fix: honor environment kubeContext over helmDefaults.kubeContext - #2841
Conversation
- 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
left a comment
There was a problem hiding this comment.
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 viast.Env.KubeContext(pkg/state/create.go:444-446), never in the child'sEnvironmentsmap, sogetKubeContextstill 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) andst.Environments[name].KubeContext(child's) disagree; worth a deliberate precedence decision. getHelm(pkg/app/app.go:1029) keys its global helm instance onst.HelmDefaults.KubeContextand doesn't consider the env kubeContext (per-release invocations pass--kube-contextexplicitly, so impact is limited to env-secrets decryption etc.).
When both
helmDefaults.kubeContextand an environment'skubeContextare set, releases that set nokubeContextof their own run against thehelmDefaultscontext, not the environment's.getKubeContextdocuments the order release, then environment, thenhelmDefaults(added for #2309), butExecuteTemplatescopieshelmDefaults.kubeContextinto every release without one, sogetKubeContextreturns it at the first step and never reaches the environment.Example:
helmfile -e prod syncwith a stubhelmthat logs its arguments:--kube-context default-ctx upgrade --install app chart ... --kube-context default-ctx --namespace ns-default-ctx ...--kube-context default-ctx upgrade --install app chart ... --kube-context prod-ctx --namespace ns-prod-ctx ...diffandapplychange the same way. The leading--kube-context default-ctxis the global flag helmfile already passes fromhelmDefaults; a release with its ownkubeContextgets the same pair of flags today, and helm uses the last one.The fix: where
ExecuteTemplatesused to copyhelmDefaults.kubeContext, it now stores the release's effective context fromgetKubeContext, so the environment'skubeContextwins and.Release.KubeContext, hooks and release IDs name the context helm uses. A release's ownkubeContextstill wins over both. The same holds when only the environment setskubeContext: helm already got the environment's context there, but the release field stayed empty, so.Release.KubeContextrendered""and aneeds:entry with the full ID (prod-ctx/ns/app) failed withdepend(s) on an undefined release. Both now use the environment's context.needs: [app]andneeds: [ns/app]keep matching, with or without a stored context.--kube-contextkeeps winning. The loader writes the flag's value intohelmDefaults.kubeContextas well asOverrideKubeContext, so the new code alone would have let the environment'skubeContextbeat the flag while templates render (helm would still get the flag later, fromApplyOverrides).getKubeContextnow returnsOverrideKubeContextfirst, the orderApplyOverridesalready applies to every release, and its comment says so. With-e prod --kube-context cli-ctx,sync,diffandtemplatepass--kube-context cli-ctxand renderns-cli-ctx, as onmain. A top-levelkubeContext:is the same override, so with it andhelmDefaults.kubeContextboth set,.Release.KubeContextand release IDs now name the top-level context, which helm already received.The
helmDefaultsand environments sections ofdocs/configuration.mdnow state the precedence. Releases in a sub-helmfile still do not get the parent environment'skubeContext(getKubeContextreadsst.Environments, while the parent's value lands only inst.Env.KubeContext); that is the same onmainand is #1213, so I left it out of this change.Behavior change on upgrade:
helmDefaults.kubeContextand an environment'skubeContextdeploys that environment's releases to the environment's context, not thehelmDefaultsone, which is the fix.helmfile buildoutput shows the same change. Release IDs in that setup change the same way, so aneeds:entry that spells out thehelmDefaultscontext (default-ctx/ns/app) has to name the environment's context (prod-ctx/ns/app). Releases already installed in thehelmDefaultscontext stay there: the nextsyncinstalls them in the environment's context, anddestroyno 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).kubeContext, templates that read.Release.KubeContextnow render that context instead of an empty string. The cluster helm targets does not change. The defaulthelmfile fetchoutput path uses.Release.KubeContext, so it gains the context segment (out/ns/a/...becomesout/ns/prod-ctx/a/...), and with both values set the segment changes from thehelmDefaultscontext to the environment's. The go-getter chart cache directory is built the same way, so such a chart is downloaded once more. Aneeds:entry with an explicitly empty context (/ns/app) no longer matches a release whose context comes from the environment;app,ns/appandprod-ctx/ns/appdo.kubeContext: prod-ctxand one that only inheritsprod-ctxfrom the environment, now fail withfound 2 duplicate releases with ID "prod-ctx/ns/a". Before, both were upgraded to the same cluster.Tests:
TestHelmState_executeTemplatesKubeContext, a table overhelmDefaultsonly, environment overhelmDefaults, release over both, environment only, no context anywhere, the top-levelkubeContextoverhelmDefaults,--kube-contextover the environment, and--kube-contextwith a release's ownkubeContext(templates still render the release's context, as onmain). It checks the effective context, the stored field and a templated namespace. Onmainfour cases fail: environment overhelmDefaults(expected "env-context", actual "default-context"), environment only (actual ""), the top-levelkubeContext(actual "default-context"), and--kube-contextwith a release's ownkubeContext, wheregetKubeContextreturns the release's context instead of the flag's.TestHelmState_getKubeContextgains a row where--kube-contextbeats the release and environment contexts.TestListKubeContextPrecedencegoes through the desired state loader (which writes--kube-contextintohelmDefaults.kubeContexttoo) and reads the context back from a templated namespace: environment overhelmDefaults, environment only, and--kube-contextover the environment. Onmainthe first two fail (actual "ns-default-context"andactual "ns-"); without the override check ingetKubeContextthe third fails (actual "ns-env-context").go test ./pkg/state/ ./pkg/app/passes;gofmtandgo vet ./pkg/state/ ./pkg/app/are clean.TestTemplateNeedsWithEnvironmentKubeContexttemplates a release withneeds: [prod-ctx/ns/a]where only the environment sets the context; onmainit fails with the undefined-release error.helm,sync,diffandtemplategive the before/after above; with--kube-contextthe arguments matchmain; with only an environmentkubeContext,needs: [prod-ctx/ns/a]fails onmainand passes here.