fix: pass --kube-context to helm template when using jsonPatches (#2363)

fix: pass --kube-context to helm template when using jsonPatches (#2309)

When using jsonPatches or strategicMergePatches in helmfile, the
`helm template` command was not receiving the `--kube-context` flag.
This caused issues when `--dry-run=server` was used (introduced in
PR #2271 to support lookup() functions), because helm would connect
to the wrong cluster context.

Root Cause:
1. `flagsForTemplate()` did not call `appendConnectionFlags()`, unlike
   `flagsForUpgrade()` and `flagsForDiff()` which both include this call.
2. `processChartification()` did not include `--kube-context` when
   setting `chartifyOpts.TemplateArgs` for internal helm template calls.

Fix:
1. Added `appendConnectionFlags()` call to `flagsForTemplate()` to ensure
   kube-context and other connection flags are passed to helm template.
2. Added `getKubeContext()` helper function that resolves kube-context
   with proper priority: release > environment > helmDefaults.
3. Modified `processChartification()` to include `--kube-context` in
   chartifyOpts.TemplateArgs when chartify needs to run helm template.
4. Added compatibility check for `--validate` flag to avoid Helm 4
   mutual exclusion error between --validate and --dry-run (Issue #2355).

Fixes #2309

Signed-off-by: Aditya Menon <amenon@canarytechnologies.com>
This commit is contained in:
Aditya Menon
2026-01-16 20:32:33 +08:00
committed by GitHub
parent 8a66d26a10
commit c4a828686e
8 changed files with 367 additions and 36 deletions
+1
View File
@@ -119,6 +119,7 @@ ${kubectl} create namespace ${test_ns} || fail "Could not create namespace ${tes
. ${dir}/test-cases/issue-2291.sh
. ${dir}/test-cases/oci-parallel-pull.sh
. ${dir}/test-cases/issue-2297-local-chart-transformers.sh
. ${dir}/test-cases/issue-2309-kube-context-template.sh
# ALL DONE -----------------------------------------------------------------------------------------------------------
@@ -0,0 +1,32 @@
# Issue #2309: Test that helm template receives --kube-context when using jsonPatches
# https://github.com/helmfile/helmfile/issues/2309
#
# This test verifies that helmfile correctly passes --kube-context to helm template
# when helmDefaults.kubeContext is set and jsonPatches are used.
issue_2309_case_input_dir="${cases_dir}/issue-2309-kube-context-template/input"
issue_2309_case_output_dir="${cases_dir}/issue-2309-kube-context-template/output"
config_file="helmfile.yaml.gotmpl"
issue_2309_tmp=$(mktemp -d)
issue_2309_template_output=${issue_2309_tmp}/issue_2309.template.log
test_start "helmfile template with kube-context and jsonPatches (issue #2309)"
info "Running helmfile template with kubeContext and jsonPatches"
${helmfile} -f ${issue_2309_case_input_dir}/${config_file} template > ${issue_2309_template_output} || fail "\"helmfile template\" shouldn't fail"
info "Checking template output"
cat ${issue_2309_template_output}
# Verify the output contains the patched ConfigMap
if grep -q "patched: \"true\"" ${issue_2309_template_output}; then
info "Found patched value in output - jsonPatches applied successfully"
else
fail "jsonPatches were not applied - missing 'patched: true' in output"
fi
# Compare with expected output
./dyff between -bs ${issue_2309_case_output_dir}/template ${issue_2309_template_output} || fail "\"helmfile template\" output should match expected"
test_pass "helmfile template with kube-context and jsonPatches (issue #2309)"
@@ -0,0 +1,30 @@
# Issue #2309: helm template should receive --kube-context when using jsonPatches
# https://github.com/helmfile/helmfile/issues/2309
helmDefaults:
kubeContext: test-context
repositories:
- name: incubator
url: https://charts.helm.sh/incubator
---
releases:
- name: test-release
chart: incubator/raw
namespace: default
values:
- resources:
- apiVersion: v1
kind: ConfigMap
metadata:
name: test-configmap
data:
key: value
jsonPatches:
- target:
version: v1
kind: ConfigMap
name: test-configmap
patch:
- op: add
path: /data/patched
value: "true"
@@ -0,0 +1,15 @@
---
# Source: raw/templates/patched_resources.yaml
apiVersion: v1
data:
key: value
patched: "true"
kind: ConfigMap
metadata:
labels:
app: raw
chart: raw-0.2.5
heritage: Helm
release: test-release
name: test-configmap