Skip to content

kubectl: forward --context and --cluster into completion generators - #332

Closed
warp-agent-staging[bot] wants to merge 3 commits into
mainfrom
factory/app-46-kubectl-context-forwarding
Closed

kubectl: forward --context and --cluster into completion generators#332
warp-agent-staging[bot] wants to merge 3 commits into
mainfrom
factory/app-46-kubectl-context-forwarding

Conversation

@warp-agent-staging

@warp-agent-staging warp-agent-staging Bot commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Description

The kubectl generators build their query command from the tokens already on the line, so completions are scoped to what the user has typed. Only --kubeconfig and --namespace/-n were forwarded, so a --context (or --cluster) written on the command line was silently dropped. With

kubectl --context staging-cluster --namespace <TAB>

the generated command was kubectl … get namespace -o custom-columns=:.metadata.name — no --context — so the suggestions came from the shell's active context rather than from staging-cluster. The same omission affected the resource and api-resources generators.

This is the one remaining symptom of warpdotdev/warp#5186 (the --context/--cluster value completions themselves were fixed by #247), and it is what warpdotdev/warp#3929 asks for directly (kubectl --context staging-cluster -n project1 get pods).

Changes

Three independent commits, in order:

  1. kubectl: forward --context and --cluster — mirrors the existing --kubeconfig / --namespace handling via space_or_equals_delimited_option_value, so both --flag value and --flag=value work. Forwarded values are now shell-quoted: tokens reach generators with their shell quoting already stripped, so a value containing whitespace previously split into multiple arguments and produced a broken command. Quoting uses single quotes with ' escaped as '\'', the technique git.rs and scp.rs already use for interpolated token values, which also neutralizes $, backticks and backslashes.
  2. kubectl: complete --user valueskubectl --user <TAB> had no generator and fell back to file paths; kubectl config get-users supplies them.
  3. kubecolor, oc: complete --context, --cluster and --user values — both specs declare these as persistent options with no generatorName despite already registering kubectl's generators, so kubecolor get --context <TAB> still showed file paths. This is the un-migrated version of what Fix kubectl --context and --cluster persistent flag completions #247 fixed for kubectl. Commit 3 depends on commit 2 for the user generator; dropping commit 2 means dropping the --user lines in commit 3 as well.

Commits 2 and 3 are drive-by wiring — drop either without affecting the fix in commit 1.

Overlap with #331

#331 was opened ~15 minutes before this PR and independently fixes the same --context/--cluster forwarding gap, plus --user. Only one of these should land. The differences, so a reviewer can pick quickly:

If #331 lands, the useful remainder of this PR is its third commit (kubecolor/oc wiring) plus the full-command test, which can be rebased on top. The companion rev bump in warpdotdev/warp needs to be re-pointed at whichever commit lands.

Notes

  • No regression risk to the already-working --context/--cluster value completion. While completing those values the partial value gets forwarded into config get-contexts / get-clusters, which kubectl accepts and still answers with the full list (exit 0).
  • Worst case is empty completions, not wrong ones. An invalid context on a cluster query exits non-zero, which the completer turns into zero suggestions rather than surfacing error text.
  • --user is deliberately not forwarded into the generated commands; only its own value completion is added. Forwarding it was outside the scope of this fix.
  • For oc, the context/cluster/user generators run kubectl (they are kubectl's generators, shared). That matches how oc's existing --namespace generator already behaves.

Testing

./script/presubmit passes: prettier JSON check, cargo fmt, cargo clippy --all-targets --all-features -D warnings, and cargo test (161 tests).

New and tightened tests assert the generated command, not merely that completion happens:

  • test_full_generated_command_forwards_context_to_namespace_query asserts the complete command string for the reported case.
  • test_context_long_flag_before_subcommand, test_context_flag_after_subcommand, test_context_equals_syntax, test_cluster_flag_forwarded, test_cluster_equals_syntax.
  • test_no_flags_forwarded_when_absent, test_forwarded_values_with_whitespace_are_quoted, test_forwarded_value_with_single_quote_is_escaped and test_forwarded_value_does_not_expand_shell_metacharacters.
  • test_context_and_namespace_flags_before_subcommand already passed a --context but only asserted namespace forwarding, leaving the gap unasserted. It now checks both, so the gap cannot silently reopen.

No visual capture was taken. Verifying this in a running client means building the full Warp client against a bumped warp-command-signatures rev, which is expensive relative to the value here; #247 already carries screenshots of the --context/--cluster value completion working, and the assertions above cover the generated command precisely.

A follow-up PR in warpdotdev/warp bumps the pinned warp-command-signatures rev so this reaches the client.

@warp-agent-staging

Copy link
Copy Markdown
Contributor Author

This PR was generated with Warp.

View run View conversation

The kubectl generators build their query command from the tokens already
on the line so completions are scoped to what the user has typed. Only
--kubeconfig and --namespace/-n were forwarded, so a --context (or
--cluster) written on the command line was dropped: with

    kubectl --context staging-cluster --namespace <TAB>

the generated command was

    kubectl ... get namespace -o custom-columns=:.metadata.name

which enumerates namespaces from the shell's active context rather than
from staging-cluster. Forward both flags, mirroring the existing
--kubeconfig and --namespace handling.

Forwarded values are now double-quoted. Tokens reach generators with
their shell quoting already stripped, so a value containing whitespace
previously split into multiple arguments and produced a broken command.

Tests now assert the forwarded flags in the generated command, including
one assertion on the full command string, and
test_context_and_namespace_flags_before_subcommand -- which passed a
--context but only checked namespace forwarding -- now checks both so
the gap cannot silently reopen.

Co-Authored-By: Warp Agent <agent@warp.dev>
`kubectl --user <TAB>` had no generator, so it fell back to file path
completions. `kubectl config get-users` lists the users defined in the
kubeconfig; wire it up the same way `--cluster` uses `config
get-clusters`, filtering out the "NAME" header since `config get-users`
has no `-o name` form.

Co-Authored-By: Warp Agent <agent@warp.dev>
Both specs declare persistent --context, --cluster and --user options
with no generatorName, so `kubecolor get --context <TAB>` fell back to
file path completions -- the un-migrated version of what
#247 fixed for kubectl. Both already
register kubectl's generators, so this is spec wiring plus registering
the new `user` generator.

Note this commit depends on the preceding `user` generator commit;
dropping that one requires dropping the --user lines here too.

Co-Authored-By: Warp Agent <agent@warp.dev>
@warp-agent-staging
warp-agent-staging Bot force-pushed the factory/app-46-kubectl-context-forwarding branch from 24f7cca to c85b0a9 Compare August 13, 2026 23:31
warp-agent-staging Bot pushed a commit to warpdotdev/warp that referenced this pull request Aug 13, 2026
Picks up warpdotdev/command-signatures#332, which forwards a --context
or --cluster written on the command line into the commands the kubectl
completion generators run. Without it, completions after a --context
enumerated from the shell's active context instead of the one on the
line (#5186, #3929).

Co-Authored-By: Warp Agent <agent@warp.dev>
@warp-agent-staging

Copy link
Copy Markdown
Contributor Author

Consolidated into #331, which fixes the same gap and was opened first. Its unique parts — the kubecolor.json/oc.json wiring and the full-generated-command assertion — have been ported over there, and #331's stronger flag resolution (last-occurrence-wins, stops at a bare --) is kept. Closing to leave one PR for this work.

Responding as wilson: Open session · View factory task

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant