Skip to content

Commit 56b0cac

Browse files
committed
Update Makefile to make using envtest and controller-gen easier
Rework makefile rules to automatically download binaries needed for running envtest tests. New versions of envtest ship a binary instead of shell script, making the install process different, so the new rules are copied from the Makefile bootstrapped by recent versions of controller-gen Signed-off-by: Angel Misevski <[email protected]>
1 parent d69efe9 commit 56b0cac

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

Makefile

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ endif
5050
OPERATOR_SDK_VERSION = v1.8.0
5151
OPM_VERSION = v1.19.5
5252

53-
CONTROLLER_GEN_VERSION = v0.6.1
54-
CONTROLLER_GEN=$(GOBIN)/controller-gen-$(CONTROLLER_GEN_VERSION)
55-
5653
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
5754
CRD_OPTIONS ?= "crd:crdVersions=v1,trivialVersions=true"
5855

@@ -98,11 +95,8 @@ update_devworkspace_crds:
9895
###### End rules for dealing with devfile/api
9996

10097
### test: Runs tests
101-
ENVTEST_ASSETS_DIR = $(shell pwd)/bin/testbin
102-
test: generate fmt vet manifests
103-
mkdir -p $(ENVTEST_ASSETS_DIR)
104-
test -f $(ENVTEST_ASSETS_DIR)/setup-envtest.sh || curl -sSLo $(ENVTEST_ASSETS_DIR)/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v0.6.3/hack/setup-envtest.sh
105-
source $(ENVTEST_ASSETS_DIR)/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test $(shell go list ./... | grep -v test/e2e) -coverprofile cover.out
98+
test: generate fmt vet manifests envtest
99+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test $(shell go list ./... | grep -v test/e2e) -coverprofile cover.out
106100

107101
### test_e2e: Runs e2e test on the cluster set in context. DevWorkspace Operator must be already deployed
108102
test_e2e:
@@ -195,24 +189,6 @@ docker-push:
195189
endif
196190
$(DOCKER) push ${DWO_IMG}
197191

198-
### controller-gen: Finds or downloads controller-gen
199-
# download controller-gen if necessary
200-
controller-gen:
201-
ifeq (, $(shell which controller-gen-$(CONTROLLER_GEN_VERSION) 2>/dev/null))
202-
@echo "Installing controller gen as $(GOBIN)/controller-gen-$(CONTROLLER_GEN_VERSION)"
203-
@{ \
204-
set -e ;\
205-
CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\
206-
cd $$CONTROLLER_GEN_TMP_DIR ;\
207-
go mod init tmp ;\
208-
go get -d -v sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_GEN_VERSION)
209-
go build -o $(GOBIN)/controller-gen-$(CONTROLLER_GEN_VERSION) sigs.k8s.io/controller-tools/cmd/controller-gen
210-
rm -rf $$CONTROLLER_GEN_TMP_DIR ;\
211-
}
212-
else
213-
@echo "Using installed $(GOBIN)/controller-gen-$(CONTROLLER_GEN_VERSION)"
214-
endif
215-
216192
### compile-devworkspace-controller: Compiles the devworkspace-controller binary
217193
.PHONY: compile-devworkspace-controller
218194
compile-devworkspace-controller:
@@ -249,3 +225,25 @@ help: Makefile
249225
@echo ' ROUTING_SUFFIX - Cluster routing suffix (e.g. $$(minikube ip).nip.io, apps-crc.testing)'
250226
@echo ' PULL_POLICY - Image pull policy for controller'
251227
@echo ' DEVWORKSPACE_API_VERSION - Branch or tag of the github.com/devfile/api to depend on. Defaults to master'
228+
229+
# Automatic setup of required binaries: controller-gen, envtest
230+
LOCALBIN ?= $(shell pwd)/bin
231+
$(LOCALBIN):
232+
mkdir -p $(LOCALBIN)
233+
## Tool Binaries
234+
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
235+
CONTROLLER_GEN_VERSION = v0.6.1
236+
ENVTEST ?= $(LOCALBIN)/setup-envtest
237+
ENVTEST_K8S_VERSION = 1.21.2
238+
239+
### controller-gen: Finds or downloads controller-gen
240+
# download controller-gen if necessary
241+
.PHONY: controller-gen
242+
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
243+
$(CONTROLLER_GEN): $(LOCALBIN)
244+
test -s $(LOCALBIN)/controller-gen || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_GEN_VERSION)
245+
246+
.PHONY: envtest
247+
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
248+
$(ENVTEST): $(LOCALBIN)
249+
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest

0 commit comments

Comments
 (0)