forked from ardanlabs/service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakefile
318 lines (246 loc) · 9.97 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
SHELL := /bin/bash
# ==============================================================================
# Testing running system
# For testing a simple query on the system. Don't forget to `make seed` first.
# curl --user "[email protected]:gophers" http://localhost:3000/v1/users/token
# export TOKEN="COPY TOKEN STRING FROM LAST CALL"
# curl -H "Authorization: Bearer ${TOKEN}" http://localhost:3000/v1/users/1/2
#
# For testing load on the service.
# go install github.com/rakyll/hey@latest
# hey -m GET -c 100 -n 10000 -H "Authorization: Bearer ${TOKEN}" http://localhost:3000/v1/users/1/2
#
# Access metrics directly (4000) or through the sidecar (3001)
# go install github.com/divan/expvarmon@latest
# expvarmon -ports=":4000" -vars="build,requests,goroutines,errors,panics,mem:memstats.Alloc"
# expvarmon -ports=":3001" -endpoint="/metrics" -vars="build,requests,goroutines,errors,panics,mem:memstats.Alloc"
#
# 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
#
# Test debug endpoints.
# curl http://localhost:4000/debug/liveness
# curl http://localhost:4000/debug/readiness
#
# Running pgcli client for database.
# brew install pgcli
# pgcli postgresql://postgres:postgres@localhost
#
# Launch zipkin.
# http://localhost:9411/zipkin/
# ==============================================================================
# Install dependencies
dev.setup.mac:
brew update
brew list kind || brew install kind
brew list kubectl || brew install kubectl
brew list kustomize || brew install kustomize
# ==============================================================================
# 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-amd64:$(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-amd64:$(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
# Upgrade to latest Kind (>=v0.11): e.g. brew upgrade kind
# For full Kind v0.11 release notes: https://github.com/kubernetes-sigs/kind/releases/tag/v0.11.0
# Kind release used for our project: https://github.com/kubernetes-sigs/kind/releases/tag/v0.11.1
# The image used below was copied by the above link and supports both amd64 and arm64.
kind-up:
kind create cluster \
--image kindest/node:v1.23.0@sha256:49824ab1727c04e56a21a5d8372a402fcd32ea51ac96a2706a12af38934f81ac \
--name $(KIND_CLUSTER) \
--config zarf/k8s/kind/kind-config.yaml
kubectl config set-context --current --namespace=sales-system
kind-down:
kind delete cluster --name $(KIND_CLUSTER)
kind-load:
cd zarf/k8s/kind/sales-pod; kustomize edit set image sales-api-image=sales-api-amd64:$(VERSION)
cd zarf/k8s/kind/sales-pod; kustomize edit set image metrics-image=metrics-amd64:$(VERSION)
kind load docker-image sales-api-amd64:$(VERSION) --name $(KIND_CLUSTER)
kind load docker-image metrics-amd64:$(VERSION) --name $(KIND_CLUSTER)
kind-apply:
kustomize build zarf/k8s/kind/database-pod | kubectl apply -f -
kubectl wait --namespace=database-system --timeout=120s --for=condition=Available deployment/database-pod
kustomize build zarf/k8s/kind/zipkin-pod | kubectl apply -f -
kubectl wait --namespace=zipkin-system --timeout=120s --for=condition=Available deployment/zipkin-pod
kustomize build zarf/k8s/kind/sales-pod | kubectl apply -f -
kind-services-delete:
kustomize build zarf/k8s/kind/sales-pod | kubectl delete -f -
kustomize build zarf/k8s/kind/zipkin-pod | kubectl delete -f -
kustomize build zarf/k8s/kind/database-pod | kubectl delete -f -
kind-restart:
kubectl rollout restart deployment sales-pod
kind-update: all kind-load kind-restart
kind-update-apply: all kind-load kind-apply
kind-logs:
kubectl logs -l app=sales --all-containers=true -f --tail=100 | go run app/tooling/logfmt/main.go
kind-logs-sales:
kubectl logs -l app=sales --all-containers=true -f --tail=100 | go run app/tooling/logfmt/main.go -service=SALES-API
kind-logs-metrics:
kubectl logs -l app=sales --all-containers=true -f --tail=100 | go run app/tooling/logfmt/main.go -service=METRICS
kind-logs-db:
kubectl logs -l app=database --namespace=database-system --all-containers=true -f --tail=100
kind-logs-zipkin:
kubectl logs -l app=zipkin --namespace=zipkin-system --all-containers=true -f --tail=100
kind-status:
kubectl get nodes -o wide
kubectl get svc -o wide
kubectl get pods -o wide --watch --all-namespaces
kind-status-sales:
kubectl get pods -o wide --watch --namespace=sales-system
kind-status-db:
kubectl get pods -o wide --watch --namespace=database-system
kind-status-zipkin:
kubectl get pods -o wide --watch --namespace=zipkin-system
kind-describe:
kubectl describe nodes
kubectl describe svc
kubectl describe pod -l app=sales
kind-describe-deployment:
kubectl describe deployment sales-pod
kind-describe-replicaset:
kubectl get rs
kubectl describe rs -l app=sales
kind-events:
kubectl get ev --sort-by metadata.creationTimestamp
kind-events-warn:
kubectl get ev --field-selector type=Warning --sort-by metadata.creationTimestamp
kind-context-sales:
kubectl config set-context --current --namespace=sales-system
kind-shell:
kubectl exec -it $(shell kubectl get pods | grep sales | cut -c1-26) --container sales-api -- /bin/sh
kind-database:
# ./admin --db-disable-tls=1 migrate
# ./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
# ==============================================================================
# Running tests within the local computer
test:
go test ./... -count=1
staticcheck -checks=all ./...
# ==============================================================================
# Modules support
deps-reset:
git checkout -- go.mod
go mod tidy
go mod vendor
tidy:
go mod tidy
go mod vendor
deps-upgrade:
# go get $(go list -f '{{if not (or .Main .Indirect)}}{{.Path}}{{end}}' -m all)
go get -u -v ./...
go mod tidy
go mod vendor
deps-cleancache:
go clean -modcache
list:
go list -mod=mod all
# ==============================================================================
# Docker support
docker-down:
docker rm -f $(shell docker ps -aq)
docker-clean:
docker system prune -f
docker-kind-logs:
docker logs -f $(KIND_CLUSTER)-control-plane
# ==============================================================================
# GCP
export PROJECT = ardan-starter-kit
CLUSTER := ardan-starter-cluster
DATABASE := ardan-starter-db
ZONE := us-central1-b
gcp-config:
@echo Setting environment for $(PROJECT)
gcloud config set project $(PROJECT)
gcloud config set compute/zone $(ZONE)
gcloud auth configure-docker
gcp-project:
gcloud projects create $(PROJECT)
gcloud beta billing projects link $(PROJECT) --billing-account=$(ACCOUNT_ID)
gcloud services enable container.googleapis.com
gcp-cluster:
gcloud container clusters create $(CLUSTER) --enable-ip-alias --num-nodes=2 --machine-type=n1-standard-2
gcloud compute instances list
gcp-upload:
docker tag sales-api-amd64:1.0 gcr.io/$(PROJECT)/sales-api-amd64:$(VERSION)
docker tag metrics-amd64:1.0 gcr.io/$(PROJECT)/metrics-amd64:$(VERSION)
docker push gcr.io/$(PROJECT)/sales-api-amd64:$(VERSION)
docker push gcr.io/$(PROJECT)/metrics-amd64:$(VERSION)
gcp-database:
# Create User/Password
gcloud beta sql instances create $(DATABASE) --database-version=POSTGRES_9_6 --no-backup --tier=db-f1-micro --zone=$(ZONE) --no-assign-ip --network=default
gcloud sql instances describe $(DATABASE)
gcp-db-assign-ip:
gcloud sql instances patch $(DATABASE) --authorized-networks=[$(PUBLIC-IP)/32]
gcloud sql instances describe $(DATABASE)
gcp-db-private-ip:
# IMPORTANT: Make sure you run this command and get the private IP of the DB.
gcloud sql instances describe $(DATABASE)
gcp-services:
kustomize build zarf/k8s/gcp/sales-pod | kubectl apply -f -
gcp-status:
gcloud container clusters list
kubectl get nodes -o wide
kubectl get svc -o wide
kubectl get pods -o wide --watch
gcp-status-full:
kubectl describe nodes
kubectl describe svc
kubectl describe pod -l app=sales
gcp-events:
kubectl get ev --sort-by metadata.creationTimestamp
gcp-events-warn:
kubectl get ev --field-selector type=Warning --sort-by metadata.creationTimestamp
gcp-logs:
kubectl logs -l app=sales --all-containers=true -f --tail=100 | go run app/tooling/logfmt/main.go
gcp-logs-sales:
kubectl logs -l app=sales --all-containers=true -f --tail=100 | go run app/tooling/logfmt/main.go -service=SALES-API | jq
gcp-shell:
kubectl exec -it $(shell kubectl get pods | grep sales | cut -c1-26 | head -1) --container app -- /bin/sh
# ./admin --db-disable-tls=1 migrate
# ./admin --db-disable-tls=1 seed
gcp-delete-all: gcp-delete
kustomize build zarf/k8s/gcp/sales-pod | kubectl delete -f -
gcloud container clusters delete $(CLUSTER)
gcloud projects delete sales-api
gcloud container images delete gcr.io/$(PROJECT)/sales-api-amd64:$(VERSION) --force-delete-tags
gcloud container images delete gcr.io/$(PROJECT)/metrics-amd64:$(VERSION) --force-delete-tags
docker image remove gcr.io/sales-api/sales-api-amd64:$(VERSION)
docker image remove gcr.io/sales-api/metrics-amd64:$(VERSION)
#===============================================================================
# GKE Installation
#
# Install the Google Cloud SDK. This contains the gcloud client needed to perform
# some operations
# https://cloud.google.com/sdk/
#
# Installing the K8s kubectl client.
# https://kubernetes.io/docs/tasks/tools/install-kubectl/