Skip to content

Commit 97f701a

Browse files
committed
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 <[email protected]>
1 parent 6432519 commit 97f701a

File tree

3 files changed

+37
-22
lines changed

3 files changed

+37
-22
lines changed

.github/workflows/test.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,8 @@ jobs:
2323
uses: actions/setup-go@v2
2424
with:
2525
go-version: 1.16.x
26-
- name: Setup Kubebuilder
27-
uses: fluxcd/pkg/actions/kubebuilder@main
2826
- name: Run tests
2927
run: make test
30-
env:
31-
KUBEBUILDER_ASSETS: ${{ github.workspace }}/kubebuilder/bin
3228
- name: Check if working tree is dirty
3329
run: |
3430
if [[ $(git diff --stat) != '' ]]; then

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313

1414
# Local build output dir
1515
bin/
16+
testbin/

Makefile

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@ else
1111
GOBIN=$(shell go env GOBIN)
1212
endif
1313

14+
# Architecture to use envtest with
15+
ENVTEST_ARCH ?= amd64
16+
1417
all: manager
1518

1619
# Run tests
17-
test: generate fmt vet manifests
18-
go test ./... -coverprofile cover.out
20+
KUBEBUILDER_ASSETS?="$(shell $(ENVTEST) --arch=$(ENVTEST_ARCH) use -i $(ENVTEST_KUBERNETES_VERSION) --bin-dir=$(ENVTEST_ASSETS_DIR) -p path)"
21+
test: generate fmt vet manifests install-envtest
22+
KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS) go test ./... -coverprofile cover.out
1923

2024
# Build manager binary
2125
manager: generate fmt vet
@@ -62,19 +66,33 @@ docker-build: test
6266
docker-push:
6367
docker push ${IMG}
6468

65-
# find or download controller-gen
66-
# download controller-gen if necessary
67-
controller-gen:
68-
ifeq (, $(shell which controller-gen))
69-
@{ \
70-
set -e ;\
71-
CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\
72-
cd $$CONTROLLER_GEN_TMP_DIR ;\
73-
go mod init tmp ;\
74-
go get sigs.k8s.io/controller-tools/cmd/[email protected] ;\
75-
rm -rf $$CONTROLLER_GEN_TMP_DIR ;\
76-
}
77-
CONTROLLER_GEN=$(GOBIN)/controller-gen
78-
else
79-
CONTROLLER_GEN=$(shell which controller-gen)
80-
endif
69+
# Find or download controller-gen
70+
CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
71+
.PHONY: controller-gen
72+
controller-gen: ## Download controller-gen locally if necessary.
73+
$(call go-install-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/[email protected])
74+
75+
ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
76+
ENVTEST_KUBERNETES_VERSION?=latest
77+
install-envtest: setup-envtest
78+
mkdir -p ${ENVTEST_ASSETS_DIR}
79+
$(ENVTEST) use $(ENVTEST_KUBERNETES_VERSION) --arch=$(ENVTEST_ARCH) --bin-dir=$(ENVTEST_ASSETS_DIR)
80+
81+
ENVTEST = $(shell pwd)/bin/setup-envtest
82+
.PHONY: envtest
83+
setup-envtest: ## Download envtest-setup locally if necessary.
84+
$(call go-install-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest@latest)
85+
86+
# go-install-tool will 'go install' any package $2 and install it to $1.
87+
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
88+
define go-install-tool
89+
@[ -f $(1) ] || { \
90+
set -e ;\
91+
TMP_DIR=$$(mktemp -d) ;\
92+
cd $$TMP_DIR ;\
93+
go mod init tmp ;\
94+
echo "Downloading $(2)" ;\
95+
GOBIN=$(PROJECT_DIR)/bin go install $(2) ;\
96+
rm -rf $$TMP_DIR ;\
97+
}
98+
endef

0 commit comments

Comments
 (0)