11# Image URL to use all building/pushing image targets
22IMG ?= controller:latest
3- # ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
4- ENVTEST_K8S_VERSION = 1.31.0
53
64# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
75ifeq (,$(shell go env GOBIN) )
@@ -60,13 +58,37 @@ vet: ## Run go vet against code.
6058 go vet ./...
6159
6260.PHONY : test
63- test : manifests generate fmt vet envtest # # Run tests.
61+ test : manifests generate fmt vet setup- envtest # # Run tests.
6462 KUBEBUILDER_ASSETS=" $( shell $( ENVTEST) use $( ENVTEST_K8S_VERSION) --bin-dir $( LOCALBIN) -p path) " go test $$(go list ./... | grep -v /e2e ) -coverprofile cover.out
6563
66- # Utilize Kind or modify the e2e tests to load the image locally, enabling compatibility with other vendors.
67- .PHONY : test-e2e # Run the e2e tests against a Kind k8s instance that is spun up.
68- test-e2e :
69- go test ./test/e2e/ -v -ginkgo.v
64+ # TODO(user): To use a different vendor for e2e tests, modify the setup under 'tests/e2e'.
65+ # The default setup assumes Kind is pre-installed and builds/loads the Manager Docker image locally.
66+ # CertManager is installed by default; skip with:
67+ # - CERT_MANAGER_INSTALL_SKIP=true
68+ KIND_CLUSTER ?= moperator-test-e2e
69+
70+ .PHONY : setup-test-e2e
71+ setup-test-e2e : # # Set up a Kind cluster for e2e tests if it does not exist
72+ @command -v $(KIND ) > /dev/null 2>&1 || { \
73+ echo " Kind is not installed. Please install Kind manually." ; \
74+ exit 1; \
75+ }
76+ @case " $$ ($( KIND) get clusters)" in \
77+ * " $( KIND_CLUSTER) " * ) \
78+ echo " Kind cluster '$( KIND_CLUSTER) ' already exists. Skipping creation." ;; \
79+ * ) \
80+ echo " Creating Kind cluster '$( KIND_CLUSTER) '..." ; \
81+ $(KIND ) create cluster --name $(KIND_CLUSTER ) ;; \
82+ esac
83+
84+ .PHONY : test-e2e
85+ test-e2e : setup-test-e2e manifests generate fmt vet # # Run the e2e tests. Expected an isolated environment using Kind.
86+ KIND=$(KIND ) KIND_CLUSTER=$(KIND_CLUSTER ) go test -tags=e2e ./test/e2e/ -v -ginkgo.v
87+ $(MAKE ) cleanup-test-e2e
88+
89+ .PHONY : cleanup-test-e2e
90+ cleanup-test-e2e : # # Tear down the Kind cluster used for e2e tests
91+ @$(KIND ) delete cluster --name $(KIND_CLUSTER )
7092
7193.PHONY : lint
7294lint : golangci-lint # # Run golangci-lint linter
@@ -76,6 +98,10 @@ lint: golangci-lint ## Run golangci-lint linter
7698lint-fix : golangci-lint # # Run golangci-lint linter and perform fixes
7799 $(GOLANGCI_LINT ) run --fix
78100
101+ .PHONY : lint-config
102+ lint-config : golangci-lint # # Verify golangci-lint linter configuration
103+ $(GOLANGCI_LINT ) config verify
104+
79105# #@ Build
80106
81107.PHONY : build
@@ -84,7 +110,7 @@ build: manifests generate fmt vet ## Build manager binary.
84110
85111.PHONY : run
86112run : manifests generate fmt vet # # Run a controller from your host.
87- source etc/config.env && go run ./cmd/main.go
113+ go run ./cmd/main.go
88114
89115# If you wish to build the manager image targeting other platforms you can use the --platform flag.
90116# (i.e. docker build --platform linux/arm64). However, you must enable docker buildKit for it.
@@ -152,16 +178,20 @@ $(LOCALBIN):
152178
153179# # Tool Binaries
154180KUBECTL ?= kubectl
181+ KIND ?= kind
155182KUSTOMIZE ?= $(LOCALBIN ) /kustomize
156183CONTROLLER_GEN ?= $(LOCALBIN ) /controller-gen
157184ENVTEST ?= $(LOCALBIN ) /setup-envtest
158185GOLANGCI_LINT = $(LOCALBIN ) /golangci-lint
159186
160187# # Tool Versions
161- KUSTOMIZE_VERSION ?= v5.4.3
162- CONTROLLER_TOOLS_VERSION ?= v0.16.1
163- ENVTEST_VERSION ?= release-0.19
164- GOLANGCI_LINT_VERSION ?= v1.59.1
188+ KUSTOMIZE_VERSION ?= v5.6.0
189+ CONTROLLER_TOOLS_VERSION ?= v0.18.0
190+ # ENVTEST_VERSION is the version of controller-runtime release branch to fetch the envtest setup script (i.e. release-0.20)
191+ ENVTEST_VERSION ?= $(shell go list -m -f "{{ .Version }}" sigs.k8s.io/controller-runtime | awk -F'[v.]' '{printf "release-% d.% d", $$2, $$3}')
192+ # ENVTEST_K8S_VERSION is the version of Kubernetes to use for setting up ENVTEST binaries (i.e. 1.31)
193+ ENVTEST_K8S_VERSION ?= $(shell go list -m -f "{{ .Version }}" k8s.io/api | awk -F'[v.]' '{printf "1.% d", $$3}')
194+ GOLANGCI_LINT_VERSION ?= v2.3.0
165195
166196.PHONY : kustomize
167197kustomize : $(KUSTOMIZE ) # # Download kustomize locally if necessary.
@@ -173,6 +203,14 @@ controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessar
173203$(CONTROLLER_GEN ) : $(LOCALBIN )
174204 $(call go-install-tool,$(CONTROLLER_GEN ) ,sigs.k8s.io/controller-tools/cmd/controller-gen,$(CONTROLLER_TOOLS_VERSION ) )
175205
206+ .PHONY : setup-envtest
207+ setup-envtest : envtest # # Download the binaries required for ENVTEST in the local bin directory.
208+ @echo " Setting up envtest binaries for Kubernetes version $( ENVTEST_K8S_VERSION) ..."
209+ @$(ENVTEST ) use $(ENVTEST_K8S_VERSION ) --bin-dir $(LOCALBIN ) -p path || { \
210+ echo " Error: Failed to set up envtest binaries for version $( ENVTEST_K8S_VERSION) ." ; \
211+ exit 1; \
212+ }
213+
176214.PHONY : envtest
177215envtest : $(ENVTEST ) # # Download setup-envtest locally if necessary.
178216$(ENVTEST ) : $(LOCALBIN )
@@ -181,20 +219,20 @@ $(ENVTEST): $(LOCALBIN)
181219.PHONY : golangci-lint
182220golangci-lint : $(GOLANGCI_LINT ) # # Download golangci-lint locally if necessary.
183221$(GOLANGCI_LINT ) : $(LOCALBIN )
184- $(call go-install-tool,$(GOLANGCI_LINT ) ,github.com/golangci/golangci-lint/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION ) )
222+ $(call go-install-tool,$(GOLANGCI_LINT ) ,github.com/golangci/golangci-lint/v2/ cmd/golangci-lint,$(GOLANGCI_LINT_VERSION ) )
185223
186224# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
187225# $1 - target path with name of binary
188226# $2 - package url which can be installed
189227# $3 - specific version of package
190228define go-install-tool
191- @[ -f "$(1 ) -$(3 ) " ] || { \
229+ @[ -f "$(1 ) -$(3 ) " ] && [ " $$( readlink -- " $( 1 ) " 2>/dev/null ) " = " $( 1 ) - $( 3 ) " ] || { \
192230set -e; \
193231package=$(2 ) @$(3 ) ;\
194232echo "Downloading $${package}" ;\
195- rm -f $(1 ) || true ;\
233+ rm -f $(1 ) ;\
196234GOBIN=$(LOCALBIN ) go install $${package} ;\
197235mv $(1 ) $(1 ) -$(3 ) ;\
198236} ;\
199- ln -sf $( 1 ) -$(3 ) $(1 )
237+ ln -sf $$( realpath $( 1 ) -$(3 ) ) $(1 )
200238endef
0 commit comments