@@ -17,6 +17,8 @@ limitations under the License.
17
17
package templates
18
18
19
19
import (
20
+ "strings"
21
+
20
22
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
21
23
)
22
24
@@ -38,6 +40,8 @@ type Makefile struct {
38
40
KustomizeVersion string
39
41
// ControllerRuntimeVersion version to be used to download the envtest setup script
40
42
ControllerRuntimeVersion string
43
+ // ControllerRuntimeReleaseBranchName store the name of the branch to be used to install setup-envtest
44
+ ControllerRuntimeReleaseBranchName string
41
45
}
42
46
43
47
// SetTemplateDefaults implements file.Template
@@ -54,9 +58,30 @@ func (f *Makefile) SetTemplateDefaults() error {
54
58
f .Image = "controller:latest"
55
59
}
56
60
61
+ if f .ControllerRuntimeReleaseBranchName == "" {
62
+ f .ControllerRuntimeReleaseBranchName = getControllerRuntimeReleaseBranch (f .ControllerRuntimeVersion )
63
+ }
64
+
57
65
return nil
58
66
}
59
67
68
+ func getControllerRuntimeReleaseBranch (version string ) string {
69
+ // Remove the "v" prefix
70
+ version = strings .TrimPrefix (version , "v" )
71
+
72
+ // Split the version string into its components
73
+ parts := strings .SplitN (version , "." , 3 )
74
+
75
+ // Check if we have at least two components (major and minor)
76
+ if len (parts ) < 2 {
77
+ // return latest working version as invalid
78
+ return "latest"
79
+ }
80
+
81
+ // Return the formatted version
82
+ return "release-" + parts [0 ] + "." + parts [1 ]
83
+ }
84
+
60
85
//nolint:lll
61
86
const makefileTemplate = `
62
87
# Image URL to use all building/pushing image targets
@@ -132,14 +157,6 @@ test: manifests generate fmt vet envtest ## Run tests.
132
157
.PHONY: test-e2e # Run the e2e tests against a Kind k8s instance that is spun up.
133
158
test-e2e:
134
159
go test ./test/e2e/ -v -ginkgo.v
135
-
136
- GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint
137
- GOLANGCI_LINT_VERSION ?= v1.54.2
138
- golangci-lint:
139
- @[ -f $(GOLANGCI_LINT) ] || { \
140
- set -e ;\
141
- curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell dirname $(GOLANGCI_LINT)) $(GOLANGCI_LINT_VERSION) ;\
142
- }
143
160
144
161
.PHONY: lint
145
162
lint: golangci-lint ## Run golangci-lint linter & yamllint
@@ -207,10 +224,10 @@ deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in
207
224
$(KUSTOMIZE) build config/default | $(KUBECTL) apply -f -
208
225
209
226
.PHONY: undeploy
210
- undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
227
+ undeploy: kustomize ## Undeploy controller from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
211
228
$(KUSTOMIZE) build config/default | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
212
229
213
- ##@ Build Dependencies
230
+ ##@ Dependencies
214
231
215
232
## Location to install dependencies to
216
233
LOCALBIN ?= $(shell pwd)/bin
@@ -219,31 +236,48 @@ $(LOCALBIN):
219
236
220
237
## Tool Binaries
221
238
KUBECTL ?= kubectl
222
- KUSTOMIZE ?= $(LOCALBIN)/kustomize
223
- CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
224
- ENVTEST ?= $(LOCALBIN)/setup-envtest
239
+ KUSTOMIZE ?= $(LOCALBIN)/kustomize-$(KUSTOMIZE_VERSION)
240
+ CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen-$(CONTROLLER_TOOLS_VERSION)
241
+ ENVTEST ?= $(LOCALBIN)/setup-envtest-$(ENVTEST_VERSION)
242
+ GOLANGCI_LINT = $(LOCALBIN)/golangci-lint-$(GOLANGCI_LINT_VERSION)
225
243
226
244
## Tool Versions
227
245
KUSTOMIZE_VERSION ?= {{ .KustomizeVersion }}
228
246
CONTROLLER_TOOLS_VERSION ?= {{ .ControllerToolsVersion }}
247
+ ENVTEST_VERSION ?= {{ .ControllerRuntimeReleaseBranchName }}
248
+ GOLANGCI_LINT_VERSION ?= v1.54.2
229
249
230
250
.PHONY: kustomize
231
- kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. If wrong version is installed, it will be removed before downloading.
251
+ kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
232
252
$(KUSTOMIZE): $(LOCALBIN)
233
- @if test -x $(LOCALBIN)/kustomize && ! $(LOCALBIN)/kustomize version | grep -q $(KUSTOMIZE_VERSION); then \
234
- echo "$(LOCALBIN)/kustomize version is not expected $(KUSTOMIZE_VERSION). Removing it before installing."; \
235
- rm -rf $(LOCALBIN)/kustomize; \
236
- fi
237
- test -s $(LOCALBIN)/kustomize || GOBIN=$(LOCALBIN) GO111MODULE=on go install sigs.k8s.io/kustomize/kustomize/v5@$(KUSTOMIZE_VERSION)
253
+ $(call go-install-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v5,$(KUSTOMIZE_VERSION))
238
254
239
255
.PHONY: controller-gen
240
- controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. If wrong version is installed, it will be overwritten.
256
+ controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
241
257
$(CONTROLLER_GEN): $(LOCALBIN)
242
- test -s $(LOCALBIN)/controller-gen && $(LOCALBIN)/controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION) || \
243
- GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
258
+ $(call go-install-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen,$(CONTROLLER_TOOLS_VERSION))
244
259
245
260
.PHONY: envtest
246
- envtest: $(ENVTEST) ## Download envtest- setup locally if necessary.
261
+ envtest: $(ENVTEST) ## Download setup-envtest locally if necessary.
247
262
$(ENVTEST): $(LOCALBIN)
248
- test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
263
+ $(call go-install-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest,$(ENVTEST_VERSION))
264
+
265
+ .PHONY: golangci-lint
266
+ golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
267
+ $(GOLANGCI_LINT): $(LOCALBIN)
268
+ $(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint,${GOLANGCI_LINT_VERSION})
269
+
270
+ # go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
271
+ # $1 - target path with name of binary (ideally with version)
272
+ # $2 - package url which can be installed
273
+ # $3 - specific version of package
274
+ define go-install-tool
275
+ @[ -f $(1) ] || { \
276
+ set -e; \
277
+ package=$(2)@$(3) ;\
278
+ echo "Downloading $${package}" ;\
279
+ GOBIN=$(LOCALBIN) go install $${package} ;\
280
+ mv "$$(echo "$(1)" | sed "s/-$(3)$$//")" $(1) ;\
281
+ }
282
+ endef
249
283
`
0 commit comments