Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

// Declare cmdFeatures and cmdColorzied here so we can access them in the PreRun hooks
Expand Down
39 changes: 39 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io"
"net"
"os"
"os/exec"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -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
Expand Down
29 changes: 0 additions & 29 deletions cmd/shared/factories.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
Loading