forked from topolvm/topolvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
303 lines (243 loc) · 11.1 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
include versions.mk
SUDO := sudo
CURL := curl -sSLf
BINDIR := $(shell pwd)/bin
CONTROLLER_GEN := $(BINDIR)/controller-gen
CONTAINER_STRUCTURE_TEST := $(BINDIR)/container-structure-test
GOLANGCI_LINT = $(BINDIR)/golangci-lint
PROTOC := PATH=$(BINDIR):$(PATH) $(BINDIR)/protoc -I=$(shell pwd)/include:.
PACKAGES := unzip lvm2 xfsprogs thin-provisioning-tools patch
ENVTEST_ASSETS_DIR := $(shell pwd)/testbin
GO_FILES=$(shell find -name '*.go' -not -name '*_test.go')
GOOS := $(shell go env GOOS)
GOARCH := $(shell go env GOARCH)
GOFLAGS =
export GOFLAGS
BUILD_TARGET=hypertopolvm
TOPOLVM_VERSION ?= devel
IMAGE_TAG ?= latest
ORIGINAL_IMAGE_TAG ?=
STRUCTURE_TEST_TARGET ?= normal
PUSH ?= false
BUILDX_PUSH_OPTIONS := "-o type=tar,dest=build/topolvm.tar"
ifeq ($(PUSH),true)
BUILDX_PUSH_OPTIONS := --push
endif
# Set the shell used to bash for better error handling.
SHELL = /bin/bash
.SHELLFLAGS = -e -o pipefail -c
define CRD_TEMPLATE
{{ if not .Values.useLegacy }}
%s
{{ end }}
endef
export CRD_TEMPLATE
define LEGACY_CRD_TEMPLATE
{{ if .Values.useLegacy }}
%s
{{ end }}
endef
export LEGACY_CRD_TEMPLATE
CONTAINER_STRUCTURE_TEST_IMAGE=$(IMAGE_PREFIX)topolvm:devel
ifeq ($(STRUCTURE_TEST_TARGET),with-sidecar)
CONTAINER_STRUCTURE_TEST_IMAGE=$(IMAGE_PREFIX)topolvm-with-sidecar:devel
endif
##@ 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
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
pkg/lvmd/proto/lvmd.pb.go: pkg/lvmd/proto/lvmd.proto
$(PROTOC) --go_out=module=github.com/topolvm/topolvm:. $<
pkg/lvmd/proto/lvmd_grpc.pb.go: pkg/lvmd/proto/lvmd.proto
$(PROTOC) --go-grpc_out=module=github.com/topolvm/topolvm:. $<
docs/lvmd-protocol.md: pkg/lvmd/proto/lvmd.proto
$(PROTOC) --doc_out=./docs --doc_opt=markdown,$@ $<
PROTOBUF_GEN = pkg/lvmd/proto/lvmd.pb.go pkg/lvmd/proto/lvmd_grpc.pb.go docs/lvmd-protocol.md
.PHONY: manifests
manifests: generate-legacy-api ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) \
crd:crdVersions=v1 \
rbac:roleName=topolvm-controller \
webhook \
paths="./api/...;./internal/...;./cmd/..." \
output:crd:artifacts:config=config/crd/bases
cat config/crd/bases/topolvm.io_logicalvolumes.yaml | xargs -d" " printf "$$CRD_TEMPLATE" > charts/topolvm/templates/crds/topolvm.io_logicalvolumes.yaml
cat config/crd/bases/topolvm.cybozu.com_logicalvolumes.yaml | xargs -d" " printf "$$LEGACY_CRD_TEMPLATE" > charts/topolvm/templates/crds/topolvm.cybozu.com_logicalvolumes.yaml
.PHONY: generate-api ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
generate-api:
$(CONTROLLER_GEN) object:headerFile="./hack/boilerplate.go.txt" paths="./api/..."
.PHONY: generate-legacy-api
generate-legacy-api: ## Generate legacy api code.
mkdir -p api/legacy/v1
cp -r api/v1/* api/legacy/v1
sed -i -e 's/topolvm.io/topolvm.cybozu.com/g' api/legacy/v1/groupversion_info.go
.PHONY: generate-helm-docs
generate-helm-docs:
./bin/helm-docs -c charts/topolvm/
.PHONY: generate
generate: manifests generate-api generate-helm-docs
# The generated files depend not only on the proto file but also on the protoc command.
# So we need to force the generation of the files every time.
$(MAKE) -B $(PROTOBUF_GEN)
.PHONY: check-uncommitted
check-uncommitted: generate ## Check if latest generated artifacts are committed.
git diff --exit-code --name-only
.PHONY: lint
lint: ## Run lint
test -z "$$(gofmt -s -l . | grep -vE '^vendor|^api/v1/zz_generated.deepcopy.go' | tee /dev/stderr)"
$(GOLANGCI_LINT) run
go vet ./...
test -z "$$(go vet ./... | grep -v '^vendor' | tee /dev/stderr)"
.PHONY: lint-fix
lint-fix: ## Run golangci-lint linter and perform fixes
$(GOLANGCI_LINT) run --fix
.PHONY: test
test: lint ## Run lint and unit tests.
go install ./...
mkdir -p $(ENVTEST_ASSETS_DIR)
source <($(BINDIR)/setup-envtest use $(ENVTEST_KUBERNETES_VERSION) --bin-dir=$(ENVTEST_ASSETS_DIR) -p env); GOLANG_PROTOBUF_REGISTRATION_CONFLICT=warn go test -count=1 -race -v --timeout=120s ./...
groupname-test: ## Run unit tests that depends on the groupname.
go install ./...
mkdir -p $(ENVTEST_ASSETS_DIR)
source <($(BINDIR)/setup-envtest use $(ENVTEST_KUBERNETES_VERSION) --bin-dir=$(ENVTEST_ASSETS_DIR) -p env); GOLANG_PROTOBUF_REGISTRATION_CONFLICT=warn TEST_LEGACY=true go test -count=1 -race -v --timeout=60s ./internal/client/*
TEST_LEGACY=true go test -count=1 -race -v --timeout=60s ./constants*.go
.PHONY: clean
clean: ## Clean built files on the working directory.
rm -rf build/
.PHONY: distclean
distclean: clean ## Clean all on the working directory.
rm -rf bin/
rm -rf include/
rm -rf testbin/
rm -f $(HOME)/.docker/cli-plugins/docker-buildx
docker run --privileged --rm tonistiigi/binfmt --uninstall linux/amd64,linux/arm64/v8,linux/ppc64le
##@ Build
.PHONY: build
build: build-topolvm csi-sidecars ## Build binaries.
.PHONY: build-topolvm
build-topolvm: build/hypertopolvm build/lvmd
build/hypertopolvm: $(GO_FILES)
mkdir -p build
GOARCH=$(GOARCH) go build -o $@ -ldflags "-w -s -X github.com/topolvm/topolvm.Version=$(TOPOLVM_VERSION)" ./cmd/hypertopolvm
build/lvmd: $(GO_FILES)
mkdir -p build
GOARCH=$(GOARCH) CGO_ENABLED=0 go build -o $@ -ldflags "-w -s -X github.com/topolvm/topolvm.Version=$(TOPOLVM_VERSION)" ./cmd/lvmd
.PHONY: csi-sidecars
csi-sidecars: ## Build sidecar binaries.
mkdir -p build
make -f csi-sidecars.mk OUTPUT_DIR=build BUILD_PLATFORMS="linux $(GOARCH)"
.PHONY: image
image: image-normal image-with-sidecar ## Build topolvm images.
.PHONY: image-normal
image-normal:
docker buildx build --no-cache --load -t $(IMAGE_PREFIX)topolvm:devel --build-arg TOPOLVM_VERSION=$(TOPOLVM_VERSION) --target topolvm .
.PHONY: image-with-sidecar
image-with-sidecar:
docker buildx build --no-cache --load -t $(IMAGE_PREFIX)topolvm-with-sidecar:devel --build-arg TOPOLVM_VERSION=$(TOPOLVM_VERSION) --target topolvm-with-sidecar .
.PHONY: container-structure-test
container-structure-test: ## Run container-structure-test.
$(CONTAINER_STRUCTURE_TEST) test \
--image $(CONTAINER_STRUCTURE_TEST_IMAGE) \
--config container-structure-test.yaml
ifeq ($(STRUCTURE_TEST_TARGET),with-sidecar)
$(CONTAINER_STRUCTURE_TEST) test \
--image $(CONTAINER_STRUCTURE_TEST_IMAGE) \
--config container-structure-test-with-sidecar.yaml
endif
.PHONY: create-docker-container
create-docker-container: ## Create docker-container.
docker buildx create --use
.PHONY: multi-platform-images
multi-platform-images: multi-platform-image-normal multi-platform-image-with-sidecar ## Build or push multi-platform topolvm images.
.PHONY: multi-platform-image-normal
multi-platform-image-normal:
mkdir -p build
docker buildx build --no-cache $(BUILDX_PUSH_OPTIONS) \
--platform linux/amd64,linux/arm64/v8,linux/ppc64le \
-t $(IMAGE_PREFIX)topolvm:$(IMAGE_TAG) \
--build-arg TOPOLVM_VERSION=$(TOPOLVM_VERSION) \
--target topolvm \
.
.PHONY: multi-platform-image-with-sidecar
multi-platform-image-with-sidecar:
mkdir -p build
docker buildx build --no-cache $(BUILDX_PUSH_OPTIONS) \
--platform linux/amd64,linux/arm64/v8,linux/ppc64le \
-t $(IMAGE_PREFIX)topolvm-with-sidecar:$(IMAGE_TAG) \
--build-arg TOPOLVM_VERSION=$(TOPOLVM_VERSION) \
--target topolvm-with-sidecar \
.
.PHONY: tag
tag: ## Tag topolvm images.
docker buildx imagetools create \
--tag $(IMAGE_PREFIX)topolvm:$(IMAGE_TAG) \
$(IMAGE_PREFIX)topolvm:$(ORIGINAL_IMAGE_TAG)
docker buildx imagetools create \
--tag $(IMAGE_PREFIX)topolvm-with-sidecar:$(IMAGE_TAG) \
$(IMAGE_PREFIX)topolvm-with-sidecar:$(ORIGINAL_IMAGE_TAG)
##@ Chart Testing
.PHONY: ct-lint
ct-lint: ## Lint and validate a chart.
docker run \
--rm \
--user $(shell id -u $(USER)) \
--workdir /data \
--volume $(shell pwd):/data \
quay.io/helmpack/chart-testing:v$(CHART_TESTING_VERSION) \
ct lint --config ct.yaml
##@ Setup
$(BINDIR):
mkdir -p $@
.PHONY: install-kind
install-kind: | $(BINDIR)
GOBIN=$(BINDIR) go install sigs.k8s.io/kind@$(KIND_VERSION)
.PHONY: install-container-structure-test
install-container-structure-test: | $(BINDIR)
$(CURL) -o $(CONTAINER_STRUCTURE_TEST) \
https://github.com/GoogleContainerTools/container-structure-test/releases/download/v$(CONTAINER_STRUCTURE_TEST_VERSION)/container-structure-test-linux-amd64 \
&& chmod +x $(CONTAINER_STRUCTURE_TEST)
.PHONY: install-helm
install-helm: | $(BINDIR)
$(CURL) https://get.helm.sh/helm-v$(HELM_VERSION)-linux-amd64.tar.gz \
| tar xvz -C $(BINDIR) --strip-components 1 linux-amd64/helm
.PHONY: install-helm-docs
install-helm-docs: | $(BINDIR)
GOBIN=$(BINDIR) go install github.com/norwoodj/helm-docs/cmd/helm-docs@v$(HELM_DOCS_VERSION)
.PHONY: tools
tools: install-kind install-container-structure-test install-helm install-helm-docs | $(BINDIR) ## Install development tools.
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell dirname $(GOLANGCI_LINT)) $(GOLANGCI_LINT_VERSION)
GOBIN=$(BINDIR) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@$(ENVTEST_BRANCH)
GOBIN=$(BINDIR) go install sigs.k8s.io/controller-tools/cmd/controller-gen@v$(CONTROLLER_TOOLS_VERSION)
$(CURL) -o protoc.zip https://github.com/protocolbuffers/protobuf/releases/download/v$(PROTOC_VERSION)/protoc-$(PROTOC_VERSION)-linux-x86_64.zip
unzip -o protoc.zip bin/protoc 'include/*'
rm -f protoc.zip
GOBIN=$(BINDIR) go install google.golang.org/protobuf/cmd/protoc-gen-go@v$(PROTOC_GEN_GO_VERSION)
GOBIN=$(BINDIR) go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v$(PROTOC_GEN_GO_GRPC_VERSION)
GOBIN=$(BINDIR) go install github.com/pseudomuto/protoc-gen-doc/cmd/protoc-gen-doc@v$(PROTOC_GEN_DOC_VERSION)
.PHONY: setup
setup: ## Setup local environment.
$(SUDO) apt-get update
$(SUDO) apt-get -y install --no-install-recommends $(PACKAGES)
$(MAKE) tools
$(MAKE) setup-docker-buildx
.PHONY: setup-docker-buildx
setup-docker-buildx: ## Setup docker buildx environment.
$(MAKE) $(HOME)/.docker/cli-plugins/docker-buildx
# https://github.com/tonistiigi/binfmt
docker run --privileged --rm tonistiigi/binfmt --install linux/amd64,linux/arm64/v8,linux/ppc64le
# https://docs.docker.com/build/buildx/install/
$(HOME)/.docker/cli-plugins/docker-buildx:
mkdir -p $(HOME)/.docker/cli-plugins
$(CURL) -o $@ \
https://github.com/docker/buildx/releases/download/v$(BUILDX_VERSION)/buildx-v$(BUILDX_VERSION).$(GOOS)-$(GOARCH) \
&& chmod +x $@