Skip to content

Commit a799d66

Browse files
authored
chore: refine Makefile (#9442)
1 parent 30c6e9c commit a799d66

2 files changed

Lines changed: 95 additions & 128 deletions

File tree

Makefile

Lines changed: 63 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ SKIP_GO_GEN ?= true
3636
CHART_PATH = deploy/helm
3737
WEBHOOK_CERT_DIR ?= /tmp/k8s-webhook-server/serving-certs
3838

39-
40-
4139
# Go setup
4240
export GO111MODULE = auto
4341
export GOSUMDB = sum.golang.org
@@ -62,41 +60,18 @@ GOPROXY := https://proxy.golang.org
6260
endif
6361
export GOPROXY
6462

65-
6663
LD_FLAGS="-s -w \
6764
-X github.com/apecloud/kubeblocks/version.Version=v${VERSION} \
6865
-X github.com/apecloud/kubeblocks/version.BuildDate=`date -u +'%Y-%m-%dT%H:%M:%SZ'` \
6966
-X github.com/apecloud/kubeblocks/version.GitCommit=${GIT_COMMIT} \
7067
-X github.com/apecloud/kubeblocks/version.GitVersion=${GIT_VERSION}"
68+
7169
# Which architecture to build - see $(ALL_ARCH) for options.
7270
# if the 'local' rule is being run, detect the ARCH from 'go env'
7371
# if it wasn't specified by the caller.
7472
local : ARCH ?= $(shell go env GOOS)-$(shell go env GOARCH)
7573
ARCH ?= linux-amd64
7674

77-
TAG_LATEST ?= false
78-
BUILDX_ENABLED ?= ""
79-
ifeq ($(BUILDX_ENABLED), "")
80-
ifeq ($(shell docker buildx inspect 2>/dev/null | awk '/Status/ { print $$2 }'), running)
81-
BUILDX_ENABLED = true
82-
else
83-
BUILDX_ENABLED = false
84-
endif
85-
endif
86-
BUILDX_BUILDER ?= "x-builder"
87-
88-
define BUILDX_ERROR
89-
buildx not enabled, refusing to run this recipe
90-
endef
91-
92-
DOCKER_BUILD_ARGS =
93-
DOCKER_NO_BUILD_CACHE ?= false
94-
95-
ifeq ($(DOCKER_NO_BUILD_CACHE), true)
96-
DOCKER_BUILD_ARGS = $(DOCKER_BUILD_ARGS) --no-cache
97-
endif
98-
99-
10075
.DEFAULT_GOAL := help
10176

10277
##@ General
@@ -170,11 +145,6 @@ fmt: ## Run go fmt against code.
170145
vet: test-go-generate ## Run go vet against code.
171146
GOOS=$(GOOS) $(GO) vet -mod=mod ./...
172147

173-
.PHONY: cue-fmt
174-
cue-fmt: cuetool ## Run cue fmt against code.
175-
git ls-files --exclude-standard | grep "\.cue$$" | xargs $(CUE) fmt
176-
git ls-files --exclude-standard | grep "\.cue$$" | xargs $(CUE) fix
177-
178148
.PHONY: lint-fast
179149
lint-fast: staticcheck vet golangci-lint # [INTERNAL] Run all lint job against code.
180150

@@ -183,15 +153,15 @@ lint: test-go-generate generate ## Run default lint job against code.
183153
$(MAKE) golangci-lint
184154

185155
.PHONY: golangci-lint
186-
golangci-lint: golangci generate ## Run golangci-lint against code.
156+
golangci-lint: golangci-lint-bin generate ## Run golangci-lint against code.
187157
$(GOLANGCILINT) run ./...
188158

189159
.PHONY: staticcheck
190-
staticcheck: staticchecktool test-go-generate generate ## Run staticcheck against code.
160+
staticcheck: staticcheck-bin test-go-generate generate ## Run staticcheck against code.
191161
$(STATICCHECK) ./...
192162

193163
.PHONY: build-checks
194-
build-checks: generate fmt vet goimports lint-fast ## Run build checks.
164+
build-checks: fmt vet goimports lint-fast ## Run build checks.
195165

196166
.PHONY: mod-download
197167
mod-download: ## Run go mod download against go modules.
@@ -252,7 +222,7 @@ else
252222
endif
253223

254224
.PHONY: goimports
255-
goimports: goimportstool ## Run goimports against code.
225+
goimports: goimports-bin ## Run goimports against code.
256226
$(GOIMPORTS) -local github.com/apecloud/kubeblocks -w $$(git ls-files|grep "\.go$$" | grep -v $(GENERATED_CLIENT_PKG) | grep -v $(GENERATED_DEEP_COPY_FILE))
257227

258228
.PHONY: api-doc
@@ -266,15 +236,15 @@ doc: api-doc ## generate all documents.
266236
##@ Operator Controller Manager
267237

268238
.PHONY: manager
269-
manager: cue-fmt generate manager-go-generate test-go-generate build-checks ## Build manager binary.
239+
manager: generate manager-go-generate build-checks ## Build manager binary.
270240
$(GO) build -ldflags=${LD_FLAGS} -o bin/manager ./cmd/manager/main.go
271241

272242
.PHONY: dataprotection
273-
dataprotection: generate test-go-generate build-checks ## Build dataprotection binary.
243+
dataprotection: generate build-checks ## Build dataprotection binary.
274244
$(GO) build -ldflags=${LD_FLAGS} -o bin/dataprotection ./cmd/dataprotection/main.go
275245

276246
.PHONY: kbagent
277-
kbagent: generate test-go-generate build-checks
247+
kbagent: generate build-checks
278248
$(GO) build -ldflags=${LD_FLAGS} -o bin/kbagent ./cmd/kbagent/main.go
279249

280250
.PHONY: helmhook
@@ -380,7 +350,7 @@ bump-chart-ver: \
380350
bump-chart-ver: ## Bump helm chart version.
381351

382352
.PHONY: helm-package
383-
helm-package: bump-chart-ver ## Do helm package.
353+
helm-package: helmtool bump-chart-ver ## Do helm package.
384354
$(HELM) package $(CHART_PATH)
385355

386356
##@ Build Dependencies
@@ -390,117 +360,66 @@ LOCALBIN ?= $(shell pwd)/bin
390360
$(LOCALBIN):
391361
mkdir -p $(LOCALBIN)
392362

393-
## Tool Binaries
394-
KUSTOMIZE ?= $(LOCALBIN)/kustomize
395-
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
396-
ENVTEST ?= $(LOCALBIN)/setup-envtest
397-
398363
## Tool Versions
399364
KUSTOMIZE_VERSION ?= v5.1.1
400365
CONTROLLER_TOOLS_VERSION ?= v0.14.0
401-
CUE_VERSION ?= v0.4.3
366+
ENVTEST_VERSION ?= release-0.21
367+
368+
## Tool Binaries
369+
KUSTOMIZE ?= $(LOCALBIN)/kustomize-$(KUSTOMIZE_VERSION)
370+
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen-$(CONTROLLER_TOOLS_VERSION)
371+
ENVTEST ?= $(LOCALBIN)/setup-envtest-$(ENVTEST_VERSION)
402372

403373
KUSTOMIZE_INSTALL_SCRIPT ?= "$(GITHUB_PROXY)https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
404374
.PHONY: kustomize
405-
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
406-
$(KUSTOMIZE): $(LOCALBIN)
407-
ifeq (, $(shell ls $(LOCALBIN)/kustomize 2>/dev/null))
408-
curl -s $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN)
409-
endif
375+
kustomize: $(LOCALBIN) ## Download kustomize locally if necessary.
376+
@[ -f $(KUSTOMIZE) ] || { \
377+
echo "Installing kustomize with version $(KUSTOMIZE_VERSION)"; \
378+
curl -s $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN) && \
379+
mv "$(LOCALBIN)/kustomize" "$(KUSTOMIZE)"; \
380+
}
410381

411382
.PHONY: controller-gen
412383
controller-gen: $(LOCALBIN) ## Download controller-gen locally if necessary.
413-
@{ \
414-
set -e ;\
415-
if [ ! -f "$(CONTROLLER_GEN)" ] || [ "$$($(CONTROLLER_GEN) --version 2>&1 | awk '{print $$NF}')" != "$(CONTROLLER_TOOLS_VERSION)" ]; then \
416-
echo 'Installing controller-gen@$(CONTROLLER_TOOLS_VERSION)...' ;\
417-
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION) ;\
418-
echo 'Successfully installed' ;\
419-
fi \
420-
}
384+
$(call go-install-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen,$(CONTROLLER_TOOLS_VERSION))
421385

422386
.PHONY: envtest
423-
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
424-
$(ENVTEST): $(LOCALBIN)
425-
ifeq (, $(shell ls $(LOCALBIN)/setup-envtest 2>/dev/null))
426-
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@v0.0.0-20240320141353-395cfc7486e6
427-
endif
428-
429-
.PHONY: install-docker-buildx
430-
install-docker-buildx: ## Create `docker buildx` builder.
431-
@if ! docker buildx inspect $(BUILDX_BUILDER) > /dev/null; then \
432-
echo "Buildx builder $(BUILDX_BUILDER) does not exist, creating..."; \
433-
docker buildx create --name=$(BUILDX_BUILDER) --use --driver=docker-container --platform linux/amd64,linux/arm64; \
434-
else \
435-
echo "Buildx builder $(BUILDX_BUILDER) already exists"; \
436-
fi
437-
438-
.PHONY: golangci
439-
golangci: GOLANGCILINT_VERSION = v1.64.5
440-
golangci: ## Download golangci-lint locally if necessary.
441-
ifneq ($(shell which golangci-lint),)
442-
@echo golangci-lint is already installed
443-
GOLANGCILINT=$(shell which golangci-lint)
444-
else ifeq (, $(shell which $(GOBIN)/golangci-lint))
445-
@{ \
446-
set -e ;\
447-
echo 'installing golangci-lint-$(GOLANGCILINT_VERSION)' ;\
448-
curl -sSfL $(GITHUB_PROXY)https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOBIN) $(GOLANGCILINT_VERSION) ;\
449-
echo 'Successfully installed' ;\
450-
}
451-
GOLANGCILINT=$(GOBIN)/golangci-lint
452-
else
453-
@echo golangci-lint is already installed
454-
GOLANGCILINT=$(GOBIN)/golangci-lint
455-
endif
456-
457-
.PHONY: staticchecktool
458-
staticchecktool: ## Download staticcheck locally if necessary.
459-
ifeq (, $(shell which staticcheck))
460-
@{ \
461-
set -e ;\
462-
echo 'installing honnef.co/go/tools/cmd/staticcheck' ;\
463-
go install honnef.co/go/tools/cmd/staticcheck@latest;\
387+
envtest: $(LOCALBIN) ## Download envtest-setup locally if necessary.
388+
$(call go-install-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest,$(ENVTEST_VERSION))
389+
390+
GOLANGCILINT_VERSION = v1.64.8
391+
GOLANGCILINT = $(LOCALBIN)/golangci-lint-$(GOLANGCILINT_VERSION)
392+
.PHONY: golangci-lint-bin
393+
golangci-lint-bin: $(LOCALBIN) ## Download golangci-lint locally if necessary.
394+
@[ -f $(GOLANGCILINT) ] || { \
395+
echo "Installing golangci-lint with version $(GOLANGCILINT_VERSION)"; \
396+
curl -sSfL $(GITHUB_PROXY)https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(LOCALBIN) $(GOLANGCILINT_VERSION) && \
397+
mv "$(LOCALBIN)/golangci-lint" "$(GOLANGCILINT)"; \
464398
}
465-
STATICCHECK=$(GOBIN)/staticcheck
466-
else
467-
STATICCHECK=$(shell which staticcheck)
468-
endif
469399

470-
.PHONY: goimportstool
471-
goimportstool: ## Download goimports locally if necessary.
472-
ifeq (, $(shell which goimports))
473-
@{ \
474-
set -e ;\
475-
go install golang.org/x/tools/cmd/goimports@latest ;\
476-
}
477-
GOIMPORTS=$(GOBIN)/goimports
478-
else
479-
GOIMPORTS=$(shell which goimports)
480-
endif
400+
STATICCHECK_VERSION = v0.6.1
401+
STATICCHECK = $(LOCALBIN)/staticcheck-$(STATICCHECK_VERSION)
402+
.PHONY: staticcheck-bin
403+
staticcheck-bin: $(LOCALBIN) ## Download staticcheck locally if necessary.
404+
$(call go-install-tool,$(STATICCHECK),honnef.co/go/tools/cmd/staticcheck,$(STATICCHECK_VERSION))
481405

482-
.PHONY: cuetool
483-
cuetool: ## Download cue locally if necessary.
484-
ifeq (, $(shell which cue))
485-
@{ \
486-
set -e ;\
487-
go install cuelang.org/go/cmd/cue@$(CUE_VERSION) ;\
488-
}
489-
CUE=$(GOBIN)/cue
490-
else
491-
CUE=$(shell which cue)
492-
endif
406+
GOIMPORTS_VERSION = v0.34.0
407+
GOIMPORTS = $(LOCALBIN)/goimports-$(GOIMPORTS_VERSION)
408+
.PHONY: goimports-bin
409+
goimports-bin: $(LOCALBIN) ## Download goimports locally if necessary.
410+
$(call go-install-tool,$(GOIMPORTS),golang.org/x/tools/cmd/goimports,$(GOIMPORTS_VERSION))
493411

494412
.PHONY: helmtool
495413
helmtool: ## Download helm locally if necessary.
496414
ifeq (, $(shell which helm))
497415
@{ \
498416
set -e ;\
499417
echo 'installing helm' ;\
500-
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash;\
418+
curl $(GITHUB_PROXY)https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash;\
501419
echo 'Successfully installed' ;\
502420
}
503-
HELM=$(GOBIN)/helm
421+
# Hopefully the command will be installed in PATH
422+
HELM=helm
504423
else
505424
HELM=$(shell which helm)
506425
endif
@@ -514,9 +433,25 @@ ifeq (, $(shell which kubectl))
514433
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 ;\
515434
echo 'Successfully installed' ;\
516435
}
517-
endif
436+
# Hopefully the command will be installed in PATH
437+
KUBECTL=kubectl
438+
else
518439
KUBECTL=$(shell which kubectl)
440+
endif
519441

442+
# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
443+
# $1 - target path with name of binary (ideally with version)
444+
# $2 - package url which can be installed
445+
# $3 - specific version of package
446+
define go-install-tool
447+
@[ -f $(1) ] || { \
448+
set -e; \
449+
package=$(2)@$(3) ;\
450+
echo "Installing $${package}" ;\
451+
GOBIN=$(LOCALBIN) go install $${package} ;\
452+
mv "$$(echo "$(1)" | sed "s/-$(3)$$//")" $(1) ;\
453+
}
454+
endef
520455

521456
# NOTE: include must be placed at the end
522457
include docker/docker.mk

docker/docker.mk

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,28 @@
1717
#along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
#
1919

20+
TAG_LATEST ?= false
21+
BUILDX_ENABLED ?= ""
22+
ifeq ($(BUILDX_ENABLED), "")
23+
ifeq ($(shell docker buildx inspect 2>/dev/null | awk '/Status/ { print $$2 }'), running)
24+
BUILDX_ENABLED = true
25+
else
26+
BUILDX_ENABLED = false
27+
endif
28+
endif
29+
BUILDX_BUILDER ?= "x-builder"
30+
31+
define BUILDX_ERROR
32+
buildx not enabled, refusing to run this recipe
33+
endef
34+
35+
DOCKER_BUILD_ARGS =
36+
DOCKER_NO_BUILD_CACHE ?= false
37+
38+
ifeq ($(DOCKER_NO_BUILD_CACHE), true)
39+
DOCKER_BUILD_ARGS = $(DOCKER_BUILD_ARGS) --no-cache
40+
endif
41+
2042
# To use buildx: https://github.com/docker/buildx#docker-ce
2143
export DOCKER_CLI_EXPERIMENTAL=enabled
2244

@@ -51,6 +73,16 @@ DOCKER_BUILD_ARGS += $(GO_BUILD_ARGS) $(BUILD_ARGS)
5173

5274
##@ Docker containers
5375

76+
.PHONY: install-docker-buildx
77+
install-docker-buildx: ## Create `docker buildx` builder.
78+
@if ! docker buildx inspect $(BUILDX_BUILDER) > /dev/null; then \
79+
echo "Buildx builder $(BUILDX_BUILDER) does not exist, creating..."; \
80+
docker buildx create --name=$(BUILDX_BUILDER) --use --driver=docker-container --platform linux/amd64,linux/arm64; \
81+
else \
82+
echo "Buildx builder $(BUILDX_BUILDER) already exists"; \
83+
fi
84+
85+
5486
.PHONY: build-dev-container-image
5587
build-dev-container-image: DOCKER_BUILD_ARGS += --build-arg DEBIAN_MIRROR=$(DEBIAN_MIRROR)
5688
build-dev-container-image: install-docker-buildx ## Build dev container image.

0 commit comments

Comments
 (0)