-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
294 lines (228 loc) · 9.96 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
SHELL := /bin/bash
# ==============================================================================
# Testing running system
# Deploy First Mentality
#
# Other commands to install.
# go install github.com/divan/expvarmon@latest
# go install github.com/rakyll/hey@latest
#
# For full Kind v0.17 release notes: https://github.com/kubernetes-sigs/kind/releases/tag/v0.17.0
#
# For testing a simple query on the system. Don't forget to `make seed` first.
# curl -il --user "[email protected]:gophers" http://sales-service.sales-system.svc.cluster.local:3000/v1/users/token/54bb2165-71e1-41a6-af3e-7da4a0e1e2c1
# export TOKEN="COPY TOKEN STRING FROM LAST CALL"
# curl -il -H "Authorization: Bearer ${TOKEN}" http://sales-service.sales-system.svc.cluster.local:3000/v1/users/1/2
#
# For testing load on the service.
# hey -m GET -c 100 -n 10000 -H "Authorization: Bearer ${TOKEN}" http://sales-service.sales-system.svc.cluster.local:3000/v1/users/1/2
#
# 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
#
# Vault Information.
# 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
#
# To show what calls are being made underneath to the proxy and checksum db.
# 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/
# ==============================================================================
# Install dependencies
GOLANG := golang:1.19
ALPINE := alpine:3.17
KIND := kindest/node:v1.25.3
POSTGRES := postgres:15-alpine
VAULT := hashicorp/vault:1.12
ZIPKIN := openzipkin/zipkin:2.23
TELEPRESENCE := docker.io/datawire/tel2:2.10.4
dev.setup.mac.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.setup.mac: dev.setup.mac.common
brew datawire/blackbird/telepresence || brew install datawire/blackbird/telepresence
dev.setup.mac.arm64: dev.setup.mac.common
brew 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 $(ZIPKIN)
docker pull $(TELEPRESENCE)
# ==============================================================================
# Building containers
# $(shell git rev-parse --short HEAD)
VERSION := 1.0
all: sales metrics
sales:
docker build \
-f zarf/docker/dockerfile.sales-api \
-t sales-api:$(VERSION) \
--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:$(VERSION) \
--build-arg BUILD_REF=$(VERSION) \
--build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \
.
# ==============================================================================
# Running from within k8s/kind
KIND_CLUSTER := ardan-starter-cluster
dev-up:
kind create cluster \
--image kindest/node:v1.25.3@sha256:f52781bc0d7a19fb6c405c2af83abfeb311f130707a0e219175677e366cc45d1 \
--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 $(ZIPKIN) --name $(KIND_CLUSTER)
telepresence --context=kind-$(KIND_CLUSTER) helm install
telepresence --context=kind-$(KIND_CLUSTER) connect
dev-down:
telepresence quit -s
kind delete cluster --name $(KIND_CLUSTER)
dev-load:
cd zarf/k8s/dev/sales; kustomize edit set image sales-api-image=sales-api:$(VERSION)
kind load docker-image sales-api:$(VERSION) --name $(KIND_CLUSTER)
cd zarf/k8s/dev/sales; kustomize edit set image metrics-image=metrics:$(VERSION)
kind load docker-image metrics:$(VERSION) --name $(KIND_CLUSTER)
dev-apply:
kustomize build zarf/k8s/dev/vault | kubectl apply -f -
kustomize build zarf/k8s/dev/database | kubectl apply -f -
kubectl wait --timeout=120s --namespace=sales-system --for=condition=Available deployment/database
kustomize build zarf/k8s/dev/zipkin | kubectl apply -f -
kubectl wait --timeout=120s --namespace=sales-system --for=condition=Available deployment/zipkin
kustomize build zarf/k8s/dev/sales | kubectl apply -f -
kubectl wait --timeout=120s --namespace=sales-system --for=condition=Available deployment/sales
dev-restart:
kubectl rollout restart deployment sales --namespace=sales-system
dev-update: all dev-load dev-restart
dev-update-apply: all dev-load dev-apply
dev-logs:
kubectl logs --namespace=sales-system -l app=sales --all-containers=true -f --tail=100 --max-log-requests=6 | go run app/tooling/logfmt/main.go -service=SALES-API
dev-logs-init:
kubectl logs --namespace=sales-system -l app=sales -f --tail=100 -c init-vault-server
kubectl logs --namespace=sales-system -l app=sales -f --tail=100 -c init-vault
kubectl logs --namespace=sales-system -l app=sales -f --tail=100 -c init-migrate
kubectl logs --namespace=sales-system -l app=sales -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=sales-system sales
dev-describe-sales:
kubectl describe pod --namespace=sales-system -l app=sales
# *** OTHER ****************************************************************
dev-logs-vault:
kubectl logs --namespace=sales-system -l app=vault --all-containers=true -f --tail=100
dev-logs-db:
kubectl logs --namespace=sales-system -l app=database --all-containers=true -f --tail=100
dev-logs-zipkin:
kubectl logs --namespace=sales-system -l app=zipkin --all-containers=true -f --tail=100
# *** EXTRAS *******************************************************************
dev-services-delete:
kustomize build zarf/k8s/dev/sales | kubectl delete -f -
kustomize build zarf/k8s/dev/zipkin | kubectl delete -f -
kustomize build zarf/k8s/dev/database | kubectl delete -f -
dev-describe-replicaset:
kubectl get rs
kubectl describe rs --namespace=sales-system -l app=sales
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=sales-system -it $(shell kubectl get pods --namespace=sales-system | grep sales | cut -c1-26) --container sales-api -- /bin/sh
dev-database:
# ./sales-admin --db-disable-tls=1 migrate
# ./sales-admin --db-disable-tls=1 seed
# ==============================================================================
# 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:
go run app/tooling/sales-admin/main.go gentoken 5cf37266-3473-4006-984f-9325122678b7 54bb2165-71e1-41a6-af3e-7da4a0e1e2c1
pgcli:
pgcli postgresql://postgres:[email protected]
liveness:
curl -il http://sales-service.sales-system.svc.cluster.local:4000/debug/liveness
readiness:
curl -il http://sales-service.sales-system.svc.cluster.local:4000/debug/readiness
# ==============================================================================
# Metrics and Tracing
metrics-view:
expvarmon -ports="sales-service.sales-system.svc.cluster.local:4000" -vars="build,requests,goroutines,errors,panics,mem:memstats.Alloc"
metrics-view-sidecar:
expvarmon -ports="sales-service.sales-system.svc.cluster.local:3001" -endpoint="/metrics" -vars="build,requests,goroutines,errors,panics,mem:memstats.Alloc"
zipkin:
open -a "Google Chrome" http://zipkin-service.sales-system.svc.cluster.local:9411/zipkin/
# ==============================================================================
# Running tests within the local computer
# go install honnef.co/go/tools/cmd/staticcheck@latest
# go install golang.org/x/vuln/cmd/govulncheck@latest
test:
CGO_ENABLED=0 go test -count=1 ./...
CGO_ENABLED=0 go vet ./...
staticcheck -checks=all ./...
govulncheck ./...
# ==============================================================================
# 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