|
1 | | -# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary. |
2 | | -ENVTEST_K8S_VERSION = 1.33.0 |
3 | | - |
4 | 1 | # 'sample' folder, where you mentioned the `go mod download` should take place |
5 | 2 | SAMPLE_DIR = sample |
6 | 3 |
|
@@ -85,23 +82,43 @@ GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint |
85 | 82 | ADDLICENSE_VERSION ?= v1.1.1 |
86 | 83 | GOIMPORTS_VERSION ?= v0.36.0 |
87 | 84 | GOLANGCI_LINT_VERSION ?= v2.4 |
| 85 | +#ENVTEST_VERSION is the version of controller-runtime release branch to fetch the envtest setup script (i.e. release-0.20) |
| 86 | +ENVTEST_VERSION ?= $(shell go list -m -f "{{ .Version }}" sigs.k8s.io/controller-runtime | awk -F'[v.]' '{printf "release-%d.%d", $$2, $$3}') |
| 87 | +#ENVTEST_K8S_VERSION is the version of Kubernetes to use for setting up ENVTEST binaries (i.e. 1.31) |
| 88 | +ENVTEST_K8S_VERSION ?= $(shell go list -m -f "{{ .Version }}" k8s.io/api | awk -F'[v.]' '{printf "1.%d.%d",$$3, $$2}') |
88 | 89 |
|
89 | 90 | .PHONY: addlicense |
90 | 91 | addlicense: $(ADDLICENSE) ## Download addlicense locally if necessary. |
91 | 92 | $(ADDLICENSE): $(LOCALBIN) |
92 | | - test -s $(LOCALBIN)/addlicense || GOBIN=$(LOCALBIN) go install github.com/google/addlicense@$(ADDLICENSE_VERSION) |
| 93 | + $(call go-install-tool,$(ADDLICENSE),github.com/google/addlicense,$(ADDLICENSE_VERSION)) |
93 | 94 |
|
94 | 95 | .PHONY: goimports |
95 | 96 | goimports: $(GOIMPORTS) ## Download goimports locally if necessary. |
96 | 97 | $(GOIMPORTS): $(LOCALBIN) |
97 | | - test -s $(LOCALBIN)/goimports || GOBIN=$(LOCALBIN) go install golang.org/x/tools/cmd/goimports@$(GOIMPORTS_VERSION) |
| 98 | + $(call go-install-tool,$(GOIMPORTS),golang.org/x/tools/cmd/goimports,$(GOIMPORTS_VERSION)) |
98 | 99 |
|
99 | 100 | .PHONY: envtest |
100 | 101 | envtest: $(ENVTEST) ## Download envtest-setup locally if necessary. |
101 | 102 | $(ENVTEST): $(LOCALBIN) |
102 | | - test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest |
| 103 | + $(call go-install-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest,$(ENVTEST_VERSION)) |
103 | 104 |
|
104 | 105 | .PHONY: goimports |
105 | 106 | golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary. |
106 | 107 | $(GOLANGCI_LINT): $(LOCALBIN) |
107 | | - test -s $(LOCALBIN)/golangci-lint || GOBIN=$(LOCALBIN) go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION) |
| 108 | + $(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/v2/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION)) |
| 109 | + |
| 110 | +# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist |
| 111 | +# $1 - target path with name of binary |
| 112 | +# $2 - package url which can be installed |
| 113 | +# $3 - specific version of package |
| 114 | +define go-install-tool |
| 115 | +@[ -f "$(1)-$(3)" ] && [ "$$(readlink -- "$(1)" 2>/dev/null)" = "$(1)-$(3)" ] || { \ |
| 116 | +set -e; \ |
| 117 | +package=$(2)@$(3) ;\ |
| 118 | +echo "Downloading $${package}" ;\ |
| 119 | +rm -f $(1) ;\ |
| 120 | +GOBIN=$(LOCALBIN) go install $${package} ;\ |
| 121 | +mv $(1) $(1)-$(3) ;\ |
| 122 | +} ;\ |
| 123 | +ln -sf $$(realpath $(1)-$(3)) $(1) |
| 124 | +endef |
0 commit comments