@@ -23,7 +23,6 @@ LIBGIT2_VERSION ?= 1.1.1
2323
2424# Other dependency versions
2525ENVTEST_BIN_VERSION ?= 1.19.2
26- KUBEBUILDER_ASSETS ?= $(shell $(SETUP_ENVTEST ) use -i $(ENVTEST_BIN_VERSION ) -p path)
2726
2827# libgit2 related magical paths
2928# These are used to determine if the target libgit2 version is already available on
@@ -67,6 +66,9 @@ ifdef HAS_OPENSSL
6766 MAKE_PKG_CONFIG_PATH := $(MAKE_PKG_CONFIG_PATH):$(HAS_OPENSSL)/lib/pkgconfig
6867endif
6968
69+ # Architecture to use envtest with
70+ ENVTEST_ARCH ?= amd64
71+
7072all : build
7173
7274build : $(LIBGIT2 ) # # Build manager binary
7981 go build -o bin/manager main.go
8082endif
8183
82- test : $(LIBGIT2 ) test-api # # Run tests
84+ KUBEBUILDER_ASSETS? ="$(shell $(ENVTEST ) --arch=$(ENVTEST_ARCH ) use -i $(ENVTEST_KUBERNETES_VERSION ) --bin-dir=$(ENVTEST_ASSETS_DIR ) -p path) "
85+ test : $(LIBGIT2 ) install-envtest test-api # # Run tests
8386ifeq ($(shell uname -s) ,Darwin)
8487 LD_LIBRARY_PATH=$(LIBGIT2_LIB_PATH) \
8588 PKG_CONFIG_PATH=$(MAKE_PKG_CONFIG_PATH) \
8689 CGO_LDFLAGS="-Wl,-rpath,$(LIBGIT2_LIB_PATH)" \
90+ KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS) \
8791 go test ./... -coverprofile cover.out
8892else
8993 LD_LIBRARY_PATH=$(LIBGIT2_LIB_PATH) \
9094 PKG_CONFIG_PATH=$(MAKE_PKG_CONFIG_PATH) \
95+ KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS) \
9196 go test ./... -coverprofile cover.out
9297endif
9398
@@ -126,7 +131,7 @@ manifests: controller-gen ## Generate manifests, e.g. CRD, RBAC, etc.
126131 cd api; $(CONTROLLER_GEN ) $(CRD_OPTIONS ) rbac:roleName=manager-role paths=" ./..." output:crd:artifacts:config=" ../config/crd/bases"
127132
128133api-docs : gen-crd-api-reference-docs # # Generate API reference documentation
129- $(API_REF_GEN ) -api-dir=./api/v1beta1 -config=./hack/api-docs/config.json -template-dir=./hack/api-docs/template -out-file=./docs/api/source.md
134+ $(GEN_CRD_API_REFERENCE_DOCS ) -api-dir=./api/v1beta1 -config=./hack/api-docs/config.json -template-dir=./hack/api-docs/template -out-file=./docs/api/source.md
130135
131136tidy : # # Run go mod tidy
132137 go mod tidy
@@ -162,50 +167,28 @@ docker-build: ## Build the Docker image
162167docker-push : # # Push Docker image
163168 docker push $(IMG ) :$(TAG )
164169
165- controller-gen : # # Find or download controller-gen
166- ifeq (, $(shell which controller-gen) )
167- @{ \
168- set -e; \
169- CONTROLLER_GEN_TMP_DIR=$$(mktemp -d); \
170- cd $$CONTROLLER_GEN_TMP_DIR; \
171- go mod init tmp; \
172- go get sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_GEN_VERSION); \
173- rm -rf $$CONTROLLER_GEN_TMP_DIR; \
174- }
175- CONTROLLER_GEN =$(GOBIN ) /controller-gen
176- else
177- CONTROLLER_GEN =$(shell which controller-gen)
178- endif
179-
180- gen-crd-api-reference-docs : # # Find or download gen-crd-api-reference-docs
181- ifeq (, $(shell which gen-crd-api-reference-docs) )
182- @{ \
183- set -e; \
184- API_REF_GEN_TMP_DIR=$$(mktemp -d); \
185- cd $$API_REF_GEN_TMP_DIR; \
186- go mod init tmp; \
187- go get github.com/ahmetb/gen-crd-api-reference-docs@$(GEN_API_REF_DOCS_VERSION); \
188- rm -rf $$API_REF_GEN_TMP_DIR; \
189- }
190- API_REF_GEN =$(GOBIN ) /gen-crd-api-reference-docs
191- else
192- API_REF_GEN =$(shell which gen-crd-api-reference-docs)
193- endif
194-
195- setup-envtest : # # Find or download setup-envtest
196- ifeq (, $(shell which setup-envtest) )
197- @{ \
198- set -e; \
199- SETUP_ENVTEST_TMP_DIR=$$(mktemp -d); \
200- cd $$SETUP_ENVTEST_TMP_DIR; \
201- go mod init tmp; \
202- go get sigs.k8s.io/controller-runtime/tools/setup-envtest@latest; \
203- rm -rf $$SETUP_ENVTEST_TMP_DIR; \
204- }
205- SETUP_ENVTEST =$(GOBIN ) /setup-envtest
206- else
207- SETUP_ENVTEST =$(shell which setup-envtest)
208- endif
170+ # Find or download controller-gen
171+ CONTROLLER_GEN = $(shell pwd) /bin/controller-gen
172+ .PHONY : controller-gen
173+ controller-gen : # # Download controller-gen locally if necessary.
174+ $(call go-install-tool,$(CONTROLLER_GEN ) ,sigs.k8s.io/controller-tools/cmd/[email protected] ) 175+
176+ # Find or download gen-crd-api-reference-docs
177+ GEN_CRD_API_REFERENCE_DOCS = $(shell pwd) /bin/gen-crd-api-reference-docs
178+ .PHONY : gen-crd-api-reference-docs
179+ gen-crd-api-reference-docs : # # Download gen-crd-api-reference-docs locally if necessary
180+ $(call go-install-tool,$(GEN_CRD_API_REFERENCE_DOCS ) ,github.com/ahmetb/[email protected] ) 181+
182+ ENVTEST = $(shell pwd) /bin/setup-envtest
183+ .PHONY : envtest
184+ setup-envtest : # # Download setup-envtest locally if necessary.
185+ $(call go-install-tool,$(ENVTEST ) ,sigs.k8s.io/controller-runtime/tools/setup-envtest@latest)
186+
187+ ENVTEST_ASSETS_DIR =$(shell pwd) /testbin
188+ ENVTEST_KUBERNETES_VERSION? =latest
189+ install-envtest : setup-envtest # # Download envtest binaries locally.
190+ mkdir -p ${ENVTEST_ASSETS_DIR}
191+ $(ENVTEST ) use $(ENVTEST_KUBERNETES_VERSION ) --arch=$(ENVTEST_ARCH ) --bin-dir=$(ENVTEST_ASSETS_DIR )
209192
210193libgit2 : $(LIBGIT2 ) # # Detect or download libgit2 library
211194
@@ -237,3 +220,17 @@ ifneq (, $(shell git status --porcelain --untracked-files=no))
237220 exit 1; \
238221 }
239222endif
223+
224+ # go-install-tool will 'go install' any package $2 and install it to $1.
225+ PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST ) ) ) )
226+ define go-install-tool
227+ @[ -f $(1 ) ] || { \
228+ set -e ;\
229+ TMP_DIR=$$(mktemp -d ) ;\
230+ cd $$TMP_DIR ;\
231+ go mod init tmp ;\
232+ echo "Downloading $(2 ) " ;\
233+ GOBIN=$(PROJECT_DIR ) /bin go install $(2 ) ;\
234+ rm -rf $$TMP_DIR ;\
235+ }
236+ endef
0 commit comments