Skip to content

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

Merged
yxxhero merged 1 commit into
helmfile:mainfrom
aditmeno:fix/issue-2309-kube-context-jsonpatches
Jan 16, 2026
Merged

yxxhero merged 1 commit into
helmfile:mainfrom
aditmeno:fix/issue-2309-kube-context-jsonpatches

Conversation

@aditmeno

Copy link
Copy Markdown
Contributor

Summary

Fixes #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 instead of the one specified in helmDefaults.kubeContext.

Example that was failing:

helmDefaults:
  kubeContext: minikube

releases:
  - name: test
    chart: ./test
    jsonPatches:
      - target:
          group: apps
          version: v1
          kind: Deployment
          name: test
        patch:
          - op: add
            path: /spec/template/spec/containers/0/args/-
            value: "test"

Running helmfile diff would fail because the internal helm template call (used by chartify for jsonPatches) did not receive --kube-context=minikube.

Root Cause

  1. flagsForTemplate() missing connection flags: The function did NOT call appendConnectionFlags(), unlike flagsForUpgrade() and flagsForDiff() which both include this call.

  2. processChartification() missing kube-context: When --dry-run=server is added to chartifyOpts.TemplateArgs, the --kube-context flag was not included.

Changes

File Change
pkg/state/state.go Added appendConnectionFlags() call to flagsForTemplate()
pkg/state/state.go Added new getKubeContext() helper function with priority: release > environment > helmDefaults
pkg/state/state.go Modified processChartification() to include --kube-context in chartify's TemplateArgs
pkg/state/state_test.go Added unit tests for flagsForTemplate() kube-context handling
pkg/state/state_test.go Added unit tests for getKubeContext() priority logic
pkg/app/app_test.go Updated test expectations for new --kube-context flag
pkg/app/app_template_test.go Updated test expectations for new --kube-context flag
test/integration/test-cases/ Added integration test for issue #2309

Compatibility with PR #2362 (Issue #2355)

This PR includes a compatibility check for the --validate flag to avoid Helm 4's mutual exclusion error between --validate and --dry-run. When opts.Validate is set, we skip adding --dry-run=server to prevent the conflict.

Test Coverage

Unit Tests Added

  1. TestHelmState_flagsForTemplate - 3 new test cases:

    • Tests kube-context from helmDefaults.kubeContext
    • Tests kube-context from release.kubeContext (override)
    • Tests kube-context from environment
  2. TestHelmState_getKubeContext - 5 test cases:

    • No kube-context set (returns empty)
    • Only helmDefaults.kubeContext set
    • Release kubeContext overrides helmDefaults
    • Environment kubeContext used when release not set
    • Release kubeContext takes priority over environment

Integration Test Added

  • test/integration/test-cases/issue-2309-kube-context-template.sh
  • Tests helmfile template with kubeContext and jsonPatches
  • Verifies that jsonPatches are applied correctly with kube-context

How to Test

# Run unit tests
go test ./pkg/state/... -v -run "TestHelmState_flagsForTemplate|TestHelmState_getKubeContext"

# Run all unit tests
go test ./...

# Run integration tests (requires minikube)
make integration

Checklist

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes an issue where the helm template command does not receive the --kube-context flag when using jsonPatches or strategicMergePatches. This became problematic after PR #2271 introduced --dry-run=server to support lookup() functions, which requires correct cluster context to connect to the right cluster.

Changes:

  • Added appendConnectionFlags() call to flagsForTemplate() to include kube-context in template flags
  • Added getKubeContext() helper function to resolve kube-context with proper priority (release > environment > helmDefaults)
  • Modified processChartification() to pass --kube-context to chartify's internal helm template call
  • Added compatibility check for --validate flag to avoid Helm 4 mutual exclusion error with --dry-run

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
pkg/state/state.go Added getKubeContext() helper, modified processChartification() to include --kube-context in chartify template args, added appendConnectionFlags() to flagsForTemplate()
pkg/state/state_test.go Added unit tests for flagsForTemplate() kube-context handling and getKubeContext() priority logic
pkg/app/app_test.go Updated test expectations to include new --kube-context flag
pkg/app/app_template_test.go Updated test expectations to include new --kube-context flag
test/integration/test-cases/issue-2309-kube-context-template.sh Added integration test verifying jsonPatches work with kube-context
test/integration/test-cases/issue-2309-kube-context-template/input/helmfile.yaml.gotmpl Test configuration file with kubeContext and jsonPatches
test/integration/test-cases/issue-2309-kube-context-template/output/template Expected template output for integration test
test/integration/run.sh Registered new integration test in test runner

Comment thread pkg/state/state.go Resolved
Comment thread pkg/state/state_test.go Resolved
Comment thread pkg/state/state_test.go Resolved
…mfile#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 helmfile#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 helmfile#2355).

Fixes helmfile#2309

Signed-off-by: Aditya Menon <[email protected]>
@aditmeno
aditmeno force-pushed the fix/issue-2309-kube-context-jsonpatches branch from 5798d9f to 1418911 Compare January 15, 2026 15:18
@aditmeno
aditmeno requested a review from Copilot January 15, 2026 15:19

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot reviewed 8 out of 8 changed files in this pull request and generated no new comments.

@yxxhero
yxxhero merged commit c4a8286 into helmfile:main Jan 16, 2026
28 checks passed
@aditmeno
aditmeno deleted the fix/issue-2309-kube-context-jsonpatches branch January 16, 2026 17:06
aditmeno added a commit to aditmeno/helmfile that referenced this pull request Jan 16, 2026
Resolves merge conflicts from PR helmfile#2363 (kube-context fix):
- Accepted main's comprehensive processChartification() implementation
  which includes both the kube-context fix and the validate flag check
- Added both integration tests to run.sh

Signed-off-by: Aditya Menon <[email protected]>
@tomcruz17

Copy link
Copy Markdown

@yxxhero @mumoshu Any chance you can get a release in with this fix? Thank you!

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.

helm template does not receive the --kube-context arg if kustomize (jsonPatches) is used

4 participants