-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
184 lines (159 loc) · 6.79 KB
/
Makefile
File metadata and controls
184 lines (159 loc) · 6.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# Makefile for namespace-cleaner
# Description: Build, test, and deploy namespace-cleaner
# Default target
first:
@echo "Please use an explicit command, e.g., 'make build' or 'make help'"
.PHONY: build test-unit docker-build image dry-run run stop clean help test-integration _setup-kind-cluster _delete-kind-cluster
# Build targets
build: ## Build the Go binary
@echo "Building executable..."
@mkdir -p bin
@cd cmd/namespace-cleaner && \
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o ../../bin/namespace-cleaner .
@echo "Binary built: bin/namespace-cleaner"
image: ## Build Docker image
@echo "Building Docker image..."
@docker build -t namespace-cleaner:test . | sed 's/^/ /'
docker-build: image ## Build Docker image and load into Kind
@echo "Loading image into Kind..."
@kind load docker-image namespace-cleaner:test --name integration-test
@echo "Docker image loaded into Kind"
# Test targets
test-unit: ## Run unit tests with coverage
@echo "=============================================="
@echo "Starting unit tests at $(shell date)"
@echo "Test configuration:"
@echo " - Race detector: enabled"
@echo " - Coverage mode: atomic"
@echo " - Verbose output: maximum"
@echo "=============================================="
@mkdir -p coverage-report
@FAILED=0; \
for pkg in internal/cleaner internal/clients internal/config pkg/stats cmd/namespace-cleaner; do \
echo "Testing $$pkg..."; \
cd $$pkg && \
OUTPUT=$$(go test -v -race -coverprofile=../../coverage-report/$$(basename $$pkg)-coverage.tmp -covermode=atomic . 2>&1) || FAILED=1; \
echo "$$OUTPUT" | sed "s/^/ ▶ $$(basename $$pkg): /"; \
cd ../..; \
[ $$FAILED -eq 1 ] && break; \
done; \
if [ $$FAILED -eq 1 ]; then \
echo "Unit tests failed - exiting"; \
exit 1; \
fi; \
echo "mode: atomic" > coverage-report/coverage.tmp; \
find coverage-report -name '*-coverage.tmp' -exec grep -h -v "mode: atomic" {} >> coverage-report/coverage.tmp \;; \
go tool cover -func=coverage-report/coverage.tmp | tee coverage-report/coverage.out; \
rm -f coverage-report/*-coverage.tmp; \
echo "Unit tests completed"
test-integration-locally: _setup-kind-cluster docker-build test-integration ## Run integration tests on Kind cluster
test-integration:
@export KUBECONFIG=$$HOME/.kube/kind-config-integration-test
@echo "Running integration tests at $(shell date)"
# Ensure namespace exists
@kubectl create namespace das || true
# Apply RBAC and configmap
@echo "Applying manifests..."
@kubectl apply -f manifests/
# Check outcome
@echo "Verifying ConfigMap..."
@kubectl -n das get cm
@echo "Verifying CronJob..."
@kubectl -n das get cronjob
@echo "Verifying NetPol..."
@kubectl -n das get netpol
@echo "Verifying RBAC..."
@kubectl -n das get rolebindings.rbac.authorization.k8s.io
@kubectl -n das get roles.rbac.authorization.k8s.io
@echo "Verifying ServiceAccount..."
@kubectl -n das get serviceaccount
# Apply integration test pod manifest
@echo "Creating integration test pod..."
@kubectl apply -f tests/integration-test-pod.yaml -n das
# Verify pod spec
@echo "Verifying pod configuration..."
@kubectl -n das get pod namespace-cleaner-integration-test -o jsonpath='{.spec.serviceAccountName}' | grep -q "namespace-cleaner" || (echo "ServiceAccount mismatch!" && exit 1)
# Describe pod for initial diagnostics
@echo "Describing pod to capture configuration and events:"
@kubectl -n das describe pod namespace-cleaner-integration-test
# Wait for pod to complete
@echo "Waiting for pod to complete..."
@POD_NAME=namespace-cleaner-integration-test; \
for i in $$(seq 1 60); do \
STATUS=$$(kubectl -n das get pod $$POD_NAME -o jsonpath='{.status.phase}' 2>/dev/null); \
if [ "$$STATUS" = "Succeeded" ]; then \
echo "[$$(date +%T)] Pod $$POD_NAME completed successfully."; \
break; \
elif [ "$$STATUS" = "Failed" ]; then \
echo "[$$(date +%T)] Pod $$POD_NAME failed."; \
kubectl -n das describe pod $$POD_NAME; \
kubectl -n das logs $$POD_NAME --timestamps=true; \
exit 1; \
fi; \
echo "[$$(date +%T)] Waiting for pod $$POD_NAME to complete... Current status: $${STATUS:-Pending}"; \
sleep 5; \
done || \
(echo "[$$(date +%T)] Timeout waiting for pod $$POD_NAME to complete." && \
kubectl -n das describe pod/$$POD_NAME && \
kubectl -n das logs $$POD_NAME --timestamps=true && \
exit 1)
# Show logs with timestamps
@echo "[$$(date +%T)] Pod logs (with timestamps):"
@kubectl -n das logs namespace-cleaner-integration-test --timestamps=true
@echo "[$$(date +%T)] Integration tests passed"
dry-run: _dry-run-setup ## Run dry-run mode on real cluster
@echo "Starting dry run..."
@kubectl -n das apply -f tests/dry-run-job.yaml
@echo "Waiting for job to start (up to 5 minutes)..."
@kubectl -n das wait --for=condition=ready pod -l job-name=namespace-cleaner-dry-run --timeout=300s || \
(echo "Pod did not become ready"; exit 1)
@echo "Pod logs:"
@kubectl -n das logs -f -l job-name=namespace-cleaner-dry-run
@kubectl -n das delete -f tests/dry-run-job.yaml || true
@$(MAKE) stop
@echo "Dry run completed"
_dry-run-setup:
@echo "Setting up dry-run dependencies on real cluster..."
@kubectl apply -f manifests/
@kubectl apply -f tests/dry-run-config.yaml
_setup-kind-cluster:
@echo "Setting up Kind cluster..."
@if ! command -v kind >/dev/null; then \
echo "'kind' not found. Please install Kind first."; \
exit 1; \
fi
@echo "Ensuring no previous integration-test cluster exists..."
@kind get clusters | grep -q integration-test && kind delete cluster --name integration-test || true
@echo "Creating new Kind cluster: integration-test"
@kind create cluster --name integration-test --wait 60s
@kubectl cluster-info
@echo "Kind cluster created"
_delete-kind-cluster:
@echo "Deleting Kind cluster..."
@kind get clusters | grep -q integration-test && kind delete cluster --name integration-test || true
@echo "Kind cluster deleted"
# Deployment target
run: ## Deploy to production cluster
@echo "Deploying to production..."
@kubectl apply -f manifests/
@echo "Deployment complete. CronJob running on cluster"
# Stop target (clean up dry-run resources)
stop:
@echo "Cleaning up leftover dry-run resources..."
@kubectl delete -f manifests/rbac.yaml \
-f manifests/serviceaccount.yaml \
-f manifests/netpol.yaml \
-f manifests/configmap.yaml \
-f tests/dry-run-config.yaml \
--ignore-not-found > /dev/null 2>&1 || true
# Cleanup target
clean: stop ## Remove all resources
@echo "Full cleanup..."
@kubectl delete -f manifests/ --ignore-not-found > /dev/null 2>&1 || true
@kubectl delete -f tests/dry-run-job.yaml --ignore-not-found > /dev/null 2>&1 || true
@rm -rf bin/ coverage-report/
@echo "All resources cleaned"
# Help target
help: ## Display this help message
@echo "Available targets:"
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf " %-20s %s\n", $$1, $$2}' $(MAKEFILE_LIST)