-
Notifications
You must be signed in to change notification settings - Fork 2
/
Makefile.base
2027 lines (1840 loc) · 82.7 KB
/
Makefile.base
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
## Manual: http://github.com/tkrop/go-make/MANUAL.md
SHELL := /bin/bash
# Setup variables for special purposes.
NULL :=
COMMA := ,
SPACE := $(NULL) $(NULL)
TAB := $(NULL) $(NULL)
define NEWLINE
endef
# Setup colored message templates.
COLOR-info := 1;96
COLOR-debug := 1;94
COLOR-error := 1;91
COLOR-warning := 1;93
COLOR-failure := 1;91
COLOR-success := 1;92
COLOR-topic := 1;94
COLOR-cmd := 1;95
COLOR-target := 1;96
# Finding: \$\(call *[ce]*msg
ts = $(shell date '+%F %T.%3N')
xifeq = $(if $(and $(findstring x$(1),x$(2)),$(findstring x$(2),x$(1))),$(3),$(4))
msg = $(strip \033[$(COLOR-$(1))m$(2)\033[0m $(3))
cmsg = $(ts) $(call msg,$(1),$(1):,$(2))
emsg = $(if $(2),echo -e "$(ts) $(call msg,$(1),$(1):,$(2))" > /dev/stderr)
dmsg = $(if $(findstring --trace,$(MAKEFLAGS)),$(call emsg,$(1),$(2)),echo -n)
escape = $(subst $$,\$$,$(subst ",\",$(subst \",\\",$(1))))
cerror = $(if $(1),$(error $(shell $(call emsg,error,$(1)))))
cinfo = $(shell $(call emsg,info,$(1)))
cdebug = $(shell $(call dmsg,debug,$(1)))
# Check whether bash version is higher or equal version 3.2 to be compatible.
ifneq ($(shell echo $${BASH_VERSION} | grep -E -v "^([4-9]+|3\.2)"),)
$(call cerror,bash version incompatible [$${BASH_VERSION}])
endif
# Check whether bash is running in minmal compatibility mode for testing.
ifdef BASH_COMPAT
$(call cdebug,bash compatibility mode set [$(BASH_COMPAT)])
endif
# System depdendent helper functions.
ifeq ("$(shell uname)","Linux")
else ifeq ($(shell uname),Darwin)
path-setup = $(shell \
if [ "$(1)" == "opt" ]; then \
XPATH="$(shell brew --prefix)/$(1)/$(2)/libexec/gnubin"; \
else XPATH="$(shell brew --prefix)/$(1)"; fi; \
test ! -e "$${XPATH}/$(3)" && brew install $(2) >&2; \
if [ -n "$(4)" ]; then echo -n "$${XPATH}:$(4)"; \
else echo -n "$${XPATH}"; fi)
export PATH := $(call path-setup,opt,coreutils,ls,$(PATH))
export PATH := $(call path-setup,opt,findutils,find,$(PATH))
export PATH := $(call path-setup,bin,bash,bash,$(PATH))
# export PATH := $(call path-setup,bin,curl,curl,$(PATH))
export PATH := $(call path-setup,opt,gawk,awk,$(PATH))
export PATH := $(call path-setup,opt,gsed,sed,$(PATH))
export PATH := $(call path-setup,opt,make,make,$(PATH))
# Ensure export of path to $(shell ...) in gmake 3.81.
ifneq ($(shell echo $(MAKE_VERSION) | grep -E "^3\.[0-9]+"),)
export SHELL := PATH="$(PATH)" $(shell brew --prefix)/bin/bash
else
export SHELL := $(shell brew --prefix)/bin/bash
endif
else
$(call cerror,unsupported operating system [$(shell uname --all)])
endif
# Log execution environment for debugging.
$(call cdebug,$(shell uname --all))
$(call cdebug,using PATH [$${PATH}])
$(call cdebug,using SHELL [$${SHELL}])
$(call cdebug,using BASH [$${0} - $${BASH_VERSION}])
$(call cdebug,using AWK [$(shell awk --version | head -n 1)])
# Helper function to find and remove source files.
find-all = find $(1) ! -path "./run/*" ! -path "./build/*" \( $(2) \) 2>/dev/null | \
sed -e "s/^.\///g"
find-go = $(call find-all,$(1),-type f -regex "^[a-z0-9/_.-]*\.go$$" $(2))
clean-file = $(call find-all,$(1),-type f \( $(2) \) \
-exec rm --verbose --force {} \;)
clean-dir = $(call find-all,$(1),-type d \( $(2) \) \
-exec rm --verbose --force --recursive {} \;)
# Helper function for conversion of variables name.
upper = $(shell echo "$(1)" | tr '[:lower:]' '[:upper:]')
# Setup default makeflags create consistent behavior.
MAKEFLAGS += --warn-undefined-variables
MAKEFLAGS += --no-builtin-rules
MAKEFLAGS += --no-print-directory
MAKEFLAGS += --not-keep-going
MAKEFLAGS := $(sort $(MAKEFLAGS))
# Setup default directories of projects.
DIR_CONFIG := $(HOME)/.config/go-make/$(CURDIR:$(HOME)/%=%)
DIR_BUILD := $(CURDIR)/build
DIR_RUN := $(CURDIR)/run
DIR_CRED := $(DIR_RUN)/creds
$(call cdebug,using DIR_CONFIG [$(DIR_CONFIG)])
ifneq ("$(wildcard $(CURDIR)/Makefile.vars)","")
include Makefile.vars
else
$(shell $(call emsg,warning,please customize variables in $(CURDIR)/Makefile.vars))
endif
# Setup default browser command to open coverage reports.
BROWSER ?= xdg-open
# Setup default verions used for building, releasing, and publishing.
VERSION ?= snapshot
IMAGE_VERSION ?= $(VERSION)
ifeq ($(wildcard VERSION),VERSION)
BUILD_VERSION := v$(shell cat VERSION)
else
BUILD_VERSION ?= $(VERSION)
endif
mod-readonly = $(call xifeq,$(IMAGE_VERSION),snapshot,,-mod=readonly)
# Setup default variables for building, linking, and installing.
LDFLAGS ?=
LD_FLAGS ?= $(strip $(LDFLAGS))
LD_FLAGS_LINUX ?= $(strip -s -w $(LDFLAGS))
TESTFLAGS ?=
TEST_FLAGS ?= $(strip $(mod-readonly) -count=1 -race $(TESTFLAGS))
BUILDFLAGS ?= # -linkmode 'external' -extldflags '-static'
BUILD_FLAGS ?= $(strip $(mod-readonly) -buildvcs=auto -trimpath $(BUILDFLAGS))
INSTALLFLAGS ?=
INSTALL_FLAGS ?= $(strip $(mod-readonly) -buildvcs=auto $(INSTALLFLAGS))
# Function to ensure compatibility of gawk and mawk.
awk-gensub = $(shell echo "false" | \
awk '{ print gensub("false","true","g") }' 2>/dev/null)
$(call cdebug,using GENSUB [$(awk-gensub)])
ifeq ($(awk-gensub),false)
awk-gensub = function gensub(r, s, h, t) { \
if (t == 0) { t = $$0 } \
if (h ~ "^[gG]") { gsub(r, s, t) } \
else if (int(h) != 0) { \
for (i = 1; i <= int(h); i++) { \
if (match(t, r)) { \
t = substr(t, 1, RSTART-1) s substr(t, RSTART+RLENGTH) \
} \
} \
} \
return t \
}
else
awk-gensub =
endif
# Setup default go compiler environment.
export GO ?= go
export GOPATH ?= $(shell $(GO) env GOPATH)
export GOBIN ?= $(GOPATH)/bin
# Export private repositories that do not need to be downloaded.
export GOPRIVATE ?= github.bus.zalan.do
# Setup go to use desired and consistent go versions.
GOVERSION := $(shell $(GO) version $$(which $(GO)) | sed -E "s/.*go([0-9.]+).*/\1/")
# Function for checking and wanring about go and package version mismatches.
go-check-version = \
if [ -f "$(GOBIN)/$(1)" ]; then \
MODULE="$(call go-pkg,$(TOOLS_GO),origin,^$(1)$$,,$(2))"; \
GOVERSION="$$($(GO) version -v "$(GOBIN)/$(1)" | sed "s/.*: go//")"; \
if ! [[ "$${GOVERSION}" =~ ^$(GOVERSION)* ]]; then \
CHECK="$${GOVERSION} => $(GOVERSION)"; \
$(call go-update,$(1),$${MODULE},$${GOVERSION}, \
build [$${CHECK}], or install matching compiler version); \
fi; \
if [ "$${MODULE\#\#*@}" != "latest" ]; then \
MPATH="$${MODULE%%@*}"; MPATH="$${MPATH%/cmd}"; \
MVERSION="$$($(GO) version -m "$(GOBIN)/$(1)" | \
grep -E "^\s*(mod|dep)\s*$${MPATH}" | cut -f 4)"; \
if [ "$${MVERSION}" != "$${MODULE\#\#*@}" ]; then \
CHECK="$${MVERSION} => $${MODULE\#\#*@}"; \
$(call go-update,$(1),$${MODULE},$${MVERSION}, \
version [$${CHECK}], to sync with project version); \
fi; \
fi; \
fi
# Funtion to warn about automated updating of go packages.
go-update = \
if [ "$(3)" != "(devel)" ]; then \
$(call emsg,warning,$(1) $(4): updating now!); \
echo '$(call go-install,$(2))'; $(call go-install,$(2)); \
else \
$(call emsg,warning,$(1) $(4): run 'make update-$(1)' $(5)); \
fi
# Function to install go programs from package the go tooling list.
go-install-module = \
$(call go-install,$(call go-pkg,$(TOOLS_GO),module,^$(1)$$,,$(2)))
go-install = \
$(GO) install $(INSTALL_FLAGS) "$(1)"
# Function for determining the actual version of a go package from downloading.
VERSION_REGEX := 's/.*"Path":\s*"([^"]*)".*"Version":\s*"([^"]*)".*/\1@\2/g'
go-download-version = \
$(GO) mod download --json $(1) 2>/dev/null | \
sed --regexp-extended --null-data $(VERSION_REGEX)
# Function for go-package handling (TODO: check spliting and simplifying).
go-pkg = $(shell awk -v mode="$(2)" -v filter="$(3)" \
-v not="$(4)" -v version="$(5)" '$(awk-gensub) \
BEGIN { FS = "[/@]"; RS = "[ \n\r]" } \
function package(vindex) { \
if (vindex) { \
return substr($$0, 1, vindex - 1) \
} else { return $$0 } \
} { \
field = NF; \
if (vindex = index($$0, "@")) { field-- } \
if ($$(field) ~ "v[0-9]+") { field-- } \
if (!filter || \
(not && ($$(field) !~ filter)) || \
(!not && ($$(field) ~ filter))) { \
if (mode == "command") { \
if (!($$(field) in map)) { \
print $$(field); map[$$(field)]++ \
} \
} else if (mode == "module") { \
if (!((pkg = package(vindex)) in map)) { \
if (version ~ "^[0-9.]+(-[0-9]{14}-[0-9a-f]{12})?$$") { \
print pkg ((version == "latest") ? "@latest" : "@v" version) \
} else { \
print ((vindex) ? $$0 : $$0 "@latest") \
} map[pkg]++ \
} \
} else if (mode == "origin") { \
if (!((pkg = package(vindex)) in map)) { \
print $$1 "/" $$2 "/" $$3 "@" \
((vindex) ? substr($$0, vindex + 1) : "latest"); \
map[pkg]++ \
} \
} else if (mode == "package") { \
if (!((pkg = package(vindex)) in map)) { \
print pkg; map[pkg]++ \
} \
} \
} \
}' <<<"$(1)")
# Setup go-make to use desired build and config scripts.
GOMAKE_DEP := github.com/tkrop/[email protected]
GOMAKE_MAKEFILE := $(realpath $(firstword $(MAKEFILE_LIST)))
GOMAKE_MAKEFILES := $(GOMAKE_MAKEFILE) \
$(wildcard Makefile.vars) $(wildcard Makefile.ext)
GOMAKE_CONFIG := $(patsubst %/,%,$(dir $(GOMAKE_MAKEFILE)))
gomake-version = $(shell if [ ! -f "$(GOBIN)/go-make" ]; then \
echo "(devel)"; else $(GO) version -m "$(GOBIN)/go-make" | \
grep -E "^\s*(mod|dep)\s*github.com/tkrop/go-make" | cut -f 4; fi)
# Ensure that we can find default config files.
ifeq ($(wildcard $(GOPATH)/pkg/mod/$(GOMAKE_DEP)/config/),)
$(shell $(GO) install $(GOMAKE_DEP) >/dev/null 2>&1)
endif
# Function to search for default config files.
search-default = $(firstword $(wildcard $(1)) $(wildcard $(GOMAKE_CONFIG)/$(1) \
$(wildcard $(GOPATH)/pkg/mod/$(GOMAKE_DEP)/config/$(1))))
TOOL_VERSION_K8S ?= v0.31.0
TOOL_VERSION_GOLANGCI ?= v1.60.3
TOOL_VERSION_REVIVE ?= v1.3.3
# Setup default tool command versions to be installed.
TOOLS_GO += \
github.com/golangci/golangci-lint/cmd/golangci-lint@$(TOOL_VERSION_GOLANGCI) \
github.com/zalando/zally/cli/zally \
golang.org/x/vuln/cmd/govulncheck \
github.com/uudashr/gocognit/cmd/gocognit \
github.com/fzipp/gocyclo/cmd/gocyclo \
github.com/mgechev/revive@$(TOOL_VERSION_REVIVE) \
github.com/securego/gosec/v2/cmd/gosec \
github.com/tsenart/deadcode \
honnef.co/go/tools/cmd/staticcheck \
github.com/zricethezav/gitleaks/v8 \
github.com/icholy/gomajor \
github.com/golang/mock/mockgen \
github.com/tkrop/go-testing/cmd/mock \
k8s.io/code-generator/cmd/deepcopy-gen@$(TOOL_VERSION_K8S) \
k8s.io/code-generator/cmd/client-gen@$(TOOL_VERSION_K8S) \
k8s.io/code-generator/cmd/lister-gen@$(TOOL_VERSION_K8S) \
k8s.io/code-generator/cmd/informer-gen@$(TOOL_VERSION_K8S) \
sigs.k8s.io/controller-tools/cmd/controller-gen \
sigs.k8s.io/kind \
$(GOMAKE_DEP)
TOOLS_SH += \
github.com/anchore/syft \
github.com/anchore/grype
TOOLS_NPM += \
markdownlint-cli
NVM_BIN ?=
# Check for the actually used go-make version.
$(call cdebug, go-check-version = $(call escape,$(call go-check-version,go-make,)))
$(shell $(call go-check-version,go-make,))
# Setup sensible defaults for workflow configuration variables.
IMAGE_PUSH ?= pulls
CODE_QUALITY ?= base
TEST_TIMEOUT ?= 10s
# Setup default config files to be used.
FILE_MAKE ?= $(call search-default,Makefile)
FILE_VARS ?= $(call search-default,Makefile.vars)
FILE_GOLANGCI ?= $(call search-default,.golangci.yaml)
FILE_MARKDOWN ?= $(call search-default,.markdownlint.yaml)
FILE_GITLEAKS ?= $(call search-default,.gitleaks.toml)
FILE_REVIVE ?= $(call search-default,revive.toml)
FILE_CODACY ?= $(call search-default,.codacy.yaml)
FILE_IMAGE ?= Dockerfile
FILE_DEEPCOPY ?= zz_generated.deepcopy.go
FILE_GO_HEADER ?=
# Setup custom delivery files to be scanned for updating go versions.
DELIVERY := $(strip $(wildcard delivery*.yaml) $(wildcard .github/workflows/*.yaml))
DELIVERY_REGEX ?= (cdp-runtime\/go-|go-version: \^?)[0-9.]*
GOMOD_REGEX ?= ^(go )[0-9.]*$$
# Setup default git command and repository information.
GIT ?= git
GITHOOKS ?= pre-commit # commit-msg
GITAUTHOR ?= $$(echo -n "$$(git config --get user.name) <$$(git config --get user.email)>")
GITROOT := $(shell git rev-parse --show-toplevel 2>/dev/null || echo $(CURDIR))
GITURL := $(shell $(GIT) remote get-url origin 2>/dev/null | \
sed "s/^https:\/\///; s/^git@//; s/.git$$//; s/:/\//")
GITHOST := $(word 1,$(subst /, ,$(GITURL)))
GITOWNER := $(word 2,$(subst /, ,$(GITURL)))
GITREPO := $(word 3,$(subst /, ,$(GITURL)))
$(call cdebug,using GITURL [$(GITURL)])
TEAM ?= $(shell \
if [ -f ".zappr.yaml" ]; then \
grep "X-Zalando-Team:" .zappr.yaml | \
sed "s/.*:[[:space:]]*\([a-z-]*\).*/\1/"; \
else echo "${GITOWNER}"; fi)
# Setup container image command (supports: docker or podman).
IMAGE_CMD ?= $(shell command -v docker || command -v podman)
# Setup default container image name and host.
ifdef IMAGE_NAME
ifneq ($(words $(subst /, ,$(IMAGE_NAME))),3)
$(call cerror,invalid base image name [$(IMAGE_NAME)] \
- image name format is repo/team/name!)
endif
IMAGE_HOST := $(word 1,$(subst /, ,$(IMAGE_NAME)))
IMAGE_TEAM := $(word 2,$(subst /, ,$(IMAGE_NAME)))
IMAGE_ARTIFACT := $(word 3,$(subst /, ,$(IMAGE_NAME)))
else
IMAGE_HOST ?= pierone.stups.zalan.do
IMAGE_TEAM ?= $(TEAM)
IMAGE_ARTIFACT ?= $(GITREPO)
endif
IMAGE := $(IMAGE_HOST)/$(IMAGE_TEAM)/$(IMAGE_ARTIFACT):$(IMAGE_VERSION)
$(call cdebug,using IMAGE [$(IMAGE)])
# Setup default database connection.
DB_HOST ?= 127.0.0.1
DB_PORT ?= 5432
DB_NAME ?= db
DB_USER ?= user
DB_PASSWORD ?= pass
DB_VERSION ?= latest
DB_IMAGE ?= postgres:$(DB_VERSION)
# Setup default aws startup settings.
AWS_SERVICES ?= sqs s3
AWS_VERSION ?= latest
AWS_IMAGE ?= localstack/localstack:$(AWS_VERSION)
# Setup codacy integration.
CODACY ?= enabled
ifeq ($(IMAGE_VERSION),snapshot)
CODACY_CONTINUE ?= false
else
CODACY_CONTINUE ?= true
endif
CODACY_PROVIDER ?= ghe
CODACY_USER ?= $(GITOWNER)
CODACY_PROJECT ?= $(GITREPO)
CODACY_API_BASE_URL ?= https://codacy.bus.zalan.do
CODACY_CLIENTS ?= aligncheck deadcode
CODACY_BINARIES ?= gosec staticcheck
ifeq (CODACY_API_BASE_URL,$(CODACY_API_BASE_URL))
CODACY_REPORTER_VERSION ?= 13.12.3
endif
CODACY_GOSEC_VERSION ?= 0.4.5
CODACY_STATICCHECK_VERSION ?= 3.0.12
CODACY_PROJECT_TOKEN ?=
CODACY_API_TOKEN ?=
$(call cdebug,using CODACY_API_BASE_URL [$(CODACY_API_BASE_URL)])
KUBE_APIS ?= $(shell FILES=($$($(call find-go,.,-path "*/apis/*/*/*.go"))); \
if [ -n "$${FILES}" ]; then grep -E "\+(kubebuilder|k8s)" "$${FILES[@]}" | \
sed "s|apis/[^/]*/[^/]*/[^/]*.go:.*|apis|" | sort --unique; \
fi)
GEN_KUBE_APIS ?= $(KUBE_APIS:%=gen-kube-apis/%)
$(call cdebug,using GEN_KUBE_APIS [$(GEN_KUBE_APIS)])
GEN_KUBE_TARGETS := install-deepcopy-gen install-client-gen \
install-lister-gen install-informer-gen install-controller-gen
# Default target list for all and pipeline builds.
TARGETS_ALL ?= init test lint build image
TARGETS_INIT ?= init-hooks init-go $(if $(filter $(CODACY),enabled),init-codacy,)
TARGETS_COMMIT ?= test-go test-unit lint-leaks? lint-$(CODE_QUALITY) lint-markdown
TARGETS_TEST ?= test-all $(if $(filter $(CODACY),enabled),test-upload,)
TARGETS_LINT ?= lint-leaks? lint-$(CODE_QUALITY) lint-markdown lint-apis \
$(if $(filter $(CODACY),enabled),lint-codacy,)
TARGETS_CLEAN ?= clean-build
TARGETS_UPDATE ?= update-go update-deps update-make
INIT_MAKE ?= $(notdir $(FILE_MAKE) $(FILE_VARS) $(FILE_MARKDOWN) \
$(FILE_CODACY) $(FILE_REVIVE))
UPDATE_MAKE ?= $(notdir $(FILE_MAKE) $(FILE_GOLANGCI) $(FILE_MARKDOWN) \
$(FILE_GITLEAKS) $(FILE_CODACY) $(FILE_REVIVE))
# Stub function for general setup in run-targets (default: true).
run-setup ?= true
# Stub function for common variables in run-targets (called with one arg).
run-vars ?=
# Stub function for local variables in run-targets (called with one arg).
run-vars-local ?=
# Stub function for container specific variables in run-targets (called with one arg).
run-vars-image ?=
# Stub function to set up aws localstack run-target (default: true).
run-setup-aws ?= true
# General setup of tokens for run-targets.
run-token-create = \
ztoken > $(DIR_CRED)/token; echo "Bearer" > $(DIR_CRED)/type
run-token-link = \
test -n "$(1)" && test -n "$(2)" && test -n "$(3)" && ( \
test -h "$(DIR_CRED)/$(1)-$(2)" || ln -s type "$(DIR_CRED)/$(1)-$(2)" && \
test -h "$(DIR_CRED)/$(1)-$(3)" || ln -s token "$(DIR_CRED)/$(1)-$(3)" \
) || test -n "$(1)" && test -n "$(2)" && test -z "$(3)" && ( \
test -h "$(DIR_CRED)/$(1)" || ln -s type "$(DIR_CRED)/$(1)" && \
test -h "$(DIR_CRED)/$(2)" || ln -s token "$(DIR_CRED)/$(2)" \
) || test -n "$(1)" && test -z "$(2)" && test -z "$(3)" && ( \
test -h "$(DIR_CRED)/$(1)" || ln -s token "$(DIR_CRED)/$(1)" \
) || true
# Setup default environment variables.
GEN_DIRS += $(strip $(KUBE_APIS:%/apis=%/clients) $(KUBE_APIS:%/apis=%/manifests))
$(call cdebug,using GEN_DIRS [$(GEN_DIRS)])
GEN_SOURCES += $(strip $(shell $(call find-go,.,-name "mock_*_test.go" -o \
-name "mock_*.go" -o -name "zz_generated.*.go")) \
$(shell if [ -n "$(GEN_DIRS)" ]; then $(call find-go,$(GEN_DIRS),); fi))
SOURCES := $(filter-out $(GEN_SOURCES),$(shell $(call find-go,.,)))
MODULES := $(shell echo $(SOURCES) | xargs -r dirname | sort --unique)
COMMANDS := $(shell $(call find-go,.,-name "main.go") | xargs -r readlink -f | \
awk -F '/' '{ for ( field = NF-1; field > 0; field--) { \
if ($$field != "cmd") { print $$field; break; } \
} }' | sort --unique)
COMMANDS_REGEX := $(shell echo "^$(COMMANDS)$$" | tr ' ' '|' )
COMMANDS_GO := $(sort $(call go-pkg,$(TOOLS_GO),command,$(COMMANDS_REGEX),not,))
COMMANDS_SH := $(sort $(call go-pkg,$(TOOLS_SH),command,$(COMMANDS_REGEX),not,))
COMMANDS_PKG := $(shell $(call find-go,.,-name "main.go") | xargs -r readlink -f | \
awk -F '/' '$(awk-gensub) { for ( field = NF-1; field > 0; field--) { \
if ($$field != "cmd") { \
target = gensub("/main.go", "", "1", gensub("$(CURDIR)", ".", "1")); \
print $$field "=" target; break; \
} \
} }')
IMAGES := $(shell $(call find-all,.,-type f -name "$(FILE_IMAGE)*"))
# Setup of runtime arguments for commands that support arguments.
CMDWORDS ?= call show git- test- lint run- version- update
CMDARGS ?= $(shell awk '{ split("$(CMDWORDS)",seps); \
for (i = 1; i <= NF; i++) { if (found) { \
line = ((line) ? line " " $$i : $$i); continue \
} \
for (p in seps) { if ($$i ~ "^"seps[p] ) { found = 1; } } } \
} END { print line }' <<<"$(MAKECMDGOALS)")
ARGS ?=
ifdef CMDARGS
ARGS := $(strip $(CMDARGS) $(ARGS))
$(eval $(subst :,\:,$(CMDARGS))::;@:)
endif
$(call cinfo,executing [make $(filter-out $(ARGS),$(MAKECMDGOALS)) ARGS=\"$(ARGS)\"])
# Optimized generation of mocks.
gen-mocks = \
if [ -n "$(SOURCES)" ]; then \
grep "//go:generate[[:space:]]*mockgen" $(SOURCES) | \
$(gen-mocks-awk) | sort --unique; \
fi
gen-mocks-awk = \
awk '{ \
source = "_"; target = "_"; package = "_"; \
origin = substr($$1, 0, index($$1, "://go:generate") - 1); \
path = substr(origin, 0, match(origin, "/[^/]*$$")); \
for (i = 1; i <= NF; i++) { \
if ($$i ~ "^-source=") { \
cmd = "readlink -f \"" path "/" substr($$i, 9) "\""; \
cmd | getline source; close(cmd) \
} else if ($$i ~ "^-destination=") { \
cmd = "readlink -f \"" path "/" substr($$i, 14) "\""; \
cmd | getline target; close(cmd) \
} \
} \
if (source == "_") { package = $$(NF-1) } \
print target "|" source "|" package "|" origin \
}'
$(call cdebug, gen-mocks = $(call escape,$(call gen-mocks)))
MOCKS := $(shell $(gen-mocks))
MOCKS_TARGETS := $(shell echo "$(MOCKS)" | \
awk 'BEGIN {FS="|"; RS=" "} ($$1 != "_") { print $$1 }' | sort --unique)
MOCKS_SOURCES := $(shell echo "$(MOCKS)" | \
awk 'BEGIN {FS="|"; RS=" "} ($$2 != "_") { print $$2 }' | sort --unique)
MOCKS_ORIGINS := $(shell echo "$(MOCKS)" | \
awk 'BEGIN {FS="|"; RS=" "} ($$4 != "_") { print $$4 }' | sort --unique)
$(call cdebug,using MOCKS [$(MOCKS)])
# Prepare target lists used to define rules and preconditions.
TARGETS_PUBLISH := $(addprefix version-publish-, all $(COMMANDS))
TARGETS_INIT_ALL := $(addprefix init/,$(sort $(INIT_MAKE) $(UPDATE_MAKE)))
TARGETS_INIT_MAKE := $(addprefix init/,$(INIT_MAKE))
TARGETS_INIT_CODACY := $(addprefix init-, $(CODACY_BINARIES))
TARGETS_TEST_INIT := test-clean init-code
TARGETS_LINT_CODACY_CLIENTS := $(addprefix lint-, $(CODACY_CLIENTS))
TARGETS_LINT_CODACY_BINARIES := $(addprefix lint-, $(CODACY_BINARIES))
TARGETS_IMAGE_BUILD := $(addprefix image-build/, $(IMAGES))
TARGETS_IMAGE_PUSH := $(addprefix image-push/, $(IMAGES))
TARGETS_BUILD := $(addprefix build-, $(COMMANDS))
TARGETS_BUILD_LINUX := $(addprefix $(DIR_BUILD)/linux/, $(COMMANDS))
TARGETS_INSTALL := $(addprefix install-, $(COMMANDS))
TARGETS_INSTALL_GO := $(addprefix install-, $(COMMANDS_GO))
TARGETS_INSTALL_SH := $(addprefix install-, $(COMMANDS_SH))
TARGETS_INSTALL_NPM := $(addprefix install-, $(TOOLS_NPM:-cli=))
TARGETS_INSTALL_ALL := $(TARGETS_INSTALL) $(TARGETS_INSTALL_GO) \
$(TARGETS_INSTALL_SH) $(TARGETS_INSTALL_NPM)
TARGETS_UNINSTALL := $(addprefix uninstall-, $(COMMANDS))
TARGETS_UNINSTALL_GO := $(addprefix uninstall-, $(COMMANDS_GO))
TARGETS_UNINSTALL_SH := $(addprefix uninstall-, $(COMMANDS_SH))
TARGETS_UNINSTALL_NPM := $(addprefix uninstall-, $(TOOLS_NPM:-cli=))
TARGETS_UNINSTALL_CODACY := $(addprefix uninstall-codacy-, $(CODACY_BINARIES))
TARGETS_UNINSTALL_ALL := \
$(filter-out $(call xifeq,$(gomake-version),(devel),%-go-make,), \
$(TARGETS_UNINSTALL) $(TARGETS_UNINSTALL_GO) $(TARGETS_UNINSTALL_SH) \
$(TARGETS_UNINSTALL_NPM) $(TARGETS_UNINSTALL_CODACY))
TARGETS_RUN := $(addprefix run-, $(COMMANDS))
TARGETS_RUN_GO := $(addprefix run-go-, $(COMMANDS))
TARGETS_RUN_IMAGE := $(addprefix run-image-,$(if $(IMAGES),$(COMMANDS)))
TARGETS_RUN_CLEAN := $(addprefix run-clean-,$(if $(IMAGES),$(COMMANDS)) db aws)
TARGETS_CLEAN_ALL := clean-init $(TARGETS_CLEAN) clean-run $(TARGETS_UNINSTALL_ALL)
TARGETS_CLEAN_RUN := $(addprefix clean-run-,$(if $(IMAGES),$(COMMANDS)) db aws)
TARGETS_UPDATE_GO := $(addprefix update-,$(COMMANDS_GO))
TARGETS_UPDATE_MAKE := $(addprefix update/,$(UPDATE_MAKE))
TARGETS_UPDATE_MAKE? := $(addsuffix ?,$(TARGETS_UPDATE_MAKE))
TARGETS_UPDATE_ALL := update-go update-deps update-tools update-make
TARGETS_UPDATE_ALL? := update-go? update-deps? update-make?
TARGETS_UPDATE? := $(filter $(addsuffix ?,$(TARGETS_UPDATE)), \
$(TARGETS_UPDATE_ALL?) $(TARGETS_UPDATE_MAKE?))
## Standard: default targets to test, lint, and build.
#@ executes the default targets.
all:: $(TARGETS_ALL)
#@ executes the default targets after cleaning up.
all-clean:: clean clean-run all
#@ executes the pre-commit check targets.
commit:: $(TARGETS_COMMIT)
#@ show short cut for show-help.
help:: show-help
##@ executes the given arguments as commands (for testing).
call::; @$(ARGS)
$(DIR_CONFIG) $(DIR_BUILD) $(DIR_RUN) $(DIR_CRED) $(GOBIN):
@if [ ! -d "$@" ]; then mkdir -p $@; fi;
## Make-Support: targets to support user understanding.
show-help = awk ' \
BEGIN { \
printf("\n$(call msg,topic,Usage:,) $(call msg,cmd,(go-)make, \
) $(call msg,target,<target>,)\n") \
} \
/^\#\# .*/ { \
if (i = index($$0, ":")) { \
printf("\n$(call msg,topic,%s,)%s\n", \
substr($$0, 4, i - 4), substr($$0, i)) \
} else { \
printf("\n$(call msg,topic,%s,)\n", substr($$0, 4)) \
} next \
} \
/^\#@ / { \
if (i = index($$0, ":")) { \
printf(" $(call msg,target,%-25s,) %s\n", \
substr($$0, 4, i - 4), substr($$0, i + 2)) \
} else { line = substr($$0, 4) } next \
} \
/^[a-zA-Z_0-9?-]+:/ { \
if (line) { \
target = substr($$1, 1, length($$1) - 1); \
if (i = index(line, " \# ")) { \
target = target " " substr(line, 1, i - 1); \
line = substr(line, i + 3) \
} \
printf(" $(call msg,target,%-25s,) %s\n", target, line); \
line = "" \
} \
}'
show-vars = awk ' \
($$0 ~ "^[[:alnum:]_-]* [?+]=") { \
if (comment) { print comment } print $$0; var = 1; next \
} \
(var && $$0 ~ "^([\t]|$$)") { \
print $$0; next \
} \
($$0 ~ "^\#") { \
comment = $$0; var = 0; next \
} { \
comment = ""; var = 0; next \
}'
show-targets = awk -v RS= -F: ' \
/(^|\n)\# Files(\n|$$)/,/(^|\n)\# Finished Make data base/ { \
if ($$1 !~ "^[\#.]") { print $$1 } \
}' | LC_ALL=C sort --unique | \
egrep --invert-match --regexp='^[^[:alnum:]]'
$(DIR_CONFIG)/targets: $(DIR_CONFIG) $(GOMAKE_MAKEFILES)
@$(call emsg,info,updating targets [$(DIR_CONFIG)/targets]); \
$(MAKE) --no-builtin-rules --no-builtin-variables --print-data-base \
--question --makefile="$(GOMAKE_MAKEFILE)" 2>/dev/null | \
$(show-targets) > "$(DIR_CONFIG)/targets";
#@ shows this short extracted help about main target families.
show-help::
@cat $(MAKEFILE_LIST) | $(show-help) || true;
#@ shows all supported (customizable) variable (with defaults).
show-vars:
@cat $(GOMAKE_MAKEFILE) | $(show-vars) || true;
#@ shows all supported targets (without prerequesites).
show-targets: $(DIR_CONFIG)/targets
@cat $(DIR_CONFIG)/targets || true;
#@ shows the raw base makefile as implemented.
show-raw:
@cat $(GOMAKE_MAKEFILE) || true;
#@ shows the effective makefile as evaluated by make.
show-make:
@$(MAKE) --no-builtin-rules --no-builtin-variables --print-data-base \
--question --makefile="$(GOMAKE_MAKEFILE)" 2>/dev/null || true;
## Git-Support: targets for standard git commands (experimental).
GITFIX ?= $(GIT) commit --amend --no-edit
GITPUSH ?= $(GIT) push --force-with-lease
GITBRANCH ?= $(GIT) branch --show-current
GITPRUNE ?=$(GIT) fetch --all --prune --verbose
GITTIP ?= $(GIT) log --max-count=1 --pretty=format:"%T"
GITHASH ?= $(GIT) log --max-count=1 --pretty=format:"%H"
GITLOG ?= $(GIT) log --pretty=format
# https://github.com/pvdlg/conventional-changelog-metahub#commit-types
COMMIT_CONVENTION ?= feat deprecate remove docs fix style refactor \
perf test build ci chore
git-message = \
if [ -z "$${MESSAGE}" ]; then MESSAGE="chore: update deps"; fi; \
if ! [[ "$${MESSAGE}" =~ ^[^:]+:.*$$ ]]; then MESSAGE="$(1): $${MESSAGE}"; fi; \
if ! [[ "$${MESSAGE}" =~ ^.*\([^()]*\#[0-9]+\)$$ ]]; then \
COMMIT="$$($(GITLOG):"%s" | grep -Eo "[^() ]*\#[0-9]+" | sort -n -u)"; \
NUMBER="$${COMMIT\#\#*\#}"; CONTEXT="$${COMMIT%%\#*}"; \
MESSAGE="$${MESSAGE} ($${CONTEXT}\#$$((NUMBER+$(2))))"; \
fi
git-main = \
$$($(GIT) remote show origin 2>/dev/null | grep "HEAD branch:" | cut -d ' ' -f 5)
git-clean = \
$(GITPRUNE); HISTORY=($$($(GIT) log --pretty=format:"%T" "$${MAIN}")); \
for BRANCH in $$($(GIT) branch | cut -c 3- | grep -vw "$(1)"); do \
TIP="$$($(GITTIP) "$${BRANCH}")"; \
if [[ "$${HISTORY[@]}" =~ $${TIP} ]]; then \
$(GIT) branch --delete --force "$${BRANCH}"; \
elif [ "$(ARGS)" == "all" ]; then \
$(GIT) branch --delete "$${BRANCH}"; \
else $(call emsg,info,keeping branch [$${BRANCH}]); \
fi; \
done; $(GITPRUNE)
git-verify = awk -v mode="$(1)" -v author="$(GITAUTHOR)" '$(awk-gensub) \
BEGIN { \
re_type_suffix = "((\\[|\\()[a-z]+(\\]|\\)))?!?: "; \
re_type_missing = "^([a-z]+)" re_type_suffix; \
re_type_invalid = "^(" gensub(" ", "|", "g", \
"$(COMMIT_CONVENTION)") ")" re_type_suffix; \
re_issue = "([a-z]+/[a-z]+)?\#[0-9]+"; \
re_issue_list = "\\(" re_issue "((,|\\) *\\()" re_issue ")*\\)"; \
re_issue_missing = re_issue_list "$$"; \
re_issue_invalid = "^[^(]*" re_issue_list "$$"; \
signed_by = "Signed-off-by:"; \
re_signed_missing = signed_by " +[^\\n]*$$"; \
re_signed_author = "^" signed_by " +" author "$$"; \
} \
function verify() { \
msg = gensub("((:?\\n)\#[^\\n]*|\\n)*$$", "", "g", msg); \
title = (i = index(msg,"\n")) ? substr(msg, 0, i - 1) : msg; \
if (title !~ re_type_missing) { \
printf("$(call cmsg,error,commit type missing \
[title=%s])\n", title); errors++ \
} else if (title !~ re_type_invalid) { \
printf("$(call cmsg,error,commit type invalid \
[title=%s])\n", title); errors++ \
} \
if (title !~ re_issue_missing) { \
printf("$(call cmsg,error,issue missing \
[titel=%s])\n", title); errors++ \
} else if (title !~ re_issue_invalid) { \
printf("$(call cmsg,error,issue invalid \
[titel=%s])\n", title); errors++ \
} \
sign = (i = index(msg, signed_by)) ? substr(msg, i) : ""; \
if (sign !~ re_signed_missing) { \
printf("$(call cmsg,error,signed-off-by missing \
[msg=%s])\n", msg); errors++ \
} else if (sign !~ "^" re_signed_missing) { \
printf("$(call cmsg,error,signed-off-by by to many \
[sign=%s])\n", sign); errors++ \
} else if (mode == "message" && sign !~ re_signed_author) { \
printf("$(call cmsg,error,signed-off-by not the author \
[sign=%s; author=%s])\n", sign, author); errors++ \
} msg = "" \
} \
(mode == "message") { \
if (msg != "") { \
msg = msg "\n" $$0 \
} else { msg = $$0 } next \
} \
(mode != "message" && $$0 ~ "^ ") { \
if (msg != "") { \
msg = msg "\n" substr($$0, 5) \
} else { msg = substr($$0, 5) } next \
} \
(mode != "message") { if (msg) { verify(); } } \
END { \
if (mode == "message" || msg) { verify(); } if (errors) { \
printf("$(call cmsg,failure,git-verify $(ARGS) \
[errors=%d])\n", errors); exit 1 \
} \
printf("$(call cmsg,success,git-verify $(ARGS) \
[errors=%d])\n", errors) \
}'
GITFORMAT ?= "%Cred%h %t %Cgreen%ad%Creset | %s %C(bold blue)[%an]%Creset%C(yellow)%d%Creset"
#@ prints the git log as in pretty short format.
git-log::
@$(GIT) log --graph --date=iso --pretty=format:$(GITFORMAT);
#@ prints the git log as graph in pretty short format.
git-graph::
@$(GIT) log --graph --date=iso --pretty=format:$(GITFORMAT) --all;
#@ [all] # cleans up the git history removing merged branches except the current.
git-clean::
@BRANCH="$$($(GITBRANCH))"; MAIN="$(git-main)"; \
$(call git-clean,$${MAIN}\|$${BRANCH});
#@ [all] # checks out default branch and cleans up the git history removing merged branches.
git-reset::
@BRANCH="$$($(GITBRANCH))"; MAIN="$(git-main)"; \
if [ "$${BRANCH}" != "$${MAIN}" ]; then \
$(GIT) checkout "$${MAIN}" && $(GIT) pull || ( \
$(GIT) stash && $(GIT) checkout "$${MAIN}" && \
$(GIT) pull && $(GIT) stash apply \
) \
fi || exit 1; $(call git-clean,$${MAIN});
#@ <mode> [msg|log-file] # checks whether git log follows the commit conventions.
git-verify::
@$(call dmsg,info,git-verify); MODE="$(firstword $(ARGS))"; \
case "$${MODE}" in \
( "log" ) cat "$(wordlist 2,$(words $(ARGS)),$(ARGS))";; \
( "message" ) cat "$(wordlist 2,$(words $(ARGS)),$(ARGS))";; \
( "branch" ) $(GIT) log --no-merges --format=raw "$$($(GITBRANCH))";; \
( "pull" | * ) MAIN="$(git-main)" && $(GIT) fetch origin "$${MAIN}" --verbose && \
$(GIT) log --no-merges --format=raw "$$($(GITBRANCH))" "^origin/$${MAIN}";; \
esac | $(call git-verify,$${MODE}) > /dev/stderr
#@ <branch> <message> # creates a branch with the current change set using next issue.
git-create:: git-create-feat
# TODO: check whether I'm on the default branch and on remote HEAD.
$(addprefix git-create-,$(COMMIT_CONVENTION)):: git-create-%:
@BRANCH="$(firstword $(ARGS))"; \
MESSAGE="$(wordlist 2,$(words $(ARGS)),$(ARGS))"; \
if [ -z "$${BRANCH}" ]; then BRANCH="update-deps"; fi; \
$(call git-message,$*,1); \
$(GIT) checkout -B "$${BRANCH}" && \
ARGS="" $(GIT) commit --signoff --message="$${MESSAGE}" && \
$(GIT) push --set-upstream origin "$${BRANCH}" || \
$(GIT) checkout $${BRANCH}; exit 0;
#@ <message> # commits the current change set using the given message with current issue.
git-commit:: git-commit-feat
$(addprefix git-commit-,$(COMMIT_CONVENTION)):: git-commit-%:
@MESSAGE="$(ARGS)"; $(call git-message,$*,0); \
ARGS="" $(GIT) commit --signoff --message "$${MESSAGE}"; exit 0;
#@ [(no-)edit|(no-)verify] # pushes the latest changes to the previous commit of the origin.
TARGETS_GIT_FIX := git-fix git-fix-all git-fix-edit git-fix-no-edit git-fix-verify git-fix-no-verify
$(TARGETS_GIT_FIX):: git-fix%:
@ARGS=" $(ARGS) $(*:-%=%) "; \
if [[ "$${ARGS}" =~ " all " ]]; then git add .; fi; \
GITFIX=($(patsubst %,"%",$(GITFIX))); GITPUSH=($(patsubst %,"%",$(GITPUSH))); \
if [[ "$${ARGS}" =~ " verify " ]]; then GITFIX=("$${GITFIX[@]/--no-verify}"); fi; \
if [[ "$${ARGS}" =~ " no-verify " ]]; then GITFIX+=("--no-verify"); fi; \
if [[ "$${ARGS}" =~ " edit " ]]; then GITFIX=("$${GITFIX[@]/--no-edit}"); fi; \
if [[ "$${ARGS}" =~ " no-edit " ]]; then GITFIX+=("--no-edit"); fi; \
for KEY in "$${!GITFIX[@]}"; do \
if [ "$${GITFIX[$${KEY}]}" == "" ]; then unset GITFIX[$${KEY}]; fi; \
done; \
echo "$${GITFIX[@]} && $${GITPUSH[@]}"; \
ARGS="" "$${GITFIX[@]}" && $${GITPUSH[@]}; exit 0;
#@ pushes the current branch to the same branch of the origin.
git-push::
@$(GIT) push --set-upstream origin \
$$($(GIT) branch 2> /dev/null | sed -e '/^[^*]/d; s/* \(.*\)/\1/');
## Init: targets to initialize tools and (re-)sources.
#@ initialize the project to prepare it for building.
init:: $(TARGETS_INIT)
#@ initialize the go module and package dependencies.
init-go:: go.sum
go.sum: go.mod
@$(call emsg,info,updating [go.sum]);
$(if $(SOURCES),$(GO) mod tidy -x -v -e -compat=$(GOVERSION) && \
touch go.sum && $(GO) mod download;)
go.mod:
@$(call emsg,info,updating [go.mod]);
$(if $(SOURCES),$(GO) mod init "$(GITURL)";)
#@ initialize git pre-commit and commit-message hooks.
init-hooks:: $(GITHOOKS:%=.git/hooks/%)
git-hooks-commit-msg = echo -ne '\#!/bin/sh\n\n\
command -v $(GOBIN)/go-make >/dev/null || \\\n\
GOBIN=$(GOBIN) $(GO) install $(INSTALL_FLAGS) $(GOMAKE_DEP) && \\\n\
$(GOBIN)/go-make git-verify message $${1};\n' | sed 's/^ *//g'
git-hooks-pre-commit = echo -ne '\#!/bin/sh\n\n\
command -v $(GOBIN)/go-make >/dev/null || \\\n\
GOBIN=$(GOBIN) $(GO) install $(INSTALL_FLAGS) $(GOMAKE_DEP) && \\\n\
$(GOBIN)/go-make commit;\n' | sed 's/^ *//g'
.git/hooks/commit-msg: $(MAKEFILE_LIST)
@if ! diff <(cat $@ 2>/dev/null) <($(git-hooks-commit-msg)) >/dev/null; then \
$(git-hooks-commit-msg) >$@; chmod 755 $@; \
fi;
.git/hooks/pre-commit: $(MAKEFILE_LIST)
@if ! diff <(cat $@ 2>/dev/null) <($(git-hooks-pre-commit)) >/dev/null; then \
$(git-hooks-pre-commit) >$@; chmod 755 $@; \
fi;
#@ initialize the generated sources.
init-code:: init-hooks $(MOCKS_TARGETS) gen-kube-apis
$(MOCKS_TARGETS): go.sum $(MOCKS_SOURCES) install-mockgen install-mock
@MOCK=($(subst |,$(SPACE),$(firstword $(filter $@|%,$(MOCKS))))); \
TARGET="$${MOCK[0]}"; SOURCE="$${MOCK[1]}"; \
PACKAGE="$${MOCK[2]}"; ORIGIN="$${MOCK[3]}"; \
if [ ! -f "$${TARGET}" ]; then \
echo "$(GO) generate \"$${ORIGIN}\""; $(GO) generate "$${ORIGIN}"; \
elif [ "$${SOURCE}" != "_" -a "$${TARGET}" -ot "$${SOURCE}" ]; then \
echo "$(GO) generate \"$${ORIGIN}\""; $(GO) generate "$${ORIGIN}"; \
elif [ "$${PACKAGE}" != "_" -a "$${TARGET}" -ot "go.sum" ]; then \
echo "$(GO) generate \"$${ORIGIN}\""; $(GO) generate "$${ORIGIN}"; \
fi; \
#@ initialize the codacy support - if enabled.
init-codacy:: $(TARGETS_INIT_CODACY)
$(TARGETS_INIT_CODACY):: init-%:
@VERSION="$(CODACY_$(call upper,$*)_VERSION)"; \
FILE="$(GOBIN)/codacy-$*-$${VERSION}"; \
if [ ! -f $${FILE} ]; then \
BASE="https://github.com/codacy/codacy-$*/releases/download"; \
echo curl --silent --location --output $${FILE} \
$${BASE}/$${VERSION}/codacy-$*-$${VERSION}; \
curl --silent --location --output $${FILE} \
$${BASE}/$${VERSION}/codacy-$*-$${VERSION}; \
chmod 700 $${FILE}; \
fi; \
#@ <version> # initialize project by copying config files from template.
init-make:: $(TARGETS_INIT_MAKE)
$(TARGETS_INIT_ALL):: init/%:
@DIR="$${PWD}"; FILE="$*"; #FILE="$${FILE##$${DIR}/}"; \
if [ ! -f "$${DIR}/$${FILE}" -o "$(ARGS)" == "current" ]; then \
echo "install: $${FILE} (installed)"; \
cp "$(GOMAKE_CONFIG)/$${FILE}" "$${DIR}/$${FILE}"; \
else \
$(call emsg,warning,install: $${FILE} (blocked)); \
fi; \
#@ <version> # initialize project by copying Makefile.base from template.
init-make!::
cp "$(GOMAKE_CONFIG)/Makefile.base" "Makefile"; \
## Generate: targets to generate code and documentation.
#@ generate kubernetes api clients and manifests.
gen-kube-apis:: $(GEN_KUBE_APIS)
$(GEN_KUBE_APIS):: gen-kube-apis/%: $(GEN_KUBE_TARGETS)
@HEADER="$(FILE_GO_HEADER)"; BPKG="$*"; BPKG="$${BPKG%/apis}"; \
if [ -d "$${BPKG}/clients" ] && [ -d "$${BPKG}/manifests" ] && \
[ -z "$$(find $${BPKG}/{apis,clients,manifests} -type f \
! -name $(FILE_DEEPCOPY) -printf '%T@ %p\n' 2>/dev/null | \
sort -rn | head -1 | grep "$${BPKG}/apis/")" ]; then \
$(call emsg,info,generate apis skipped [$${BPKG}]); exit 0; \
else $(call emsg,info,generate apis [$${BPKG}]); fi; \
\
go get k8s.io/api/core/v1; \
for APKG in $$(find $${BPKG} -path "*/apis/*/*" -type d); do \
MANDIR="$${BPKG}/manifests"; \
APIPKG="$(GITURL)/$${APKG}"; APIBPKG="$${APIPKG%/*/*}"; \
CLIDIR="$${BPKG}/clients"; CLIPKG="$(GITURL)/$${CLIDIR}"; \
$(call emsg,info,generate manifests [$${APKG}]); \
$(GOBIN)/controller-gen crd:crdVersions=v1 paths=./$${BPKG}/apis/... \
"output:crd:dir=$${MANDIR}"; \
$(call emsg,info,generate deepcopy [$${APKG}]); \
$(GOBIN)/deepcopy-gen --go-header-file "$${HEADER}" \
--output-file $(FILE_DEEPCOPY) --bounding-dirs "$${APIBPKG}" \
"$${APIPKG}"; \
$(call emsg,info,generate clients [$${APKG}:clientsets]); \
$(GOBIN)/client-gen --go-header-file "$${HEADER}" \
--clientset-name versioned --input-base "" --input "$${APIPKG}" \
--output-pkg "$${CLIPKG}/clientset" --output-dir "$${CLIDIR}/clientset"; \
$(call emsg,info,generate clients [$${APKG}:listeners]); \
$(GOBIN)/lister-gen --go-header-file "$${HEADER}" \
--output-pkg "$${CLIPKG}/listers" --output-dir "$${CLIDIR}/listers" \
"$${APIPKG}"; \
$(call emsg,info,generate clients [$${APKG}:informers]); \
$(GOBIN)/informer-gen --go-header-file "$${HEADER}" \
--versioned-clientset-package "$${CLIPKG}/clientset/versioned" \
--output-pkg "$${CLIPKG}/informers" --output-dir "$${CLIDIR}/informers" \
--listers-package "$${CLIPKG}/listers" "$${APIPKG}"; \
done; \
$(call emsg,info,update packages [$${BPKG}]); \
$(GO) get -u ./$${BPKG}/clients/...; \
## Test: targets to test the source code (and compiler environment).
TEST_ALL := $(DIR_BUILD)/test-all.cover
TEST_UNIT := $(DIR_BUILD)/test-unit.cover
TEST_BENCH := $(DIR_BUILD)/test-bench.cover
#@ execute default test set.
test:: $(TARGETS_TEST)
#@ <pkg>[/<test>] # execute all tests.
test-all:: $(TARGETS_TEST_INIT) $(TEST_ALL)
#@ <pkg>[/<test>] # execute only unit tests.
test-unit:: $(TARGETS_TEST_INIT) $(TEST_UNIT)
#@ <pkg>[/<test>] # execute benchmarks.
test-bench:: $(TARGETS_TEST_INIT) $(TEST_BENCH)
# remove all coverage files of test and benchmarks.
test-clean::
@rm --verbose --force $(TEST_ALL) $(TEST_UNIT) $(TEST_BENCH);
#@ start the test coverage report.
test-cover::
@FILE=$$(ls -Art "$(TEST_ALL)" "$(TEST_UNIT)" \
"$(TEST_BENCH)" 2>/dev/null); \
$(GO) tool cover -o "$${FILE}.html" -html="$${FILE}"; \
if [ $(ARGS) == "white" ]; then \
sed -i 's/black/whitesmoke/g' "$${FILE}.html"; \
fi; \
sensible-browser "$${FILE}.html";
# $(GO) tool cover -html="$${FILE}"; \
#@ upload the test coverage report to codacy.
test-upload::
@FILE=$$(ls -Art "$(TEST_ALL)" "$(TEST_UNIT)" \
"$(TEST_BENCH)" 2>/dev/null); COMMIT="$$($(GITHASH))"; \
SCRIPT="https://coverage.codacy.com/get.sh"; \
if [ -n "$(CODACY_PROJECT_TOKEN)" ]; then \
bash <(curl --silent --location $${SCRIPT}) report \
--project-token $(CODACY_PROJECT_TOKEN) --commit-uuid $${COMMIT} \
--codacy-api-base-url $(CODACY_API_BASE_URL) --language go \
--force-coverage-parser go --coverage-reports "$${FILE}"; \
elif [ -n "$(CODACY_API_TOKEN)" ]; then \
bash <(curl --silent --location $${SCRIPT}) report \
--organization-provider $(CODACY_PROVIDER) \
--username $(CODACY_USER) --project-name $(CODACY_PROJECT) \
--api-token $(CODACY_API_TOKEN) --commit-uuid $${COMMIT} \
--codacy-api-base-url $(CODACY_API_BASE_URL) --language go \
--force-coverage-parser go --coverage-reports "$${FILE}"; \
fi; \
#@ test whether project creates conflicting program names.
test-build::
@echo -e "$(COMMANDS_PKG)" | awk 'BEGIN{ RS="\\s"; FS="="; \
fmt = "$(call cmsg,error,program conflict) [%s && %s => %s]\n" \
} { if ($$1 in cmds) { failures++; \
printf(fmt, cmds[$$1], $$2 "/main.go", $$1 "/main.go") \