Skip to content

feat(cmd): add global --kubeconfig and --context flags - #5489

Open
a1994sc wants to merge 4 commits into
mainfrom
feature/kube-context
Open

a1994sc wants to merge 4 commits into
mainfrom
feature/kube-context

Conversation

@a1994sc

@a1994sc a1994sc commented Oct 18, 2026 •

Copy link
Copy Markdown
Member

Description

Adds global --kubeconfig and --context flags so a Zarf invocation can be pointed at a specific cluster, the way kubectl --context does.

cluster.ClientAndConfig built its client from clientcmd.NewDefaultClientConfigLoadingRules() with nil overrides, so the two slots that would carry a kubeconfig path and a context name were never filled. KUBECONFIG worked only because the default rules read it; there was no way at all to pick a context.

Both flags are also readable from a config file (kubeconfig, context) and from ZARF_KUBECONFIG / ZARF_CONTEXT.

The selection reaches everything, not just Zarf's own client. That was the point of #2509 — one place to configure Kubernetes for all the tools Zarf carries — so it is threaded through four paths:

path how
Zarf's own client cluster.ConnectOptions fills loadingRules.ExplicitPath and ConfigOverrides.CurrentContext
zarf tools kubectl|helm|monitor KUBECONFIG in the process environment
commands an action shells out to KUBECONFIG appended to the action environment
wait: actions ConnectOptions through actions.RunOptions

The Helm SDK (cli.New()) and src/pkg/wait pick it up from the same KUBECONFIG, so they agree without special casing.

zarf tools kubectl --context <name> is unchanged and still uses kubectl's own flag.

Notes for review

  • A context selection cannot be expressed in an env var, so it is written as a small overlay file placed ahead of the real kubeconfig in KUBECONFIG: clientcmd takes current-context from the first file that sets one and everything else from the rest. The overlay therefore holds a context name and no credentials, which matters because a vendored tool can call os.Exit itself and skip the cleanup. An earlier draft copied the merged kubeconfig and was dropped for that reason. Selecting only --kubeconfig writes nothing at all.
  • An unknown context is an error, not a fallback. Silently falling back to current-context would deploy to the wrong cluster.
  • The vendored-flag panic fix was split out into fix(cmd): don't redefine vendored tool flag shorthands #5496 at your request. This PR is based on that branch, so its diff no longer carries it. It is still needed here: zarf --context dev tools … is exactly the invocation shape that panics. This PR goes back to a main base once fix(cmd): don't redefine vendored tool flag shorthands #5496 merges.
  • Touches a core path. cluster.New, NewWithWait and ClientAndConfig take variadic ConnectOptions, which is source compatible, so no call site changed and this is not a breaking change.
  • dev deploy is wired too, since it builds its own DeployOptions rather than going through Deploy.
  • Overlaps feat: let library callers deploy through an existing cluster connection #5254 (DeployOptions.Cluster for library callers holding a connection). The two are complementary rather than competing: a *cluster.Cluster cannot express the selection to a shelled-out kubectl or to a wait: action, which is what ConnectOptions carries. Only the DeployOptions struct is shared; whichever merges second rebases on the other.

Testing

Unit tests cover the overlay contents and permissions, context resolution through ClientAndConfig, the preRun wiring and vendored-hook chaining, the shorthand branch, the dummy-flag guard, and ConnectOptions reaching wait and the action environment. Each was checked to fail without the change.

Verified by hand against a kubeconfig with two contexts, for package deploy/package list, tools kubectl, tools helm, config-file and env-var sources, an unknown context, and an onDeploy action running zarf tools kubectl config current-context.

Not covered: no e2e test, which would need a second cluster context in CI.

Related Issue

Fixes #4307
Fixes #2509

Relates to #5223

Checklist before merging

@a1994sc
a1994sc requested review from a team as code owners October 18, 2026 20:32
@netlify

netlify Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for zarf-docs ready!

Name Link
🔨 Latest commit 51fd372
🔍 Latest deploy log https://app.netlify.com/projects/zarf-docs/deploys/6ae3d67e9aa2c29e8b2dee85
😎 Deploy Preview https://deploy-preview-5489--zarf-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@codecov

codecov Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 71.91011% with 50 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/cmd/root.go 69.76% 13 Missing ⚠️
src/pkg/cluster/cluster.go 75.92% 10 Missing and 3 partials ⚠️
src/pkg/packager/deploy.go 67.64% 8 Missing and 3 partials ⚠️
src/pkg/packager/actions/actions.go 77.27% 5 Missing ⚠️
src/pkg/packager/dev.go 50.00% 3 Missing and 1 partial ⚠️
src/cmd/internal.go 60.00% 2 Missing ⚠️
src/pkg/wait/wait.go 50.00% 2 Missing ⚠️
Files with missing lines Coverage Δ
src/cmd/cmd.go 85.18% <100.00%> (+3.36%) ⬆️
src/cmd/helm.go 52.17% <100.00%> (+7.17%) ⬆️
src/cmd/kubectl.go 67.50% <100.00%> (+0.83%) ⬆️
src/cmd/viper.go 62.38% <ø> (ø)
src/cmd/internal.go 36.56% <60.00%> (+1.75%) ⬆️
src/pkg/wait/wait.go 27.00% <50.00%> (+5.88%) ⬆️
src/pkg/packager/dev.go 62.96% <50.00%> (-0.03%) ⬇️
src/pkg/packager/actions/actions.go 70.71% <77.27%> (+8.09%) ⬆️
src/pkg/packager/deploy.go 45.06% <67.64%> (+0.77%) ⬆️
src/cmd/root.go 44.44% <69.76%> (+6.94%) ⬆️
... and 1 more

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@a1994sc
a1994sc force-pushed the feature/kube-context branch 2 times, most recently from fc1d083 to 6ea1e4f Compare October 18, 2026 21:49
@AustinAbro321

Copy link
Copy Markdown
Member

@a1994sc Will have to dig into the details more, but I believe these are changes that we want. Thanks for the PR! Would you mind first separating the fixing the panic on vendored flags into a separate PR. I want to give space to evaluate that separately as there's multiple ways to go about it

@a1994sc
a1994sc force-pushed the feature/kube-context branch from 6ea1e4f to 99f14bc Compare October 19, 2026 18:08
@a1994sc
a1994sc changed the base branch from main to fix/vendor-flag-shorthand October 19, 2026 18:08
@a1994sc
a1994sc added this pull request to stack #5497 October 19, 2026 18:08
@a1994sc

a1994sc commented Oct 19, 2026

Copy link
Copy Markdown
Member Author

@AustinAbro321 created a seperate pr, and stacked the pr

@a1994sc
a1994sc force-pushed the feature/kube-context branch from 99f14bc to c0779b1 Compare October 20, 2026 21:32
Base automatically changed from fix/vendor-flag-shorthand to main October 27, 2026 14:32
Signed-off-by: Allen Conlon <software@conlon.dev>
Signed-off-by: Allen Conlon <software@conlon.dev>
Signed-off-by: Allen Conlon <software@conlon.dev>
Signed-off-by: Allen Conlon <software@conlon.dev>
@a1994sc
a1994sc force-pushed the feature/kube-context branch from c0779b1 to 51fd372 Compare October 29, 2026 23:27

This branch has not been deployed

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

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

feat: add --context flag for which kube-config context to use Add a --kubeconfig flag in the root command

2 participants