|
1 | | -# Copyright 2019 The Kubernetes Authors. |
| 1 | +# Copyright 2026 The Flux Authors. |
2 | 2 | # SPDX-License-Identifier: Apache-2.0 |
3 | 3 |
|
4 | | -GOPATH := $(shell go env GOPATH) |
5 | | -MYGOBIN := $(shell go env GOPATH)/bin |
6 | | -SHELL := /bin/bash |
7 | | -export PATH := $(MYGOBIN):$(PATH) |
| 4 | +# Get the currently used golang install path |
| 5 | +# (in GOPATH/bin, unless GOBIN is set). |
| 6 | +ifeq (,$(shell go env GOBIN)) |
| 7 | +GOBIN=$(shell go env GOPATH)/bin |
| 8 | +else |
| 9 | +GOBIN=$(shell go env GOBIN) |
| 10 | +endif |
8 | 11 |
|
9 | | -.PHONY: all |
10 | | -all: generate license fix vet fmt test lint tidy |
11 | | - |
12 | | -"$(MYGOBIN)/stringer": |
13 | | - go install golang.org/x/tools/cmd/stringer@v0.12.0 |
14 | | - |
15 | | -"$(MYGOBIN)/addlicense": |
16 | | - go install github.com/google/addlicense@v1.0.0 |
17 | | - |
18 | | -"$(MYGOBIN)/golangci-lint": |
19 | | - go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.7.2 |
20 | | - |
21 | | -"$(MYGOBIN)/deepcopy-gen": |
22 | | - go install k8s.io/code-generator/cmd/deepcopy-gen@v0.25.2 |
23 | | - |
24 | | -"$(MYGOBIN)/ginkgo": |
25 | | - go install github.com/onsi/ginkgo/v2/ginkgo@v2.2.0 |
26 | | - |
27 | | -"$(MYGOBIN)/mdrip": |
28 | | - go install github.com/monopole/mdrip@v1.0.2 |
29 | | - |
30 | | -"$(MYGOBIN)/kind": |
31 | | - go install sigs.k8s.io/kind@v0.16.0 |
32 | | - |
33 | | -# The following target intended for reference by a file in |
34 | | -# https://github.com/kubernetes/test-infra/tree/master/config/jobs/kubernetes-sigs/cli-utils |
35 | | -.PHONY: prow-presubmit-check |
36 | | -prow-presubmit-check: \ |
37 | | - test lint verify-license |
38 | | - |
39 | | -.PHONY: prow-presubmit-check-e2e |
40 | | -prow-presubmit-check-e2e: \ |
41 | | - install-column-apt test-e2e verify-kapply-e2e |
42 | | - |
43 | | -.PHONY: prow-presubmit-check-stress |
44 | | -prow-presubmit-check-stress: \ |
45 | | - test-stress |
| 12 | +# Setting SHELL to bash allows bash commands to be executed by recipes. |
| 13 | +# Options are set to exit when a recipe line exits non-zero or a piped command fails. |
| 14 | +SHELL = /usr/bin/env bash -o pipefail |
| 15 | +.SHELLFLAGS = -ec |
46 | 16 |
|
47 | | -.PHONY: fix |
48 | | -fix: |
49 | | - go fix ./... |
| 17 | +.PHONY: all |
| 18 | +all: tidy vet fmt lint test |
50 | 19 |
|
51 | 20 | .PHONY: fmt |
52 | 21 | fmt: |
53 | 22 | go fmt ./... |
54 | 23 |
|
55 | | -# Install column (required by verify-kapply-e2e) |
56 | | -# Update is included because the kubekins-e2e container build strips out the package cache. |
57 | | -# In newer versions of debian, column is in the bsdextrautils package, |
58 | | -# but in buster (used by kubekins-e2e) it's in bsdmainutils. |
59 | | -.PHONY: install-column-apt |
60 | | -install-column-apt: |
61 | | - apt-get update |
62 | | - apt-get install -y bsdmainutils |
63 | | - |
64 | | -.PHONY: generate-deepcopy |
65 | | -generate-deepcopy: "$(MYGOBIN)/deepcopy-gen" |
66 | | - hack/run-in-gopath.sh deepcopy-gen --input-dirs ./pkg/apis/... -O zz_generated.deepcopy --go-header-file ./LICENSE_TEMPLATE_GO |
67 | | - |
68 | | -.PHONY: generate |
69 | | -generate: "$(MYGOBIN)/stringer" generate-deepcopy |
70 | | - go generate ./... |
71 | | - |
72 | | -.PHONY: license |
73 | | -license: "$(MYGOBIN)/addlicense" |
74 | | - "$(MYGOBIN)/addlicense" -v -y 2021 -c "The Kubernetes Authors." -f LICENSE_TEMPLATE . |
75 | | - |
76 | | -.PHONY: verify-license |
77 | | -verify-license: "$(MYGOBIN)/addlicense" |
78 | | - "$(MYGOBIN)/addlicense" -check . |
| 24 | +.PHONY: lint |
| 25 | +lint: golangci-lint ## Run golangci linters and ESLint. |
| 26 | + $(GOLANGCI_LINT) run |
79 | 27 |
|
80 | 28 | .PHONY: tidy |
81 | 29 | tidy: |
82 | 30 | go mod tidy |
83 | 31 |
|
84 | | -.PHONY: lint |
85 | | -lint: "$(MYGOBIN)/golangci-lint" |
86 | | - "$(MYGOBIN)/golangci-lint" run ./... |
87 | | - |
88 | 32 | .PHONY: test |
89 | 33 | test: |
90 | | - go test -race -cover ./cmd/... ./pkg/... |
91 | | - |
92 | | -.PHONY: test-e2e |
93 | | -test-e2e: "$(MYGOBIN)/ginkgo" "$(MYGOBIN)/kind" |
94 | | - kind delete cluster --name=cli-utils-e2e && kind create cluster --name=cli-utils-e2e --wait 5m |
95 | | - "$(MYGOBIN)/ginkgo" -v ./test/e2e/... -- -v 3 |
96 | | - |
97 | | -.PHONY: test-e2e-focus |
98 | | -test-e2e-focus: "$(MYGOBIN)/ginkgo" "$(MYGOBIN)/kind" |
99 | | - kind delete cluster --name=cli-utils-e2e && kind create cluster --name=cli-utils-e2e --wait 5m |
100 | | - "$(MYGOBIN)"/ginkgo -v -focus ".*$(FOCUS).*" ./test/e2e/... -- -v 5 |
101 | | - |
102 | | -.PHONY: test-stress |
103 | | -test-stress: "$(MYGOBIN)/ginkgo" "$(MYGOBIN)/kind" |
104 | | - kind delete cluster --name=cli-utils-e2e && kind create cluster --name=cli-utils-e2e --wait 5m \ |
105 | | - --config=./test/stress/kind-cluster.yaml |
106 | | - kubectl wait nodes --for=condition=ready --all --timeout=5m |
107 | | - "$(MYGOBIN)/ginkgo" -v ./test/stress/... -- -v 3 |
| 34 | + go test -race -cover ./pkg/... |
108 | 35 |
|
109 | 36 | .PHONY: vet |
110 | 37 | vet: |
111 | 38 | go vet ./... |
112 | 39 |
|
113 | | -.PHONY: build |
114 | | -build: |
115 | | - go build -o bin/kapply github.com/fluxcd/cli-utils/cmd; |
116 | | - mv bin/kapply "$(MYGOBIN)" |
117 | | - |
118 | | -.PHONY: build-with-race-detector |
119 | | -build-with-race-detector: |
120 | | - go build -race -o bin/kapply github.com/fluxcd/cli-utils/cmd; |
121 | | - mv bin/kapply "$(MYGOBIN)" |
122 | | - |
123 | | -.PHONY: verify-kapply-e2e |
124 | | -verify-kapply-e2e: test-examples-e2e-kapply |
125 | | - |
126 | | -.PHONY: test-examples-e2e-kapply |
127 | | -test-examples-e2e-kapply: "$(MYGOBIN)/mdrip" "$(MYGOBIN)/kind" |
128 | | - ( \ |
129 | | - set -e; \ |
130 | | - /bin/rm -f bin/kapply; \ |
131 | | - /bin/rm -f "$(MYGOBIN)/kapply"; \ |
132 | | - echo "Installing kapply from ."; \ |
133 | | - make build-with-race-detector; \ |
134 | | - ./hack/testExamplesE2EAgainstKapply.sh .; \ |
135 | | - ) |
136 | | - |
137 | | -.PHONY: nuke |
138 | | -nuke: |
139 | | - sudo rm -rf "$(GOPATH)/pkg/mod/sigs.k8s.io" |
| 40 | +##@ Dependencies |
| 41 | + |
| 42 | +## Location to install dependencies to |
| 43 | +LOCALBIN ?= $(shell pwd)/bin |
| 44 | +$(LOCALBIN): |
| 45 | + mkdir -p $(LOCALBIN) |
| 46 | + |
| 47 | +## Tool Binaries |
| 48 | +GOLANGCI_LINT = $(LOCALBIN)/golangci-lint-$(GOLANGCI_LINT_VERSION) |
| 49 | +GOVULNCHECK ?= $(LOCALBIN)/govulncheck |
| 50 | + |
| 51 | +GOLANGCI_LINT_VERSION ?= v2.11.4 |
| 52 | +.PHONY: golangci-lint |
| 53 | +golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary. |
| 54 | +$(GOLANGCI_LINT): $(LOCALBIN) |
| 55 | + $(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/v2/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION)) |
| 56 | + |
| 57 | +.PHONY: govulncheck |
| 58 | +govulncheck: $(GOVULNCHECK) ## Run govulncheck. |
| 59 | +$(GOVULNCHECK): $(LOCALBIN) |
| 60 | + $(call go-install-tool,$(GOVULNCHECK),golang.org/x/vuln/cmd/govulncheck,latest) |
| 61 | + @$(GOVULNCHECK) ./... |
| 62 | + |
| 63 | +# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist |
| 64 | +# $1 - target path with name of binary (ideally with version) |
| 65 | +# $2 - package url which can be installed |
| 66 | +# $3 - specific version of package |
| 67 | +define go-install-tool |
| 68 | +@[ -f $(1) ] || { \ |
| 69 | +set -e; \ |
| 70 | +package=$(2)@$(3) ;\ |
| 71 | +echo "Downloading $${package}" ;\ |
| 72 | +GOBIN=$(LOCALBIN) go install $${package} ;\ |
| 73 | +mv "$$(echo "$(1)" | sed "s/-$(3)$$//")" $(1) ;\ |
| 74 | +} |
| 75 | +endef |
| 76 | + |
| 77 | +##@ General |
| 78 | + |
| 79 | +.PHONY: help |
| 80 | +help: ## Display this help. |
| 81 | + @awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST) |
0 commit comments