generated from cybozu-go/neco-template
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Makefile
230 lines (189 loc) · 8.66 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
# Tool versions
CTRL_TOOLS_VERSION=0.16.2
CTRL_RUNTIME_VERSION := $(shell awk '/sigs.k8s.io\/controller-runtime/ {print substr($$2, 2)}' go.mod)
KUSTOMIZE_VERSION = 5.4.3
HELM_VERSION = 3.15.4
CRD_TO_MARKDOWN_VERSION = 0.0.3
MYSQLSH_VERSION = 8.4.3-1
MDBOOK_VERSION = 0.4.40
GORELEASER_VERSION = 1.26.2
YQ_VERSION = 4.44.3
OS_VERSION := $(shell . /etc/os-release; echo $$VERSION_ID)
# Test tools
BIN_DIR := $(shell pwd)/bin
STATICCHECK := $(BIN_DIR)/staticcheck
NILERR := $(BIN_DIR)/nilerr
SUDO = sudo
PKG_LIST := zstd python3 libpython3.8
ifneq ($(CI),true)
# Don't install the mysql packages in GitHub Actions.
# refs: https://github.com/actions/virtual-environments/pull/4674
PKG_LIST := $(PKG_LIST) mysql-client mysql-server-core-8.0
endif
# Set the shell used to bash for better error handling.
SHELL = /bin/bash
.SHELLFLAGS = -e -o pipefail -c
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS = "crd:crdVersions=v1,maxDescLen=50"
# for Go
GOOS = $(shell go env GOOS)
GOARCH = $(shell go env GOARCH)
SUFFIX =
.PHONY: all
all: build
##@ General
# The help target prints out all targets with their descriptions organized
# beneath their categories. The categories are represented by '##@' and the
# target descriptions by '##'. The awk commands is responsible for reading the
# entire set of makefiles included in this invocation, looking for lines of the
# file as xyz: ## something, and then pretty-format the target and help. Then,
# if there's a line with ##@ something, that gets pretty-printed as a category.
# More info on the usage of ANSI control characters for terminal formatting:
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
# More info on the awk command:
# http://linuxcommand.org/lc3_adv_awk.php
.PHONY: help
help: ## Display this help.
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
##@ Development
.PHONY: manifests
manifests: controller-gen kustomize yq ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
rm -rf charts/moco/templates/generated/
mkdir -p charts/moco/templates/generated/crds/
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
$(KUSTOMIZE) build config/crd -o config/crd/tests # Outputs static CRDs for use with Envtest.
$(KUSTOMIZE) build config/kustomize-to-helm/overlays/templates | $(YQ) e ". | del(select(.kind==\"ValidatingAdmissionPolicy\" or .kind==\"ValidatingAdmissionPolicyBinding\").metadata.namespace)" - > charts/moco/templates/generated/generated.yaml # Manually remove namespaces because the API version supported by kustomize is out of date.
echo '{{- if .Values.crds.enabled }}' > charts/moco/templates/generated/crds/moco_crds.yaml
$(KUSTOMIZE) build config/kustomize-to-helm/overlays/crds | $(YQ) e "." - >> charts/moco/templates/generated/crds/moco_crds.yaml
echo '{{- end }}' >> charts/moco/templates/generated/crds/moco_crds.yaml
.PHONY: generate
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
.PHONY: apidoc
apidoc: crd-to-markdown $(wildcard api/*/*_types.go)
$(CRD_TO_MARKDOWN) --links docs/links.csv -f api/v1beta2/mysqlcluster_types.go -f api/v1beta2/job_types.go -n MySQLCluster > docs/crd_mysqlcluster_v1beta2.md
$(CRD_TO_MARKDOWN) --links docs/links.csv -f api/v1beta2/backuppolicy_types.go -f api/v1beta2/job_types.go -n BackupPolicy > docs/crd_backuppolicy_v1beta2.md
.PHONY: book
book: mdbook
rm -rf docs/book
cd docs; $(MDBOOK) build
.PHONY: check-generate
check-generate:
$(MAKE) manifests generate apidoc
go mod tidy
git diff --exit-code --name-only
.PHONY: envtest
envtest: setup-envtest
source <($(SETUP_ENVTEST) use -p env); \
export MOCO_CHECK_INTERVAL=100ms; \
export MOCO_CLONE_WAIT_DURATION=100ms; \
go test -v -count 1 -race ./clustering -ginkgo.randomize-all -ginkgo.v -ginkgo.fail-fast
source <($(SETUP_ENVTEST) use -p env); \
export DEBUG_CONTROLLER=1; \
go test -v -count 1 -race ./controllers -ginkgo.randomize-all -ginkgo.v -ginkgo.fail-fast
source <($(SETUP_ENVTEST) use -p env); \
go test -v -count 1 -race ./api/... -ginkgo.randomize-all -ginkgo.v
source <($(SETUP_ENVTEST) use -p env); \
go test -v -count 1 -race ./backup -ginkgo.randomize-all -ginkgo.v -ginkgo.fail-fast
.PHONY: test-dbop
test-dbop:
-docker network create test-moco
TEST_MYSQL=1 MYSQL_VERSION=$(MYSQL_VERSION) go test -v -count 1 -race ./pkg/dbop -ginkgo.v -ginkgo.randomize-all
.PHONY: test-bkop
test-bkop:
@if which mysqlsh; then : ; else echo 'Run "make setup" to prepare test tools.'; exit 1; fi
-docker network create test-moco
TEST_MYSQL=1 MYSQL_VERSION=$(MYSQL_VERSION) go test -v -count 1 -race ./pkg/bkop -ginkgo.v -ginkgo.randomize-all
.PHONY: test
test: test-tools
go test -v -count 1 -race ./pkg/...
go install ./...
go vet ./...
test -z $$(gofmt -s -l . | tee /dev/stderr)
$(STATICCHECK) ./...
# Disabled temporary due to a false positive with nilerr 0.1.1 built with Go 1.17
# https://github.com/cybozu-go/moco/runs/4221024784?check_suite_focus=true
# $(NILERR) ./...
##@ Build
.PHONY: build
build:
mkdir -p bin
GOBIN=$(shell pwd)/bin go install ./cmd/...
.PHONY: release-build
release-build: goreleaser
$(GORELEASER) build --snapshot --clean
.PHONY: release-manifests-build
release-manifests-build: kustomize
rm -rf build
mkdir -p build
$(KUSTOMIZE) build . > build/moco.yaml
##@ Tools
CONTROLLER_GEN := $(shell pwd)/bin/controller-gen
controller-gen: ## Download controller-gen locally if necessary.
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen@v$(CTRL_TOOLS_VERSION))
SETUP_ENVTEST := $(shell pwd)/bin/setup-envtest
.PHONY: setup-envtest
setup-envtest: ## Download setup-envtest locally if necessary
# see https://github.com/kubernetes-sigs/controller-runtime/tree/master/tools/setup-envtest
GOBIN=$(shell pwd)/bin go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
KUSTOMIZE := $(shell pwd)/bin/kustomize
.PHONY: kustomize
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
$(KUSTOMIZE):
mkdir -p bin
curl -fsL https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize%2Fv$(KUSTOMIZE_VERSION)/kustomize_v$(KUSTOMIZE_VERSION)_linux_amd64.tar.gz | \
tar -C bin -xzf -
HELM := $(shell pwd)/bin/helm
.PHONY: helm
helm: $(HELM) ## Download helm locally if necessary.
$(HELM):
mkdir -p $(BIN_DIR)
curl -L -sS https://get.helm.sh/helm-v$(HELM_VERSION)-linux-amd64.tar.gz \
| tar xz -C $(BIN_DIR) --strip-components 1 linux-amd64/helm
CRD_TO_MARKDOWN := $(shell pwd)/bin/crd-to-markdown
.PHONY: crd-to-markdown
crd-to-markdown: ## Download crd-to-markdown locally if necessary.
$(call go-get-tool,$(CRD_TO_MARKDOWN),github.com/clamoriniere/crd-to-markdown@v$(CRD_TO_MARKDOWN_VERSION))
MDBOOK := $(shell pwd)/bin/mdbook
.PHONY: mdbook
mdbook: ## Donwload mdbook locally if necessary
mkdir -p bin
curl -fsL https://github.com/rust-lang/mdBook/releases/download/v$(MDBOOK_VERSION)/mdbook-v$(MDBOOK_VERSION)-x86_64-unknown-linux-gnu.tar.gz | tar -C bin -xzf -
GORELEASER := $(shell pwd)/bin/goreleaser
.PHONY: goreleaser
goreleaser: $(GORELEASER) ## Download goreleaser locally if necessary.
$(GORELEASER):
mkdir -p $(BIN_DIR)
curl -L -sS https://github.com/goreleaser/goreleaser/releases/download/v$(GORELEASER_VERSION)/goreleaser_Linux_x86_64.tar.gz \
| tar xz -C $(BIN_DIR) goreleaser
YQ := $(shell pwd)/bin/yq_linux_amd64
.PHONY: yq
yq: $(YQ) ## Download yq locally if necessary.
$(YQ):
mkdir -p $(BIN_DIR)
curl -L -sS https://github.com/mikefarah/yq/releases/download/v$(YQ_VERSION)/yq_linux_amd64.tar.gz \
| tar -C $(BIN_DIR) -xzf -
# go-get-tool will 'go get' any package $2 and install it to $1.
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
define go-get-tool
@[ -f $(1) ] || { \
set -e ;\
echo "Downloading $(2)" ;\
GOBIN=$(PROJECT_DIR)/bin go install $(2) ;\
}
endef
.PHONY: test-tools
test-tools: $(STATICCHECK) $(NILERR)
$(STATICCHECK):
mkdir -p $(BIN_DIR)
GOBIN=$(BIN_DIR) go install honnef.co/go/tools/cmd/staticcheck@latest
$(NILERR):
mkdir -p $(BIN_DIR)
GOBIN=$(BIN_DIR) go install github.com/gostaticanalysis/nilerr/cmd/nilerr@latest
.PHONY: setup
setup:
$(SUDO) apt-get update
$(SUDO) apt-get install -y --no-install-recommends $(PKG_LIST)
curl -o /tmp/mysqlsh.deb -fsL https://dev.mysql.com/get/Downloads/MySQL-Shell/mysql-shell_$(MYSQLSH_VERSION)ubuntu$(OS_VERSION)_amd64.deb
$(SUDO) dpkg -i /tmp/mysqlsh.deb
rm -f /tmp/mysqlsh.deb