forked from ardanlabs/service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makefile
524 lines (409 loc) · 17.7 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
# 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
# ==============================================================================
# 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, Max 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
# ==============================================================================
# Windows Users ONLY - Install Telepresence
#
# Unfortunately you can't use brew to install telepresence because you will
# receive a bad binary. Please follow these instruction.
#
# $ sudo curl -fL https://app.getambassador.io/download/tel2/linux/amd64/latest/telepresence -o /usr/local/bin/telepresence
# $ sudo chmod a+x /usr/local/bin/telepresence
#
# Restart your wsl environment.
# ==============================================================================
# Linux Users ONLY - Install Telepresence
#
# https://www.telepresence.io/docs/latest/quick-start/?os=gnu-linux
# ==============================================================================
# M1 Mac Users ONLY - Uninstall Telepresence If Installed Intel Version
#
# $ sudo rm -rf /Library/Developer/CommandLineTools
# $ sudo xcode-select --install
# Then it installed with brew (arm64)
# ==============================================================================
# Install Tooling and Dependencies
#
# If you are running a mac machine with brew, run these commands:
# $ make dev-brew or make dev-brew-arm64
# $ make dev-docker
# $ make dev-gotooling
#
# If you are running a linux machine with brew, run these commands:
# $ make dev-brew-common
# $ make dev-docker
# $ make dev-gotooling
# Follow instructions above for Telepresence.
#
# If you are a windows user with brew, run these commands:
# $ make dev-brew-common
# $ make dev-docker
# $ make dev-gotooling
# Follow instructions above for Telepresence.
# ==============================================================================
# Running Test
#
# Running the tests is a good way to verify you have installed most of the
# dependencies properly.
#
# $ make test
#
# To run the tests, linter, vet, and the vuln check.
#
# # make test-all
# ==============================================================================
# Starting The Project
#
# If you want to use telepresence (recommended):
# $ make dev-up
# $ make dev-update-apply
#
# If telepresence is not working for you:
# $ make dev-up-local
# $ make dev-update-apply
#
# Note: If you attempted to run with telepresence and it didn't work, you may
# want to restart the cluser.
# $ make dev-down-local
#
# Note: When running without telepresence, if you see a command where there is
# a `-local` option, you will need to use that command.
# ==============================================================================
# CLASS NOTES
#
# Kind
# For full Kind v0.20 release notes: https://github.com/kubernetes-sigs/kind/releases/tag/v0.20.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
# $ ./sales-admin genkey
#
# Testing Coverage
# $ go test -coverprofile p.out
# $ go tool cover -html p.out
#
# Hashicorp Vault
# READ THIS: https://developer.hashicorp.com/vault/docs/concepts/tokens
# $ export VAULT_TOKEN=mytoken
# $ export VAULT_ADDR='http://vault-service.sales-system.svc.cluster.local:8200'
# $ vault secrets list
# $ vault kv get secret/sales
# $ vault kv put secret/sales key="some data"
# $ kubectl logs --namespace=sales-system -l app=sales -c init-vault-server
# $ curl -H "X-Vault-Token: mytoken" -X GET http://vault-service.sales-system.svc.cluster.local:8200/v1/secret/data/54bb2165-71e1-41a6-af3e-7da4a0e1e2c1
# $ curl -H "X-Vault-Token: mytoken" -H "Content-Type: application/json" -X POST -d '{"data":{"pk":"PEM"}}' http://vault-service.sales-system.svc.cluster.local:8200/v1/secret/data/54bb2165-71e1-41a6-af3e-7da4a0e1e2c1
#
# 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/[email protected]
#
# OPA Playground
# https://play.openpolicyagent.org/
# https://academy.styra.com/
# https://www.openpolicyagent.org/docs/latest/policy-reference/
# ==============================================================================
# Define dependencies
GOLANG := golang:1.21
ALPINE := alpine:3.18
KIND := kindest/node:v1.27.3
POSTGRES := postgres:15.4
VAULT := hashicorp/vault:1.14
GRAFANA := grafana/grafana:9.5.3
PROMETHEUS := prom/prometheus:v2.45.0
TEMPO := grafana/tempo:2.2.0
LOKI := grafana/loki:2.8.3
PROMTAIL := grafana/promtail:2.8.3
TELEPRESENCE := datawire/ambassador-telepresence-manager:2.14.2
KIND_CLUSTER := ardan-starter-cluster
NAMESPACE := sales-system
APP := sales
BASE_IMAGE_NAME := ardanlabs/service
SERVICE_NAME := sales-api
VERSION := 0.0.1
SERVICE_IMAGE := $(BASE_IMAGE_NAME)/$(SERVICE_NAME):$(VERSION)
METRICS_IMAGE := $(BASE_IMAGE_NAME)/$(SERVICE_NAME)-metrics:$(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-common:
brew update
brew tap hashicorp/tap
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 vault || brew install vault
dev-brew: dev-brew-common
brew list datawire/blackbird/telepresence || brew install datawire/blackbird/telepresence
dev-brew-arm64: dev-brew-common
brew list datawire/blackbird/telepresence-arm64 || brew install datawire/blackbird/telepresence-arm64
dev-docker:
docker pull $(GOLANG)
docker pull $(ALPINE)
docker pull $(KIND)
docker pull $(POSTGRES)
docker pull $(VAULT)
docker pull $(GRAFANA)
docker pull $(PROMETHEUS)
docker pull $(TEMPO)
docker pull $(LOKI)
docker pull $(PROMTAIL)
docker pull $(TELEPRESENCE)
# ==============================================================================
# Building containers
all: service metrics
service:
docker build \
-f zarf/docker/dockerfile.service \
-t $(SERVICE_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"` \
.
# ==============================================================================
# Running from within k8s/kind
dev-up-local:
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 $(TELEPRESENCE) --name $(KIND_CLUSTER)
kind load docker-image $(POSTGRES) --name $(KIND_CLUSTER)
kind load docker-image $(VAULT) --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)
dev-up: dev-up-local
telepresence --context=kind-$(KIND_CLUSTER) helm install --request-timeout 2m
telepresence --context=kind-$(KIND_CLUSTER) connect
dev-down-local:
kind delete cluster --name $(KIND_CLUSTER)
dev-down:
telepresence quit -s
kind delete cluster --name $(KIND_CLUSTER)
# ------------------------------------------------------------------------------
dev-load:
cd zarf/k8s/dev/sales; kustomize edit set image service-image=$(SERVICE_IMAGE)
kind load docker-image $(SERVICE_IMAGE) --name $(KIND_CLUSTER)
cd zarf/k8s/dev/sales; kustomize edit set image metrics-image=$(METRICS_IMAGE)
kind load docker-image $(METRICS_IMAGE) --name $(KIND_CLUSTER)
dev-apply:
kustomize build zarf/k8s/dev/vault | 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/grafana | kubectl apply -f -
kubectl wait pods --namespace=$(NAMESPACE) --selector app=grafana --timeout=120s --for=condition=Ready
kustomize build zarf/k8s/dev/prometheus | kubectl apply -f -
kubectl wait pods --namespace=$(NAMESPACE) --selector app=prometheus --timeout=120s --for=condition=Ready
kustomize build zarf/k8s/dev/tempo | kubectl apply -f -
kubectl wait pods --namespace=$(NAMESPACE) --selector app=tempo --timeout=120s --for=condition=Ready
kustomize build zarf/k8s/dev/loki | kubectl apply -f -
kubectl wait pods --namespace=$(NAMESPACE) --selector app=loki --timeout=120s --for=condition=Ready
kustomize build zarf/k8s/dev/promtail | kubectl apply -f -
kubectl wait pods --namespace=$(NAMESPACE) --selector app=promtail --timeout=120s --for=condition=Ready
kustomize build zarf/k8s/dev/sales | kubectl apply -f -
kubectl wait pods --namespace=$(NAMESPACE) --selector app=$(APP) --timeout=120s --for=condition=Ready
dev-restart:
kubectl rollout restart deployment $(APP) --namespace=$(NAMESPACE)
dev-update: all dev-load dev-restart
dev-update-apply: all dev-load dev-apply
# ------------------------------------------------------------------------------
dev-logs:
kubectl logs --namespace=$(NAMESPACE) -l app=$(APP) --all-containers=true -f --tail=100 --max-log-requests=6 | go run app/tooling/logfmt/main.go -service=$(SERVICE_NAME)
dev-logs-init:
kubectl logs --namespace=$(NAMESPACE) -l app=$(APP) -f --tail=100 -c init-vault-system
kubectl logs --namespace=$(NAMESPACE) -l app=$(APP) -f --tail=100 -c init-vault-loadkeys
kubectl logs --namespace=$(NAMESPACE) -l app=$(APP) -f --tail=100 -c init-migrate
kubectl logs --namespace=$(NAMESPACE) -l app=$(APP) -f --tail=100 -c init-seed
dev-status:
kubectl get nodes -o wide
kubectl get svc -o wide
kubectl get pods -o wide --watch --all-namespaces
dev-describe:
kubectl describe nodes
kubectl describe svc
dev-describe-deployment:
kubectl describe deployment --namespace=$(NAMESPACE) $(APP)
dev-describe-sales:
kubectl describe pod --namespace=$(NAMESPACE) -l app=$(APP)
dev-describe-telepresence:
kubectl describe pod --namespace=ambassador -l app=traffic-manager
# ------------------------------------------------------------------------------
dev-logs-vault:
kubectl logs --namespace=$(NAMESPACE) -l app=vault --all-containers=true -f --tail=100
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=$(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)
# ==============================================================================
# Administration
migrate:
go run app/tooling/sales-admin/main.go migrate
seed: migrate
go run app/tooling/sales-admin/main.go seed
vault:
go run app/tooling/sales-admin/main.go vault
token-gen:
go run app/tooling/sales-admin/main.go gentoken 5cf37266-3473-4006-984f-9325122678b7 54bb2165-71e1-41a6-af3e-7da4a0e1e2c1
pgcli-local:
pgcli postgresql://postgres:postgres@localhost
pgcli:
pgcli postgresql://postgres:postgres@database-service.$(NAMESPACE).svc.cluster.local
liveness-local:
curl -il http://localhost:3000/v1/liveness
liveness:
curl -il http://$(SERVICE_NAME).$(NAMESPACE).svc.cluster.local:3000/v1/liveness
readiness-local:
curl -il http://localhost:3000/v1/readiness
readiness:
curl -il http://$(SERVICE_NAME).$(NAMESPACE).svc.cluster.local:3000/v1/readiness
# ==============================================================================
# Metrics and Tracing
metrics-view-local-sc:
expvarmon -ports="localhost:4000" -vars="build,requests,goroutines,errors,panics,mem:memstats.Alloc"
metrics-view-sc:
expvarmon -ports="$(SERVICE_NAME).$(NAMESPACE).svc.cluster.local:4000" -vars="build,requests,goroutines,errors,panics,mem:memstats.Alloc"
metrics-view-local:
expvarmon -ports="localhost:3001" -endpoint="/metrics" -vars="build,requests,goroutines,errors,panics,mem:memstats.Alloc"
metrics-view:
expvarmon -ports="$(SERVICE_NAME).$(NAMESPACE).svc.cluster.local:3001" -endpoint="/metrics" -vars="build,requests,goroutines,errors,panics,mem:memstats.Alloc"
grafana-local:
open -a "Google Chrome" http://localhost:3100/
grafana:
open -a "Google Chrome" http://grafana-service.$(NAMESPACE).svc.cluster.local:3100/
# ==============================================================================
# Running tests within the local computer
test-race:
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-race lint vuln-check
# make docs ARGS="-out json"
# make docs ARGS="-out html"
docs:
go run app/tooling/docs/main.go $(ARGS)
# ==============================================================================
# Hitting endpoints
token-local:
curl -il --user "[email protected]:gophers" http://localhost:3000/v1/users/token/54bb2165-71e1-41a6-af3e-7da4a0e1e2c1
token:
curl -il --user "[email protected]:gophers" http://$(SERVICE_NAME).$(NAMESPACE).svc.cluster.local:3000/v1/users/token/54bb2165-71e1-41a6-af3e-7da4a0e1e2c1
# export TOKEN="COPY TOKEN STRING FROM LAST CALL"
users-local:
curl -il -H "Authorization: Bearer ${TOKEN}" http://localhost:3000/v1/users?page=1&rows=2
users:
curl -il -H "Authorization: Bearer ${TOKEN}" http://$(SERVICE_NAME).$(NAMESPACE).svc.cluster.local:3000/v1/users?page=1&rows=2
load-local:
hey -m GET -c 100 -n 10000 -H "Authorization: Bearer ${TOKEN}" "http://localhost:3000/v1/users?page=1&rows=2"
load:
hey -m GET -c 100 -n 10000 -H "Authorization: Bearer ${TOKEN}" "http://$(SERVICE_NAME).$(NAMESPACE).svc.cluster.local:3000/v1/users?page=1&rows=2"
# ==============================================================================
# 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
# ==============================================================================
# Admin Frontend
ADMIN_FRONTEND_PREFIX := ./app/frontends/admin
write-token-to-env:
echo "NEXT_PUBLIC_BASE_API_URL=http://localhost:3000/v1" > ${ADMIN_FRONTEND_PREFIX}/.env
make token | grep -o '"ey.*"' | awk '{print "NEXT_PUBLIC_TOKEN="$$1}' >> ${ADMIN_FRONTEND_PREFIX}/.env
admin-gui-install:
npm install --prefix ${ADMIN_FRONTEND_PREFIX}
admin-gui-dev: admin-gui-install
npm run dev --prefix ${ADMIN_FRONTEND_PREFIX}
admin-gui-build: admin-gui-install
npm run build --prefix ${ADMIN_FRONTEND_PREFIX}
admin-gui-start-build: admin-gui-build
npm run start --prefix ${ADMIN_FRONTEND_PREFIX}
admin-gui-run: write-token-to-env admin-gui-start-build