Skip to content

feat: add --kube-context flag support#924

Merged
yxxhero merged 4 commits intomasterfrom
feat/kube-context-flag
Feb 14, 2026
Merged

feat: add --kube-context flag support#924
yxxhero merged 4 commits intomasterfrom
feat/kube-context-flag

Conversation

@yxxhero
Copy link
Collaborator

@yxxhero yxxhero commented Feb 1, 2026

Summary

  • Add native --kube-context flag to all helm-diff commands (upgrade, revision, rollback, release)
  • Allow users to specify kubeconfig context directly instead of using HELM_KUBECONTEXT environment variable
  • Pass the context through to helm commands for consistent behavior

Changes

  • Added kubeContext field to command structs (diffCmd, revision, rollback, release)
  • Updated getRelease, getHooks, getRevision, and getChart functions to accept and use kubeContext parameter
  • Added --kube-context flag to all commands
  • Pass context to helm commands and action configuration

Fixes #603

Add native --kube-context flag to all helm-diff commands (upgrade, revision,
rollback, release) to allow users to specify kubeconfig context directly
instead of relying on HELM_KUBECONTEXT environment variable workaround.

Fixes #603

Signed-off-by: yxxhero <aiopsclub@163.com>
Copy link

Copilot AI left a comment

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 adds first-class support for a --kube-context flag across helm-diff commands so users can select a kubeconfig context without relying solely on the HELM_KUBECONTEXT workaround, and ensures that this context is consistently propagated to underlying Helm operations.

Changes:

  • Extend diffCmd, revision, rollback, and release structs to carry a kubeContext field and expose a --kube-context flag on upgrade, revision, rollback, and release commands.
  • Thread kubeContext through helper functions (getRelease, getHooks, getRevision, getChart), the templating path, and value-reuse logic so Helm CLI invocations receive --kube-context (and --namespace where appropriate).
  • For three-way-merge/ownership flows, set envSettings.KubeContext before initializing action.Configuration so the Helm SDK also respects the chosen context.

Reviewed changes

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

Show a summary per file
File Description
cmd/upgrade.go Adds kubeContext to diffCmd, wires a --kube-context flag into helm diff upgrade, and passes the context through to getRelease, getHooks, template, and the Helm SDK action configuration where three-way merge/ownership is used.
cmd/rollback.go Adds a kubeContext field and --kube-context flag to the rollback command and passes that context into getRelease/getRevision calls so rollbacks use the selected kube context.
cmd/revision.go Extends the revision command with a kubeContext field and flag and forwards it into getRelease/getRevision so revision comparisons run against the correct context.
cmd/release.go Adds kubeContext support and flag to the release comparison command and threads it into getRelease/getChart for both releases being compared.
cmd/helm.go Updates Helm helper functions to accept and append --kube-context (and now --namespace for some calls) and ensures template and writeExistingValues propagate the selected context to the underlying helm CLI.

Comment on lines +145 to 149
func getHooks(release, namespace, kubeContext string) ([]byte, error) {
args := []string{"get", "hooks", release}
if namespace != "" {
args = append(args, "--namespace", namespace)
}
Copy link

Copilot AI Feb 1, 2026

Choose a reason for hiding this comment

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

The new kubeContext plumbing in the Helm CLI helper functions (e.g., getRelease/getHooks/getRevision/getChart) is not covered by tests, even though we use a fake HELM_BIN in main_test.go to assert exact argument lists. It would be good to add tests that run a diff command with --kube-context and extend the stubs to expect the --kube-context flag so regressions in how the context is forwarded to Helm are caught early.

Copilot uses AI. Check for mistakes.
Comment on lines 191 to +207
flags = append(flags, "--namespace", d.namespace)
}
if d.kubeContext != "" {
flags = append(flags, "--kube-context", d.kubeContext)
}
Copy link

Copilot AI Feb 1, 2026

Choose a reason for hiding this comment

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

template now appends --kube-context (and --namespace) to the Helm CLI flags, but there is no test asserting that these flags are actually passed through. Please add coverage (using the fake HELM_BIN harness) for a helm diff upgrade invocation with --kube-context so changes to this flag wiring do not silently break context selection.

Copilot uses AI. Check for mistakes.
Comment on lines +430 to +434
if d.namespace != "" {
args = append(args, "--namespace", d.namespace)
}
if d.kubeContext != "" {
args = append(args, "--kube-context", d.kubeContext)
Copy link

Copilot AI Feb 1, 2026

Choose a reason for hiding this comment

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

writeExistingValues now forwards --namespace and --kube-context to helm get values, but this behavior is not exercised by tests. Consider adding a test path that triggers value reuse (e.g., --reuse-values/--reset-then-reuse-values) with --kube-context set and updating the fake Helm stubs to expect these flags, to ensure existing-values resolution stays aligned with the selected kube context.

Copilot uses AI. Check for mistakes.
Add tests to verify that --kube-context flag is properly passed to
helm commands, addressing review comments from PR #924:

- Test basic --kube-context flag propagation to helm commands
- Test --kube-context with --reuse-values flag
- Test that --namespace is also passed when using --kube-context
- Add wildcard argument matching for dynamic temp file paths

Signed-off-by: yxxhero <aiopsclub@163.com>
…commands

Signed-off-by: yxxhero <aiopsclub@163.com>
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

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

cmd/upgrade.go Outdated
Comment on lines 299 to 302
if d.kubeContext != "" {
envSettings.KubeContext = d.kubeContext
}
if err := actionConfig.Init(envSettings.RESTClientGetter(), envSettings.Namespace(), os.Getenv("HELM_DRIVER")); err != nil {
Copy link

Copilot AI Feb 14, 2026

Choose a reason for hiding this comment

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

envSettings is a package-level variable and runHelm3 mutates envSettings.KubeContext when --kube-context is set. Because it is not reset, subsequent executions in the same process (e.g., tests or library usage) can unintentionally reuse the previous kube context, and it also introduces a potential data race if commands are ever run concurrently. Prefer using a fresh cli.New() (or copying the settings) inside runHelm3 for actionConfig.Init, or save/restore the previous KubeContext value around the initialization.

Suggested change
if d.kubeContext != "" {
envSettings.KubeContext = d.kubeContext
}
if err := actionConfig.Init(envSettings.RESTClientGetter(), envSettings.Namespace(), os.Getenv("HELM_DRIVER")); err != nil {
// Use a fresh copy of envSettings to avoid mutating shared state.
localEnv := cli.New()
*localEnv = *envSettings
if d.kubeContext != "" {
localEnv.KubeContext = d.kubeContext
}
if err := actionConfig.Init(localEnv.RESTClientGetter(), localEnv.Namespace(), os.Getenv("HELM_DRIVER")); err != nil {

Copilot uses AI. Check for mistakes.
main_test.go Outdated
},
{
cmd: []string{"get", "values"},
args: []string{"test-release", "--output", "yaml", "--all", "--kube-context", "test-context", "--namespace", "*"},
Copy link

Copilot AI Feb 14, 2026

Choose a reason for hiding this comment

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

The fake helm stub for get values with both --kube-context and --namespace has the flags in a different order than the production code builds them (writeExistingValues appends --namespace before --kube-context). If a test run happens to set HELM_NAMESPACE (making d.namespace non-empty), the actual invocation will be ... --namespace <ns> --kube-context test-context and this stub won't match, causing a hard-to-diagnose test failure. Update the stub (or matching logic) so it matches the real argument order.

Suggested change
args: []string{"test-release", "--output", "yaml", "--all", "--kube-context", "test-context", "--namespace", "*"},
args: []string{"test-release", "--output", "yaml", "--all", "--namespace", "*", "--kube-context", "test-context"},

Copilot uses AI. Check for mistakes.
Comment on lines 43 to 62
func TestHelmDiffWithKubeContext(t *testing.T) {
os.Setenv(env, envValue)
defer os.Unsetenv(env)

helmBin, helmBinSet := os.LookupEnv("HELM_BIN")
os.Setenv("HELM_BIN", os.Args[0])
defer func() {
if helmBinSet {
os.Setenv("HELM_BIN", helmBin)
} else {
os.Unsetenv("HELM_BIN")
}
}()

oldArgs := os.Args
defer func() { os.Args = oldArgs }()

os.Args = []string{"helm-diff", "upgrade", "-f", "test/testdata/test-values.yaml", "--kube-context", "test-context", "test-release", "test/testdata/test-chart"}
require.NoError(t, cmd.New().Execute())
}
Copy link

Copilot AI Feb 14, 2026

Choose a reason for hiding this comment

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

These tests duplicate the same environment setup/teardown logic (BECOME_FAKE_HELM, HELM_BIN, and os.Args) across many functions, which makes future edits error-prone. Consider extracting a small helper (or a table-driven test) to centralize this setup and reduce repetition.

Copilot uses AI. Check for mistakes.
- Use local copy of envSettings in actionConfig.Init to avoid mutating
  shared state and prevent data races
- Fix test stub flag order to match production code (--namespace before
  --kube-context)
- Extract helmDiffTestHelper to reduce duplicated test setup code

Signed-off-by: yxxhero <aiopsclub@163.com>
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

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

@yxxhero yxxhero merged commit 1c05fd9 into master Feb 14, 2026
30 checks passed
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.

[feature request] support for --kube-context

1 participant