mirror of
https://github.com/vmware-tanzu/velero.git
synced 2026-08-28 03:46:14 +00:00
* Issue #3194: Add velero client set-context-as-velero-namespace 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> * Fix CI: rename changelog to PR number, add unit tests for coverage - 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> * Move set-namespace-from-context under client config 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> * Replace set-namespace-from-context with namespace-mode=auto kaovilai noted on #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> * Address PR review: doc, fallback test, t.Setenv Resolve feedback from PR #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> --------- Signed-off-by: lubronzhan <lubron.zhan@broadcom.com> Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
83 lines
2.7 KiB
Go
83 lines
2.7 KiB
Go
/*
|
|
Copyright 2017, 2019 the Velero contributors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package client
|
|
|
|
import (
|
|
"fmt"
|
|
"runtime"
|
|
|
|
"github.com/cockroachdb/errors"
|
|
"k8s.io/client-go/rest"
|
|
"k8s.io/client-go/tools/clientcmd"
|
|
|
|
"github.com/vmware-tanzu/velero/pkg/buildinfo"
|
|
)
|
|
|
|
// Config returns a *rest.Config, using either the kubeconfig (if specified) or an in-cluster
|
|
// configuration.
|
|
func Config(kubeconfig, kubecontext, baseName string, qps float32, burst int) (*rest.Config, error) {
|
|
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
|
|
loadingRules.ExplicitPath = kubeconfig
|
|
configOverrides := &clientcmd.ConfigOverrides{CurrentContext: kubecontext}
|
|
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)
|
|
|
|
clientConfig, err := kubeConfig.ClientConfig()
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "error finding Kubernetes API server config in --kubeconfig, $KUBECONFIG, or in-cluster configuration")
|
|
}
|
|
|
|
if qps > 0.0 {
|
|
clientConfig.QPS = qps
|
|
}
|
|
if burst > 0 {
|
|
clientConfig.Burst = burst
|
|
}
|
|
|
|
clientConfig.UserAgent = buildUserAgent(
|
|
baseName,
|
|
buildinfo.Version,
|
|
buildinfo.FormattedGitSHA(),
|
|
runtime.GOOS,
|
|
runtime.GOARCH,
|
|
)
|
|
|
|
return clientConfig, nil
|
|
}
|
|
|
|
// NamespaceFromKubeContext returns the namespace associated with the given kubeconfig context
|
|
// (or the current context if kubecontext is empty), using the given kubeconfig file (or the
|
|
// default loading rules if kubeconfig is empty).
|
|
func NamespaceFromKubeContext(kubeconfig, kubecontext string) (string, error) {
|
|
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
|
|
loadingRules.ExplicitPath = kubeconfig
|
|
configOverrides := &clientcmd.ConfigOverrides{CurrentContext: kubecontext}
|
|
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)
|
|
|
|
namespace, _, err := kubeConfig.Namespace()
|
|
if err != nil {
|
|
return "", errors.Wrap(err, "error finding namespace in --kubeconfig, $KUBECONFIG, or in-cluster configuration")
|
|
}
|
|
|
|
return namespace, nil
|
|
}
|
|
|
|
// buildUserAgent builds a User-Agent string from given args.
|
|
func buildUserAgent(command, version, formattedSha, os, arch string) string {
|
|
return fmt.Sprintf(
|
|
"%s/%s (%s/%s) %s", command, version, os, arch, formattedSha)
|
|
}
|