Makefile206 lines, 8.9 KB
| 1 | # Database migrations. Pick another service with SERVICE=auth, for example. |
| 2 | SERVICE ?= wallet |
| 3 | DB_URL ?= postgres://payments:payments@localhost:5432/$(SERVICE)?sslmode=disable |
| 4 | MIGRATE := migrate -path db/$(SERVICE)/migrations -database "$(DB_URL)" |
| 5 | |
| 6 | .PHONY: migrate-up migrate-down migrate-version migrate-new |
| 7 | |
| 8 | migrate-up: |
| 9 | @$(MIGRATE) up |
| 10 | |
| 11 | migrate-down: |
| 12 | @$(MIGRATE) down 1 |
| 13 | |
| 14 | migrate-version: |
| 15 | @$(MIGRATE) version |
| 16 | |
| 17 | migrate-new: |
| 18 | @test -n "$(name)" || (echo "usage: make migrate-new name=add_something" && exit 1) |
| 19 | migrate create -ext sql -dir db/$(SERVICE)/migrations -seq $(name) |
| 20 | |
| 21 | .PHONY: generate test test-integration test-race cover loadtest |
| 22 | |
| 23 | # Regenerate the Go code for every service's SQL queries, and for our |
| 24 | # protobuf contracts, as long as they pass buf's lint rules. |
| 25 | generate: |
| 26 | @sqlc generate |
| 27 | @buf lint |
| 28 | @buf generate |
| 29 | |
| 30 | # Run part of the suite with PKGS=./internal/wallet TESTFLAGS='-run Transfer'. |
| 31 | PKGS ?= ./... |
| 32 | |
| 33 | # Unit tests: seconds, and no Docker. Integration tests skip with -short. |
| 34 | test: |
| 35 | @go test -short -count=1 $(TESTFLAGS) $(PKGS) |
| 36 | |
| 37 | # Every test, on throwaway Postgres, NATS and Redis containers. Four packages |
| 38 | # at a time: each starts its own, and Docker shouldn't start a dozen at once. |
| 39 | test-integration: |
| 40 | @go test -count=1 -p 4 $(TESTFLAGS) $(PKGS) |
| 41 | |
| 42 | # Every test, under the race detector. CI runs this one. |
| 43 | test-race: |
| 44 | @go test -race -count=1 -p 4 $(TESTFLAGS) $(PKGS) |
| 45 | |
| 46 | # Coverage of every package that has tests, then in total. For a closer look: |
| 47 | # go tool cover -html=cover.out |
| 48 | TESTED = $(shell go list -f '{{if or .TestGoFiles .XTestGoFiles}}{{.ImportPath}}{{end}}' ./...) |
| 49 | cover: |
| 50 | @go test -count=1 -p 4 -coverprofile=cover.out $(TESTFLAGS) $(TESTED) |
| 51 | @go tool cover -func=cover.out | tail -1 |
| 52 | |
| 53 | # A load test of transfers through the gateway, with every service running. |
| 54 | # k6 runs from its official image: there's nothing to install. |
| 55 | loadtest: |
| 56 | @docker run --rm -t --add-host=host.docker.internal:host-gateway -v ./loadtest:/loadtest \ |
| 57 | -e BASE_URL=http://host.docker.internal:8080 \ |
| 58 | grafana/k6:2.4.0 run /loadtest/transfers.js |
| 59 | |
| 60 | # The whole system, from images, set up for a load test: k6 signs up fifty |
| 61 | # users from one address, and each sends far more than the five transfers in |
| 62 | # ten minutes that our velocity limit allows. |
| 63 | .PHONY: loadtest-up sagas |
| 64 | loadtest-up: |
| 65 | @GATEWAY_IP_LIMIT=1000 TRANSFER_RISK_MAX_COUNT=1000 docker compose --profile app up -d --build --wait |
| 66 | |
| 67 | # How long the last load test's sagas took: what people waiting for their |
| 68 | # money feel, after the 202. |
| 69 | sagas: |
| 70 | @docker compose exec -T postgres psql -U payments -d transfer < loadtest/sagas.sql |
| 71 | |
| 72 | .PHONY: run-wallet run-auth run-transfer run-notification dead-letters replay-dead-letters run-gateway keys reconciliation |
| 73 | |
| 74 | # Run the wallet service against the local database, stamped with a git version. |
| 75 | # Extra flags for a service go in ARGS: make run-wallet ARGS=-migrate |
| 76 | VERSION ?= $(shell git describe --always --dirty) |
| 77 | |
| 78 | # Every service sends its traces to Jaeger, over OTLP. Tests don't: without |
| 79 | # the variable, tracing is off. |
| 80 | run-%: export OTEL_EXPORTER_OTLP_ENDPOINT = http://localhost:4318 |
| 81 | |
| 82 | run-wallet: |
| 83 | @go build -ldflags "-X main.version=$(VERSION)" -o bin/wallet ./cmd/wallet |
| 84 | @WALLET_DATABASE_URL="$(DB_URL)" \ |
| 85 | WALLET_AUTH_PUBLIC_KEY_FILE=secrets/auth_ed25519.pub.pem ./bin/wallet $(ARGS) |
| 86 | |
| 87 | run-auth: |
| 88 | @go build -ldflags "-X main.version=$(VERSION)" -o bin/auth ./cmd/auth |
| 89 | @AUTH_DATABASE_URL="postgres://payments:payments@localhost:5432/auth?sslmode=disable" \ |
| 90 | AUTH_PRIVATE_KEY_FILE=secrets/auth_ed25519.pem ./bin/auth $(ARGS) |
| 91 | |
| 92 | run-transfer: |
| 93 | @go build -ldflags "-X main.version=$(VERSION)" -o bin/transfer ./cmd/transfer |
| 94 | @TRANSFER_DATABASE_URL="postgres://payments:payments@localhost:5432/transfer?sslmode=disable" \ |
| 95 | TRANSFER_AUTH_PUBLIC_KEY_FILE=secrets/auth_ed25519.pub.pem ./bin/transfer $(ARGS) |
| 96 | |
| 97 | NOTIFICATION_DB_URL := postgres://payments:payments@localhost:5432/notification?sslmode=disable |
| 98 | |
| 99 | run-notification: |
| 100 | @go build -ldflags "-X main.version=$(VERSION)" -o bin/notification ./cmd/notification |
| 101 | @NOTIFICATION_DATABASE_URL="$(NOTIFICATION_DB_URL)" ./bin/notification $(ARGS) |
| 102 | |
| 103 | # The messages the notification service gave up on, and why. |
| 104 | dead-letters: |
| 105 | @docker compose exec -T postgres psql -U payments -d notification -c \ |
| 106 | "SELECT stream, seq, deliveries, left(error, 70) AS error FROM dead_letters \ |
| 107 | WHERE replayed_at IS NULL" |
| 108 | |
| 109 | # Once whatever broke them is fixed, send them again. |
| 110 | replay-dead-letters: |
| 111 | @go build -ldflags "-X main.version=$(VERSION)" -o bin/notification ./cmd/notification |
| 112 | @NOTIFICATION_DATABASE_URL="$(NOTIFICATION_DB_URL)" ./bin/notification -replay-dead-letters |
| 113 | |
| 114 | # What the last reconciliation found: nothing at all, or exactly what's off. |
| 115 | reconciliation: |
| 116 | @docker compose exec -T postgres psql -U payments -d wallet -c \ |
| 117 | "SELECT d.* FROM job_runs, jsonb_to_recordset(summary->'discrepancies') \ |
| 118 | AS d(\"check\" text, ref text, currency text, expected bigint, actual bigint) \ |
| 119 | WHERE id = (SELECT max(id) FROM job_runs WHERE job = 'reconcile' AND status = 'succeeded')" |
| 120 | |
| 121 | # The slowest queries on average, in every database, since Postgres started: |
| 122 | # SELECT pg_stat_statements_reset() starts the count again. |
| 123 | .PHONY: slow-queries |
| 124 | slow-queries: |
| 125 | @docker compose exec -T postgres psql -U payments -c "SELECT calls, round(mean_exec_time) AS mean_ms, \ |
| 126 | left(regexp_replace(query, '\s+', ' ', 'g'), 80) AS query FROM pg_stat_statements \ |
| 127 | ORDER BY mean_exec_time DESC LIMIT 8" |
| 128 | |
| 129 | # The public API on port 8080, in front of the others. |
| 130 | run-gateway: |
| 131 | @go build -ldflags "-X main.version=$(VERSION)" -o bin/gateway ./cmd/gateway |
| 132 | @GATEWAY_AUTH_PUBLIC_KEY_FILE=secrets/auth_ed25519.pub.pem ./bin/gateway $(ARGS) |
| 133 | |
| 134 | .PHONY: run-webhook |
| 135 | |
| 136 | # Webhooks, on port 8085: our users' servers hear about their transfers. |
| 137 | run-webhook: |
| 138 | @go build -ldflags "-X main.version=$(VERSION)" -o bin/webhook ./cmd/webhook |
| 139 | @WEBHOOK_DATABASE_URL="postgres://payments:payments@localhost:5432/webhook?sslmode=disable" \ |
| 140 | WEBHOOK_AUTH_PUBLIC_KEY_FILE=secrets/auth_ed25519.pub.pem ./bin/webhook $(ARGS) |
| 141 | |
| 142 | # A new Ed25519 key pair for signing access tokens, readable only by you. The |
| 143 | # auth service gets the private key, every other service the public one. |
| 144 | keys: |
| 145 | @mkdir -p secrets |
| 146 | @test ! -e secrets/auth_ed25519.pem || (echo "secrets/auth_ed25519.pem exists, keeping it" && exit 1) |
| 147 | @umask 077 && openssl genpkey -algorithm ed25519 -out secrets/auth_ed25519.pem |
| 148 | @openssl pkey -in secrets/auth_ed25519.pem -pubout -out secrets/auth_ed25519.pub.pem |
| 149 | @ls -l secrets |
| 150 | |
| 151 | # Certificates for mTLS between our services, from a new development CA: a |
| 152 | # directory each, in secrets/tls, for a week. To renew them, run it again, |
| 153 | # and restart the services. |
| 154 | .PHONY: certs |
| 155 | certs: |
| 156 | @go run ./cmd/devca -dir secrets/tls gateway wallet auth transfer notification ops |
| 157 | |
| 158 | # Kubernetes, on this machine. Create, then apply: the same line creates an |
| 159 | # object the first time, and updates it every time after. |
| 160 | KUBE_APPLY := --dry-run=client -o yaml | kubectl apply -f - |
| 161 | |
| 162 | # The cluster's Secrets, from files in secrets/ and the development passwords |
| 163 | # in deploy/postgres: never in git, never in the chart. |
| 164 | .PHONY: k8s-secrets |
| 165 | k8s-secrets: |
| 166 | @kubectl create secret generic postgres --from-literal=password=payments $(KUBE_APPLY) |
| 167 | @kubectl create secret generic auth-private-key \ |
| 168 | --from-file=private.pem=secrets/auth_ed25519.pem $(KUBE_APPLY) |
| 169 | @kubectl create secret generic auth-public-key \ |
| 170 | --from-file=public.pem=secrets/auth_ed25519.pub.pem $(KUBE_APPLY) |
| 171 | @for s in gateway wallet auth transfer notification; do \ |
| 172 | kubectl create secret generic $$s-tls --from-file=secrets/tls/$$s $(KUBE_APPLY); \ |
| 173 | done |
| 174 | @for s in wallet auth transfer notification webhook; do \ |
| 175 | kubectl create secret generic $$s-db \ |
| 176 | --from-literal=url=postgres://$${s}_app:$${s}_app@postgres:5432/$$s?sslmode=disable \ |
| 177 | --from-literal=owner-url=postgres://$${s}_owner:$${s}_owner@postgres:5432/$$s?sslmode=disable \ |
| 178 | $(KUBE_APPLY); \ |
| 179 | done |
| 180 | |
| 181 | # An image's tag says what's in it: never latest. kind loads our images |
| 182 | # straight into its nodes, so there's no registry to push them to. |
| 183 | TAG ?= $(shell git describe --always --dirty) |
| 184 | SERVICES := gateway wallet auth transfer notification webhook |
| 185 | |
| 186 | .PHONY: images deploy |
| 187 | images: |
| 188 | @for s in $(SERVICES); do \ |
| 189 | docker build -q --build-arg SERVICE=$$s --build-arg VERSION=$(TAG) -t payments/$$s:$(TAG) . \ |
| 190 | || exit 1; \ |
| 191 | done |
| 192 | @kind load docker-image --name payments $(SERVICES:%=payments/%:$(TAG)) |
| 193 | |
| 194 | # Our chart, at TAG: installed the first time, upgraded every time after. |
| 195 | deploy: |
| 196 | @helm upgrade --install payments deploy/helm/payments \ |
| 197 | -f deploy/helm/payments/values-kind.yaml --set image.tag=$(TAG) --wait --timeout 5m |
| 198 | |
| 199 | # Chaos experiments: the whole system, set up for a load test, with Toxiproxy |
| 200 | # in two places: between the gateway and the transfer service, and between |
| 201 | # the transfer service and the wallet. Then: go run ./cmd/chaos |
| 202 | .PHONY: chaos-up |
| 203 | chaos-up: |
| 204 | @GATEWAY_IP_LIMIT=1000 TRANSFER_RISK_MAX_COUNT=1000 \ |
| 205 | GATEWAY_TRANSFER_URL=http://toxiproxy:18083 TRANSFER_WALLET_ADDR=dns:///toxiproxy:19081 \ |
| 206 | docker compose --profile app --profile chaos up -d --build --wait $(SERVICES) toxiproxy |