generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Makefile
197 lines (156 loc) · 6.05 KB
/
Makefile
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
SHELL := /bin/bash
export BINARIES ?= $(shell go list ./cmd/... | grep -v integration | grep -v cmds | grep -v metrics-node-sampler/cmd | grep -v metrics-prometheus-collector/cmd | grep -v umc-archiver/cmd)
export GO111MODULE ?= on
export GOPATH ?= $(shell go env GOPATH)
export GOOS ?= $(shell go env GOOS)
export GOARCH ?= $(shell go env GOARCH)
export GIT_BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD)
export GIT_COMMIT ?= $(shell git rev-parse --short HEAD)
export DATE ?= $(shell date -u '+%Y-%m-%d_%I:%M:%S%p')
export VERSION ?= $(shell git tag --sort=committerdate | tail -1)
export VERSION_FLAGS = -X main.version=$(VERSION) -X main.commit=$(GIT_COMMIT) -X main.date="$(DATE)"
export LD_FLAGS ?= -ldflags="$(VERSION_FLAGS) -w -s"
## --------------------------------------
## Directories
## --------------------------------------
COMMON_SELF_DIR := $(dir $(lastword $(MAKEFILE_LIST)))
# the root directory of this repo
ifeq ($(origin ROOT_DIR),undefined)
ROOT_DIR := $(abspath $(COMMON_SELF_DIR))
endif
# a directory that contains project built binaries
ifeq ($(origin BIN_DIR), undefined)
BIN_DIR := $(ROOT_DIR)/bin
endif
ifeq ($(origin TOOLS_DIR), undefined)
TOOLS_DIR := $(ROOT_DIR)/.tools
endif
$(TOOLS_DIR):
mkdir -p $(TOOLS_DIR) && cd $(TOOLS_DIR) && go mod init tempmod
# We are using Kubebuilder Test Assets for integration testing
K8S_VERSION=1.28.3
KUBEBUILDER := $(abspath $(TOOLS_DIR)/kubebuilder)
export KUBEBUILDER_ASSETS := $(KUBEBUILDER)/bin
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= crd
## --------------------------------------
## Make targets
## --------------------------------------
default: all
all: fmt vet test build
verify: tools fmt vet verify-licenses verify-generated
help: ## Show this help.
@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)
clean: ## Remove all build and test related artifacts
rm -rf ./.out bin
test-local: $(KUBEBUILDER_ASSETS) ## Run local tests
go test -count=1 -v ./...
HAS_GOTESTSUM := $(shell command -v gotestsum)
.PHONY: test-ci-deps
test-ci-deps:
ifndef HAS_GOTESTSUM
go install gotest.tools/gotestsum@latest
endif
KUBE_JUNIT_REPORT_DIR ?= /tmp/logs/artifacts
SORTABLE_DATE := $(shell date "+%Y%m%d-%H%M%S")
JUNIT_FILENAME_PREFIX := $(KUBE_JUNIT_REPORT_DIR)/junit_$(SORTABLE_DATE)
JUNIT_XML_FILENAME := $(JUNIT_FILENAME_PREFIX).xml
test: test-ci-deps $(KUBEBUILDER_ASSETS)
mkdir -p $(KUBE_JUNIT_REPORT_DIR)
gotestsum --junitfile $(JUNIT_XML_FILENAME)
vet: ## Run go vet on this project
go vet ./...
fmt: ## Format the code
go fmt ./...
build-dir:
@mkdir bin 2>/dev/null || true
## Some binaries are Linux/amd64-only. To build, run
## GOOS=linux GOARCH=amd64 make build
build: proto build-dir ## Build this project go binaries
@$(foreach pkg,$(BINARIES),\
go build -o bin $(LD_FLAGS) $(pkg);)
## Some binaries are Linux/amd64-only. To build, run
## GOOS=linux GOARCH=amd64 make build
build-docker: build-dir ## Build this project go binaries
@$(foreach pkg,$(BINARIES),\
go build -o bin $(LD_FLAGS) $(pkg);)
generate: controller-gen
$(CONTROLLER_GEN) object paths="./..."
manifests: controller-gen
$(CONTROLLER_GEN) $(CRD_OPTIONS) paths="./..." output:crd:artifacts:config=config/crds
.PHONY: proto
proto: protoc
protoc -I ./proto \
--go_out . --go_opt paths=source_relative \
--go-grpc_out . --go-grpc_opt paths=source_relative \
--grpc-gateway_out . --grpc-gateway_opt paths=source_relative \
./proto/pkg/sampler/api/api.proto
protoc -I ./proto \
--go_out . --go_opt paths=source_relative \
./proto/pkg/collector/api/api.proto
$(KUBEBUILDER_ASSETS):
curl -sSLo envtest-bins.tar.gz "https://storage.googleapis.com/kubebuilder-tools/kubebuilder-tools-$(K8S_VERSION)-$(GOOS)-amd64.tar.gz"
mkdir -p $(KUBEBUILDER)
tar -C $(KUBEBUILDER) --strip-components=1 -zvxf envtest-bins.tar.gz
rm envtest-bins.tar.gz
## --------------------------------------
.PHONY: docker
docker: ## Build the docker image
docker build . --progress=plain --platform=linux/amd64 -t usage-metrics-collector:v$(VERSION)
.PHONY: tools
tools: ## Install dev tools.
tools/install.sh
.PHONY: lint
lint: tools ## Lint the code.
golangci-lint run
.PHONY: reviewable
reviewable: all
# find or download controller-gen
# download controller-gen if necessary
controller-gen:
ifeq (, $(shell which controller-gen))
@{ \
set -e ;\
CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\
cd $$CONTROLLER_GEN_TMP_DIR ;\
go mod init tmp ;\
go get sigs.k8s.io/controller-tools/cmd/[email protected] ;\
rm -rf $$CONTROLLER_GEN_TMP_DIR ;\
}
CONTROLLER_GEN=$(GOBIN)/controller-gen
else
CONTROLLER_GEN=$(shell which controller-gen)
endif
run-local:
docker build . -t usage-metrics-collector:v0.0.0
kind load docker-image usage-metrics-collector:v0.0.0
kustomize build config | kubectl apply -f -
## --------------------------------------
## License
## --------------------------------------
HAS_ADDLICENSE:=$(shell command -v addlicense)
.PHONY: verify-licenses
verify-licenses: addlicense
find . -type f -name "*.go" ! -path "*/vendor/*" | xargs $(GOPATH)/bin/addlicense -check || (echo 'Run "make update-licenses"' && exit 1)
.PHONY: update-licenses
update-licenses: addlicense
find . -type f -name "*.go" ! -path "*/vendor/*" | xargs $(GOPATH)/bin/addlicense -c "The Kubernetes Authors."
.PHONY: addlicense
addlicense:
ifndef HAS_ADDLICENSE
go install github.com/google/[email protected]
endif
## --------------------------------------
## Generated files
## --------------------------------------
HAS_PROTOC:=$(shell command -v protoc)
.PHONY: verify-generated
verify-generated: proto
git grep -l "Code generated by" | xargs git diff --exit-code -- || (echo 'Generated files do not match. Run "make update-generated"' && exit 1)
.PHONY: update-generated
update-generated: proto
.PHONY: protoc
protoc:
ifndef HAS_PROTOC
tools/install-protoc.sh
endif