-
Notifications
You must be signed in to change notification settings - Fork 14
/
Makefile
354 lines (299 loc) · 11.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
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
#
# Copyright 2022 The KubeBlocks Authors
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
################################################################################
# Variables #
################################################################################
APP_NAME = kbcli
VERSION ?= 0.9.0-alpha.0
GITHUB_PROXY ?=
GIT_COMMIT = $(shell git rev-list -1 HEAD)
GIT_VERSION = $(shell git describe --always --abbrev=0 --tag)
ADDON_BRANCH ?= main
# Go setup
export GO111MODULE = auto
export GOSUMDB = sum.golang.org
export GONOPROXY = github.com/apecloud
export GONOSUMDB = github.com/apecloud
export GOPRIVATE = github.com/apecloud
GO ?= go
GOFMT ?= gofmt
GOOS ?= $(shell $(GO) env GOOS)
GOARCH ?= $(shell $(GO) env GOARCH)
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell $(GO) env GOBIN))
GOBIN=$(shell $(GO) env GOPATH)/bin
else
GOBIN=$(shell $(GO) env GOBIN)
endif
GOPROXY := $(shell go env GOPROXY)
ifeq ($(GOPROXY),)
GOPROXY := https://proxy.golang.org
## use following GOPROXY settings for Chinese mainland developers.
#GOPROXY := https://goproxy.cn
endif
export GOPROXY
# build tags
BUILD_TAGS="containers_image_openpgp"
TAG_LATEST ?= false
BUILDX_ENABLED ?= ""
ifeq ($(BUILDX_ENABLED), "")
ifeq ($(shell docker buildx inspect 2>/dev/null | awk '/Status/ { print $$2 }'), running)
BUILDX_ENABLED = true
else
BUILDX_ENABLED = false
endif
endif
BUILDX_BUILDER ?= "x-builder"
define BUILDX_ERROR
buildx not enabled, refusing to run this recipe
endef
DOCKER_BUILD_ARGS =
DOCKER_NO_BUILD_CACHE ?= false
ifeq ($(DOCKER_NO_BUILD_CACHE), true)
DOCKER_BUILD_ARGS = $(DOCKER_BUILD_ARGS) --no-cache
endif
.DEFAULT_GOAL := help
##@ 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
# https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
.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: generate
generate: build-kbcli-embed-chart
.PHONY: fmt
fmt: ## Run go fmt against code.
$(GOFMT) -l -w -s $$(git ls-files --exclude-standard | grep "\.go$$")
.PHONY: vet
vet: ## Run go vet against code.
GOOS=$(GOOS) $(GO) vet -tags $(BUILD_TAGS) -mod=mod ./...
.PHONY: cue-fmt
cue-fmt: cuetool ## Run cue fmt against code.
git ls-files --exclude-standard | grep "\.cue$$" | xargs $(CUE) fmt
git ls-files --exclude-standard | grep "\.cue$$" | xargs $(CUE) fix
.PHONY: lint-fast
lint-fast: staticcheck vet golangci-lint # [INTERNAL] Run all lint job against code.
.PHONY: lint
lint: generate ## Run default lint job against code.
$(MAKE) golangci-lint
.PHONY: golangci-lint
golangci-lint: golangci generate ## Run golangci-lint against code.
$(GOLANGCILINT) run ./...
.PHONY: staticcheck
staticcheck: staticchecktool generate ## Run staticcheck against code.
$(STATICCHECK) -tags $(BUILD_TAGS) ./...
.PHONY: build-checks
build-checks: generate fmt vet goimports lint-fast ## Run build checks.
.PHONY: mod-download
mod-download: ## Run go mod download against go modules.
$(GO) mod download
.PHONY: module
module: ## Run go mod tidy->verify against go modules.
$(GO) mod tidy -compat=1.21
$(GO) mod verify
TEST_PACKAGES ?= ./pkg/... ./cmd/...
OUTPUT_COVERAGE=-coverprofile cover.out
.PHONY: test
test: generate ## Run operator controller tests with current $KUBECONFIG context. if existing k8s cluster is k3d or minikube, specify EXISTING_CLUSTER_TYPE.
$(GO) test -tags $(BUILD_TAGS) -p 1 $(TEST_PACKAGES) $(OUTPUT_COVERAGE)
.PHONY: test-fast
test-fast:
$(GO) test -tags $(BUILD_TAGS) -short $(TEST_PACKAGES) $(OUTPUT_COVERAGE)
.PHONY: cover-report
cover-report: ## Generate cover.html from cover.out
$(GO) tool cover -html=cover.out -o cover.html
ifeq ($(GOOS), darwin)
open ./cover.html
else
echo "open cover.html with a HTML viewer."
endif
.PHONY: goimports
goimports: goimportstool ## Run goimports against code.
$(GOIMPORTS) -local github.com/apecloud/kbcli -w $$(git ls-files|grep "\.go$$")
##@ CLI
K3S_VERSION ?= v1.30.2+k3s2
K3D_VERSION ?= 5.4.4
K3S_IMG_TAG ?= $(subst +,-,$(K3S_VERSION))
FETCH_ADDON_ENABLED ?= true
CLI_LD_FLAGS ="-s -w \
-X github.com/apecloud/kbcli/version.BuildDate=`date -u +'%Y-%m-%dT%H:%M:%SZ'` \
-X github.com/apecloud/kbcli/version.GitCommit=$(GIT_COMMIT) \
-X github.com/apecloud/kbcli/version.GitVersion=$(GIT_VERSION) \
-X github.com/apecloud/kbcli/version.Version=$(VERSION) \
-X github.com/apecloud/kbcli/version.K3sImageTag=$(K3S_IMG_TAG) \
-X github.com/apecloud/kbcli/version.K3dVersion=$(K3D_VERSION) \
-X github.com/apecloud/kbcli/version.DefaultKubeBlocksVersion=$(VERSION)"
bin/kbcli.%: ## Cross build bin/kbcli.$(OS).$(ARCH).
GOOS=$(word 2,$(subst ., ,$@)) GOARCH=$(word 3,$(subst ., ,$@)) $(GO) build -tags $(BUILD_TAGS) -ldflags=${CLI_LD_FLAGS} -o $@ cmd/cli/main.go
.PHONY: fetch-addons
fetch-addons: ## update addon submodule
ifeq ($(FETCH_ADDON_ENABLED), true)
git submodule update --init --recursive --remote --force
git submodule
endif
.PHONY: kbcli-fast
kbcli-fast: OS=$(shell $(GO) env GOOS)
kbcli-fast: ARCH=$(shell $(GO) env GOARCH)
kbcli-fast: build-kbcli-embed-chart
$(MAKE) bin/kbcli.$(OS).$(ARCH)
@mv bin/kbcli.$(OS).$(ARCH) bin/kbcli
create-kbcli-embed-charts-dir:
mkdir -p pkg/cluster/charts/
build-single-kbcli-embed-chart.%: chart=$(word 2,$(subst ., ,$@))
build-single-kbcli-embed-chart.%:
$(HELM) dependency update addons/addons-cluster/$(chart) --skip-refresh
$(HELM) package addons/addons-cluster/$(chart)
@if [ ! -f "pkg/cluster/charts/$(chart).tgz" ]; then \
echo "Moving new chart to the charts directory..."; \
mv $(chart)-*.tgz pkg/cluster/charts/$(chart).tgz; \
exit 0; \
fi; \
rm -rf $(chart)-new $(chart)-old; \
echo "Existing chart found, comparing..."; \
echo "Extracting new chart..."; \
mkdir $(chart)-new && tar -xzf $(chart)-*.tgz -C $(chart)-new; \
echo "Extracting old chart..."; \
mkdir $(chart)-old && tar -xzf pkg/cluster/charts/$(chart).tgz -C $(chart)-old; \
echo "Comparing charts..."; \
diff -r $(chart)-new $(chart)-old > chart.diff; \
if [ -s chart.diff ]; then \
echo "Differences found, updating chart..."; \
mv $(chart)-*.tgz pkg/cluster/charts/$(chart).tgz; \
else \
echo "No differences found, cleaning up..."; \
rm $(chart)-*.tgz; \
fi; \
echo "Cleaning up..."; \
rm -rf chart.diff; \
rm -rf $(chart)-new $(chart)-old; \
.PHONY: build-kbcli-embed-chart
build-kbcli-embed-chart: helmtool fetch-addons create-kbcli-embed-charts-dir \
build-single-kbcli-embed-chart.apecloud-mysql \
build-single-kbcli-embed-chart.mysql \
build-single-kbcli-embed-chart.redis \
build-single-kbcli-embed-chart.postgresql \
build-single-kbcli-embed-chart.kafka \
build-single-kbcli-embed-chart.mongodb \
build-single-kbcli-embed-chart.elasticsearch \
build-single-kbcli-embed-chart.qdrant \
build-single-kbcli-embed-chart.etcd
.PHONY: kbcli
kbcli: build-checks kbcli-fast ## Build bin/kbcli.
.PHONY: clean-kbcli
clean-kbcli: ## Clean bin/kbcli*.
rm -f bin/kbcli*
.PHONY: kbcli-doc
kbcli-doc: ## generate CLI command reference manual.
$(GO) run -tags $(BUILD_TAGS) ./hack/docgen/cli/main.go ./docs/user_docs/cli
.PHONY: install-docker-buildx
install-docker-buildx: ## Create `docker buildx` builder.
@if ! docker buildx inspect $(BUILDX_BUILDER) > /dev/null; then \
echo "Buildx builder $(BUILDX_BUILDER) does not exist, creating..."; \
docker buildx create --name=$(BUILDX_BUILDER) --use --driver=docker-container --platform linux/amd64,linux/arm64; \
else \
echo "Buildx builder $(BUILDX_BUILDER) already exists"; \
fi
.PHONY: golangci
golangci: GOLANGCILINT_VERSION = v1.55.2
golangci: ## Download golangci-lint locally if necessary.
ifneq ($(shell which golangci-lint),)
@echo golangci-lint is already installed
GOLANGCILINT=$(shell which golangci-lint)
else ifeq (, $(shell which $(GOBIN)/golangci-lint))
@{ \
set -e ;\
echo 'installing golangci-lint-$(GOLANGCILINT_VERSION)' ;\
curl -sSfL $(GITHUB_PROXY)https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOBIN) $(GOLANGCILINT_VERSION) ;\
echo 'Successfully installed' ;\
}
GOLANGCILINT=$(GOBIN)/golangci-lint
else
@echo golangci-lint is already installed
GOLANGCILINT=$(GOBIN)/golangci-lint
endif
.PHONY: staticchecktool
staticchecktool: ## Download staticcheck locally if necessary.
ifeq (, $(shell which staticcheck))
@{ \
set -e ;\
echo 'installing honnef.co/go/tools/cmd/staticcheck' ;\
go install honnef.co/go/tools/cmd/staticcheck@latest;\
}
STATICCHECK=$(GOBIN)/staticcheck
else
STATICCHECK=$(shell which staticcheck)
endif
.PHONY: goimportstool
goimportstool: ## Download goimports locally if necessary.
ifeq (, $(shell which goimports))
@{ \
set -e ;\
go install golang.org/x/tools/cmd/goimports@latest ;\
}
GOIMPORTS=$(GOBIN)/goimports
else
GOIMPORTS=$(shell which goimports)
endif
.PHONY: cuetool
cuetool: ## Download cue locally if necessary.
ifeq (, $(shell which cue))
@{ \
set -e ;\
go install cuelang.org/go/cmd/cue@$(CUE_VERSION) ;\
}
CUE=$(GOBIN)/cue
else
CUE=$(shell which cue)
endif
.PHONY: helmtool
helmtool: ## Download helm locally if necessary.
ifeq (, $(shell which helm))
@{ \
set -e ;\
echo 'installing helm' ;\
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash;\
echo 'Successfully installed' ;\
}
HELM=$(GOBIN)/helm
else
HELM=$(shell which helm)
endif
.PHONY: kubectl
kubectl: ## Download kubectl locally if necessary.
ifeq (, $(shell which kubectl))
@{ \
set -e ;\
echo 'installing kubectl' ;\
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/$(GOOS)/$(GOARCH)/kubectl" && chmod +x kubectl && sudo mv kubectl /usr/local/bin ;\
echo 'Successfully installed' ;\
}
endif
KUBECTL=$(shell which kubectl)
.PHONY: check-license-header
check-license-header: ## Run license header check.
@./hack/license/header-check.sh
.PHONY: fix-license-header
fix-license-header: ## Run license header fix.
@./hack/license/header-check.sh fix
# NOTE: include must be placed at the end
include docker/docker.mk