-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
221 lines (184 loc) · 8.38 KB
/
Copy pathMakefile
File metadata and controls
221 lines (184 loc) · 8.38 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
.PHONY: help build run stop restart logs clean \
up down ps \
local local-up local-down local-restart local-logs local-ps \
test-up test-down test-restart test-logs test-ps \
prod prod-up prod-down prod-restart prod-logs prod-ps \
docker-build docker-clean \
k8s-deploy k8s-delete k8s-status k8s-logs k8s-validate \
test restore health \
monitoring-up monitoring-down
# ============================================================
# Default target
# ============================================================
help: ## Show all available commands
@echo ""
@echo " .NET Microservice — Makefile Commands"
@echo " ======================================"
@echo ""
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
awk 'BEGIN {FS = ":.*?## "}; {printf " \033[36m%-22s\033[0m %s\n", $$1, $$2}'
@echo ""
# ============================================================
# Variables
# ============================================================
SOLUTION := Microservice.sln
DOCKER_DIR := docker
COMPOSE := docker compose
COMPOSE_BASE := -f $(DOCKER_DIR)/docker-compose.base.yml
COMPOSE_LOCAL := $(COMPOSE) $(COMPOSE_BASE) -f $(DOCKER_DIR)/docker-compose.local.yml --env-file $(DOCKER_DIR)/env/.env.local
COMPOSE_TEST := $(COMPOSE) $(COMPOSE_BASE) -f $(DOCKER_DIR)/docker-compose.test.yml --env-file $(DOCKER_DIR)/env/.env.test
COMPOSE_PROD := $(COMPOSE) $(COMPOSE_BASE) -f $(DOCKER_DIR)/docker-compose.production.yml --env-file $(DOCKER_DIR)/env/.env.production
KUBECTL := kubectl
NAMESPACE := microservice
REGISTRY ?=
TAG ?= latest
# ============================================================
# .NET — Build & Restore
# ============================================================
restore: ## Restore NuGet packages
dotnet restore $(SOLUTION)
build: ## Build the solution
dotnet build $(SOLUTION) --no-restore
run: local-up ## Start all services via Docker (local mode)
test: ## Run tests
dotnet test $(SOLUTION) --no-build --verbosity normal
clean: ## Clean build artifacts
dotnet clean $(SOLUTION)
rm -rf src/**/bin src/**/obj
# ============================================================
# Docker — Build images
# ============================================================
docker-build: ## Build all Docker images
$(COMPOSE_LOCAL) build
docker-clean: ## Remove all containers, volumes and images
$(COMPOSE_LOCAL) down -v --rmi local
docker system prune -f
# ============================================================
# Local environment (development)
# ============================================================
local-up: ## Start all services in local/dev mode
$(COMPOSE_LOCAL) up -d --build
local-down: ## Stop and remove local containers
$(COMPOSE_LOCAL) down
local-restart: ## Restart local containers
$(COMPOSE_LOCAL) down
$(COMPOSE_LOCAL) up -d --build
local-logs: ## Follow logs for local containers
$(COMPOSE_LOCAL) logs -f
local-ps: ## List running local containers
$(COMPOSE_LOCAL) ps
# ============================================================
# Test environment
# ============================================================
test-up: ## Start all services in test mode
$(COMPOSE_TEST) up -d --build
test-down: ## Stop and remove test containers
$(COMPOSE_TEST) down
test-restart: ## Restart test containers
$(COMPOSE_TEST) down
$(COMPOSE_TEST) up -d --build
test-logs: ## Follow logs for test containers
$(COMPOSE_TEST) logs -f
test-ps: ## List running test containers
$(COMPOSE_TEST) ps
# ============================================================
# Production environment
# ============================================================
prod-up: ## Start all services in production mode
$(COMPOSE_PROD) up -d --build
prod-down: ## Stop and remove production containers
$(COMPOSE_PROD) down
prod-restart: ## Restart production containers
$(COMPOSE_PROD) down
$(COMPOSE_PROD) up -d --build
prod-logs: ## Follow logs for production containers
$(COMPOSE_PROD) logs -f
prod-ps: ## List running production containers
$(COMPOSE_PROD) ps
# ============================================================
# Single service operations (e.g. make up-identity-api)
# ============================================================
up-%: ## Start a single service in local mode (e.g. make up-identity-api)
$(COMPOSE_LOCAL) up -d --build $*
down-%: ## Stop a single service (e.g. make down-product-api)
$(COMPOSE_LOCAL) stop $*
$(COMPOSE_LOCAL) rm -f $*
logs-%: ## Follow logs for a single service (e.g. make logs-api-gateway)
$(COMPOSE_LOCAL) logs -f $*
restart-%: ## Restart a single service (e.g. make restart-order-api)
$(COMPOSE_LOCAL) up -d --build --force-recreate $*
# ============================================================
# Monitoring — Prometheus & Grafana
# ============================================================
monitoring-up: ## Start only monitoring stack (Prometheus + Grafana)
$(COMPOSE_LOCAL) up -d --build prometheus grafana
monitoring-down: ## Stop monitoring containers
$(COMPOSE_LOCAL) stop prometheus grafana
$(COMPOSE_LOCAL) rm -f prometheus grafana
# ============================================================
# Kubernetes
# ============================================================
k8s-validate: ## Validate Kustomize manifests
$(KUBECTL) kustomize k8s/
k8s-deploy: ## Deploy to Kubernetes cluster
@if [ -n "$(REGISTRY)" ]; then \
echo "📦 Registry: $(REGISTRY)"; \
docker build -t $(REGISTRY)/identity-api:$(TAG) -f docker/images/identity-api/Dockerfile . && \
docker build -t $(REGISTRY)/product-api:$(TAG) -f docker/images/product-api/Dockerfile . && \
docker build -t $(REGISTRY)/order-api:$(TAG) -f docker/images/order-api/Dockerfile . && \
docker build -t $(REGISTRY)/api-gateway:$(TAG) -f docker/images/api-gateway/Dockerfile . && \
docker push $(REGISTRY)/identity-api:$(TAG) && \
docker push $(REGISTRY)/product-api:$(TAG) && \
docker push $(REGISTRY)/order-api:$(TAG) && \
docker push $(REGISTRY)/api-gateway:$(TAG); \
fi
$(KUBECTL) apply -k k8s/
@echo ""
@echo "⏳ Waiting for rollout..."
$(KUBECTL) -n $(NAMESPACE) rollout status deployment/identity-api --timeout=120s
$(KUBECTL) -n $(NAMESPACE) rollout status deployment/product-api --timeout=120s
$(KUBECTL) -n $(NAMESPACE) rollout status deployment/order-api --timeout=120s
$(KUBECTL) -n $(NAMESPACE) rollout status deployment/api-gateway --timeout=120s
$(KUBECTL) -n $(NAMESPACE) rollout status deployment/prometheus --timeout=120s
$(KUBECTL) -n $(NAMESPACE) rollout status deployment/grafana --timeout=120s
@echo ""
@echo "✅ Deployment complete!"
k8s-delete: ## Delete all Kubernetes resources
$(KUBECTL) delete -k k8s/ --ignore-not-found
k8s-status: ## Show Kubernetes pod, service and ingress status
@echo "📊 Pods:"
@$(KUBECTL) -n $(NAMESPACE) get pods -o wide
@echo ""
@echo "🌐 Services:"
@$(KUBECTL) -n $(NAMESPACE) get svc
@echo ""
@echo "🔗 Ingress:"
@$(KUBECTL) -n $(NAMESPACE) get ingress
@echo ""
@echo "📈 HPA:"
@$(KUBECTL) -n $(NAMESPACE) get hpa
k8s-logs: ## Follow all Kubernetes pod logs
$(KUBECTL) -n $(NAMESPACE) logs -f -l app.kubernetes.io/part-of=dotnet-microservice --all-containers=true --max-log-requests=10
# ============================================================
# Health Check
# ============================================================
health: ## Check health status of all services
@echo "🏥 Health Check"
@echo "==============="
@printf " API Gateway : " && curl -sf http://localhost:5050/health && echo " ✅" || echo "❌"
@printf " Identity API: " && curl -sf http://localhost:5001/health && echo " ✅" || echo "❌"
@printf " Product API : " && curl -sf http://localhost:5002/health && echo " ✅" || echo "❌"
@printf " Order API : " && curl -sf http://localhost:5003/health && echo " ✅" || echo "❌"
@printf " Prometheus : " && curl -sf http://localhost:9090/-/ready && echo " ✅" || echo "❌"
@printf " Grafana : " && curl -sf http://localhost:3000/api/health | grep -q ok && echo " ✅" || echo "❌"
@echo ""
# ============================================================
# Shortcuts
# ============================================================
local: local-up ## Alias for local-up
up: local-up ## Alias for local-up
down: local-down ## Alias for local-down
prod: prod-up ## Alias for prod-up
ps: local-ps ## Alias for local-ps
logs: local-logs ## Alias for local-logs
restart: local-restart ## Alias for local-restart