-
Notifications
You must be signed in to change notification settings - Fork 653
/
makefile
587 lines (457 loc) · 19.2 KB
/
makefile
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
# Check to see if we can use ash, in Alpine images, or default to BASH.
SHELL_PATH = /bin/ash
SHELL = $(if $(wildcard $(SHELL_PATH)),/bin/ash,/bin/bash)
# Deploy First Mentality
# ==============================================================================
# Go Installation
#
# You need to have Go version 1.22 to run this code.
#
# https://go.dev/dl/
#
# If you are not allowed to update your Go frontend, you can install
# and use a 1.22 frontend.
#
# $ go install golang.org/dl/go1.22@latest
# $ go1.22 download
#
# This means you need to use `go1.22` instead of `go` for any command
# using the Go frontend tooling from the makefile.
# ==============================================================================
# Brew Installation
#
# Having brew installed will simplify the process of installing all the tooling.
#
# Run this command to install brew on your machine. This works for Linux, Mac and Windows.
# The script explains what it will do and then pauses before it does it.
# $ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
#
# WINDOWS MACHINES
# These are extra things you will most likely need to do after installing brew
#
# Run these three commands in your terminal to add Homebrew to your PATH:
# Replace <name> with your username.
# $ echo '# Set PATH, MANPATH, etc., for Homebrew.' >> /home/<name>/.profile
# $ echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> /home/<name>/.profile
# $ eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
#
# Install Homebrew's dependencies:
# $ sudo apt-get install build-essential
#
# Install GCC:
# $ brew install gcc
# ==============================================================================
# Install Tooling and Dependencies
#
# This project uses Docker and it is expected to be installed. Please provide
# Docker at least 4 CPUs. To use Podman instead please alias Docker CLI to
# Podman CLI or symlink the Docker socket to the Podman socket. More
# information on migrating from Docker to Podman can be found at
# https://podman-desktop.io/docs/migrating-from-docker.
#
# Run these commands to install everything needed.
# $ make dev-brew
# $ make dev-docker
# $ make dev-gotooling
# ==============================================================================
# Running Test
#
# Running the tests is a good way to verify you have installed most of the
# dependencies properly.
#
# $ make test
# ==============================================================================
# Running The Project
#
# $ make dev-up
# $ make dev-update-apply
# $ make token
# $ export TOKEN=<token>
# $ make users
#
# You can use `make dev-status` to look at the status of your KIND cluster.
# ==============================================================================
# CLASS NOTES
#
# Kind
# For full Kind v0.24 release notes: https://github.com/kubernetes-sigs/kind/releases/tag/v0.25.0
#
# RSA Keys
# To generate a private/public key PEM file.
# $ openssl genpkey -algorithm RSA -out private.pem -pkeyopt rsa_keygen_bits:2048
# $ openssl rsa -pubout -in private.pem -out public.pem
# $ ./admin genkey
#
# Testing Coverage
# $ go test -coverprofile p.out
# $ go tool cover -html p.out
#
# Module Call Examples
# $ curl https://proxy.golang.org/github.com/ardanlabs/conf/@v/list
# $ curl https://proxy.golang.org/github.com/ardanlabs/conf/v3/@v/list
# $ curl https://proxy.golang.org/github.com/ardanlabs/conf/v3/@v/v3.1.1.info
# $ curl https://proxy.golang.org/github.com/ardanlabs/conf/v3/@v/v3.1.1.mod
# $ curl https://proxy.golang.org/github.com/ardanlabs/conf/v3/@v/v3.1.1.zip
# $ curl https://sum.golang.org/lookup/github.com/ardanlabs/conf/v3@v3.1.1
#
# OPA Playground
# https://play.openpolicyagent.org/
# https://academy.styra.com/
# https://www.openpolicyagent.org/docs/latest/policy-reference/
# ==============================================================================
# Define dependencies
GOLANG := golang:1.23
ALPINE := alpine:3.20
KIND := kindest/node:v1.31.2
POSTGRES := postgres:17.2
GRAFANA := grafana/grafana:11.3.0
PROMETHEUS := prom/prometheus:v2.55.0
TEMPO := grafana/tempo:2.6.0
LOKI := grafana/loki:3.2.0
PROMTAIL := grafana/promtail:3.2.0
KIND_CLUSTER := ardan-starter-cluster
NAMESPACE := sales-system
SALES_APP := sales
AUTH_APP := auth
BASE_IMAGE_NAME := localhost/ardanlabs
VERSION := 0.0.1
SALES_IMAGE := $(BASE_IMAGE_NAME)/$(SALES_APP):$(VERSION)
METRICS_IMAGE := $(BASE_IMAGE_NAME)/metrics:$(VERSION)
AUTH_IMAGE := $(BASE_IMAGE_NAME)/$(AUTH_APP):$(VERSION)
# VERSION := "0.0.1-$(shell git rev-parse --short HEAD)"
# ==============================================================================
# Install dependencies
dev-gotooling:
go install github.com/divan/expvarmon@latest
go install github.com/rakyll/hey@latest
go install honnef.co/go/tools/cmd/staticcheck@latest
go install golang.org/x/vuln/cmd/govulncheck@latest
go install golang.org/x/tools/cmd/goimports@latest
dev-brew:
brew update
brew list kind || brew install kind
brew list kubectl || brew install kubectl
brew list kustomize || brew install kustomize
brew list pgcli || brew install pgcli
brew list watch || brew install watch
dev-docker:
docker pull $(GOLANG) & \
docker pull $(ALPINE) & \
docker pull $(KIND) & \
docker pull $(POSTGRES) & \
docker pull $(GRAFANA) & \
docker pull $(PROMETHEUS) & \
docker pull $(TEMPO) & \
docker pull $(LOKI) & \
docker pull $(PROMTAIL) & \
wait;
# ==============================================================================
# Building containers
build: sales metrics auth
sales:
docker build \
-f zarf/docker/dockerfile.sales \
-t $(SALES_IMAGE) \
--build-arg BUILD_REF=$(VERSION) \
--build-arg BUILD_DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ") \
.
metrics:
docker build \
-f zarf/docker/dockerfile.metrics \
-t $(METRICS_IMAGE) \
--build-arg BUILD_REF=$(VERSION) \
--build-arg BUILD_DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ") \
.
auth:
docker build \
-f zarf/docker/dockerfile.auth \
-t $(AUTH_IMAGE) \
--build-arg BUILD_REF=$(VERSION) \
--build-arg BUILD_DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ") \
.
# ==============================================================================
# Running from within k8s/kind
dev-up:
kind create cluster \
--image $(KIND) \
--name $(KIND_CLUSTER) \
--config zarf/k8s/dev/kind-config.yaml
kubectl wait --timeout=120s --namespace=local-path-storage --for=condition=Available deployment/local-path-provisioner
kind load docker-image $(POSTGRES) --name $(KIND_CLUSTER) & \
kind load docker-image $(GRAFANA) --name $(KIND_CLUSTER) & \
kind load docker-image $(PROMETHEUS) --name $(KIND_CLUSTER) & \
kind load docker-image $(TEMPO) --name $(KIND_CLUSTER) & \
kind load docker-image $(LOKI) --name $(KIND_CLUSTER) & \
kind load docker-image $(PROMTAIL) --name $(KIND_CLUSTER) & \
wait;
dev-down:
kind delete cluster --name $(KIND_CLUSTER)
dev-status-all:
kubectl get nodes -o wide
kubectl get svc -o wide
kubectl get pods -o wide --watch --all-namespaces
dev-status:
watch -n 2 kubectl get pods -o wide --all-namespaces
# ------------------------------------------------------------------------------
dev-load:
kind load docker-image $(SALES_IMAGE) --name $(KIND_CLUSTER) & \
kind load docker-image $(METRICS_IMAGE) --name $(KIND_CLUSTER) & \
kind load docker-image $(AUTH_IMAGE) --name $(KIND_CLUSTER) & \
wait;
dev-apply:
kustomize build zarf/k8s/dev/grafana | kubectl apply -f -
kustomize build zarf/k8s/dev/prometheus | kubectl apply -f -
kustomize build zarf/k8s/dev/tempo | kubectl apply -f -
kustomize build zarf/k8s/dev/loki | kubectl apply -f -
kustomize build zarf/k8s/dev/promtail | kubectl apply -f -
kustomize build zarf/k8s/dev/database | kubectl apply -f -
kubectl rollout status --namespace=$(NAMESPACE) --watch --timeout=120s sts/database
kustomize build zarf/k8s/dev/auth | kubectl apply -f -
kubectl wait pods --namespace=$(NAMESPACE) --selector app=$(AUTH_APP) --timeout=120s --for=condition=Ready
kustomize build zarf/k8s/dev/sales | kubectl apply -f -
kubectl wait pods --namespace=$(NAMESPACE) --selector app=$(SALES_APP) --timeout=120s --for=condition=Ready
dev-restart:
kubectl rollout restart deployment $(AUTH_APP) --namespace=$(NAMESPACE)
kubectl rollout restart deployment $(SALES_APP) --namespace=$(NAMESPACE)
dev-update: build dev-load dev-restart
dev-update-apply: build dev-load dev-apply
dev-logs:
kubectl logs --namespace=$(NAMESPACE) -l app=$(SALES_APP) --all-containers=true -f --tail=100 --max-log-requests=6 | go run api/tooling/logfmt/main.go -service=$(SALES_APP)
dev-logs-auth:
kubectl logs --namespace=$(NAMESPACE) -l app=$(AUTH_APP) --all-containers=true -f --tail=100 | go run api/tooling/logfmt/main.go
# ------------------------------------------------------------------------------
dev-logs-init:
kubectl logs --namespace=$(NAMESPACE) -l app=$(SALES_APP) -f --tail=100 -c init-migrate-seed
dev-describe-node:
kubectl describe node
dev-describe-deployment:
kubectl describe deployment --namespace=$(NAMESPACE) $(SALES_APP)
dev-describe-sales:
kubectl describe pod --namespace=$(NAMESPACE) -l app=$(SALES_APP)
dev-describe-auth:
kubectl describe pod --namespace=$(NAMESPACE) -l app=$(AUTH_APP)
dev-describe-database:
kubectl describe pod --namespace=$(NAMESPACE) -l app=database
dev-describe-grafana:
kubectl describe pod --namespace=$(NAMESPACE) -l app=grafana
# ------------------------------------------------------------------------------
dev-logs-db:
kubectl logs --namespace=$(NAMESPACE) -l app=database --all-containers=true -f --tail=100
dev-logs-grafana:
kubectl logs --namespace=$(NAMESPACE) -l app=grafana --all-containers=true -f --tail=100
dev-logs-tempo:
kubectl logs --namespace=$(NAMESPACE) -l app=tempo --all-containers=true -f --tail=100
dev-logs-loki:
kubectl logs --namespace=$(NAMESPACE) -l app=loki --all-containers=true -f --tail=100
dev-logs-promtail:
kubectl logs --namespace=$(NAMESPACE) -l app=promtail --all-containers=true -f --tail=100
# ------------------------------------------------------------------------------
dev-services-delete:
kustomize build zarf/k8s/dev/sales | kubectl delete -f -
kustomize build zarf/k8s/dev/grafana | kubectl delete -f -
kustomize build zarf/k8s/dev/tempo | kubectl delete -f -
kustomize build zarf/k8s/dev/loki | kubectl delete -f -
kustomize build zarf/k8s/dev/promtail | kubectl delete -f -
kustomize build zarf/k8s/dev/database | kubectl delete -f -
dev-describe-replicaset:
kubectl get rs
kubectl describe rs --namespace=$(NAMESPACE) -l app=$(SALES_APP)
dev-events:
kubectl get ev --sort-by metadata.creationTimestamp
dev-events-warn:
kubectl get ev --field-selector type=Warning --sort-by metadata.creationTimestamp
dev-shell:
kubectl exec --namespace=$(NAMESPACE) -it $(shell kubectl get pods --namespace=$(NAMESPACE) | grep sales | cut -c1-26) --container sales-api -- /bin/sh
dev-database-restart:
kubectl rollout restart statefulset database --namespace=$(NAMESPACE)
# ==============================================================================
# Docker Compose
compose-up:
cd ./zarf/compose/ && docker compose -f docker_compose.yaml -p compose up -d
compose-build-up: build compose-up
compose-down:
cd ./zarf/compose/ && docker compose -f docker_compose.yaml down
compose-logs:
cd ./zarf/compose/ && docker compose -f docker_compose.yaml logs
# ==============================================================================
# Administration
migrate:
export SALES_DB_HOST=localhost; go run api/tooling/admin/main.go migrate
seed: migrate
export SALES_DB_HOST=localhost; go run api/tooling/admin/main.go seed
pgcli:
pgcli postgresql://postgres:postgres@localhost
liveness:
curl -il http://localhost:3000/v1/liveness
readiness:
curl -il http://localhost:3000/v1/readiness
token-gen:
export SALES_DB_HOST=localhost; go run api/tooling/admin/main.go gentoken 5cf37266-3473-4006-984f-9325122678b7 54bb2165-71e1-41a6-af3e-7da4a0e1e2c1
# ==============================================================================
# Metrics and Tracing
metrics-view-sc:
expvarmon -ports="localhost:3010" -vars="build,requests,goroutines,errors,panics,mem:memstats.HeapAlloc,mem:memstats.HeapSys,mem:memstats.Sys"
metrics-view:
expvarmon -ports="localhost:4020" -endpoint="/metrics" -vars="build,requests,goroutines,errors,panics,mem:memstats.HeapAlloc,mem:memstats.HeapSys,mem:memstats.Sys"
grafana:
open http://localhost:3100/
statsviz:
open http://localhost:3010/debug/statsviz
# ==============================================================================
# Running tests within the local computer
test-down:
docker stop servicetest
docker rm servicetest -v
test-r:
CGO_ENABLED=1 go test -race -count=1 ./...
test-only:
CGO_ENABLED=0 go test -count=1 ./...
lint:
CGO_ENABLED=0 go vet ./...
staticcheck -checks=all ./...
vuln-check:
govulncheck ./...
test: test-only lint vuln-check
test-race: test-r lint vuln-check
# ==============================================================================
# Hitting endpoints
token:
curl -il \
--user "admin@example.com:gophers" http://localhost:6000/v1/auth/token/54bb2165-71e1-41a6-af3e-7da4a0e1e2c1
# export TOKEN="COPY TOKEN STRING FROM LAST CALL"
users:
curl -il \
-H "Authorization: Bearer ${TOKEN}" "http://localhost:3000/v1/users?page=1&rows=2"
users-timeout:
curl -il \
--max-time 1 \
-H "Authorization: Bearer ${TOKEN}" "http://localhost:3000/v1/users?page=1&rows=2"
load:
hey -m GET -c 100 -n 1000 \
-H "Authorization: Bearer ${TOKEN}" "http://localhost:3000/v1/users?page=1&rows=2"
otel-test:
curl -il \
-H "Traceparent: 00-918dd5ecf264712262b68cf2ef8b5239-896d90f23f69f006-01" \
--user "admin@example.com:gophers" http://localhost:3000/v1/users/token/54bb2165-71e1-41a6-af3e-7da4a0e1e2c1
# ==============================================================================
# Modules support
deps-reset:
git checkout -- go.mod
go mod tidy
go mod vendor
tidy:
go mod tidy
go mod vendor
deps-list:
go list -m -u -mod=readonly all
deps-upgrade:
go get -u -v ./...
go mod tidy
go mod vendor
deps-cleancache:
go clean -modcache
list:
go list -mod=mod all
# ==============================================================================
# Class Stuff
run:
go run api/services/sales/main.go | go run api/tooling/logfmt/main.go
run-help:
go run api/services/sales/main.go --help | go run api/tooling/logfmt/main.go
curl:
curl -il http://localhost:3000/v1/hack
curl-auth:
curl -il -H "Authorization: Bearer ${TOKEN}" http://localhost:3000/v1/hackauth
load-hack:
hey -m GET -c 100 -n 100000 "http://localhost:3000/v1/hack"
admin:
go run api/tooling/admin/main.go
ready:
curl -il http://localhost:3000/v1/readiness
live:
curl -il http://localhost:3000/v1/liveness
curl-create:
curl -il -X POST \
-H "Authorization: Bearer ${TOKEN}" \
-H 'Content-Type: application/json' \
-d '{"name":"bill","email":"b@gmail.com","roles":["ADMIN"],"department":"ITO","password":"123","passwordConfirm":"123"}' \
http://localhost:3000/v1/users
# ==============================================================================
# Talk commands
talk-up:
kind create cluster \
--image $(KIND) \
--name $(KIND_CLUSTER) \
--config zarf/k8s/dev/kind-config.yaml
kubectl wait --timeout=120s --namespace=local-path-storage --for=condition=Available deployment/local-path-provisioner
kind load docker-image $(POSTGRES) --name $(KIND_CLUSTER)
talk-apply:
kustomize build zarf/k8s/dev/database | kubectl apply -f -
kubectl rollout status --namespace=$(NAMESPACE) --watch --timeout=120s sts/database
kustomize build zarf/k8s/dev/sales | kubectl apply -f -
kubectl wait pods --namespace=$(NAMESPACE) --selector app=$(SALES_APP) --timeout=120s --for=condition=Ready
talk-build: all dev-load talk-apply
talk-load:
hey -m GET -c 10 -n 1000 -H "Authorization: Bearer ${TOKEN}" "http://localhost:3000/v1/users?page=1&rows=2"
talk-logs:
kubectl logs --namespace=$(NAMESPACE) -l app=$(SALES_APP) --all-containers=true -f --tail=100 --max-log-requests=6
talk-logs-cpu:
kubectl logs --namespace=$(NAMESPACE) -l app=$(SALES_APP) --all-containers=true -f --tail=100 --max-log-requests=6 | grep SCHED
talk-logs-mem:
kubectl logs --namespace=$(NAMESPACE) -l app=$(SALES_APP) --all-containers=true -f --tail=100 --max-log-requests=6 | grep "ms clock"
talk-describe:
kubectl describe pod --namespace=$(NAMESPACE) -l app=$(SALES_APP)
talk-metrics:
expvarmon -ports="localhost:4000" -vars="build,requests,goroutines,errors,panics,mem:memstats.HeapAlloc,mem:memstats.HeapSys,mem:memstats.Sys"
# ==============================================================================
# Admin Frontend
ADMIN_FRONTEND_PREFIX := ./api/frontends/admin
write-token-to-env:
echo "VITE_SERVICE_API=http://localhost:3000/v1" > ${ADMIN_FRONTEND_PREFIX}/.env
make token | grep -o '"ey.*"' | awk '{print "VITE_SERVICE_TOKEN="$$1}' >> ${ADMIN_FRONTEND_PREFIX}/.env
admin-gui-install:
pnpm -C ${ADMIN_FRONTEND_PREFIX} install
admin-gui-dev: admin-gui-install
pnpm -C ${ADMIN_FRONTEND_PREFIX} run dev
admin-gui-build: admin-gui-install
pnpm -C ${ADMIN_FRONTEND_PREFIX} run build
admin-gui-start-build: admin-gui-build
pnpm -C ${ADMIN_FRONTEND_PREFIX} run preview
admin-gui-run: write-token-to-env admin-gui-start-build
# ==============================================================================
# Help command
help:
@echo "Usage: make <command>"
@echo ""
@echo "Commands:"
@echo " dev-gotooling Install Go tooling"
@echo " dev-brew Install brew dependencies"
@echo " dev-docker Pull Docker images"
@echo " build Build all the containers"
@echo " sales Build the sales container"
@echo " metrics Build the metrics container"
@echo " auth Build the auth container"
@echo " dev-up Start the KIND cluster"
@echo " dev-down Stop the KIND cluster"
@echo " dev-status-all Show the status of the KIND cluster"
@echo " dev-status Show the status of the pods"
@echo " dev-load Load the containers into KIND"
@echo " dev-apply Apply the manifests to KIND"
@echo " dev-restart Restart the deployments"
@echo " dev-update Build, load, and restart the deployments"
@echo " dev-update-apply Build, load, and apply the deployments"
@echo " dev-logs Show the logs for the sales service"
@echo " dev-logs-auth Show the logs for the auth service"
@echo " dev-logs-init Show the logs for the init container"
@echo " dev-describe-node Show the node details"
@echo " dev-describe-deployment Show the deployment details"
@echo " dev-describe-sales Show the sales pod details"
@echo " dev-describe-auth Show the auth pod details"
@echo " dev-describe-database Show the database pod details"
@echo " dev-describe-grafana Show the grafana pod details"
@echo " dev-logs-db Show the logs for the database service"
@echo " dev-logs-grafana Show the logs for the grafana service"
@echo " dev-logs-tempo Show the logs for the tempo service"
@echo " dev-logs-loki Show the logs for the loki service"
@echo " dev-logs-promtail Show the logs for the promtail service"
@echo " dev-services-delete Delete all"