forked from topolvm/topolvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
267 lines (214 loc) · 10.2 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
## Dependency versions
CONTROLLER_RUNTIME_VERSION=$(shell awk '/sigs\.k8s\.io\/controller-runtime/ {print substr($$2, 2)}' go.mod)
CONTROLLER_TOOLS_VERSION=$(shell awk '/sigs\.k8s\.io\/controller-tools/ {print substr($$2, 2)}' go.mod)
CSI_VERSION=1.6.0
PROTOC_VERSION=21.2
KIND_VERSION=v0.14.0
HELM_VERSION=3.9.0
HELM_DOCS_VERSION=1.11.0
YQ_VERSION=4.18.1
BUILDX_VERSION=0.9.1
GINKGO_VERSION := $(shell awk '/github.com\/onsi\/ginkgo/ {print substr($$2, 2)}' go.mod)
SUDO := sudo
CURL := curl -sSLf
BINDIR := $(shell pwd)/bin
CONTROLLER_GEN := $(BINDIR)/controller-gen
STATICCHECK := $(BINDIR)/staticcheck
PROTOC := PATH=$(BINDIR):$(PATH) $(BINDIR)/protoc -I=$(shell pwd)/include:.
PACKAGES := unzip lvm2 xfsprogs thin-provisioning-tools
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 ?=
ENVTEST_KUBERNETES_VERSION=1.24
PROTOC_GEN_GO_VERSION := $(shell awk '/google.golang.org\/protobuf/ {print substr($$2, 2)}' go.mod)
PROTOC_GEN_DOC_VERSION := $(shell awk '/github.com\/pseudomuto\/protoc-gen-doc/ {print substr($$2, 2)}' go.mod)
PROTOC_GEN_GO_GRPC_VERSION := $(shell awk '/google.golang.org\/grpc\/cmd\/protoc-gen-go-grpc/ {print substr($$2, 2)}' go.mod)
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
##@ 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
csi.proto:
$(CURL) -o $@ https://raw.githubusercontent.com/container-storage-interface/spec/v$(CSI_VERSION)/csi.proto
sed -i 's,^option go_package.*$$,option go_package = "github.com/topolvm/topolvm/csi";,' csi.proto
sed -i '/^\/\/ Code generated by make;.*$$/d' csi.proto
csi/csi.pb.go: csi.proto
mkdir -p csi
$(PROTOC) --go_out=module=github.com/topolvm/topolvm:. $<
csi/csi_grpc.pb.go: csi.proto
mkdir -p csi
$(PROTOC) --go-grpc_out=module=github.com/topolvm/topolvm:. $<
lvmd/proto/lvmd.pb.go: lvmd/proto/lvmd.proto
$(PROTOC) --go_out=module=github.com/topolvm/topolvm:. $<
lvmd/proto/lvmd_grpc.pb.go: lvmd/proto/lvmd.proto
$(PROTOC) --go-grpc_out=module=github.com/topolvm/topolvm:. $<
docs/lvmd-protocol.md: lvmd/proto/lvmd.proto
$(PROTOC) --doc_out=./docs --doc_opt=markdown,$@ $<
PROTOBUF_GEN = csi/csi.pb.go csi/csi_grpc.pb.go \
lvmd/proto/lvmd.pb.go 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/...;./controllers;./hook;./driver/k8s;./pkg/..." \
output:crd:artifacts:config=config/crd/bases
$(BINDIR)/yq eval 'del(.status)' config/crd/bases/topolvm.io_logicalvolumes.yaml | xargs -d" " printf "$$CRD_TEMPLATE" > charts/topolvm/templates/crds/topolvm.io_logicalvolumes.yaml
$(BINDIR)/yq eval 'del(.status)' 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: $(PROTOBUF_GEN) manifests generate-api generate-helm-docs
.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)"
$(STATICCHECK) ./...
go vet ./...
test -z "$$(go vet ./... | grep -v '^vendor' | tee /dev/stderr)"
.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 ./...
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 ./client/*
TEST_LEGACY=true go test -count=1 -race -v ./constants*.go
.PHONY: clean
clean: ## Clean working directory.
rm -rf build/
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
##@ 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)" ./pkg/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)" ./pkg/lvmd
.PHONY: csi-sidecars
csi-sidecars: ## Build sidecar images.
mkdir -p build
make -f csi-sidecars.mk OUTPUT_DIR=build BUILD_PLATFORMS="linux $(GOARCH)"
.PHONY: image
image: ## Build topolvm images.
docker buildx build --no-cache --load -t $(IMAGE_PREFIX)topolvm:devel --build-arg TOPOLVM_VERSION=$(TOPOLVM_VERSION) .
docker buildx build --no-cache --load -t $(IMAGE_PREFIX)topolvm-with-sidecar:devel --build-arg TOPOLVM_VERSION=$(TOPOLVM_VERSION) -f Dockerfile.with-sidecar .
.PHONY: create-docker-container
create-docker-container: ## Create docker-container.
docker buildx create --use
.PHONY: multiplatform-images
multi-platform-images: ## Push multi-platform topolvm images.
mkdir -p build
docker buildx build --no-cache $(BUILDX_PUSH_OPTIONS) \
--platform linux/amd64,linux/arm64/v8 \
-t $(IMAGE_PREFIX)topolvm:$(IMAGE_TAG) \
--build-arg TOPOLVM_VERSION=$(TOPOLVM_VERSION) \
.
docker buildx build --no-cache $(BUILDX_PUSH_OPTIONS) \
--platform linux/amd64,linux/arm64/v8 \
-t $(IMAGE_PREFIX)topolvm-with-sidecar:$(IMAGE_TAG) \
--build-arg TOPOLVM_VERSION=$(TOPOLVM_VERSION) \
-f Dockerfile.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)
##@ Setup
.PHONY: install-kind
install-kind:
GOBIN=$(BINDIR) go install sigs.k8s.io/kind@$(KIND_VERSION)
.PHONY: tools
tools: install-kind ## Install development tools.
GOBIN=$(BINDIR) go install honnef.co/go/tools/cmd/staticcheck@latest
# Follow the official documentation to install the `latest` version, because explicitly specifying the version will get an error.
GOBIN=$(BINDIR) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
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)
GOBIN=$(BINDIR) go install github.com/onsi/ginkgo/ginkgo@v$(GINKGO_VERSION)
GOBIN=$(BINDIR) go install github.com/norwoodj/helm-docs/cmd/helm-docs@v$(HELM_DOCS_VERSION)
$(CURL) https://get.helm.sh/helm-v$(HELM_VERSION)-linux-amd64.tar.gz \
| tar xvz -C $(BINDIR) --strip-components 1 linux-amd64/helm
$(CURL) https://github.com/mikefarah/yq/releases/download/v${YQ_VERSION}/yq_linux_amd64 -o $(BINDIR)/yq \
&& chmod +x $(BINDIR)/yq
.PHONY: setup
setup: ## Setup local environment.
$(SUDO) apt-get update
$(SUDO) apt-get -y install --no-install-recommends $(PACKAGES)
$(MAKE) tools
$(MAKE) $(HOME)/.docker/cli-plugins/docker-buildx
# https://github.com/tonistiigi/binfmt
docker run --privileged --rm tonistiigi/binfmt --install linux/amd64,linux/arm64/v8
# 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 $@