-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathMakefile
More file actions
180 lines (141 loc) · 5.67 KB
/
Copy pathMakefile
File metadata and controls
180 lines (141 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# If you update this file, please follow
# https://suva.sh/posts/well-documented-makefiles
# Ensure Make is run with bash shell as some syntax below is bash-specific
SHELL := /usr/bin/env bash
.DEFAULT_GOAL := help
# Active module mode, as we use go modules to manage dependencies
export GO111MODULE := on
# Directories
BIN_DIR := bin
TOOLS_DIR := hack/tools
TOOLS_BIN_DIR := $(TOOLS_DIR)/bin
export PATH := $(abspath $(BIN_DIR)):$(abspath $(TOOLS_BIN_DIR)):$(PATH)
export GOBIN := $(shell git rev-parse --show-toplevel)/hack/tools/bin
# Tooling binaries
CONTROLLER_GEN := $(TOOLS_BIN_DIR)/controller-gen
CLIENT_GEN := $(TOOLS_BIN_DIR)/client-gen
INFORMER_GEN := $(TOOLS_BIN_DIR)/informer-gen
LISTER_GEN := $(TOOLS_BIN_DIR)/lister-gen
GOLANGCI_LINT := $(TOOLS_BIN_DIR)/golangci-lint
GOLANGCI_LINT_KAL := $(abspath $(TOOLS_BIN_DIR)/golangci-lint-kal)
GOLANGCI_KAL_CONFIG := $(abspath hack/.golangci-kal.yml)
SETUP_ENVTEST := $(TOOLS_BIN_DIR)/setup-envtest
CLIENT_GEN_SCRIPT := hack/client-gen.sh
# Allow overriding manifest generation destination directory
MANIFEST_ROOT ?= config
CRD_ROOT ?= $(MANIFEST_ROOT)/crd/bases
# Kubernetes version for envtest assets (kube-apiserver + etcd).
# CEL / x-kubernetes-validations require v1.25+; isIP()/isCIDR() require v1.31+.
ENVTEST_K8S_VERSION ?= 1.32.0
# envtest binary paths (populated by the k8s-envtest target in hack/tools/Makefile).
KUBE_APISERVER := $(TOOLS_BIN_DIR)/kube-apiserver
ETCD := $(TOOLS_BIN_DIR)/etcd
# Point envtest at the local hack/tools/bin directory.
export KUBEBUILDER_ASSETS := $(abspath $(TOOLS_BIN_DIR))
.PHONY: all
all: lint tools generate test ## Runs linting, code generation, and tests
## --------------------------------------
##@ Help
## --------------------------------------
help: ## Display this help
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_-]+:.*?##/ { printf " \033[36m%-18s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
## --------------------------------------
##@ Tooling
## --------------------------------------
TOOLING_BINARIES := $(CONTROLLER_GEN) $(CLIENT_GEN) $(INFORMER_GEN) $(LISTER_GEN) $(GOLANGCI_LINT) $(SETUP_ENVTEST)
tools: $(TOOLING_BINARIES) ## Build tooling binaries
.PHONY: $(TOOLING_BINARIES)
$(TOOLING_BINARIES):
make -C $(TOOLS_DIR) $(@F)
## --------------------------------------
##@ Generate
## --------------------------------------
.PHONY: modules
modules: ## Validates the modules
go mod tidy
.PHONY: modules-download
modules-download: ## Downloads and caches the modules
go mod download
.PHONY: generate
generate: ## Run all code generation targets
$(MAKE) generate-go
$(MAKE) generate-manifests
$(MAKE) generate-client
.PHONY: generate-go
generate-go: $(CONTROLLER_GEN) ## Runs Go related generate targets
$(CONTROLLER_GEN) \
paths=./api/... \
+object:headerFile="$(abspath hack/boilerplate/boilerplate.go.txt)"
ifneq (0,$(GENERATE_CODE))
go generate ./...
endif
.PHONY: generate-manifests
generate-manifests: $(CONTROLLER_GEN) ## Generate manifests e.g. CRD, RBAC etc.
$(CONTROLLER_GEN) \
paths=./api/... \
crd:crdVersions=v1 \
output:crd:dir=$(CRD_ROOT) \
output:none
.PHONY: generate-client
generate-client: tools ## Generate api client
$(CLIENT_GEN_SCRIPT)
## --------------------------------------
##@ Testing
## --------------------------------------
# File-level prereqs for envtest binaries — trigger k8s-envtest download on first use.
$(KUBE_APISERVER) $(ETCD):
$(MAKE) -C $(TOOLS_DIR) $(@F) KUBEBUILDER_K8S_VERSION=$(ENVTEST_K8S_VERSION)
.PHONY: test
test: test-cel ## Run all tests.
.PHONY: test-cel
test-cel: generate-manifests $(KUBE_APISERVER) $(ETCD) ## Run CEL envtest integration tests (uses kube-apiserver+etcd from ENVTEST_K8S_VERSION)
cd test/cel && go test -v ./... -count=1 -timeout 120s
## --------------------------------------
##@ Linting
## --------------------------------------
.PHONY: lint
lint: ## Run all the lint targets
$(MAKE) lint-go-full
$(MAKE) lint-kal
$(MAKE) lint-markdown
$(MAKE) lint-shell
GOLANGCI_LINT_FLAGS ?= --fast-only
.PHONY: lint-go
lint-go: $(GOLANGCI_LINT) ## Lint codebase
$(GOLANGCI_LINT) run -v $(GOLANGCI_LINT_FLAGS)
# Full run: do not pass --fast-only (golangci-lint v2+; v1 used --fast=false).
.PHONY: lint-go-full
lint-go-full: GOLANGCI_LINT_FLAGS =
lint-go-full: lint-go ## Run slower linters to detect possible issues
.PHONY: lint-markdown
lint-markdown: ## Lint the project's markdown
docker run --rm -v $$(pwd):/mnt mivok/markdownlint -c /mnt/.mdlrc /mnt
.PHONY: lint-shell
lint-shell: ## Lint the project's shell scripts
docker run --rm -v "$$(pwd)":/mnt:ro koalaman/shellcheck:stable $$(ls hack/*.sh)
# Kube-API-Linter runs via golangci-lint-kal (Go 1.24+ to build; see hack/tools/Makefile golangci-lint-kal).
GOLANGCI_LINT_KAL_EXTRA_ARGS ?= --new-from-merge-base=master
.PHONY: lint-kal
lint-kal: $(GOLANGCI_KAL_CONFIG) $(GOLANGCI_LINT_KAL) ## Run kube-api-linter on api/ types
$(GOLANGCI_LINT_KAL) run --config $(GOLANGCI_KAL_CONFIG) $(GOLANGCI_LINT_KAL_EXTRA_ARGS) ./api/...
$(GOLANGCI_LINT_KAL):
$(MAKE) -C $(TOOLS_DIR) golangci-lint-kal
## --------------------------------------
##@ Cleanup
## --------------------------------------
.PHONY: clean
clean: # Clean all generated or compiled files
$(MAKE) clean-bin
$(MAKE) clean-client
$(MAKE) clean-crd
$(MAKE) modules
.PHONY: clean-bin
clean-bin: ## Remove all generated tooling binaries
rm -rf hack/tools/bin
rm -rf hack/samples/bin
.PHONY: clean-client
clean-client: ## Remove all generated client libraries
rm -rf pkg/client
.PHONY: clean-crd
clean-crd: ## Remove all generated client libraries
rm -rf config/crd