From 9075b72d576c2683f2a51fd2ba70cf7f33d5ea65 Mon Sep 17 00:00:00 2001 From: Sanskar Jaiswal Date: Tue, 11 Jan 2022 20:22:34 +0530 Subject: [PATCH] use go install instead of go get in Makefile Signed-off-by: Sanskar Jaiswal --- Makefile | 81 ++++++++++++++++++++++++-------------------------------- 1 file changed, 34 insertions(+), 47 deletions(-) diff --git a/Makefile b/Makefile index d2304e99..eff702b0 100644 --- a/Makefile +++ b/Makefile @@ -16,16 +16,20 @@ BUILD_ARGS ?= # Architectures to build images for. BUILD_PLATFORMS ?= linux/amd64 +# Architecture to use envtest with +ENVTEST_ARCH ?= amd64 + all: manager # Download the envtest binaries to testbin ENVTEST_ASSETS_DIR=$(shell pwd)/testbin ENVTEST_KUBERNETES_VERSION?=latest install-envtest: setup-envtest - $(SETUP_ENVTEST) use $(ENVTEST_KUBERNETES_VERSION) --bin-dir=$(ENVTEST_ASSETS_DIR) + mkdir -p ${ENVTEST_ASSETS_DIR} + $(ENVTEST) use $(ENVTEST_KUBERNETES_VERSION) --arch=$(ENVTEST_ARCH) --bin-dir=$(ENVTEST_ASSETS_DIR) # Run controller tests -KUBEBUILDER_ASSETS?="$(shell $(SETUP_ENVTEST) use -i $(ENVTEST_KUBERNETES_VERSION) --bin-dir=$(ENVTEST_ASSETS_DIR) -p path)" +KUBEBUILDER_ASSETS?="$(shell $(ENVTEST) --arch=$(ENVTEST_ARCH) use -i $(ENVTEST_KUBERNETES_VERSION) --bin-dir=$(ENVTEST_ASSETS_DIR) -p path)" test: generate fmt vet manifests api-docs download-crd-deps install-envtest KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS) go test ./controllers/... -v -coverprofile cover.out @@ -76,7 +80,7 @@ manifests: controller-gen # Generate API reference documentation api-docs: gen-crd-api-reference-docs - $(API_REF_GEN) -api-dir=./api/v1beta2 -config=./hack/api-docs/config.json -template-dir=./hack/api-docs/template -out-file=./docs/api/kustomize.md + $(GEN_CRD_API_REFERENCE_DOCS) -api-dir=./api/v1beta2 -config=./hack/api-docs/config.json -template-dir=./hack/api-docs/template -out-file=./docs/api/kustomize.md # Run go mod tidy tidy: @@ -113,50 +117,33 @@ docker-push: docker-deploy: kubectl -n flux-system set image deployment/kustomize-controller manager=${IMG} -# find or download controller-gen -# download controller-gen if necessary -controller-gen: -ifeq (, $(shell which controller-gen)) - @{ \ - set -e ;\ - CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\ - cd $$CONTROLLER_GEN_TMP_DIR ;\ - go mod init tmp ;\ - go get sigs.k8s.io/controller-tools/cmd/controller-gen@v0.7.0 ;\ - rm -rf $$CONTROLLER_GEN_TMP_DIR ;\ - } -CONTROLLER_GEN=$(GOBIN)/controller-gen -else -CONTROLLER_GEN=$(shell which controller-gen) -endif +# Find or download controller-gen +CONTROLLER_GEN = $(shell pwd)/bin/controller-gen +.PHONY: controller-gen +controller-gen: ## Download controller-gen locally if necessary. + $(call go-install-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen@v0.7.0) # Find or download gen-crd-api-reference-docs +GEN_CRD_API_REFERENCE_DOCS = $(shell pwd)/bin/gen-crd-api-reference-docs +.PHONY: gen-crd-api-reference-docs gen-crd-api-reference-docs: -ifeq (, $(shell which gen-crd-api-reference-docs)) - @{ \ - set -e ;\ - API_REF_GEN_TMP_DIR=$$(mktemp -d) ;\ - cd $$API_REF_GEN_TMP_DIR ;\ - go mod init tmp ;\ - go get github.com/ahmetb/gen-crd-api-reference-docs@v0.2.0 ;\ - rm -rf $$API_REF_GEN_TMP_DIR ;\ - } -API_REF_GEN=$(GOBIN)/gen-crd-api-reference-docs -else -API_REF_GEN=$(shell which gen-crd-api-reference-docs) -endif - -setup-envtest: -ifeq (, $(shell which setup-envtest)) - @{ \ - set -e ;\ - SETUP_ENVTEST_TMP_DIR=$$(mktemp -d) ;\ - cd $$SETUP_ENVTEST_TMP_DIR ;\ - go mod init tmp ;\ - go get sigs.k8s.io/controller-runtime/tools/setup-envtest@latest ;\ - rm -rf $$SETUP_ENVTEST_TMP_DIR ;\ - } -SETUP_ENVTEST=$(GOBIN)/setup-envtest -else -SETUP_ENVTEST=$(shell which setup-envtest) -endif + $(call go-install-tool,$(GEN_CRD_API_REFERENCE_DOCS),github.com/ahmetb/gen-crd-api-reference-docs@v0.3.0) + +ENVTEST = $(shell pwd)/bin/setup-envtest +.PHONY: envtest +setup-envtest: ## Download envtest-setup locally if necessary. + $(call go-install-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest@latest) + +# go-install-tool will 'go install' any package $2 and install it to $1. +PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST)))) +define go-install-tool +@[ -f $(1) ] || { \ +set -e ;\ +TMP_DIR=$$(mktemp -d) ;\ +cd $$TMP_DIR ;\ +go mod init tmp ;\ +echo "Downloading $(2)" ;\ +GOBIN=$(PROJECT_DIR)/bin go install $(2) ;\ +rm -rf $$TMP_DIR ;\ +} +endef