Skip to content

Commit b5a75f0

Browse files
authored
Refactor non-admin backup storage location commands (migtools#43)
* Refactor non-admin backup storage location commands - Introduced new subcommands for managing backup storage location requests: approve, deny, describe, and get. - Updated the create command to enhance its functionality and examples. - Improved help text for commands to provide clearer guidance on usage. - Added tests to ensure help messages are accurate and comprehensive. - Enhanced the overall structure and organization of the non-admin command set. * Enhance non-admin backup storage location management - Introduced new commands for managing non-admin backup storage location requests: approve, reject, describe, and get. - Updated the create command to support custom credential specifications and improved help text for clarity. - Refactored the Makefile to include new installation options for interactive namespace prompts and default settings. - Removed outdated demo outline documentation to streamline project structure. - Added utility functions for reading client configuration and managing namespaces, improving overall command functionality. * Add integration and unit tests for OADP CLI commands - Introduced a comprehensive suite of integration tests in `integration_test.go` to validate binary build, Makefile installation, client configuration, and command architecture. - Created unit tests for root, non-admin, and NABSL commands to ensure help text accuracy and command functionality. - Enhanced the Makefile to include dedicated targets for running unit and integration tests. - Added a `TESTING.md` document to outline the testing architecture and best practices for future contributions. - Removed outdated test files to streamline the testing structure. * Enhance integration tests and improve error handling - Added runtime checks for binary creation in `integration_test.go` to support Windows compatibility. - Improved error handling in the Makefile installation test by logging non-fatal errors during cleanup. - Updated the binary path in `testutil.go` to accommodate Windows executable naming conventions. - Removed unnecessary code in `bsl_test.go` to streamline test logic. * Mod tidy * Windows exe * Enhance installation process with automatic namespace detection - Updated the Makefile to include intelligent detection for OADP deployment namespaces during installation. - Improved help text in the Makefile and README to clarify installation options and the detection process. - Added detailed documentation in README and TESTING.md outlining the automatic detection steps and installation modes. - Ensured fallback mechanisms for namespace detection to enhance user experience. * Move shared code * Go fmt
1 parent 1d7ab68 commit b5a75f0

31 files changed

+2589
-709
lines changed

Docs/demo.outline

Lines changed: 0 additions & 18 deletions
This file was deleted.

Makefile

Lines changed: 115 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
BINARY_NAME = kubectl-oadp
77
INSTALL_PATH ?= $(HOME)/.local/bin
88
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
9+
VELERO_NAMESPACE ?= openshift-adp
10+
ASSUME_DEFAULT ?= false
911

1012
# Centralized platform definitions to avoid duplication
1113
# Matches architectures supported by Kubernetes: https://kubernetes.io/releases/download/#binaries
@@ -31,10 +33,12 @@ help: ## Show this help message
3133
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf " \033[36m%-15s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
3234
@echo ""
3335
@echo "Installation options:"
34-
@echo " \033[36mmake install\033[0m # Install to ~/.local/bin (recommended, no sudo)"
35-
@echo " \033[36mmake install-user\033[0m # Same as install (legacy alias)"
36-
@echo " \033[36mmake install-bin\033[0m # Install to ~/bin (alternative, no sudo)"
37-
@echo " \033[36mmake install-system\033[0m # Install to /usr/local/bin (requires sudo)"
36+
@echo " \033[36mmake install\033[0m # Install with auto-detection & interactive prompt"
37+
@echo " \033[36mmake install ASSUME_DEFAULT=true\033[0m # Install with default namespace (no detection/prompt)"
38+
@echo " \033[36mmake install VELERO_NAMESPACE=velero\033[0m # Install with custom namespace (no detection/prompt)"
39+
@echo " \033[36mmake install-user\033[0m # Same as install (legacy alias)"
40+
@echo " \033[36mmake install-bin\033[0m # Install to ~/bin (alternative, no sudo)"
41+
@echo " \033[36mmake install-system\033[0m # Install to /usr/local/bin (requires sudo)"
3842
@echo ""
3943
@echo "Uninstall options:"
4044
@echo " \033[36mmake uninstall\033[0m # Remove from user locations (no sudo)"
@@ -51,6 +55,11 @@ help: ## Show this help message
5155
@echo " make build PLATFORM=windows/amd64"
5256
@echo " make build PLATFORM=windows/arm64"
5357
@echo ""
58+
@echo "Testing commands:"
59+
@echo " make test # Run all tests (unit + integration)"
60+
@echo " make test-unit # Run unit tests only"
61+
@echo " make test-integration # Run integration tests only"
62+
@echo ""
5463
@echo "Release commands:"
5564
@echo " make release-build # Build binaries for all platforms"
5665
@echo " make release-archives # Create tar.gz archives for all platforms"
@@ -59,21 +68,38 @@ help: ## Show this help message
5968
.PHONY: build
6069
build: ## Build the kubectl plugin binary (use PLATFORM=os/arch for cross-compilation)
6170
@if [ -n "$(PLATFORM)" ]; then \
71+
if [ "$(GOOS)" = "windows" ]; then \
72+
binary_suffix=".exe"; \
73+
else \
74+
binary_suffix=""; \
75+
fi; \
6276
echo "Building $(BINARY_NAME) for $(PLATFORM)..."; \
63-
GOOS=$(GOOS) GOARCH=$(GOARCH) go build -o $(BINARY_NAME)-$(GOOS)-$(GOARCH) .; \
64-
echo "✅ Built $(BINARY_NAME)-$(GOOS)-$(GOARCH) successfully!"; \
77+
GOOS=$(GOOS) GOARCH=$(GOARCH) go build -o $(BINARY_NAME)-$(GOOS)-$(GOARCH)$$binary_suffix .; \
78+
echo "✅ Built $(BINARY_NAME)-$(GOOS)-$(GOARCH)$$binary_suffix successfully!"; \
6579
else \
66-
echo "Building $(BINARY_NAME) for current platform ($$(go env GOOS)/$$(go env GOARCH))..."; \
67-
go build -o $(BINARY_NAME) .; \
68-
echo "✅ Built $(BINARY_NAME) successfully!"; \
80+
GOOS=$$(go env GOOS); \
81+
if [ "$$GOOS" = "windows" ]; then \
82+
binary_name="$(BINARY_NAME).exe"; \
83+
else \
84+
binary_name="$(BINARY_NAME)"; \
85+
fi; \
86+
echo "Building $$binary_name for current platform ($$GOOS/$$(go env GOARCH))..."; \
87+
go build -o $$binary_name .; \
88+
echo "✅ Built $$binary_name successfully!"; \
6989
fi
7090

7191
# Installation targets
7292
.PHONY: install
7393
install: build ## Build and install the kubectl plugin to ~/.local/bin (no sudo required)
74-
@echo "Installing $(BINARY_NAME) to $(INSTALL_PATH)..."
75-
@mkdir -p $(INSTALL_PATH)
76-
cp $(BINARY_NAME) $(INSTALL_PATH)/
94+
@GOOS=$$(go env GOOS); \
95+
if [ "$$GOOS" = "windows" ]; then \
96+
binary_name="$(BINARY_NAME).exe"; \
97+
else \
98+
binary_name="$(BINARY_NAME)"; \
99+
fi; \
100+
echo "Installing $$binary_name to $(INSTALL_PATH)..."; \
101+
mkdir -p $(INSTALL_PATH); \
102+
cp $$binary_name $(INSTALL_PATH)/
77103
@echo "✅ Installed to $(INSTALL_PATH)"
78104
@echo ""
79105
@PATH_UPDATED=false; \
@@ -108,7 +134,66 @@ install: build ## Build and install the kubectl plugin to ~/.local/bin (no sudo
108134
if [[ "$$PATH_UPDATED" == "true" ]] || [[ "$$PATH_IN_CONFIG" == "true" ]]; then \
109135
echo "🔄 Restart terminal or run: source ~/.zshrc"; \
110136
fi; \
111-
echo "Test: kubectl oadp --help"
137+
echo ""; \
138+
echo "📋 Configuration:"; \
139+
NAMESPACE=$(VELERO_NAMESPACE); \
140+
DETECTED=false; \
141+
if [[ "$(ASSUME_DEFAULT)" != "true" && "$(VELERO_NAMESPACE)" == "openshift-adp" ]]; then \
142+
echo ""; \
143+
echo "🔍 Detecting OADP deployment in cluster..."; \
144+
DETECTED_NS=$$(kubectl get deployment openshift-adp-controller-manager --all-namespaces -o jsonpath='{.items[0].metadata.namespace}' 2>/dev/null | head -1); \
145+
if [[ -n "$$DETECTED_NS" ]]; then \
146+
echo "✅ Found OADP controller in namespace: $$DETECTED_NS"; \
147+
NAMESPACE=$$DETECTED_NS; \
148+
DETECTED=true; \
149+
else \
150+
echo " Could not find openshift-adp-controller-manager deployment"; \
151+
echo "🔍 Looking for DataProtectionApplication (DPA) resources..."; \
152+
DETECTED_NS=$$(kubectl get dataprotectionapplication --all-namespaces -o jsonpath='{.items[0].metadata.namespace}' 2>/dev/null | head -1); \
153+
if [[ -n "$$DETECTED_NS" ]]; then \
154+
echo "✅ Found DPA resource in namespace: $$DETECTED_NS"; \
155+
NAMESPACE=$$DETECTED_NS; \
156+
DETECTED=true; \
157+
else \
158+
echo " Could not find DataProtectionApplication resources"; \
159+
echo "🔍 Looking for Velero deployment as fallback..."; \
160+
DETECTED_NS=$$(kubectl get deployment velero --all-namespaces -o jsonpath='{.items[0].metadata.namespace}' 2>/dev/null | head -1); \
161+
if [[ -n "$$DETECTED_NS" ]]; then \
162+
echo "✅ Found Velero deployment in namespace: $$DETECTED_NS"; \
163+
NAMESPACE=$$DETECTED_NS; \
164+
DETECTED=true; \
165+
else \
166+
echo "⚠️ Could not detect OADP or Velero deployment in cluster"; \
167+
fi; \
168+
fi; \
169+
fi; \
170+
if [[ "$$DETECTED" == "false" ]]; then \
171+
echo "🤔 Which namespace should admin commands use for Velero resources?"; \
172+
echo " (Common options: openshift-adp, velero, oadp)"; \
173+
echo ""; \
174+
printf "Enter namespace [default: $(VELERO_NAMESPACE)]: "; \
175+
read -r user_input; \
176+
if [[ -n "$$user_input" ]]; then \
177+
NAMESPACE=$$user_input; \
178+
fi; \
179+
fi; \
180+
echo ""; \
181+
fi; \
182+
echo "Setting Velero namespace to: $$NAMESPACE"; \
183+
GOOS=$$(go env GOOS); \
184+
if [ "$$GOOS" = "windows" ]; then \
185+
binary_name="$(BINARY_NAME).exe"; \
186+
else \
187+
binary_name="$(BINARY_NAME)"; \
188+
fi; \
189+
$(INSTALL_PATH)/$$binary_name client config set namespace=$$NAMESPACE 2>/dev/null || true; \
190+
echo "✅ Client config initialized"; \
191+
echo ""; \
192+
echo "📋 Next steps:"; \
193+
echo " 1. Test admin commands: kubectl oadp backup get"; \
194+
echo " 2. Test non-admin commands: kubectl oadp nonadmin backup get"; \
195+
echo " 3. Manage NABSL requests: kubectl oadp nabsl get"; \
196+
echo " 4. Change namespace: kubectl oadp client config set namespace=<namespace>"
112197

113198
.PHONY: install-user
114199
install-user: build ## Build and install the kubectl plugin to ~/.local/bin (no sudo required)
@@ -185,14 +270,29 @@ uninstall-all: ## Uninstall the kubectl plugin from all locations (user + system
185270
.PHONY: test
186271
test: ## Run all tests
187272
@echo "Running tests..."
188-
go test ./...
273+
@echo "🧪 Running unit tests..."
274+
go test ./cmd/... ./internal/...
275+
@echo "🔗 Running integration tests..."
276+
go test . -v
189277
@echo "✅ Tests completed!"
190278

279+
.PHONY: test-unit
280+
test-unit: ## Run unit tests only
281+
@echo "Running unit tests..."
282+
go test ./cmd/... ./internal/...
283+
@echo "✅ Unit tests completed!"
284+
285+
.PHONY: test-integration
286+
test-integration: ## Run integration tests only
287+
@echo "Running integration tests..."
288+
go test . -v
289+
@echo "✅ Integration tests completed!"
290+
191291
# Cleanup targets
192292
.PHONY: clean
193293
clean: ## Remove built binaries
194294
@echo "Cleaning up..."
195-
@rm -f $(BINARY_NAME) $(BINARY_NAME)-linux-* $(BINARY_NAME)-darwin-* $(BINARY_NAME)-windows-*
295+
@rm -f $(BINARY_NAME) $(BINARY_NAME).exe $(BINARY_NAME)-linux-* $(BINARY_NAME)-darwin-* $(BINARY_NAME)-windows-*
196296
@rm -f *.tar.gz *.sha256
197297
@rm -f oadp-*.yaml oadp-*.yaml.tmp
198298
@echo "✅ Cleanup complete!"

README.md

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ kubectl oadp
2020
├── backup # Velero cluster-wide backups (admin)
2121
├── restore # Velero cluster-wide restores (admin)
2222
├── version # Version information
23+
├── nabsl-request # Manage NonAdminBackupStorageLocation approval requests
2324
└── nonadmin (na) # Namespace-scoped operations (non-admin)
2425
└── backup
2526
├── create
@@ -30,23 +31,10 @@ kubectl oadp
3031

3132
## Installation
3233

33-
### Using Krew (Available soon!)
34-
35-
```sh
36-
# Install Krew if you haven't already
37-
kubectl krew install krew
38-
39-
# Install the OADP plugin
40-
kubectl krew install oadp
41-
42-
# Verify installation
43-
kubectl oadp --help
44-
```
45-
4634
### Manual Build and Install
4735

4836
```sh
49-
# Recommended: Rootless install (no sudo required)
37+
# Recommended: Smart install with auto-detection (no sudo required)
5038
make install
5139

5240
# After install, refresh your terminal:
@@ -60,8 +48,26 @@ kubectl oadp --help
6048
make install-system
6149
```
6250

51+
The `make install` command automatically detects your OADP deployment namespace by looking for:
52+
1. **OADP Controller** (`openshift-adp-controller-manager` deployment)
53+
2. **DPA Resources** (`DataProtectionApplication` custom resources)
54+
3. **Velero Deployment** (fallback for vanilla Velero installations)
55+
56+
If no OADP resources are detected, you'll be prompted to specify the namespace manually.
57+
58+
**Installation Options:**
59+
```sh
60+
make install # Smart detection + interactive prompt
61+
make install ASSUME_DEFAULT=true # Use default namespace (no detection)
62+
make install VELERO_NAMESPACE=custom # Use specific namespace (no detection)
63+
```
64+
6365
**💡 Important:** After installation, you may need to refresh your terminal or run `source ~/.zshrc` (or `~/.bashrc`) for the `kubectl oadp` command to work.
6466

67+
You can set the velero namespace afterwards using the oadp client command
68+
69+
70+
6571
## Usage Guide
6672

6773
### Non-Admin Backup Operations

0 commit comments

Comments
 (0)