-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMakefile
More file actions
247 lines (196 loc) · 9.79 KB
/
Copy pathMakefile
File metadata and controls
247 lines (196 loc) · 9.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# OpenDecree Makefile
#
# Prerequisites: Go 1.25+, Docker, Make. All code generators run in Docker.
# Run `make help` to see available targets.
#
# Workflow: modify specs → make generate → make pre-commit → commit
# Full CI: make all (generate + lint + test + build)
# --- Configuration ---
BINARY_NAME := decree-server
BUILD_DIR := bin
TOOLS_IMAGE := decree-tools
TOOLS_SENTINEL := .tools-image-built
DOCKER_RUN_TOOLS := docker run --rm -u $(shell id -u):$(shell id -g) -e HOME=/tmp -v $(CURDIR):/workspace -w /workspace $(TOOLS_IMAGE)
GOLANGCI_LINT_VERSION := v2.11.4
# Version injection for binaries.
GIT_VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo dev)
GIT_COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo unknown)
SERVER_LDFLAGS := -X github.com/opendecree/decree/internal/version.Version=$(GIT_VERSION) -X github.com/opendecree/decree/internal/version.Commit=$(GIT_COMMIT)
CLI_LDFLAGS := -X main.cliVersion=$(GIT_VERSION) -X main.cliCommit=$(GIT_COMMIT)
# Module lists for multi-module operations.
SDK_MODULES := sdk/retry sdk/configclient sdk/adminclient sdk/configwatcher sdk/grpctransport sdk/tools sdk/contrib/viper sdk/contrib/envconfig sdk/contrib/koanf
CONTRIB_MODULES := contrib/decree-docs
.PHONY: all generate generate-proto generate-sqlc deps test lint lint-go lint-proto lint-migrations build image ui migrate sync-migrations e2e e2e-jwt examples bench bench-e2e stress chaos docs docs-api docs-cli docs-man docs-serve docs-deploy pre-commit clean tools help demo-gif validate-meta-schemas
all: generate lint test build
# --- Docker tools ---
## tools: Build the tools Docker image (only when Dockerfile.tools changes)
tools: $(TOOLS_SENTINEL)
$(TOOLS_SENTINEL): build/Dockerfile.tools
docker build -t $(TOOLS_IMAGE) -f build/Dockerfile.tools .
@touch $(TOOLS_SENTINEL)
# --- Code generation ---
## generate: Generate code from protobuf and SQL specs (Docker)
generate: $(TOOLS_SENTINEL)
$(DOCKER_RUN_TOOLS) sh -c 'buf generate && cd db && sqlc generate'
python3 scripts/patch-openapi.py
## generate-proto: Generate Go code from protobuf definitions only (Docker)
generate-proto: $(TOOLS_SENTINEL)
$(DOCKER_RUN_TOOLS) buf generate
python3 scripts/patch-openapi.py
## generate-sqlc: Generate Go code from SQL queries only (Docker)
generate-sqlc: $(TOOLS_SENTINEL)
$(DOCKER_RUN_TOOLS) bash -c "cd db && sqlc generate"
# --- Build ---
## build: Build service + CLI binaries to bin/
build:
go build -ldflags '$(SERVER_LDFLAGS)' -o $(BUILD_DIR)/$(BINARY_NAME) ./cmd/server
cd cmd/decree && go build -ldflags '$(CLI_LDFLAGS)' -o ../../$(BUILD_DIR)/decree .
## image: Build the Docker image
image:
docker build -t $(BINARY_NAME) -f build/Dockerfile .
## ui: Copy admin UI dist from ../decree-ui into internal/ui/dist (local dev). Requires decree-ui checked out at ../decree-ui.
ui:
npm --prefix ../decree-ui ci
npm --prefix ../decree-ui run build
rm -rf internal/ui/dist/assets
cp -r ../decree-ui/dist/. internal/ui/dist/
# --- Quality ---
## lint: Run all linters (Go + protobuf + migrations)
lint: lint-go lint-proto lint-migrations
## lint-go: Run golangci-lint (linters + formatter) across all modules
lint-go:
golangci-lint run ./...
@for mod in $(SDK_MODULES) $(CONTRIB_MODULES) cmd/decree; do (cd $$mod && golangci-lint run ./...) || exit 1; done
golangci-lint fmt --diff
@for mod in $(SDK_MODULES) $(CONTRIB_MODULES) cmd/decree; do (cd $$mod && golangci-lint fmt --diff) || exit 1; done
## lint-migrations: Check migrations for blocking CREATE INDEX (requires CONCURRENTLY or suppression annotation)
lint-migrations:
./scripts/check-concurrent-indexes.sh
## lint-proto: Run buf lint + breaking change detection (Docker)
## buf breaking is skipped in git worktrees (Docker cannot traverse worktree .git files).
lint-proto: $(TOOLS_SENTINEL)
$(DOCKER_RUN_TOOLS) sh -c 'buf lint && ([ -f .git ] || buf breaking --against ".git#branch=main")'
## deps: Download Go module dependencies across all modules
deps:
go mod download ./...
@for mod in api $(SDK_MODULES) $(CONTRIB_MODULES) cmd/decree; do (cd $$mod && go mod download ./...) || exit 1; done
## test: Run unit tests across all modules
test:
go test ./... -race -count=1
@for mod in $(SDK_MODULES) $(CONTRIB_MODULES) cmd/decree; do (cd $$mod && go test ./... -race -count=1) || exit 1; done
## validate-meta-schemas: Validate canonical schema/config YAMLs against the v0.1.0 meta-schemas
validate-meta-schemas:
python3 scripts/validate-meta-schemas.py
## pre-commit: Run all before-commit checks (build, vet, format, lint, test, coverage)
pre-commit:
@echo "=== Build ==="
go build ./...
@for mod in $(SDK_MODULES) $(CONTRIB_MODULES) cmd/decree; do (cd $$mod && go build ./...) || exit 1; done
@echo "=== Vet ==="
go vet ./...
@for mod in $(SDK_MODULES) $(CONTRIB_MODULES) cmd/decree; do (cd $$mod && go vet ./...) || exit 1; done
@echo "=== Format ==="
@unformatted=$$(gofumpt -l .); \
if [ -n "$$unformatted" ]; then \
echo "Unformatted files:"; echo "$$unformatted"; \
echo "Run: gofumpt -w ."; exit 1; \
fi
@echo "=== Lint ==="
golangci-lint run ./...
@echo "=== Test ==="
go test ./... -race -count=1
@for mod in $(SDK_MODULES) $(CONTRIB_MODULES) cmd/decree; do (cd $$mod && go test ./... -race -count=1) || exit 1; done
@echo "=== Coverage ==="
./scripts/check-coverage.sh
@echo ""
@echo "✓ All pre-commit checks passed"
# --- Database ---
## migrate: Run database migrations up (Docker)
migrate: $(TOOLS_SENTINEL)
$(DOCKER_RUN_TOOLS) goose -dir db/migrations postgres "$${DB_WRITE_URL:-postgres://centralconfig:localdev@localhost:5432/centralconfig?sslmode=disable}" up
## migrate-down: Roll back the last migration (Docker)
migrate-down: $(TOOLS_SENTINEL)
$(DOCKER_RUN_TOOLS) goose -dir db/migrations postgres "$${DB_WRITE_URL:-postgres://centralconfig:localdev@localhost:5432/centralconfig?sslmode=disable}" down
## sync-migrations: Copy db/migrations SQL into the CLI embed package (run after adding a migration)
sync-migrations:
cp db/migrations/*.sql cmd/decree/migrations/
@echo "Synced db/migrations → cmd/decree/migrations (the embed drift guard runs in 'make test')"
# --- Integration tests ---
## integration-test: Run Postgres integration tests via testcontainers (requires Docker)
integration-test:
go test -tags=integration -v -race -count=1 -timeout=300s \
./internal/storage/... \
./internal/audit/... \
./internal/config/... \
./internal/schema/...
## e2e: Run end-to-end tests (docker compose lifecycle)
e2e:
docker compose up -d --wait service
cd e2e && go test -tags=e2e -v -race -count=1 ./... || (cd .. && docker compose down -v && exit 1)
docker compose down -v
## e2e-jwt: Run JWT-auth e2e tests (docker compose lifecycle, service-jwt on :9091)
e2e-jwt:
docker compose up -d --wait service-jwt
cd e2e && SERVICE_JWT_ADDR=localhost:9091 JWKS_SERVER_ADDR=localhost:8090 \
go test -tags=e2e -run TestJWT -v -race -count=1 ./... || (cd .. && docker compose down -v && exit 1)
docker compose down -v
## e2e-coverage: Run e2e against a -cover instrumented server and report coverage on files excluded from unit-test total
e2e-coverage:
./scripts/e2e-coverage.sh
## upgrade-test: Run upgrade safety test (old binary → goose up → new binary). Requires TOOLS_IMAGE and SERVICE_IMAGE.
upgrade-test:
./scripts/run-upgrade-test.sh
## examples: Run SDK examples (docker compose lifecycle)
examples:
docker compose up -d --wait service
cd examples && make setup && make test || (cd .. && docker compose down -v && exit 1)
docker compose down -v
# --- Benchmarks ---
## bench: Run unit benchmarks
bench:
go test ./internal/... -bench=. -benchmem -count=3 -run=^$$
## bench-e2e: Run e2e benchmarks (docker compose lifecycle)
bench-e2e:
docker compose up -d --wait service
cd e2e && go test -tags=e2e -bench=. -benchmem -count=3 -run=^$$ -timeout=300s ./... || (cd .. && docker compose down -v && exit 1)
docker compose down -v
## stress: Run stress tests (docker compose lifecycle). CI=true uses short mode.
stress:
docker compose up -d --wait service
cd stress && go test -tags=stress -v $(if $(CI),-short,) -timeout=600s ./... || (cd .. && docker compose down -v && exit 1)
docker compose down -v
## chaos: Run chaos/fault-injection tests (docker compose lifecycle). Kills containers — destructive.
chaos:
docker compose up -d --wait service
cd chaos && go test -tags=chaos -v -timeout=300s ./... || (cd .. && docker compose down -v && exit 1)
docker compose down -v
# --- Documentation ---
## docs: Generate all documentation (API + CLI + man pages)
docs: docs-api docs-cli docs-man
## docs-api: Generate proto API reference markdown (Docker)
docs-api: $(TOOLS_SENTINEL)
@mkdir -p docs/api
$(DOCKER_RUN_TOOLS) buf generate --template buf.gen.doc.yaml
## docs-cli: Generate CLI reference markdown
docs-cli:
cd cmd/decree && go build -ldflags '$(CLI_LDFLAGS)' -o ../../$(BUILD_DIR)/decree . && cd ../.. && $(BUILD_DIR)/decree gen-docs docs/cli
## docs-man: Generate man pages
docs-man:
cd cmd/decree && go build -ldflags '$(CLI_LDFLAGS)' -o ../../$(BUILD_DIR)/decree . && cd ../.. && $(BUILD_DIR)/decree gen-man docs/man
# --- Demo recording ---
## demo-gif: Render assets/demo.tape to assets/demo.gif (needs docker + running service at :9090)
demo-gif:
docker exec decree-postgres-1 psql -U centralconfig -d centralconfig -c "TRUNCATE tenants, schemas, schema_versions, config_values CASCADE;" >/dev/null
docker run --rm --network host \
-v $(CURDIR):/vhs \
-v $(CURDIR)/bin/decree:/usr/local/bin/decree:ro \
ghcr.io/charmbracelet/vhs /vhs/assets/demo.tape
docker run --rm -v $(CURDIR)/assets:/w alpine chown -R $(shell id -u):$(shell id -g) /w
# --- Maintenance ---
## clean: Remove build artifacts and generated code
clean:
rm -rf $(BUILD_DIR)
$(DOCKER_RUN_TOOLS) rm -rf api/centralconfig/ internal/storage/dbstore/
## help: Show this help
help:
@grep -E '^## ' $(MAKEFILE_LIST) | sed 's/## //' | column -t -s ':'