From 416d616b09c67f4a45df93ef0e992c15c37bf4ac Mon Sep 17 00:00:00 2001 From: Joseph Date: Wed, 29 Apr 2026 10:42:04 -0400 Subject: [PATCH] Default namespace to openshift-adp instead of velero When no VELERO_NAMESPACE env var or config file is present, the Velero library falls back to "velero" as the default namespace. Set it to "openshift-adp" in the root command when no namespace is configured. Guard against nil config map from LoadConfig errors. Remove unused CreateVeleroFactory and CreateNonAdminFactory functions from factories.go. Signed-off-by: Joseph --- cmd/root.go | 5 +++++ cmd/root_test.go | 39 +++++++++++++++++++++++++++++++++++++++ cmd/shared/factories.go | 29 ----------------------------- 3 files changed, 44 insertions(+), 29 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index d30d0a0d..429827a4 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -382,12 +382,17 @@ func NewVeleroRootCommand(baseName string) *cobra.Command { if err != nil { fmt.Fprintf(os.Stderr, "WARNING: Error reading config file: %v\n", err) } + if config == nil { + config = clientcmd.VeleroConfig{} + } // When nonadmin mode is enabled, remove the namespace override so the // factory uses the current kubeconfig context namespace instead of an // admin namespace like openshift-adp. if isNonadminEnabled(config) { delete(config, clientcmd.ConfigKeyNamespace) + } else if config.Namespace() == "" { + config[clientcmd.ConfigKeyNamespace] = "openshift-adp" } // Declare cmdFeatures and cmdColorzied here so we can access them in the PreRun hooks diff --git a/cmd/root_test.go b/cmd/root_test.go index 79fa7d7e..96cb819c 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -23,6 +23,7 @@ import ( "io" "net" "os" + "os/exec" "strings" "testing" "time" @@ -740,6 +741,44 @@ func TestReplaceVeleroWithOADP_OutputWrapperExclusions(t *testing.T) { }) } +// TestDefaultNamespaceIsOpenShiftADP verifies that when no config file or +// VELERO_NAMESPACE env var is present, the CLI defaults to openshift-adp. +func TestDefaultNamespaceIsOpenShiftADP(t *testing.T) { + binaryPath := testutil.BuildCLIBinary(t) + + // Use a temp HOME so no ~/.config/velero/config.json exists + tempHome := t.TempDir() + + ctx, cancel := context.WithTimeout(context.Background(), testutil.TestTimeout) + defer cancel() + + cmd := exec.CommandContext(ctx, binaryPath, "--help") + cmd.Env = append(os.Environ(), + "HOME="+tempHome, + ) + // Remove VELERO_NAMESPACE if set + filtered := cmd.Env[:0] + for _, e := range cmd.Env { + if !strings.HasPrefix(e, "VELERO_NAMESPACE=") { + filtered = append(filtered, e) + } + } + cmd.Env = filtered + + out, err := cmd.CombinedOutput() + if err != nil { + t.Fatalf("command failed: %v\n%s", err, string(out)) + } + + output := string(out) + if !strings.Contains(output, `(default "openshift-adp")`) { + t.Errorf("Expected namespace default to be openshift-adp, got:\n%s", output) + } + if strings.Contains(output, `(default "velero")`) { + t.Errorf("Namespace should not default to velero, got:\n%s", output) + } +} + // TestApplyTimeoutToConfig_DialerTimeout tests that the custom dialer respects the timeout func TestApplyTimeoutToConfig_DialerTimeout(t *testing.T) { // Set a very short timeout diff --git a/cmd/shared/factories.go b/cmd/shared/factories.go index 59cc1725..cb83d1ac 100644 --- a/cmd/shared/factories.go +++ b/cmd/shared/factories.go @@ -22,8 +22,6 @@ import ( "os" "path/filepath" "strings" - - "github.com/vmware-tanzu/velero/pkg/client" ) // ClientConfig represents the structure of the Velero client configuration file @@ -59,33 +57,6 @@ func (c *ClientConfig) GetDefaultNABSL() string { return c.DefaultNABSL } -// CreateVeleroFactory creates a client factory for Velero operations (admin-scoped) -// that uses the client configuration to determine the namespace. -// Priority order: -// 1. Velero client config (~/.config/velero/config.json) -// 2. Kubeconfig context namespace -// 3. Velero default (usually "velero") -func CreateVeleroFactory() client.Factory { - cfg := client.VeleroConfig{} - - // Try to read client config to get configured namespace - if clientConfig, err := ReadVeleroClientConfig(); err == nil { - if clientConfig.Namespace != "" { - cfg[client.ConfigKeyNamespace] = clientConfig.Namespace - } - } - - return client.NewFactory("oadp-velero-cli", cfg) -} - -// CreateNonAdminFactory creates a client factory for NonAdminBackup operations -// that uses the current kubeconfig context namespace instead of hardcoded openshift-adp -func CreateNonAdminFactory() client.Factory { - // Don't set a default namespace, let it use the kubeconfig context - cfg := client.VeleroConfig{} - return client.NewFactory("oadp-velero-cli", cfg) -} - // ReadVeleroClientConfig reads the Velero client configuration from ~/.config/velero/config.json func ReadVeleroClientConfig() (*ClientConfig, error) { homeDir, err := os.UserHomeDir()