From 5d30c9a46a121d9ec5a76f2fc02d49a62a98bad2 Mon Sep 17 00:00:00 2001 From: Sanskar Jaiswal Date: Wed, 12 Jan 2022 17:34:00 +0530 Subject: [PATCH] fix makefile envtest and controller-gen usage Refactor logic to install helper tools into one function in the Makefile. Add support for envtest to help install tools like kubectl, etcd which helps users run tests more conveniently. Signed-off-by: Sanskar Jaiswal --- .github/workflows/build.yaml | 4 -- .gitignore | 1 + Makefile | 72 ++++++++++++++++++++---------------- 3 files changed, 41 insertions(+), 36 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 1922fc84..ccf43f0b 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -38,12 +38,8 @@ jobs: uses: actions/setup-go@v2 with: go-version: 1.17.x - - name: Set up kubebuilder - uses: fluxcd/pkg/actions/kubebuilder@main - name: Run tests run: make test - env: - KUBEBUILDER_ASSETS: ${{ github.workspace }}/kubebuilder/bin - name: Check if working tree is dirty run: | if [[ $(git diff --stat) != '' ]]; then diff --git a/.gitignore b/.gitignore index 2f0c3c7d..c46c3907 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ notes *.so *.dylib bin +testbin # Test binary, build with `go test -c` *.test diff --git a/Makefile b/Makefile index f034cbd9..6dc6b7eb 100644 --- a/Makefile +++ b/Makefile @@ -15,11 +15,15 @@ else GOBIN=$(shell go env GOBIN) endif +# Architecture to use envtest with +ENVTEST_ARCH ?= amd64 + all: manager # Run tests -test: generate fmt vet manifests api-docs - go test ./... -coverprofile cover.out +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 install-envtest + KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS) go test ./... -coverprofile cover.out cd api; go test ./... -coverprofile cover.out # Build manager binary @@ -56,7 +60,7 @@ manifests: controller-gen # Generate API reference documentation api-docs: gen-crd-api-reference-docs - $(API_REF_GEN) -api-dir=./api/v1beta1 -config=./hack/api-docs/config.json -template-dir=./hack/api-docs/template -out-file=./docs/api/image-reflector.md + $(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/image-reflector.md # Run go mod tidy tidy: @@ -93,35 +97,39 @@ docker-push: docker-deploy: kubectl -n flux-system set image deployment/image-reflector-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 + $(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) + +ENVTEST_ASSETS_DIR=$(shell pwd)/testbin +ENVTEST_KUBERNETES_VERSION?=latest +install-envtest: setup-envtest + mkdir -p ${ENVTEST_ASSETS_DIR} + $(ENVTEST) use $(ENVTEST_KUBERNETES_VERSION) --arch=$(ENVTEST_ARCH) --bin-dir=$(ENVTEST_ASSETS_DIR) + +# 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