Skip to content
This repository was archived by the owner on Jul 18, 2022. It is now read-only.

Commit 942e980

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 7e0fa85 commit 942e980

File tree

4 files changed

+51
-38
lines changed

4 files changed

+51
-38
lines changed

.github/workflows/build.yaml

-6
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ jobs:
2020
key: ${{ runner.os }}-go-${{ hashFiles('go.sum') }}
2121
restore-keys: |
2222
${{ runner.os }}-go-
23-
- name: Set up kubebuilder
24-
uses: fluxcd/pkg/actions/kubebuilder@main
25-
- name: Setup envtest
26-
uses: fluxcd/pkg/actions/envtest@main
27-
with:
28-
version: "1.19.2"
2923
- name: Run tests
3024
uses: ./.github/actions/run-tests
3125
env:

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ controllers/testdata/crds/*
1111
*.so
1212
*.dylib
1313
bin
14+
testbin
1415

1516
# Test binary, build with `go test -c`
1617
*.test

Makefile

+44-31
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ TEST_CRDS := controllers/testdata/crds
7575
# Log level for `make run`
7676
LOG_LEVEL ?= info
7777

78+
# Architecture to use envtest with
79+
ENVTEST_ARCH ?= amd64
80+
7881
all: manager
7982

8083
# Running the tests requires the source.toolkit.fluxcd.io CRDs
@@ -101,15 +104,18 @@ ${CACHE}/imagepolicies_${REFLECTOR_VER}.yaml:
101104
curl -s --fail https://raw.githubusercontent.com/fluxcd/image-reflector-controller/${REFLECTOR_VER}/config/crd/bases/image.toolkit.fluxcd.io_imagepolicies.yaml \
102105
-o ${CACHE}/imagepolicies_${REFLECTOR_VER}.yaml
103106

104-
test: $(LIBGIT2) test-api test_deps generate fmt vet manifests api-docs ## Run tests
107+
KUBEBUILDER_ASSETS?="$(shell $(ENVTEST) --arch=$(ENVTEST_ARCH) use -i $(ENVTEST_KUBERNETES_VERSION) --bin-dir=$(ENVTEST_ASSETS_DIR) -p path)"
108+
test: $(LIBGIT2) test-api test_deps generate fmt vet manifests api-docs install-envtest ## Run tests
105109
ifeq ($(shell uname -s),Darwin)
106110
LD_LIBRARY_PATH=$(LIBGIT2_LIB_PATH) \
107111
PKG_CONFIG_PATH=$(MAKE_PKG_CONFIG_PATH) \
108112
CGO_LDFLAGS="-Wl,-rpath,$(LIBGIT2_LIB_PATH)" \
113+
KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS) \
109114
go test ./... -coverprofile cover.out
110115
else
111116
LD_LIBRARY_PATH=$(LIBGIT2_LIB_PATH) \
112117
PKG_CONFIG_PATH=$(MAKE_PKG_CONFIG_PATH) \
118+
KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS) \
113119
go test ./... -coverprofile cover.out
114120
endif
115121

@@ -130,7 +136,7 @@ endif
130136

131137
run: $(LIBGIT2) generate fmt vet manifests # Run against the configured Kubernetes cluster in ~/.kube/config
132138
ifeq ($(shell uname -s),Darwin)
133-
CGO_LDFLAGS="-Wl,-rpath,$(LIBGIT2_LIB_PATH)" \
139+
CGO_LDFLAGS="-Wl,-rpath,$(LIBGIT2_LIB_PATH)" \
134140
go run ./main.go --log-level=${LOG_LEVEL} --log-encoding=console
135141
else
136142
go run ./main.go --log-level=${LOG_LEVEL} --log-encoding=console
@@ -196,35 +202,11 @@ docker-push: ## Push the Docker image
196202
docker-deploy: ## Set the Docker image in-cluster
197203
kubectl -n flux-system set image deployment/image-automation-controller manager=$(IMG):$(TAG)
198204

199-
controller-gen: ## Find or download controller-gen
200-
ifeq (, $(shell which controller-gen))
201-
@{ \
202-
set -e ;\
203-
CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\
204-
cd $$CONTROLLER_GEN_TMP_DIR ;\
205-
go mod init tmp ;\
206-
go get sigs.k8s.io/controller-tools/cmd/[email protected] ;\
207-
rm -rf $$CONTROLLER_GEN_TMP_DIR ;\
208-
}
209-
CONTROLLER_GEN=$(GOBIN)/controller-gen
210-
else
211-
CONTROLLER_GEN=$(shell which controller-gen)
212-
endif
213-
214-
gen-crd-api-reference-docs: ## Find or download gen-crd-api-reference-docs
215-
ifeq (, $(shell which gen-crd-api-reference-docs))
216-
@{ \
217-
set -e ;\
218-
API_REF_GEN_TMP_DIR=$$(mktemp -d) ;\
219-
cd $$API_REF_GEN_TMP_DIR ;\
220-
go mod init tmp ;\
221-
go get github.com/ahmetb/[email protected] ;\
222-
rm -rf $$API_REF_GEN_TMP_DIR ;\
223-
}
224-
API_REF_GEN=$(GOBIN)/gen-crd-api-reference-docs
225-
else
226-
API_REF_GEN=$(shell which gen-crd-api-reference-docs)
227-
endif
205+
# Find or download controller-gen
206+
CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
207+
.PHONY: controller-gen
208+
controller-gen: ## Download controller-gen locally if necessary.
209+
$(call go-install-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/[email protected])
228210

229211
libgit2: $(LIBGIT2) ## Detect or download libgit2 library
230212

@@ -238,6 +220,37 @@ ifeq (1, $(LIBGIT2_FORCE))
238220
}
239221
endif
240222

223+
# Find or download gen-crd-api-reference-docs
224+
GEN_CRD_API_REFERENCE_DOCS = $(shell pwd)/bin/gen-crd-api-reference-docs
225+
.PHONY: gen-crd-api-reference-docs
226+
gen-crd-api-reference-docs:
227+
$(call go-install-tool,$(GEN_CRD_API_REFERENCE_DOCS),github.com/ahmetb/[email protected])
228+
229+
ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
230+
ENVTEST_KUBERNETES_VERSION?=latest
231+
install-envtest: setup-envtest
232+
mkdir -p ${ENVTEST_ASSETS_DIR}
233+
$(ENVTEST) use $(ENVTEST_KUBERNETES_VERSION) --arch=$(ENVTEST_ARCH) --bin-dir=$(ENVTEST_ASSETS_DIR)
234+
235+
ENVTEST = $(shell pwd)/bin/setup-envtest
236+
.PHONY: envtest
237+
setup-envtest: ## Download envtest-setup locally if necessary.
238+
$(call go-install-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest@latest)
239+
240+
# go-install-tool will 'go install' any package $2 and install it to $1.
241+
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
242+
define go-install-tool
243+
@[ -f $(1) ] || { \
244+
set -e ;\
245+
TMP_DIR=$$(mktemp -d) ;\
246+
cd $$TMP_DIR ;\
247+
go mod init tmp ;\
248+
echo "Downloading $(2)" ;\
249+
GOBIN=$(PROJECT_DIR)/bin go install $(2) ;\
250+
rm -rf $$TMP_DIR ;\
251+
}
252+
endef
253+
241254
.PHONY: help
242255
help: ## Display this help menu
243256
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-20s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

controllers/suite_test.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package controllers
1818

1919
import (
20+
"context"
2021
"path/filepath"
2122
"testing"
2223

@@ -45,6 +46,8 @@ var k8sClient client.Client
4546
var k8sManager ctrl.Manager
4647
var imageAutoReconciler *ImageUpdateAutomationReconciler
4748
var testEnv *envtest.Environment
49+
var ctx context.Context
50+
var cancel context.CancelFunc
4851

4952
func TestAPIs(t *testing.T) {
5053
RegisterFailHandler(Fail)
@@ -58,6 +61,7 @@ var _ = BeforeSuite(func(done Done) {
5861
ctrl.SetLogger(
5962
zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)),
6063
)
64+
ctx, cancel = context.WithCancel(context.TODO())
6165

6266
By("bootstrapping test environment")
6367
testEnv = &envtest.Environment{
@@ -91,7 +95,7 @@ var _ = BeforeSuite(func(done Done) {
9195

9296
go func() {
9397
defer GinkgoRecover()
94-
err = k8sManager.Start(ctrl.SetupSignalHandler())
98+
err = k8sManager.Start(ctx)
9599
Expect(err).ToNot(HaveOccurred())
96100
}()
97101

@@ -105,6 +109,7 @@ var _ = BeforeSuite(func(done Done) {
105109
}, 60)
106110

107111
var _ = AfterSuite(func() {
112+
cancel()
108113
By("tearing down the test environment")
109114
err := testEnv.Stop()
110115
Expect(err).ToNot(HaveOccurred())

0 commit comments

Comments
 (0)