-
Notifications
You must be signed in to change notification settings - Fork 0
/
makefile
260 lines (206 loc) · 8.55 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
# 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)
# ==============================================================================
# CLASS NOTES
#
# Kind
# For full Kind v0.18 release notes: https://github.com/kubernetes-sigs/kind/releases/tag/v0.18.0
#
# You can use openSSL to test generate RSA Keys pair
# 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
#
# OPA Playground
# https://play.openpolicyagent.org/
# https://academy.styra.com/
# https://www.openpolicyagent.org/docs/latest/policy-reference/
# ==============================================================================
# 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)"
#
# 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 3 CPUs.
#
# Run these commands to install everything needed.
# $ make dev-brew
# $ make dev-docker
# $ make dev-gotooling
# ==============================================================================
# CLASS NOTES
#
# Kind
# For full Kind v0.20 release notes: https://github.com/kubernetes-sigs/kind/releases/tag/v0.20.0
# ==============================================================================
# Define dependencies
GOLANG := golang:1.21.3
ALPINE := alpine:3.18
KIND := kindest/node:v1.27.3
POSTGRES := postgres:15.4
TELEPRESENCE := datawire/tel2:2.16.1
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)
# 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 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
brew install helm
dev-docker:
docker pull $(GOLANG)
docker pull $(ALPINE)
docker pull $(KIND)
docker pull $(POSTGRES)
docker pull $(TELEPRESENCE)
# ==============================================================================
# Building containers
service:
docker build \
-f zarf/docker/Dockerfile \
-t $(SERVICE_IMAGE) \
--build-arg BUILD_REF=$(VERSION) \
--build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \
.
# Docker remove <none> TAG images:
# https://stackoverflow.com/questions/33913020/docker-remove-none-tag-images
remove-none-images:
docker rmi $(docker images --filter "dangling=true" -q --no-trunc)
# ==============================================================================
# bootstrap the dev cluster
# dev cluster all in one up
#telepresence password is @SHxx20xxxx0x04
dev-up: dev-up-local
telepresence --context=kind-$(KIND_CLUSTER) helm install
telepresence --context=kind-ardan-starter-cluster quit -u
telepresence --context=kind-$(KIND_CLUSTER) connect
# dev cluster all in one down
dev-down:
kind delete cluster --name $(KIND_CLUSTER)
# if you have issue to run telepresence, run it step by step
# follow this link: https://www.telepresence.io/docs/latest/quick-start/
# step 1
dev-up-local:
kind create cluster \
--image $(KIND) \
--name $(KIND_CLUSTER) \
--config zarf/k8s/dev/kind-config.yaml
# what is local-path-storage namespace: https://mauilion.dev/posts/kind-pvc/
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)
# step 2
# if need password is @SHxx20xxxx0x04
dev-load-telepresence:
kind load docker-image $(TELEPRESENCE) --name $(KIND_CLUSTER)
telepresence --context=kind-ardan-starter-cluster helm install
telepresence --context=kind-ardan-starter-cluster quit -u
telepresence --context=kind-ardan-starter-cluster connect
# ==============================================================================
# re=deploy service on cluster
# if you changed the code then run this command to re-build the service
dev-update: service dev-load dev-restart
# if you changed the k8s configuration then run this command to re-apply new settings
dev-update-apply: service dev-load dev-apply
dev-load:
kind load docker-image $(SERVICE_IMAGE) --name $(KIND_CLUSTER)
dev-restart:
kubectl rollout restart deployment $(APP) --namespace=$(NAMESPACE)
dev-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=$(APP) --for=condition=Ready
# ------------------------------------------------------------------------------
# run monitoring commands
# check dev status
dev-status:
kubectl get nodes -o wide
kubectl get svc -o wide
kubectl get pods -o wide --watch --all-namespaces
# check dev logs
dev-logs:
kubectl logs --namespace=$(NAMESPACE) -l app=$(APP) --all-containers=true -f --tail=100 | go run app/tooling/logfmt/main.go -service=$(SERVICE_NAME)
dev-describe-deployment:
kubectl describe deployment --namespace=$(NAMESPACE) $(APP)
dev-describe-sales:
kubectl describe pod --namespace=$(NAMESPACE) -l app=$(APP)
# use for check db migration logs when deployment restart
dev-logs-init:
kubectl logs --namespace=$(NAMESPACE) -l app=$(APP) -f --tail=100 -c init-migrate
# ==============================================================================
# run commands
run-scratch:
go run app/tooling/scratch/main.go
run-local:
go run app/services/sales-api/main.go
run-local-help:
go run app/services/sales-api/main.go --help
tidy:
go mod tidy
go mod vendor
metrics-view:
expvarmon -ports="$(SERVICE_NAME).$(NAMESPACE).svc.cluster.local:4000" -vars="build,requests,goroutines,errors,panics,mem:memstats.Alloc"
metrics-view-local:
expvarmon -ports="localhost:4000" -vars="build,requests,goroutines,errors,panics,mem:memstats.Alloc"
test-endpoint:
# k8s DNS location: https://yuminlee2.medium.com/kubernetes-dns-bdca7b7cb868#:~:text=In%20Kubernetes%2C%20DNS%20names%20are%20assigned%20to%20Pods%20and%20Services,format%20.
curl -il $(SERVICE_NAME).$(NAMESPACE).svc.cluster.local:3000/test
test-endpoint-local:
curl -il localhost:3000/test
# before tun commands below, you need to pump in token
# `make run-scratch`, copy paste token value from the output
# write token value into env variable: `export TOKEN=$token`
# then run commands below
test-endpoint-auth:
curl -il -H "Authorization: Bearer ${TOKEN}" $(SERVICE_NAME).$(NAMESPACE).svc.cluster.local:3000/test/auth
test-endpoint-auth-local:
curl -il -H "Authorization: Bearer ${TOKEN}" localhost:3000/test/auth
liveness-local:
curl -il http://localhost:4000/debug/liveness
liveness:
curl -il http://$(SERVICE_NAME).$(NAMESPACE).svc.cluster.local:4000/debug/liveness
readiness-local:
curl -il http://localhost:4000/debug/readiness
readiness:
curl -il http://$(SERVICE_NAME).$(NAMESPACE).svc.cluster.local:4000/debug/readiness
pgcli-local:
pgcli postgresql://postgres:postgres@localhost
pgcli:
pgcli postgresql://postgres:postgres@database-service.$(NAMESPACE).svc.cluster.local
# test db migration manually
migrate:
go run app/tooling/admin/main.go
# test /order endpoint, use "make query-local | jq" to test
query-local:
@curl -s "http://localhost:3000/users?page=1&rows=2&orderBy=name,ASC"
# test /order endpoint, use "make query | jq" to test
query:
@curl -s "http://$(SERVICE_NAME).$(NAMESPACE).svc.cluster.local:3000/users?page=1&rows=2&orderBy=name,ASC"