forked from topolvm/topolvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
210 lines (168 loc) · 7.8 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
## 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.4.0
PROTOC_VERSION=3.15.0
KIND_VERSION=v0.11.1
HELM_VERSION=3.5.0
HELM_DOCS_VERSION=1.5.0
SUDO=sudo
CURL=curl -Lsf
BINDIR := $(shell pwd)/bin
CONTROLLER_GEN := $(BINDIR)/controller-gen
STATICCHECK := $(BINDIR)/staticcheck
NILERR := $(BINDIR)/nilerr
PROTOC := PATH=$(BINDIR):$(PATH) $(BINDIR)/protoc -I=$(shell pwd)/include:.
PACKAGES := unzip lvm2 xfsprogs
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
## for build kind node
KIND_NODE_VERSION=v1.21.1
## TODO: update to 1.21 after envtest in controller-rutime support k8s 1.21
ENVTEST_KUBERNETES_VERSION=1.20
# Set the shell used to bash for better error handling.
SHELL = /bin/bash
.SHELLFLAGS = -e -o pipefail -c
##@ 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 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
rm -f charts/topolvm/crds/topolvm.cybozu.com_logicalvolumes.yaml
cp config/crd/bases/topolvm.cybozu.com_logicalvolumes.yaml charts/topolvm/crds/topolvm.cybozu.com_logicalvolumes.yaml
.PHONY: generate
generate: $(PROTOBUF_GEN) ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile="./hack/boilerplate.go.txt" paths="./api/..."
.PHONY: check-uncommitted
check-uncommitted: ## Check if latest generated artifacts are committed.
$(MAKE) manifests
$(MAKE) generate
git diff --exit-code --name-only
.PHONY: lint
lint: ## Run lint
test -z "$$(gofmt -s -l . | grep -v '^vendor' | tee /dev/stderr)"
$(STATICCHECK) ./...
test -z "$$($(NILERR) ./... 2>&1 | tee /dev/stderr)"
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 -race -v ./...
.PHONY: clean
clean: ## Clean working directory.
rm -rf build/
rm -rf bin/
rm -rf include/
rm -rf testbin/
##@ Build
.PHONY: build
build: build/hypertopolvm build/lvmd csi-sidecars ## Build binaries.
build/hypertopolvm: $(GO_FILES)
mkdir -p build
go build -o $@ -ldflags "-w -s -X github.com/topolvm/topolvm.Version=$(TOPOLVM_VERSION)" ./pkg/hypertopolvm
build/lvmd:
mkdir -p build
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
.PHONY: image
image: ## Build topolvm images.
docker build --no-cache -t $(IMAGE_PREFIX)topolvm:devel --build-arg TOPOLVM_VERSION=$(TOPOLVM_VERSION) .
docker build --no-cache -t $(IMAGE_PREFIX)topolvm-with-sidecar:devel --build-arg TOPOLVM_VERSION=$(TOPOLVM_VERSION) -f Dockerfile.with-sidecar .
.PHONY: tag
tag: ## Tag topolvm images.
docker tag $(IMAGE_PREFIX)topolvm:devel $(IMAGE_PREFIX)topolvm:$(IMAGE_TAG)
docker tag $(IMAGE_PREFIX)topolvm-with-sidecar:devel $(IMAGE_PREFIX)topolvm-with-sidecar:$(IMAGE_TAG)
.PHONY: push
push: ## Push topolvm images.
docker push $(IMAGE_PREFIX)topolvm:$(IMAGE_TAG)
docker push $(IMAGE_PREFIX)topolvm-with-sidecar:$(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 golang.org/x/tools/cmd/goimports@latest
GOBIN=$(BINDIR) go install honnef.co/go/tools/cmd/staticcheck@latest
GOBIN=$(BINDIR) go install github.com/gostaticanalysis/nilerr/cmd/nilerr@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 -sfL -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@latest
GOBIN=$(BINDIR) go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
GOBIN=$(BINDIR) go install github.com/pseudomuto/protoc-gen-doc/cmd/protoc-gen-doc@latest
GOBIN=$(BINDIR) go install github.com/onsi/ginkgo/ginkgo@latest
GOBIN=$(BINDIR) go install github.com/norwoodj/helm-docs/cmd/helm-docs@v$(HELM_DOCS_VERSION)
curl -L -sS https://get.helm.sh/helm-v$(HELM_VERSION)-linux-amd64.tar.gz \
| tar xvz -C $(BINDIR) --strip-components 1 linux-amd64/helm
.PHONY: setup
setup: tools ## Setup local environment.
$(SUDO) apt-get update
$(SUDO) apt-get -y install --no-install-recommends $(PACKAGES)
# 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 ;\
TMP_DIR=$$(mktemp -d) ;\
cd $$TMP_DIR ;\
go mod init tmp ;\
echo "Downloading $(2)" ;\
GOBIN=$(PROJECT_DIR)/bin go get $(2) ;\
rm -rf $$TMP_DIR ;\
}
endef
.PHONY: kind-node
kind-node:
git clone --depth 1 https://github.com/kubernetes/kubernetes.git -b $(KIND_NODE_VERSION) /tmp/kind-node
$(BINDIR)/kind build node-image --kube-root /tmp/kind-node --image quay.io/topolvm/kind-node:$(KIND_NODE_VERSION)