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
34 changes: 34 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Built binaries
kubectl-oadp

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Go workspace file
go.work

# IDE files
.vscode/
.idea/
*.swp
*.swo
*~

# OS generated files
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
88 changes: 88 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Makefile for OADP CLI
#
# Simple Makefile for building, testing, and installing the OADP CLI

# Variables
BINARY_NAME = kubectl-oadp
INSTALL_PATH ?= /usr/local/bin

# Platform variables for multi-arch builds
# Usage: make build PLATFORM=linux/amd64
PLATFORM ?=
GOOS = $(word 1,$(subst /, ,$(PLATFORM)))
GOARCH = $(word 2,$(subst /, ,$(PLATFORM)))

# Default target
.PHONY: help
help: ## Show this help message
@echo "OADP CLI Makefile"
@echo ""
@echo "Available targets:"
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf " \033[36m%-15s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
@echo ""
@echo "Build with different platforms:"
@echo " make build PLATFORM=linux/amd64"
@echo " make build PLATFORM=linux/arm64"
@echo " make build PLATFORM=darwin/amd64"
@echo " make build PLATFORM=darwin/arm64"
@echo " make build PLATFORM=windows/amd64"

# Build targets
.PHONY: build
build: ## Build the kubectl plugin binary (use PLATFORM=os/arch for cross-compilation)
@if [ -n "$(PLATFORM)" ]; then \
echo "Building $(BINARY_NAME) for $(PLATFORM)..."; \
GOOS=$(GOOS) GOARCH=$(GOARCH) go build -o $(BINARY_NAME)-$(GOOS)-$(GOARCH) .; \
echo "✅ Built $(BINARY_NAME)-$(GOOS)-$(GOARCH) successfully!"; \
else \
echo "Building $(BINARY_NAME) for current platform ($$(go env GOOS)/$$(go env GOARCH))..."; \
go build -o $(BINARY_NAME) .; \
echo "✅ Built $(BINARY_NAME) successfully!"; \
fi

# Installation targets
.PHONY: install
install: build ## Build and install the kubectl plugin
@echo "Installing $(BINARY_NAME) to $(INSTALL_PATH)..."
mv $(BINARY_NAME) $(INSTALL_PATH)/
@echo "✅ $(BINARY_NAME) installed successfully!"
@echo "You can now use: kubectl oadp --help"

# Testing targets
.PHONY: test
test: ## Run all tests
@echo "Running tests..."
go test ./...
@echo "✅ Tests completed!"

# Cleanup targets
.PHONY: clean
clean: ## Remove built binaries
@echo "Cleaning up..."
@rm -f $(BINARY_NAME) $(BINARY_NAME)-*
@echo "✅ Cleanup complete!"

# Status and utility targets
.PHONY: status
status: ## Show build status and installation info
@echo "=== OADP CLI Status ==="
@echo ""
@echo "📁 Repository:"
@pwd
@echo ""
@echo "🔧 Local binary:"
@ls -la $(BINARY_NAME) 2>/dev/null || echo " No local binary found"
@echo ""
@echo "📦 Installed plugin:"
@ls -la $(INSTALL_PATH)/$(BINARY_NAME) 2>/dev/null || echo " Plugin not installed"
@echo ""
@echo "✅ Plugin accessibility:"
@if kubectl plugin list 2>/dev/null | grep -q "kubectl-oadp"; then \
echo " ✅ kubectl-oadp plugin is installed and accessible"; \
echo " Version check:"; \
kubectl oadp version 2>/dev/null || echo " (version command not available)"; \
else \
echo " ❌ kubectl-oadp plugin is NOT accessible"; \
echo " Available plugins:"; \
kubectl plugin list 2>/dev/null | head -5 || echo " (no plugins found or kubectl not available)"; \
fi
30 changes: 25 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ oadp

### Quick Installation

Use the provided script for quick build and installation:
Use the Makefile for easy build and installation:

```sh
chmod +x quick-create.sh
./quick-create.sh
# Build and install the kubectl plugin
make install
```

### Manual Installation

1. **Build the CLI:**
```sh
go build -o kubectl-oadp .
make build
```

2. **Install as kubectl plugin:**
Expand All @@ -51,6 +51,23 @@ chmod +x quick-create.sh
kubectl oadp --help
```

### Development Workflow

```sh
# Build and test locally
make build
./kubectl-oadp --help

# Run tests
make test

# Check status
make status

# View all available commands
make help
```

## Usage Examples

### NonAdminBackup Operations
Expand Down Expand Up @@ -99,7 +116,10 @@ This project includes comprehensive CLI integration tests organized by functiona
### Quick Test Commands

```bash
# Run all tests (standard Go pattern)
# Run all tests
make test

# Standard Go pattern (also works)
go test ./...
```

Expand Down
6 changes: 1 addition & 5 deletions cmd/non-admin/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ func NewBackupCommand(f client.Factory) *cobra.Command {

c.AddCommand(
NewCreateCommand(f, "create"),
// NewGetCommand(f, "get"),
// NewLogsCommand(f),
// NewDescribeCommand(f, "describe"),
// NewDownloadCommand(f),
// NewDeleteCommand(f, "delete"),
NewDeleteCommand(f, "delete"),
)

return c
Expand Down
35 changes: 0 additions & 35 deletions cmd/non-admin/backup/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
"github.com/vmware-tanzu/velero/pkg/cmd/util/flag"
"github.com/vmware-tanzu/velero/pkg/cmd/util/output"
"github.com/vmware-tanzu/velero/pkg/util/kube"
"k8s.io/client-go/tools/clientcmd"
)

func NewCreateCommand(f client.Factory, use string) *cobra.Command {
Expand Down Expand Up @@ -206,40 +205,6 @@ func (o *CreateOptions) validateFromScheduleFlag(c *cobra.Command) error {
return nil
}

// getCurrentNamespace gets the current namespace from the kubeconfig context
func getCurrentNamespace() (string, error) {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
configOverrides := &clientcmd.ConfigOverrides{}
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)

namespace, _, err := kubeConfig.Namespace()
if err != nil {
return "", fmt.Errorf("failed to get current namespace from kubeconfig: %w", err)
}

// If no namespace is set in kubeconfig, default to the user's name from context
if namespace == "" || namespace == "default" {
rawConfig, err := kubeConfig.RawConfig()
if err != nil {
return "", fmt.Errorf("failed to get raw kubeconfig: %w", err)
}

currentContext := rawConfig.CurrentContext
if _, exists := rawConfig.Contexts[currentContext]; exists {
// Try to extract user namespace from context name (assuming format like "user/cluster/user")
parts := strings.Split(currentContext, "/")
if len(parts) >= 3 {
userNamespace := parts[2] // Assuming the user namespace is the third part
return userNamespace, nil
}
}

return "default", nil
}

return namespace, nil
}

func (o *CreateOptions) Complete(args []string, f client.Factory) error {
// If an explicit name is specified, use that name
if len(args) > 0 {
Expand Down
Loading
Loading