-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
233 lines (185 loc) · 7.43 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
### -----------------------
# --- Building
### -----------------------
# first is default target when running "make" without args
.PHONY: build
build: ##- Default 'make' target: go-format, go-build and lint.
@$(MAKE) go-format
@$(MAKE) helm
@$(MAKE) go-build
@$(MAKE) lint
@$(MAKE) reference-build
# useful to ensure that everything gets resetuped from scratch
.PHONY: all
all: init ##- Runs all of our common make targets: clean, init, build and test.
@$(MAKE) build
@$(MAKE) test
.PHONY: watch
watch: ##- Watches for changes and runs 'make build' on modifications.
@echo Watching. Use Ctrl-c to exit.
watchexec -r -w . --exts go,yaml -i *.tmp.yaml -i *.rendered.yaml $(MAKE) build
.PHONY: info
info: ##- Prints info about go.mod updates and current go version.
@$(MAKE) get-go-outdated-modules
@go version
.PHONY: lint
lint: ##- (opt) Runs golangci-lint.
golangci-lint run --timeout 5m
.PHONY: go-format
go-format: ##- (opt) Runs go format.
go fmt ./...
.PHONY: helm
helm:
helm template ./charts/backup-ns -n customer-namespace -f deploy/backup-ns/customer-namespace-backup-ns.values.yaml > ./deploy/backup-ns/customer-namespace-backup-ns.rendered.yaml
.PHONY: go-build
go-build: ##- (opt) Runs go build.
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o bin/app-linux-arm64
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bin/app-linux-amd64
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -o bin/app-darwin-arm64
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o bin/app-darwin-amd64
CGO_ENABLED=0 go build -o bin/app
# https://github.com/gotestyourself/gotestsum#format
# w/o cache https://github.com/golang/go/issues/24573 - see "go help testflag"
# note that these tests should not run verbose by default (e.g. use your IDE for this)
# TODO: add test shuffling/seeding when landed in go v1.15 (https://github.com/golang/go/issues/28592)
# tests by pkgname
.PHONY: test
test: ##- Run tests, output by package, print coverage.
@$(MAKE) go-test-by-pkg
@$(MAKE) go-test-print-coverage
# tests by testname
.PHONY: test-by-name
test-by-name: ##- Run tests, output by testname, print coverage.
@$(MAKE) go-test-by-name
@$(MAKE) go-test-print-coverage
# note that we explicitly don't want to use a -coverpkg=./... option, per pkg coverage take precedence
.PHONY: go-test-by-pkg
go-test-by-pkg: ##- (opt) Run tests, output by package.
gotestsum --format pkgname-and-test-fails --jsonfile /tmp/test.log -- -race -cover -count=1 -coverprofile=/tmp/coverage.out ./...
.PHONY: go-test-by-name
go-test-by-name: ##- (opt) Run tests, output by testname.
gotestsum --format testname --jsonfile /tmp/test.log -- -race -cover -count=1 -coverprofile=/tmp/coverage.out ./...
.PHONY: go-test-print-coverage
go-test-print-coverage: ##- (opt) Print overall test coverage (must be done after running tests).
@printf "coverage "
@go tool cover -func=/tmp/coverage.out | tail -n 1 | awk '{$$1=$$1;print}'
.PHONY: go-test-print-slowest
go-test-print-slowest: ##- Print slowest running tests (must be done after running tests).
gotestsum tool slowest --jsonfile /tmp/test.log --threshold 2s
# TODO: switch to "-m direct" after go 1.17 hits: https://github.com/golang/go/issues/40364
.PHONY: get-go-outdated-modules
get-go-outdated-modules: ##- (opt) Prints outdated (direct) go modules (from go.mod).
@((go list -u -m -f '{{if and .Update (not .Indirect)}}{{.}}{{end}}' all) 2>/dev/null | grep " ") || echo "go modules are up-to-date."
.PHONY: watch-tests
watch-tests: ##- Watches *.go files and runs package tests on modifications.
gotestsum --format testname --watch -- -race -count=1
### -----------------------
# --- Initializing
### -----------------------
.PHONY: init
init: ##- Runs make modules, tools and tidy.
@$(MAKE) modules
@$(MAKE) tools
@$(MAKE) tidy
# cache go modules (locally into .pkg)
.PHONY: modules
modules: ##- (opt) Cache packages as specified in go.mod.
go mod download
# https://marcofranssen.nl/manage-go-tools-via-go-modules/
.PHONY: tools
tools: ##- (opt) Install packages as specified in tools.go.
@cat tools.go | grep _ | awk -F'"' '{print $$2}' | xargs -P $$(nproc) -tI % go install %
.PHONY: tidy
tidy: ##- (opt) Tidy our go.sum file.
go mod tidy
### -----------------------
# --- Binary checks
### -----------------------
# Got license issues with some dependencies? Provide a custom lichen --config
# see https://github.com/uw-labs/lichen#config
.PHONY: get-licenses
get-licenses: ##- Prints licenses of embedded modules in the compiled bin/app.
lichen bin/app
.PHONY: get-embedded-modules
get-embedded-modules: ##- Prints embedded modules in the compiled bin/app.
go version -m -v bin/app
.PHONY: get-embedded-modules-count
get-embedded-modules-count: ##- (opt) Prints count of embedded modules in the compiled bin/app.
go version -m -v bin/app | grep $$'\tdep' | wc -l
### -----------------------
# --- Helpers
### -----------------------
# https://gist.github.com/prwhite/8168133 - based on comment from @m000
.PHONY: help
help: ##- Show common make targets.
@echo "usage: make <target>"
@echo "note: use 'make help-all' to see all make targets."
@echo ""
@sed -e '/#\{2\}-/!d; s/\\$$//; s/:[^#\t]*/@/; s/#\{2\}- *//' $(MAKEFILE_LIST) | grep --invert "(opt)" | sort | column -t -s '@'
.PHONY: help-all
help-all: ##- Show all make targets.
@echo "usage: make <target>"
@echo "note: make targets flagged with '(opt)' are part of a main target."
@echo ""
@sed -e '/#\{2\}-/!d; s/\\$$//; s/:[^#\t]*/@/; s/#\{2\}- *//' $(MAKEFILE_LIST) | sort | column -t -s '@'
### -----------------------
# --- Special targets
### -----------------------
# https://unix.stackexchange.com/questions/153763/dont-stop-makeing-if-a-command-fails-but-check-exit-status
# https://www.gnu.org/software/make/manual/html_node/One-Shell.html
# required to ensure make fails if one recipe fails (even on parallel jobs) and on pipefails
.ONESHELL:
# # normal POSIX bash shell mode
# SHELL = /bin/bash
# .SHELLFLAGS = -cEeuo pipefail
# wrapped make time tracing shell, use it via MAKE_TRACE_TIME=true make <target>
SHELL = ./rksh
.SHELLFLAGS = $@
### -----------------------
# --- Old bash reference implementation
### -----------------------
.PHONY: reference-build
reference-build: reference-format reference-lint
.PHONY: reference-info
reference-info:
@shellcheck --version
@shellharden --version
.PHONY: reference-format
reference-format:
cd reference
@shellharden --replace *.sh
@shellharden --replace **/*.sh
.PHONY: reference-lint
reference-lint:
cd reference
@shellcheck -x *.sh
@shellcheck -x **/*.sh
@shellharden --check *.sh
@shellharden --check **/*.sh
# kind, these steps are meant to run locally on your machine directly!
#
# https://johnharris.io/2019/04/kubernetes-in-docker-kind-of-a-big-deal/
.PHONY: clean cluster compose context deploy
.PHONY: kind-cluster-clean
kind-cluster-clean:
kind delete cluster --name backup-ns
rm -rf .kube/**
# https://hub.docker.com/r/kindest/node/tags
.PHONY: kind-cluster-init
kind-cluster-init:
kind create cluster --name backup-ns --config=kind.yaml --kubeconfig .kube/config --image "kindest/node:v1.28.13"
$(MAKE) kind-fix-kubeconfig
sleep 1
$(MAKE) kind-cluster-init-script
.PHONY: kind-fix-kubeconfig
kind-fix-kubeconfig:
sed -i.bak -e 's/127.0.0.1/host.docker.internal/' .kube/config
.PHONY: kind-cluster-init-script
kind-cluster-init-script:
docker-compose up --no-start
docker-compose start
docker-compose exec service bash test/init_kind.sh
.PHONY: kind-cluster-reset
kind-cluster-reset:
$(MAKE) kind-cluster-clean
$(MAKE) kind-cluster-init