Skip to content

Commit 951caf0

Browse files
authored
Merge pull request #3718 from lukas016/feature/update-makefile
✨ (go/v4) : Ensure versioning of tools and binaries installed via Makefile
2 parents 7fba82c + cce61e1 commit 951caf0

File tree

8 files changed

+289
-192
lines changed

8 files changed

+289
-192
lines changed

docs/book/src/component-config-tutorial/testdata/project/Makefile

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,6 @@ test: manifests generate fmt vet envtest ## Run tests.
6868
.PHONY: test-e2e # Run the e2e tests against a Kind k8s instance that is spun up.
6969
test-e2e:
7070
go test ./test/e2e/ -v -ginkgo.v
71-
72-
GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint
73-
GOLANGCI_LINT_VERSION ?= v1.54.2
74-
golangci-lint:
75-
@[ -f $(GOLANGCI_LINT) ] || { \
76-
set -e ;\
77-
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell dirname $(GOLANGCI_LINT)) $(GOLANGCI_LINT_VERSION) ;\
78-
}
7971

8072
.PHONY: lint
8173
lint: golangci-lint ## Run golangci-lint linter & yamllint
@@ -143,10 +135,10 @@ deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in
143135
$(KUSTOMIZE) build config/default | $(KUBECTL) apply -f -
144136

145137
.PHONY: undeploy
146-
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.
138+
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.
147139
$(KUSTOMIZE) build config/default | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
148140

149-
##@ Build Dependencies
141+
##@ Dependencies
150142

151143
## Location to install dependencies to
152144
LOCALBIN ?= $(shell pwd)/bin
@@ -155,30 +147,47 @@ $(LOCALBIN):
155147

156148
## Tool Binaries
157149
KUBECTL ?= kubectl
158-
KUSTOMIZE ?= $(LOCALBIN)/kustomize
159-
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
160-
ENVTEST ?= $(LOCALBIN)/setup-envtest
150+
KUSTOMIZE ?= $(LOCALBIN)/kustomize-$(KUSTOMIZE_VERSION)
151+
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen-$(CONTROLLER_TOOLS_VERSION)
152+
ENVTEST ?= $(LOCALBIN)/setup-envtest-$(ENVTEST_VERSION)
153+
GOLANGCI_LINT = $(LOCALBIN)/golangci-lint-$(GOLANGCI_LINT_VERSION)
161154

162155
## Tool Versions
163156
KUSTOMIZE_VERSION ?= v5.2.1
164157
CONTROLLER_TOOLS_VERSION ?= v0.13.0
158+
ENVTEST_VERSION ?= release-0.16
159+
GOLANGCI_LINT_VERSION ?= v1.54.2
165160

166161
.PHONY: kustomize
167-
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. If wrong version is installed, it will be removed before downloading.
162+
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
168163
$(KUSTOMIZE): $(LOCALBIN)
169-
@if test -x $(LOCALBIN)/kustomize && ! $(LOCALBIN)/kustomize version | grep -q $(KUSTOMIZE_VERSION); then \
170-
echo "$(LOCALBIN)/kustomize version is not expected $(KUSTOMIZE_VERSION). Removing it before installing."; \
171-
rm -rf $(LOCALBIN)/kustomize; \
172-
fi
173-
test -s $(LOCALBIN)/kustomize || GOBIN=$(LOCALBIN) GO111MODULE=on go install sigs.k8s.io/kustomize/kustomize/v5@$(KUSTOMIZE_VERSION)
164+
$(call go-install-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v5,$(KUSTOMIZE_VERSION))
174165

175166
.PHONY: controller-gen
176-
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. If wrong version is installed, it will be overwritten.
167+
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
177168
$(CONTROLLER_GEN): $(LOCALBIN)
178-
test -s $(LOCALBIN)/controller-gen && $(LOCALBIN)/controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION) || \
179-
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
169+
$(call go-install-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen,$(CONTROLLER_TOOLS_VERSION))
180170

181171
.PHONY: envtest
182-
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
172+
envtest: $(ENVTEST) ## Download setup-envtest locally if necessary.
183173
$(ENVTEST): $(LOCALBIN)
184-
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
174+
$(call go-install-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest,$(ENVTEST_VERSION))
175+
176+
.PHONY: golangci-lint
177+
golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
178+
$(GOLANGCI_LINT): $(LOCALBIN)
179+
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint,${GOLANGCI_LINT_VERSION})
180+
181+
# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
182+
# $1 - target path with name of binary (ideally with version)
183+
# $2 - package url which can be installed
184+
# $3 - specific version of package
185+
define go-install-tool
186+
@[ -f $(1) ] || { \
187+
set -e; \
188+
package=$(2)@$(3) ;\
189+
echo "Downloading $${package}" ;\
190+
GOBIN=$(LOCALBIN) go install $${package} ;\
191+
mv "$$(echo "$(1)" | sed "s/-$(3)$$//")" $(1) ;\
192+
}
193+
endef

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

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,6 @@ test: manifests generate fmt vet envtest ## Run tests.
6868
.PHONY: test-e2e # Run the e2e tests against a Kind k8s instance that is spun up.
6969
test-e2e:
7070
go test ./test/e2e/ -v -ginkgo.v
71-
72-
GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint
73-
GOLANGCI_LINT_VERSION ?= v1.54.2
74-
golangci-lint:
75-
@[ -f $(GOLANGCI_LINT) ] || { \
76-
set -e ;\
77-
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell dirname $(GOLANGCI_LINT)) $(GOLANGCI_LINT_VERSION) ;\
78-
}
7971

8072
.PHONY: lint
8173
lint: golangci-lint ## Run golangci-lint linter & yamllint
@@ -143,10 +135,10 @@ deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in
143135
$(KUSTOMIZE) build config/default | $(KUBECTL) apply -f -
144136

145137
.PHONY: undeploy
146-
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.
138+
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.
147139
$(KUSTOMIZE) build config/default | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
148140

149-
##@ Build Dependencies
141+
##@ Dependencies
150142

151143
## Location to install dependencies to
152144
LOCALBIN ?= $(shell pwd)/bin
@@ -155,30 +147,47 @@ $(LOCALBIN):
155147

156148
## Tool Binaries
157149
KUBECTL ?= kubectl
158-
KUSTOMIZE ?= $(LOCALBIN)/kustomize
159-
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
160-
ENVTEST ?= $(LOCALBIN)/setup-envtest
150+
KUSTOMIZE ?= $(LOCALBIN)/kustomize-$(KUSTOMIZE_VERSION)
151+
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen-$(CONTROLLER_TOOLS_VERSION)
152+
ENVTEST ?= $(LOCALBIN)/setup-envtest-$(ENVTEST_VERSION)
153+
GOLANGCI_LINT = $(LOCALBIN)/golangci-lint-$(GOLANGCI_LINT_VERSION)
161154

162155
## Tool Versions
163156
KUSTOMIZE_VERSION ?= v5.2.1
164157
CONTROLLER_TOOLS_VERSION ?= v0.13.0
158+
ENVTEST_VERSION ?= release-0.16
159+
GOLANGCI_LINT_VERSION ?= v1.54.2
165160

166161
.PHONY: kustomize
167-
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. If wrong version is installed, it will be removed before downloading.
162+
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
168163
$(KUSTOMIZE): $(LOCALBIN)
169-
@if test -x $(LOCALBIN)/kustomize && ! $(LOCALBIN)/kustomize version | grep -q $(KUSTOMIZE_VERSION); then \
170-
echo "$(LOCALBIN)/kustomize version is not expected $(KUSTOMIZE_VERSION). Removing it before installing."; \
171-
rm -rf $(LOCALBIN)/kustomize; \
172-
fi
173-
test -s $(LOCALBIN)/kustomize || GOBIN=$(LOCALBIN) GO111MODULE=on go install sigs.k8s.io/kustomize/kustomize/v5@$(KUSTOMIZE_VERSION)
164+
$(call go-install-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v5,$(KUSTOMIZE_VERSION))
174165

175166
.PHONY: controller-gen
176-
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. If wrong version is installed, it will be overwritten.
167+
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
177168
$(CONTROLLER_GEN): $(LOCALBIN)
178-
test -s $(LOCALBIN)/controller-gen && $(LOCALBIN)/controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION) || \
179-
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
169+
$(call go-install-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen,$(CONTROLLER_TOOLS_VERSION))
180170

181171
.PHONY: envtest
182-
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
172+
envtest: $(ENVTEST) ## Download setup-envtest locally if necessary.
183173
$(ENVTEST): $(LOCALBIN)
184-
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
174+
$(call go-install-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest,$(ENVTEST_VERSION))
175+
176+
.PHONY: golangci-lint
177+
golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
178+
$(GOLANGCI_LINT): $(LOCALBIN)
179+
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint,${GOLANGCI_LINT_VERSION})
180+
181+
# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
182+
# $1 - target path with name of binary (ideally with version)
183+
# $2 - package url which can be installed
184+
# $3 - specific version of package
185+
define go-install-tool
186+
@[ -f $(1) ] || { \
187+
set -e; \
188+
package=$(2)@$(3) ;\
189+
echo "Downloading $${package}" ;\
190+
GOBIN=$(LOCALBIN) go install $${package} ;\
191+
mv "$$(echo "$(1)" | sed "s/-$(3)$$//")" $(1) ;\
192+
}
193+
endef

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

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package templates
1818

1919
import (
20+
"strings"
21+
2022
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
2123
)
2224

@@ -38,6 +40,8 @@ type Makefile struct {
3840
KustomizeVersion string
3941
// ControllerRuntimeVersion version to be used to download the envtest setup script
4042
ControllerRuntimeVersion string
43+
// ControllerRuntimeReleaseBranchName store the name of the branch to be used to install setup-envtest
44+
ControllerRuntimeReleaseBranchName string
4145
}
4246

4347
// SetTemplateDefaults implements file.Template
@@ -54,9 +58,30 @@ func (f *Makefile) SetTemplateDefaults() error {
5458
f.Image = "controller:latest"
5559
}
5660

61+
if f.ControllerRuntimeReleaseBranchName == "" {
62+
f.ControllerRuntimeReleaseBranchName = getControllerRuntimeReleaseBranch(f.ControllerRuntimeVersion)
63+
}
64+
5765
return nil
5866
}
5967

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+
6085
//nolint:lll
6186
const makefileTemplate = `
6287
# Image URL to use all building/pushing image targets
@@ -132,14 +157,6 @@ test: manifests generate fmt vet envtest ## Run tests.
132157
.PHONY: test-e2e # Run the e2e tests against a Kind k8s instance that is spun up.
133158
test-e2e:
134159
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-
}
143160
144161
.PHONY: lint
145162
lint: golangci-lint ## Run golangci-lint linter & yamllint
@@ -207,10 +224,10 @@ deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in
207224
$(KUSTOMIZE) build config/default | $(KUBECTL) apply -f -
208225
209226
.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.
211228
$(KUSTOMIZE) build config/default | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
212229
213-
##@ Build Dependencies
230+
##@ Dependencies
214231
215232
## Location to install dependencies to
216233
LOCALBIN ?= $(shell pwd)/bin
@@ -219,31 +236,48 @@ $(LOCALBIN):
219236
220237
## Tool Binaries
221238
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)
225243
226244
## Tool Versions
227245
KUSTOMIZE_VERSION ?= {{ .KustomizeVersion }}
228246
CONTROLLER_TOOLS_VERSION ?= {{ .ControllerToolsVersion }}
247+
ENVTEST_VERSION ?= {{ .ControllerRuntimeReleaseBranchName }}
248+
GOLANGCI_LINT_VERSION ?= v1.54.2
229249
230250
.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.
232252
$(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))
238254
239255
.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.
241257
$(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))
244259
245260
.PHONY: envtest
246-
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
261+
envtest: $(ENVTEST) ## Download setup-envtest locally if necessary.
247262
$(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
249283
`

0 commit comments

Comments
 (0)