Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
196 lines (160 loc) · 9.45 KB
/
Copy pathMakefile
File metadata and controls
196 lines (160 loc) · 9.45 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
# Gate — developer entry points. Run `make` for the grouped target list.
#
# Docker Compose v2 is required (the `--wait` flag replaces hand-rolled polling
# loops). Override any of these on the command line, e.g. `make APP_URL=... smoke`.
COMPOSE ?= docker compose
APP_URL ?= http://localhost:8080
LOCALSTACK_URL ?= http://localhost:4566
# The built-in development keys. Against a deployed stack, which refuses them,
# `source .demo-keys.env` first: deploy-demo.sh writes the demo's keys there.
API_KEY ?= test-api-key
# A Free-tier key; invariant A drains its 20-token bucket.
FREE_API_KEY ?= free-api-key
# What a locally built image reports as its commit in /health.
export GIT_COMMIT ?= $(shell git describe --always --dirty 2>/dev/null || echo unknown)
# Unique-per-invocation suffix for the smoke test key, so repeat runs never
# land on a bucket a previous run already drained. Falls back when the recipe
# shell has no `date +%s` (e.g. some Windows shells).
SMOKE_ID := $(shell date +%s 2>/dev/null || echo 0)
# Every sbt target opens with this, so a missing sbt gives instructions instead
# of "command not found".
NEED_SBT = @command -v sbt >/dev/null 2>&1 || { echo "Error: sbt not found in PATH."; echo " Install it: https://www.scala-sbt.org/download.html"; echo " Or skip sbt entirely and use Docker: make stack"; exit 1; }
.DEFAULT_GOAL := help
.PHONY: help up up-clean down clean logs status run dev stack stack-scaled obs \
fmt test test-it test-all smoke health correctness env-drift monitoring-check tf-validate tf-test spec \
slo-check usage-check helm-check
help: ## Show this help
@awk 'BEGIN { FS = ":.*##"; printf "Usage: make <target>\n" } \
/^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } \
/^[a-z][a-z0-9-]*:.*##/ { printf " \033[36m%-12s\033[0m %s\n", $$1, $$2 }' \
$(MAKEFILE_LIST)
@echo ""
##@ Stack
up: ## Start LocalStack alone and wait until its tables/streams exist
$(COMPOSE) up -d --build --wait localstack
up-clean: ## Rebuild the LocalStack image from scratch, then start it
$(COMPOSE) build --no-cache localstack
$(COMPOSE) up -d --wait localstack
stack: ## Start the whole stack in Docker (LocalStack + app) — no sbt needed
$(COMPOSE) up -d --build --wait
@echo "Gate is up at $(APP_URL). Follow it with 'make logs'."
stack-scaled: ## Start GATE_REPLICAS (default 3) replicas behind a load balancer on :8080
$(COMPOSE) -f docker-compose.yml -f docker-compose.scale.yml up -d --build --wait
@echo "$${GATE_REPLICAS:-3} replicas behind http://localhost:8080. Stop with: $(COMPOSE) -f docker-compose.yml -f docker-compose.scale.yml down -v"
obs: ## Start the stack plus Prometheus, Grafana and Jaeger
$(COMPOSE) --profile obs up -d --build --wait
@echo "Grafana http://localhost:3000 | Prometheus http://localhost:9090 | Jaeger http://localhost:16686"
down: ## Stop every container, keeping volumes
-$(COMPOSE) --profile obs down
clean: ## Stop everything, drop volumes, and delete build artifacts
-$(COMPOSE) --profile obs down -v
rm -rf target project/target loadSim/target .bsp .metals
logs: ## Follow container logs (SERVICE=localstack to narrow to one)
$(COMPOSE) --profile obs logs -f $(SERVICE)
status: ## Show LocalStack health plus the DynamoDB tables and Kinesis streams
@echo "LocalStack health:"
@curl -s $(LOCALSTACK_URL)/_localstack/health | python3 -m json.tool 2>/dev/null || echo " not running — start it with 'make up'"
@echo ""; echo "DynamoDB tables:"
@$(COMPOSE) exec -T localstack awslocal dynamodb list-tables 2>/dev/null || echo " (unavailable)"
@echo ""; echo "Kinesis streams:"
@$(COMPOSE) exec -T localstack awslocal kinesis list-streams 2>/dev/null || echo " (unavailable)"
##@ Application
run: ## Run the app with sbt against a running LocalStack
$(NEED_SBT)
USE_LOCALSTACK=true \
DYNAMODB_ENDPOINT=$(LOCALSTACK_URL) \
KINESIS_ENDPOINT=$(LOCALSTACK_URL) \
AWS_ACCESS_KEY_ID=test \
AWS_SECRET_ACCESS_KEY=test \
AWS_REGION=us-east-1 \
ALLOW_BUILT_IN_KEYS=true \
sbt run
dev: up run ## Start LocalStack, then run the app with sbt
##@ Tests
fmt: ## Format all Scala sources (CI fails on unformatted code)
$(NEED_SBT)
sbt scalafmtAll
test: ## Run unit tests, the load simulator's own tests, and the aggregator's
$(NEED_SBT)
sbt unitTest
sbt loadSim/test aggregator/test
test-it: ## Run integration tests (TestContainers — needs Docker, not LocalStack)
$(NEED_SBT)
sbt "testOnly *IntegrationSpec"
test-all: test test-it ## Run unit and integration tests
# The keys go to the load simulator through the environment, and the line that
# sets them is silent. As flags they were echoed here and again by sbt, so a
# run against a deployed stack printed its keys into the terminal and any log.
correctness: ## Check the correctness invariants (APP_URL=... to target a deployed stack)
$(NEED_SBT)
@echo "Correctness invariants against $(APP_URL) (keys from API_KEY and FREE_API_KEY; not printed)"
@API_KEY='$(API_KEY)' FREE_API_KEY='$(FREE_API_KEY)' sbt "loadSim/run --scenario correctness --url $(APP_URL)"
spec: ## Model-check the TLA+ specs in spec/ against spec/expected.txt (needs Java, ~6 min)
bash scripts/check-specs.sh
##@ Probes
health: ## Print /health, /ready and a sample rate-limit status
@for path in /health /ready /v1/ratelimit/status/user:123; do \
echo "GET $$path"; \
curl -s -H 'Authorization: Bearer $(API_KEY)' $(APP_URL)$$path \
| python3 -m json.tool 2>/dev/null \
|| echo " no JSON response — is the service up? ('make dev' or 'make stack')"; \
done
smoke: ## Send 100 rate-limit checks and verify the X-RateLimit-* headers
@echo "Smoke test: 100 requests to $(APP_URL)/v1/ratelimit/check"
@key="smoke-$(SMOKE_ID)-$$$$"; hdr=$$(mktemp); trap 'rm -f "$$hdr"' EXIT; \
post() { curl -s -X POST $(APP_URL)/v1/ratelimit/check \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer $(API_KEY)' \
-d "{\"key\":\"$$key\",\"cost\":1}" "$$@"; }; \
code=$$(post -D "$$hdr" -o /dev/null -w '%{http_code}'); \
case "$$code" in \
000) echo "FAIL: nothing listening on $(APP_URL). Start it with 'make dev' or 'make stack'."; exit 1 ;; \
200) ;; \
*) echo "FAIL: first request returned HTTP $$code (expected 200). X-RateLimit-* headers only appear on allowed responses."; exit 1 ;; \
esac; \
for h in Limit Remaining Reset; do \
grep -qi "X-RateLimit-$$h" "$$hdr" || { echo "FAIL: response is missing the X-RateLimit-$$h header."; exit 1; }; \
done; \
i=1; \
while [ $$i -lt 100 ]; do \
post -o /dev/null || { echo "FAIL: service stopped responding after $$i requests."; exit 1; }; \
i=$$((i + 1)); \
done; \
echo "OK: 100 requests sent; X-RateLimit-Limit, -Remaining and -Reset all present."
usage-check: ## Check the usage aggregator committed every event the service published (stack running)
bash scripts/check-usage-pipeline.sh
##@ Infrastructure
HELM_CHART = deploy/helm/gate
helm-check: ## Lint and render the Helm chart, schema-check it, and check what it refuses
@command -v helm >/dev/null 2>&1 || { echo "Error: helm not found in PATH."; exit 1; }
@command -v kubeconform >/dev/null 2>&1 || { echo "Error: kubeconform not found in PATH."; exit 1; }
helm lint $(HELM_CHART) -f $(HELM_CHART)/ci/test-values.yaml
helm lint $(HELM_CHART) -f $(HELM_CHART)/ci/kind-values.yaml
helm template gate $(HELM_CHART) -f $(HELM_CHART)/ci/test-values.yaml 2>/dev/null \
| kubeconform -strict -summary -kubernetes-version 1.31.0 -schema-location default \
-schema-location 'https://raw.githubusercontent.com/datreeio/CRDs-catalog/main/{{.Group}}/{{.ResourceKind}}_{{.ResourceAPIVersion}}.json'
@for bad in "--set ingress.certificateArn= --set ingress.inboundCidrs={0.0.0.0/0}" \
"--set gate.image.tag=latest" "--set gate.env.SECRETS_MANAGER_ENABLED=false"; do \
if helm template gate $(HELM_CHART) -f $(HELM_CHART)/ci/test-values.yaml $$bad >/dev/null 2>&1; then \
echo "FAIL: the chart rendered with $$bad"; exit 1; \
fi; \
done; echo "OK: the chart refuses a public plaintext ALB, a latest tag, and Secrets Manager off"
# promtool from the Prometheus image compose already uses, so nothing to install.
PROMTOOL = docker run --rm -v "$(CURDIR)/observability/prometheus:/p" -w /p --entrypoint promtool prom/prometheus:v2.54.1
slo-check: ## Check the SLO rules, and run their unit tests (needs Docker)
$(PROMTOOL) check rules rules/slo.yml
$(PROMTOOL) test rules tests/slo_test.yml
env-drift: ## Compare env vars across application.conf, compose and Terraform
./scripts/check-env-drift.sh
monitoring-check: ## Check every app metric the Terraform dashboard and alarms use is emitted
./scripts/check-monitoring-metrics.sh
# -backend=false: validation needs no state, so it runs without AWS access or
# the S3 bucket bootstrap.sh creates.
tf-validate: ## Validate the three Terraform roots (no AWS access needed)
@command -v terraform >/dev/null 2>&1 || { echo "Error: terraform not found in PATH."; echo " Install it: https://www.terraform.io/downloads"; exit 1; }
cd terraform && terraform init -backend=false -input=false >/dev/null && terraform validate
cd terraform/bootstrap && terraform init -backend=false -input=false >/dev/null && terraform validate
cd terraform/eks && terraform init -backend=false -input=false >/dev/null && terraform validate
tf-test: ## Plan the ALB's TLS and exposure rules against a mocked AWS provider
@command -v terraform >/dev/null 2>&1 || { echo "Error: terraform not found in PATH."; exit 1; }
cd terraform/modules/ecs && terraform init -backend=false -input=false >/dev/null && terraform test