Skip to content

Commit bfb7c3d

Browse files
authored
Merge pull request #5096 from Shubhamag12/issue-5088
✨ (go/v4): Allow `ENV TEST` version to be derived from `replaces`
2 parents 25d1717 + 757e9f4 commit bfb7c3d

File tree

7 files changed

+91
-21
lines changed

7 files changed

+91
-21
lines changed

docs/book/src/cronjob-tutorial/testdata/project/Makefile

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,18 @@ GOLANGCI_LINT = $(LOCALBIN)/golangci-lint
193193
## Tool Versions
194194
KUSTOMIZE_VERSION ?= v5.7.1
195195
CONTROLLER_TOOLS_VERSION ?= v0.19.0
196+
196197
#ENVTEST_VERSION is the version of controller-runtime release branch to fetch the envtest setup script (i.e. release-0.20)
197-
ENVTEST_VERSION ?= $(shell go list -m -f "{{ .Version }}" sigs.k8s.io/controller-runtime | awk -F'[v.]' '{printf "release-%d.%d", $$2, $$3}')
198+
ENVTEST_VERSION ?= $(shell v='$(call gomodver,sigs.k8s.io/controller-runtime)'; \
199+
[ -n "$$v" ] || { echo "Set ENVTEST_VERSION manually (controller-runtime replace has no tag)" >&2; exit 1; }; \
200+
printf '%s\n' "$$v" | sed -E 's/^v?([0-9]+)\.([0-9]+).*/release-\1.\2/')
201+
198202
#ENVTEST_K8S_VERSION is the version of Kubernetes to use for setting up ENVTEST binaries (i.e. 1.31)
199-
ENVTEST_K8S_VERSION ?= $(shell go list -m -f "{{ .Version }}" k8s.io/api | awk -F'[v.]' '{printf "1.%d", $$3}')
200-
GOLANGCI_LINT_VERSION ?= v2.5.0
203+
ENVTEST_K8S_VERSION ?= $(shell v='$(call gomodver,k8s.io/api)'; \
204+
[ -n "$$v" ] || { echo "Set ENVTEST_K8S_VERSION manually (k8s.io/api replace has no tag)" >&2; exit 1; }; \
205+
printf '%s\n' "$$v" | sed -E 's/^v?[0-9]+\.([0-9]+).*/1.\1/')
201206

207+
GOLANGCI_LINT_VERSION ?= v2.5.0
202208
.PHONY: kustomize
203209
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
204210
$(KUSTOMIZE): $(LOCALBIN)
@@ -242,3 +248,7 @@ mv "$(LOCALBIN)/$$(basename "$(1)")" "$(1)-$(3)" ;\
242248
} ;\
243249
ln -sf "$$(realpath "$(1)-$(3)")" "$(1)"
244250
endef
251+
252+
define gomodver
253+
$(shell go list -m -f '{{if .Replace}}{{.Replace.Version}}{{else}}{{.Version}}{{end}}' $(1) 2>/dev/null)
254+
endef

docs/book/src/getting-started/testdata/project/Makefile

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,18 @@ GOLANGCI_LINT = $(LOCALBIN)/golangci-lint
189189
## Tool Versions
190190
KUSTOMIZE_VERSION ?= v5.7.1
191191
CONTROLLER_TOOLS_VERSION ?= v0.19.0
192+
192193
#ENVTEST_VERSION is the version of controller-runtime release branch to fetch the envtest setup script (i.e. release-0.20)
193-
ENVTEST_VERSION ?= $(shell go list -m -f "{{ .Version }}" sigs.k8s.io/controller-runtime | awk -F'[v.]' '{printf "release-%d.%d", $$2, $$3}')
194+
ENVTEST_VERSION ?= $(shell v='$(call gomodver,sigs.k8s.io/controller-runtime)'; \
195+
[ -n "$$v" ] || { echo "Set ENVTEST_VERSION manually (controller-runtime replace has no tag)" >&2; exit 1; }; \
196+
printf '%s\n' "$$v" | sed -E 's/^v?([0-9]+)\.([0-9]+).*/release-\1.\2/')
197+
194198
#ENVTEST_K8S_VERSION is the version of Kubernetes to use for setting up ENVTEST binaries (i.e. 1.31)
195-
ENVTEST_K8S_VERSION ?= $(shell go list -m -f "{{ .Version }}" k8s.io/api | awk -F'[v.]' '{printf "1.%d", $$3}')
196-
GOLANGCI_LINT_VERSION ?= v2.5.0
199+
ENVTEST_K8S_VERSION ?= $(shell v='$(call gomodver,k8s.io/api)'; \
200+
[ -n "$$v" ] || { echo "Set ENVTEST_K8S_VERSION manually (k8s.io/api replace has no tag)" >&2; exit 1; }; \
201+
printf '%s\n' "$$v" | sed -E 's/^v?[0-9]+\.([0-9]+).*/1.\1/')
197202

203+
GOLANGCI_LINT_VERSION ?= v2.5.0
198204
.PHONY: kustomize
199205
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
200206
$(KUSTOMIZE): $(LOCALBIN)
@@ -238,3 +244,7 @@ mv "$(LOCALBIN)/$$(basename "$(1)")" "$(1)-$(3)" ;\
238244
} ;\
239245
ln -sf "$$(realpath "$(1)-$(3)")" "$(1)"
240246
endef
247+
248+
define gomodver
249+
$(shell go list -m -f '{{if .Replace}}{{.Replace.Version}}{{else}}{{.Version}}{{end}}' $(1) 2>/dev/null)
250+
endef

docs/book/src/multiversion-tutorial/testdata/project/Makefile

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,18 @@ GOLANGCI_LINT = $(LOCALBIN)/golangci-lint
193193
## Tool Versions
194194
KUSTOMIZE_VERSION ?= v5.7.1
195195
CONTROLLER_TOOLS_VERSION ?= v0.19.0
196+
196197
#ENVTEST_VERSION is the version of controller-runtime release branch to fetch the envtest setup script (i.e. release-0.20)
197-
ENVTEST_VERSION ?= $(shell go list -m -f "{{ .Version }}" sigs.k8s.io/controller-runtime | awk -F'[v.]' '{printf "release-%d.%d", $$2, $$3}')
198+
ENVTEST_VERSION ?= $(shell v='$(call gomodver,sigs.k8s.io/controller-runtime)'; \
199+
[ -n "$$v" ] || { echo "Set ENVTEST_VERSION manually (controller-runtime replace has no tag)" >&2; exit 1; }; \
200+
printf '%s\n' "$$v" | sed -E 's/^v?([0-9]+)\.([0-9]+).*/release-\1.\2/')
201+
198202
#ENVTEST_K8S_VERSION is the version of Kubernetes to use for setting up ENVTEST binaries (i.e. 1.31)
199-
ENVTEST_K8S_VERSION ?= $(shell go list -m -f "{{ .Version }}" k8s.io/api | awk -F'[v.]' '{printf "1.%d", $$3}')
200-
GOLANGCI_LINT_VERSION ?= v2.5.0
203+
ENVTEST_K8S_VERSION ?= $(shell v='$(call gomodver,k8s.io/api)'; \
204+
[ -n "$$v" ] || { echo "Set ENVTEST_K8S_VERSION manually (k8s.io/api replace has no tag)" >&2; exit 1; }; \
205+
printf '%s\n' "$$v" | sed -E 's/^v?[0-9]+\.([0-9]+).*/1.\1/')
201206

207+
GOLANGCI_LINT_VERSION ?= v2.5.0
202208
.PHONY: kustomize
203209
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
204210
$(KUSTOMIZE): $(LOCALBIN)
@@ -242,3 +248,7 @@ mv "$(LOCALBIN)/$$(basename "$(1)")" "$(1)-$(3)" ;\
242248
} ;\
243249
ln -sf "$$(realpath "$(1)-$(3)")" "$(1)"
244250
endef
251+
252+
define gomodver
253+
$(shell go list -m -f '{{if .Replace}}{{.Replace.Version}}{{else}}{{.Version}}{{end}}' $(1) 2>/dev/null)
254+
endef

pkg/plugins/golang/v4/scaffolds/internal/templates/makefile.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,12 +268,18 @@ GOLANGCI_LINT = $(LOCALBIN)/golangci-lint
268268
## Tool Versions
269269
KUSTOMIZE_VERSION ?= {{ .KustomizeVersion }}
270270
CONTROLLER_TOOLS_VERSION ?= {{ .ControllerToolsVersion }}
271+
271272
#ENVTEST_VERSION is the version of controller-runtime release branch to fetch the envtest setup script (i.e. release-0.20)
272-
ENVTEST_VERSION ?= $(shell go list -m -f "{{ "{{ .Version }}" }}" sigs.k8s.io/controller-runtime | awk -F'[v.]' '{printf "release-%d.%d", $$2, $$3}')
273+
ENVTEST_VERSION ?= $(shell v='$(call gomodver,sigs.k8s.io/controller-runtime)'; \
274+
[ -n "$$v" ] || { echo "Set ENVTEST_VERSION manually (controller-runtime replace has no tag)" >&2; exit 1; }; \
275+
printf '%s\n' "$$v" | sed -E 's/^v?([0-9]+)\.([0-9]+).*/release-\1.\2/')
276+
273277
#ENVTEST_K8S_VERSION is the version of Kubernetes to use for setting up ENVTEST binaries (i.e. 1.31)
274-
ENVTEST_K8S_VERSION ?= $(shell go list -m -f "{{ "{{ .Version }}" }}" k8s.io/api | awk -F'[v.]' '{printf "1.%d", $$3}')
275-
GOLANGCI_LINT_VERSION ?= {{ .GolangciLintVersion }}
278+
ENVTEST_K8S_VERSION ?= $(shell v='$(call gomodver,k8s.io/api)'; \
279+
[ -n "$$v" ] || { echo "Set ENVTEST_K8S_VERSION manually (k8s.io/api replace has no tag)" >&2; exit 1; }; \
280+
printf '%s\n' "$$v" | sed -E 's/^v?[0-9]+\.([0-9]+).*/1.\1/')
276281
282+
GOLANGCI_LINT_VERSION ?= {{ .GolangciLintVersion }}
277283
.PHONY: kustomize
278284
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
279285
$(KUSTOMIZE): $(LOCALBIN)
@@ -317,4 +323,8 @@ mv "$(LOCALBIN)/$$(basename "$(1)")" "$(1)-$(3)" ;\
317323
} ;\
318324
ln -sf "$$(realpath "$(1)-$(3)")" "$(1)"
319325
endef
326+
327+
define gomodver
328+
$(shell go list -m -f '{{"{{"}}if .Replace{{"}}"}}{{"{{"}}.Replace.Version{{"}}"}}{{"{{"}}else{{"}}"}}{{"{{"}}.Version{{"}}"}}{{"{{"}}end{{"}}"}}' $(1) 2>/dev/null)
329+
endef
320330
`

testdata/project-v4-multigroup/Makefile

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,18 @@ GOLANGCI_LINT = $(LOCALBIN)/golangci-lint
189189
## Tool Versions
190190
KUSTOMIZE_VERSION ?= v5.7.1
191191
CONTROLLER_TOOLS_VERSION ?= v0.19.0
192+
192193
#ENVTEST_VERSION is the version of controller-runtime release branch to fetch the envtest setup script (i.e. release-0.20)
193-
ENVTEST_VERSION ?= $(shell go list -m -f "{{ .Version }}" sigs.k8s.io/controller-runtime | awk -F'[v.]' '{printf "release-%d.%d", $$2, $$3}')
194+
ENVTEST_VERSION ?= $(shell v='$(call gomodver,sigs.k8s.io/controller-runtime)'; \
195+
[ -n "$$v" ] || { echo "Set ENVTEST_VERSION manually (controller-runtime replace has no tag)" >&2; exit 1; }; \
196+
printf '%s\n' "$$v" | sed -E 's/^v?([0-9]+)\.([0-9]+).*/release-\1.\2/')
197+
194198
#ENVTEST_K8S_VERSION is the version of Kubernetes to use for setting up ENVTEST binaries (i.e. 1.31)
195-
ENVTEST_K8S_VERSION ?= $(shell go list -m -f "{{ .Version }}" k8s.io/api | awk -F'[v.]' '{printf "1.%d", $$3}')
196-
GOLANGCI_LINT_VERSION ?= v2.5.0
199+
ENVTEST_K8S_VERSION ?= $(shell v='$(call gomodver,k8s.io/api)'; \
200+
[ -n "$$v" ] || { echo "Set ENVTEST_K8S_VERSION manually (k8s.io/api replace has no tag)" >&2; exit 1; }; \
201+
printf '%s\n' "$$v" | sed -E 's/^v?[0-9]+\.([0-9]+).*/1.\1/')
197202

203+
GOLANGCI_LINT_VERSION ?= v2.5.0
198204
.PHONY: kustomize
199205
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
200206
$(KUSTOMIZE): $(LOCALBIN)
@@ -238,3 +244,7 @@ mv "$(LOCALBIN)/$$(basename "$(1)")" "$(1)-$(3)" ;\
238244
} ;\
239245
ln -sf "$$(realpath "$(1)-$(3)")" "$(1)"
240246
endef
247+
248+
define gomodver
249+
$(shell go list -m -f '{{if .Replace}}{{.Replace.Version}}{{else}}{{.Version}}{{end}}' $(1) 2>/dev/null)
250+
endef

testdata/project-v4-with-plugins/Makefile

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,18 @@ GOLANGCI_LINT = $(LOCALBIN)/golangci-lint
189189
## Tool Versions
190190
KUSTOMIZE_VERSION ?= v5.7.1
191191
CONTROLLER_TOOLS_VERSION ?= v0.19.0
192+
192193
#ENVTEST_VERSION is the version of controller-runtime release branch to fetch the envtest setup script (i.e. release-0.20)
193-
ENVTEST_VERSION ?= $(shell go list -m -f "{{ .Version }}" sigs.k8s.io/controller-runtime | awk -F'[v.]' '{printf "release-%d.%d", $$2, $$3}')
194+
ENVTEST_VERSION ?= $(shell v='$(call gomodver,sigs.k8s.io/controller-runtime)'; \
195+
[ -n "$$v" ] || { echo "Set ENVTEST_VERSION manually (controller-runtime replace has no tag)" >&2; exit 1; }; \
196+
printf '%s\n' "$$v" | sed -E 's/^v?([0-9]+)\.([0-9]+).*/release-\1.\2/')
197+
194198
#ENVTEST_K8S_VERSION is the version of Kubernetes to use for setting up ENVTEST binaries (i.e. 1.31)
195-
ENVTEST_K8S_VERSION ?= $(shell go list -m -f "{{ .Version }}" k8s.io/api | awk -F'[v.]' '{printf "1.%d", $$3}')
196-
GOLANGCI_LINT_VERSION ?= v2.5.0
199+
ENVTEST_K8S_VERSION ?= $(shell v='$(call gomodver,k8s.io/api)'; \
200+
[ -n "$$v" ] || { echo "Set ENVTEST_K8S_VERSION manually (k8s.io/api replace has no tag)" >&2; exit 1; }; \
201+
printf '%s\n' "$$v" | sed -E 's/^v?[0-9]+\.([0-9]+).*/1.\1/')
197202

203+
GOLANGCI_LINT_VERSION ?= v2.5.0
198204
.PHONY: kustomize
199205
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
200206
$(KUSTOMIZE): $(LOCALBIN)
@@ -238,3 +244,7 @@ mv "$(LOCALBIN)/$$(basename "$(1)")" "$(1)-$(3)" ;\
238244
} ;\
239245
ln -sf "$$(realpath "$(1)-$(3)")" "$(1)"
240246
endef
247+
248+
define gomodver
249+
$(shell go list -m -f '{{if .Replace}}{{.Replace.Version}}{{else}}{{.Version}}{{end}}' $(1) 2>/dev/null)
250+
endef

testdata/project-v4/Makefile

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,18 @@ GOLANGCI_LINT = $(LOCALBIN)/golangci-lint
189189
## Tool Versions
190190
KUSTOMIZE_VERSION ?= v5.7.1
191191
CONTROLLER_TOOLS_VERSION ?= v0.19.0
192+
192193
#ENVTEST_VERSION is the version of controller-runtime release branch to fetch the envtest setup script (i.e. release-0.20)
193-
ENVTEST_VERSION ?= $(shell go list -m -f "{{ .Version }}" sigs.k8s.io/controller-runtime | awk -F'[v.]' '{printf "release-%d.%d", $$2, $$3}')
194+
ENVTEST_VERSION ?= $(shell v='$(call gomodver,sigs.k8s.io/controller-runtime)'; \
195+
[ -n "$$v" ] || { echo "Set ENVTEST_VERSION manually (controller-runtime replace has no tag)" >&2; exit 1; }; \
196+
printf '%s\n' "$$v" | sed -E 's/^v?([0-9]+)\.([0-9]+).*/release-\1.\2/')
197+
194198
#ENVTEST_K8S_VERSION is the version of Kubernetes to use for setting up ENVTEST binaries (i.e. 1.31)
195-
ENVTEST_K8S_VERSION ?= $(shell go list -m -f "{{ .Version }}" k8s.io/api | awk -F'[v.]' '{printf "1.%d", $$3}')
196-
GOLANGCI_LINT_VERSION ?= v2.5.0
199+
ENVTEST_K8S_VERSION ?= $(shell v='$(call gomodver,k8s.io/api)'; \
200+
[ -n "$$v" ] || { echo "Set ENVTEST_K8S_VERSION manually (k8s.io/api replace has no tag)" >&2; exit 1; }; \
201+
printf '%s\n' "$$v" | sed -E 's/^v?[0-9]+\.([0-9]+).*/1.\1/')
197202

203+
GOLANGCI_LINT_VERSION ?= v2.5.0
198204
.PHONY: kustomize
199205
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
200206
$(KUSTOMIZE): $(LOCALBIN)
@@ -238,3 +244,7 @@ mv "$(LOCALBIN)/$$(basename "$(1)")" "$(1)-$(3)" ;\
238244
} ;\
239245
ln -sf "$$(realpath "$(1)-$(3)")" "$(1)"
240246
endef
247+
248+
define gomodver
249+
$(shell go list -m -f '{{if .Replace}}{{.Replace.Version}}{{else}}{{.Version}}{{end}}' $(1) 2>/dev/null)
250+
endef

0 commit comments

Comments
 (0)