Skip to content

feat(cli): add velero client config set namespace-mode=auto - #10127

Open
lubronzhan wants to merge 5 commits into
velero-io:mainfrom
lubronzhan:lubronzhan/client-set-context-namespace
Open

feat(cli): add velero client config set namespace-mode=auto#10127
lubronzhan wants to merge 5 commits into
velero-io:mainfrom
lubronzhan:lubronzhan/client-set-context-namespace

Conversation

@lubronzhan

@lubronzhan lubronzhan commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Adds namespace-mode=auto as a velero client config set value. When set, Velero operational commands resolve their default namespace from the current kubeconfig context (or --kubecontext/--kubeconfig if specified) on every invocation, instead of a static stored value.
  • This lets users who run Velero against a non-default namespace stay in sync automatically as they switch kubeconfig contexts, without re-running a command or passing --namespace on every invocation.

Test plan

  • go build ./... succeeds
  • go vet / gofmt clean on new/changed files
  • go test ./pkg/client/... ./pkg/cmd/cli/client/... passes
  • Added factory tests covering: namespace resolved from kubecontext in auto mode, namespace tracking a kubecontext change, --namespace flag overriding auto mode, and VELERO_NAMESPACE overriding auto mode
  • Reviewer to confirm this addresses the use case from the issue

AI-Tool-Used: Claude Code
AI-Tool-Use-Level: Category 2 (Medium)
AI-Code-Category: Category 1 (Production)

@lubronzhan
lubronzhan requested a review from a team as a code owner July 31, 2026 23:14
@netlify

netlify Bot commented Jul 31, 2026

Copy link
Copy Markdown

👷 Deploy request for velero pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 6e76b92

@lubronzhan

Copy link
Copy Markdown
Contributor Author

@kaovilai Since you proposed this approach (set-context-as-velero-namespace) on the issue, would appreciate your review here. Thanks!

@codecov

codecov Bot commented Jul 31, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.00000% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/client/config.go 75.00% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

lubronzhan and others added 2 commits July 31, 2026 16:28
…ce command

Saves the namespace of the current (or a specified) kubeconfig context
into the Velero client config file, so operational commands default to
it without requiring --namespace on every invocation.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Signed-off-by: lubronzhan <lubron.zhan@broadcom.com>
- changelogs/unreleased must be named <pr-number>-<username>; rename
  from the 0000 placeholder to 10127 to satisfy hack/changelog-check.sh.
- Extract the command's logic into setContextAsVeleroNamespace so it's
  testable without triggering os.Exit via cmd.CheckError, and add unit
  tests covering: namespace read from context, context with no explicit
  namespace, overwriting an existing config value, and invalid
  kubeconfig path. Addresses 0% codecov patch coverage on the PR.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Signed-off-by: lubronzhan <lubron.zhan@broadcom.com>
@lubronzhan
lubronzhan force-pushed the lubronzhan/client-set-context-namespace branch from 30c710f to 95b7a84 Compare July 31, 2026 23:29
@lubronzhan lubronzhan changed the title Issue #3194: Add velero client set-context-as-velero-namespace command Add velero client set-context-as-velero-namespace command Jul 31, 2026
@lubronzhan lubronzhan changed the title Add velero client set-context-as-velero-namespace command feat(cli): add velero client set-context-as-velero-namespace command Jul 31, 2026
@Lyndon-Li
Lyndon-Li requested a review from kaovilai August 3, 2026 07:14
kaovilai
kaovilai previously approved these changes Aug 4, 2026

@kaovilai kaovilai left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Adds velero client set-context-as-velero-namespace to persist the current kubeconfig context's namespace into the Velero client config — a lower-risk alternative to changing default namespace resolution. has-unit-tests, CI green (61 checks), no outstanding review threads.

Note

Responses generated with Claude

@kaovilai

kaovilai commented Aug 4, 2026

Copy link
Copy Markdown
Member

Thanks for this easy kill. I think this is a cool PR to slightly improve usability.

@lubronzhan

Copy link
Copy Markdown
Contributor Author

Thanks for this easy kill. I think this is a cool PR to slightly improve usability.

Thanks @kaovilai is it ok to merge this PR now?

@kaovilai
kaovilai requested a review from a team August 11, 2026 18:04
@kaovilai

Copy link
Copy Markdown
Member

Our repository is set up so that two reviewers are required, so we will need another reviewer. Please wait.

@shubham-pampattiwar shubham-pampattiwar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for picking this up. The code is clean and well-tested. I have a few concerns about the approach before we merge.

CLI hierarchy: This command sits at velero client set-context-as-velero-namespace, but the existing way to set the namespace is velero client config set namespace=.... Adding a sibling command to client that does a subset of what config already handles breaks the CLI structure. This should live under velero client config if we add it.

Command naming: set-context-as-velero-namespace is hard to parse. It reads like "set the context as the velero namespace" when it actually means "read the namespace from my kubeconfig context and save it as velero's default." Something like velero client config set-from-context would be shorter and clearer.

Does this close #3194?: The original issue asks for the Velero CLI to automatically use the active namespace from the current kubeconfig context. This PR still requires the user to manually run a command every time they switch contexts. It's a convenience wrapper around the existing velero client config set namespace=... -- useful, but it doesn't address the core ask. I don't think we should close #3194 with this.

Value vs surface area: Today a user can achieve this with:

velero client config set namespace=$(kubectl config view --minify -o 'jsonpath={..namespace}')

Adding a dedicated command for this means more CLI surface to maintain and document. The tradeoff might be worth it if we get the placement and naming right, but as-is I'd want to see the command restructured before merging.

Comment thread pkg/cmd/cli/client/client.go Outdated
Comment thread pkg/cmd/cli/client/setcontextnamespace.go Outdated
@lubronzhan

lubronzhan commented Aug 12, 2026

Copy link
Copy Markdown
Contributor Author

Does this close #3194?: The original issue asks for the Velero CLI to automatically use the active namespace from the current kubeconfig context. This PR still requires the user to manually run a command every time they switch contexts. It's a convenience wrapper around the existing velero client config set namespace=... -- useful, but it doesn't address the core ask. I don't think we should close #3194 with this.

That's correct, the original ask propose a breaking change on the default behavior not sure we really wants that that's why I pick this approach.

Let me update the description.

Shubham suggested nesting the new command under `config` for
hierarchy consistency, and renaming it since the original
set-context-as-velero-namespace name was long and ambiguous. Moves
it to `velero client config set-namespace-from-context`, matching
the existing config get/set subcommands and my follow-up naming
suggestion on the review thread.

AI-Tool-Used: Claude Code
AI-Tool-Use-Level: Category 3 (Low)
AI-Code-Category: Category 1 (Production)
Signed-off-by: lubronzhan <lubron.zhan@broadcom.com>
@lubronzhan
lubronzhan force-pushed the lubronzhan/client-set-context-namespace branch from bf8d727 to 7ea7304 Compare August 12, 2026 06:41
@lubronzhan

Copy link
Copy Markdown
Contributor Author

Hi @shubham-pampattiwar Please take a look see if you are ok with the new change. Thank you

@kaovilai
kaovilai self-requested a review August 13, 2026 02:41
@kaovilai

Copy link
Copy Markdown
Member

on original ask.. yeah do you think original ask is a more valid usecase? I thought its more convenient for velero to remember ns.. but if you are the type that bounce between two velero instances.. perhaps velero should defer to current ns as original issue ask. In that case, perhaps the config should allow for another option, auto namespace mode?

@lubronzhan

Copy link
Copy Markdown
Contributor Author

on original ask.. yeah do you think original ask is a more valid usecase? I thought its more convenient for velero to remember ns.. but if you are the type that bounce between two velero instances.. perhaps velero should defer to current ns as original issue ask. In that case, perhaps the config should allow for another option, auto namespace mode?

@kaovilai MM make sense. And current command could be easily replaced by config set --namespace ? Not a big difference.

So maybe a command to allow use to enable this auto context switch? Like velero client config set namespace-mode=auto

Then do you think velero client config set-namespace-from-context is still useful? If not I can remove it

@kaovilai

Copy link
Copy Markdown
Member

if we have velero client config set namespace-mode=auto then velero client config set-namespace-from-context is no longer useful.. and if it was, its probably not much harder than currently available alternative.

kaovilai noted on velero-io#10127 that a one-shot command to snapshot the
kubecontext namespace becomes redundant once a config toggle can
resolve it dynamically, and isn't much simpler than the existing
`config set namespace=...` alternative.

Drop the dedicated set-namespace-from-context subcommand and instead
teach the client Factory to resolve the operational namespace from the
current kubeconfig context on every invocation when
`namespace-mode=auto` is set via the existing generic
`config set` command. Explicit --namespace flags and VELERO_NAMESPACE
still take precedence, so the new mode only changes behavior when
neither is set.

AI-Tool-Used: Claude Code
AI-Tool-Use-Level: Category 2 (Medium)
AI-Code-Category: Category 1 (Production)
Signed-off-by: lubronzhan <lubron.zhan@broadcom.com>
@lubronzhan lubronzhan changed the title feat(cli): add velero client set-context-as-velero-namespace command feat(cli): add velero client config set namespace-mode=auto Aug 18, 2026
@lubronzhan

lubronzhan commented Aug 18, 2026

Copy link
Copy Markdown
Contributor Author

Good point @kaovilai, thanks. Replaced the one-shot set-namespace-from-context command with a velero client config set namespace-mode=auto toggle instead — when set, the client factory resolves the namespace from the current kubeconfig context dynamically on every command, so it stays in sync as you switch contexts without needing a dedicated command (or having to rerun it). `--namespace` and `VELERO_NAMESPACE` still take precedence when set explicitly.

@kaovilai kaovilai left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Document how to disable auto mode. Since the generic setter deletes a key using an empty value, users apparently need velero client config set namespace-mode=. Adding that command to namespace.md would make the feature reversible and discoverable.

Add fallback/error-path coverage. Namespace() silently falls back to the stored/default namespace when kubeconfig namespace resolution fails. Even if this behavior is intentional because the interface cannot return an error, a test should lock it down and the docs should mention the fallback.

Use t.Setenv in new tests. This provides automatic cleanup and avoids environment leakage if a test exits early.

lubronzhan added a commit to lubronzhan/velero that referenced this pull request Aug 18, 2026
Resolve feedback from PR velero-io#10127 review 4966054980:
- Document how to disable namespace-mode=auto (namespace-mode=)
  and note the fallback to the static namespace, in namespace.md.
- Add a factory test covering the fallback to the stored/default
  namespace when kubeconfig namespace resolution fails.
- Switch the VELERO_NAMESPACE override test to t.Setenv, wrapped
  in a subtest so its cleanup runs before later tests execute.

AI-Tool-Used: Claude Code
AI-Tool-Use-Level: Category 2 (Medium)
AI-Code-Category: Category 2 (Non-Production)
Resolve feedback from PR velero-io#10127 review 4966054980:
- Document how to disable namespace-mode=auto (namespace-mode=)
  and note the fallback to the static namespace, in namespace.md.
- Add a factory test covering the fallback to the stored/default
  namespace when kubeconfig namespace resolution fails.
- Switch the VELERO_NAMESPACE override test to t.Setenv, wrapped
  in a subtest so its cleanup runs before later tests execute.

AI-Tool-Used: Claude Code
AI-Tool-Use-Level: Category 2 (Medium)
AI-Code-Category: Category 2 (Non-Production)
Signed-off-by: lubronzhan <lubron.zhan@broadcom.com>
@lubronzhan
lubronzhan force-pushed the lubronzhan/client-set-context-namespace branch from 33d8485 to 6e76b92 Compare August 18, 2026 23:12
@lubronzhan

Copy link
Copy Markdown
Contributor Author

Document how to disable auto mode. Since the generic setter deletes a key using an empty value, users apparently need velero client config set namespace-mode=. Adding that command to namespace.md would make the feature reversible and discoverable.

Add fallback/error-path coverage. Namespace() silently falls back to the stored/default namespace when kubeconfig namespace resolution fails. Even if this behavior is intentional because the interface cannot return an error, a test should lock it down and the docs should mention the fallback.

Use t.Setenv in new tests. This provides automatic cleanup and avoids environment leakage if a test exits early.

Cool, done

@kaovilai kaovilai left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for this work!

@kaovilai
kaovilai requested a review from a team August 19, 2026 02:32
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.

3 participants