-
Notifications
You must be signed in to change notification settings - Fork 332
/
Makefile
331 lines (249 loc) · 12.6 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# A Self-Documenting Makefile: http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
####
## Dependency versions
####
# renovate: datasource=github-releases depName=kubernetes-sigs/controller-tools versioning=semver
CONTROLLER_GEN_VERSION := 0.16.5
# renovate: datasource=github-releases depName=golangci/golangci-lint versioning=semver
GOLANGCI_LINT_VERSION := 1.62.2
# renovate: datasource=github-releases depName=norwoodj/helm-docs versioning=semver
HELM_DOCS_VERSION = 1.14.2
# renovate: datasource=github-releases depName=kubernetes-sigs/kind versioning=semver
KIND_VERSION ?= 0.24.0
# renovate: datasource=github-releases depName=kubernetes-sigs/kubebuilder versioning=semver
KUBEBUILDER_VERSION = 3.1.0
# renovate: datasource=go depName=github.com/goph/licensei versioning=semver
LICENSEI_VERSION = 0.9.0
# renovate: datasource=go depName=github.com/stern/stern versioning=semver
STERN_VERSION := 1.31.0
ENVTEST_K8S_VERSION := 1.31.0
BIN := ${PWD}/bin
export PATH := $(BIN):$(PATH)
OS := $(shell go env GOOS)
ARCH := $(shell go env GOARCH)
DOCKER ?= docker
GOVERSION := $(shell go env GOVERSION)
# Image name to use for building/pushing image targets
IMG ?= controller:local
IMG_DEBUG ?= controller:debug
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= crd:maxDescLen=0
DRAIN_WATCH_IMAGE_TAG_NAME ?= ghcr.io/kube-logging/fluentd-drain-watch
DRAIN_WATCH_IMAGE_TAG_VERSION ?= latest
VERSION := $(shell git describe --abbrev=0 --tags)
E2E_TEST_TIMEOUT ?= 20m
TEST_COV_DIR := $(shell mkdir -p build/_test_coverage && realpath build/_test_coverage)
CONTROLLER_GEN := ${BIN}/controller-gen
ENVTEST_BIN_DIR := ${BIN}/envtest
ENVTEST_BINARY_ASSETS := ${ENVTEST_BIN_DIR}/bin
GOLANGCI_LINT := ${BIN}/golangci-lint
LINTER_FLAGS := --timeout 10m
HELM_DOCS := ${BIN}/helm-docs
KIND := ${BIN}/kind
KIND_IMAGE ?= kindest/node:v1.31.0@sha256:53df588e04085fd41ae12de0c3fe4c72f7013bba32a20e7325357a1ac94ba865
KIND_CLUSTER := kind
KUBEBUILDER := ${BIN}/kubebuilder
LICENSEI := ${BIN}/licensei
SETUP_ENVTEST := ${BIN}/setup-envtest
## =============
## == Rules ==
## =============
.PHONY: all
all: manager
.PHONY: check
check: license-check lint test
.PHONY: generate
generate: codegen fmt manifests docs helm-docs ## Generate code, documentation, etc
.PHONY: check-diff
check-diff: generate
git diff --exit-code ':(exclude)./ADOPTERS.md'
.PHONY: debug
debug: manager ## Remote debug
dlv --listen=:40000 --log --headless=true --api-version=2 exec bin/manager -- $(ARGS)
.PHONY: docker-build
docker-build: ## Build the docker image
${DOCKER} build . -t ${IMG}
@echo "updating kustomize image patch file for manager resource"
sed -i'' -e 's@image: .*@image: '"${IMG}"'@' ./config/default/manager_image_patch.yaml
.PHONY: docker-build-debug
docker-build-debug: ## Build the debug docker image
${DOCKER} build --target debug -t ${IMG_DEBUG} .
.PHONY: docker-build-e2e-test
docker-build-e2e-test: ## Build the coverage docker image
${DOCKER} build --build-arg GO_BUILD_FLAGS="-cover -covermode=atomic" -t ${IMG} --target e2e-test .
sed -i'' -e 's@image: .*@image: '"${IMG}"'@' ./config/default/manager_image_patch.yaml
.PHONY: docker-build-drain-watch
docker-build-drain-watch: ## Build the drain-watch docker image
${DOCKER} build drain-watch-image -t ${DRAIN_WATCH_IMAGE_TAG_NAME}:${DRAIN_WATCH_IMAGE_TAG_VERSION}
.PHONY: docker-push
docker-push: ## Push the docker image
${DOCKER} push ${IMG}
.PHONY: docs
docs: ## Generate docs
go run cmd/docs.go
.PHONY: fmt
fmt: ## Run go fmt against code
go fmt ./...
cd pkg/sdk && go fmt ./...
.PHONY: codegen
codegen: ${CONTROLLER_GEN} tidy ## Generate code
cd pkg/sdk && $(CONTROLLER_GEN) $(CRD_OPTIONS) output:crd:artifacts:config=../../config/crd/bases object:headerFile=./../../hack/boilerplate.go.txt paths=./logging/api/...
cd pkg/sdk && $(CONTROLLER_GEN) $(CRD_OPTIONS) output:crd:artifacts:config=../../config/crd/bases object:headerFile=./../../hack/boilerplate.go.txt paths=./logging/model/...
cd pkg/sdk && $(CONTROLLER_GEN) $(CRD_OPTIONS) output:crd:artifacts:config=../../config/crd/bases object:headerFile=./../../hack/boilerplate.go.txt paths=./extensions/api/...
.PHONY: install
install: manifests ## Install CRDs into the cluster in ~/.kube/config
kubectl apply -f config/crd/bases --server-side --force-conflicts
.PHONY: license-check
license-check: ${LICENSEI} .licensei.cache ## Run license check
${LICENSEI} check
${LICENSEI} header
.PHONY: license-cache
license-cache: ${LICENSEI} ## Generate license cache
${LICENSEI} cache
.PHONY: lint
lint: ${GOLANGCI_LINT} ## Run linter
${GOLANGCI_LINT} run ${LINTER_FLAGS}
cd pkg/sdk && ${GOLANGCI_LINT} run ${LINTER_FLAGS}
.PHONY: lint-fix
lint-fix: ${GOLANGCI_LINT} ## Run linter
${GOLANGCI_LINT} run --fix
cd pkg/sdk && ${GOLANGCI_LINT} run --fix
.PHONY: list
list: ## List all make targets
@${MAKE} -pRrn : -f $(MAKEFILE_LIST) 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | egrep -v -e '^[^[:alnum:]]' -e '^$@$$' | sort
.PHONY: manager
manager: codegen fmt vet ## Build manager binary
go build -o bin/manager main.go
HELM_MANIFEST_OVERRIDE='s@manager-role@{{ template "logging-operator.fullname" . }}\n annotations:\n {{- if .Values.rbac.retainOnDelete }}\n "helm.sh/resource-policy": keep\n {{- end }}@'
.PHONY: manifests
manifests: ${CONTROLLER_GEN} ## Generate manifests e.g. CRD, RBAC etc.
cd pkg/sdk && $(CONTROLLER_GEN) $(CRD_OPTIONS) webhook paths="./..." output:crd:artifacts:config=../../config/crd/bases output:webhook:artifacts:config=../../config/webhook
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role paths="./controllers/..." output:rbac:artifacts:config=./config/rbac
cp config/crd/bases/* charts/logging-operator/crds/
for f in config/crd/bases/*.yaml; do sed '/controller-gen.kubebuilder.io\/version/ r hack/crds.annotations.snippet.txt' $${f} > charts/logging-operator/charts/logging-operator-crds/templates/$${f##*/}; done
echo "{{- if .Values.rbac.enabled }}" > ./charts/logging-operator/templates/clusterrole.yaml
cat config/rbac/role.yaml | sed -e $(HELM_MANIFEST_OVERRIDE) | cat >> ./charts/logging-operator/templates/clusterrole.yaml
echo "{{- end }}" >> ./charts/logging-operator/templates/clusterrole.yaml
.PHONY: run
run: codegen fmt vet ## Run against the configured Kubernetes cluster in ~/.kube/config
go run ./main.go --verbose --pprof
.PHONY: test
test: codegen fmt vet manifests ${ENVTEST_BINARY_ASSETS} ${KUBEBUILDER} ## Run tests
cd pkg/sdk/logging && ENVTEST_BINARY_ASSETS=${ENVTEST_BINARY_ASSETS} go test ./... -coverprofile ${TEST_COV_DIR}/cover_logging.out
cd pkg/sdk/extensions && go test ./... -coverprofile ${TEST_COV_DIR}/cover_extensions.out
cd pkg/sdk/logging/model/syslogng/config && go test ./... -coverprofile ${TEST_COV_DIR}/cover_syslogng.out
ENVTEST_BINARY_ASSETS=${ENVTEST_BINARY_ASSETS} go test ./controllers/logging/... ./pkg/... -coverprofile ${TEST_COV_DIR}/cover_controllers_logging.out
ENVTEST_BINARY_ASSETS=${ENVTEST_BINARY_ASSETS} go test ./controllers/extensions/... ./pkg/... -coverprofile ${TEST_COV_DIR}/cover_controllers_extensions.out
.PHONY: install-go-test-coverage
install-go-test-coverage:
GOBIN=${BIN} go install github.com/vladopajic/go-test-coverage/v2@latest
.PHONY: generate-test-coverage
generate-test-coverage: install-go-test-coverage test
rm -f ${TEST_COV_DIR}/coverage_all.out
echo "mode: set" > ${TEST_COV_DIR}/coverage_all.out
find -name 'cover_*.out' | xargs cat | grep -v "mode: set" >> ${TEST_COV_DIR}/coverage_all.out
.PHONY: check-coverage
check-coverage: install-go-test-coverage generate-test-coverage
GOBIN=${BIN} go-test-coverage --config=./.testcoverage.yml
.PHONY: test-e2e
test-e2e: ${KIND} codegen manifests docker-build-e2e-test stern ## Run E2E tests
$(MAKE) test-e2e-nodeps E2E_TEST=${E2E_TEST}
.PHONY: test-e2e-ci
test-e2e-ci: ${BIN}
curl -Lo ./bin/kind https://kind.sigs.k8s.io/dl/v${KIND_VERSION}/kind-linux-amd64
chmod +x ./bin/kind
curl -L https://github.com/stern/stern/releases/download/v${STERN_VERSION}/stern_${STERN_VERSION}_linux_amd64.tar.gz | tar xz -C bin stern
chmod +x ./bin/stern
$(MAKE) test-e2e-nodeps E2E_TEST=${E2E_TEST}
.PHONY: test-e2e-nodeps
test-e2e-nodeps:
cd e2e && \
LOGGING_OPERATOR_IMAGE="${IMG}" \
KIND_PATH="$(KIND)" \
KIND_IMAGE="$(KIND_IMAGE)" \
PROJECT_DIR="$(PWD)" \
E2E_TEST_COV_DIR=${TEST_COV_DIR} \
go test -v -timeout ${E2E_TEST_TIMEOUT} ./${E2E_TEST}/...
go tool covdata textfmt -i=${TEST_COV_DIR}/covdatafiles -o ${TEST_COV_DIR}/coverage_e2e.out
@echo "--- E2E test coverage report"
go tool covdata percent -i=${TEST_COV_DIR}/covdatafiles
.PHONY: tidy
tidy: ## Tidy Go modules
find . -iname "go.mod" -not -path "./.devcontainer/*" | xargs -L1 sh -c 'cd $$(dirname $$0); go mod tidy'
.PHONY: vet
vet: ## Run go vet against code
go vet ./...
cd pkg/sdk && go vet ./...
.PHONY: kind-cluster
kind-cluster: ${KIND}
kind create cluster --name $(KIND_CLUSTER) --image $(KIND_IMAGE)
.PHONY: helm-docs
helm-docs: ${HELM_DOCS}
${HELM_DOCS} -s file -c charts/ -t ../charts-docs/templates/overrides.gotmpl -t README.md.gotmpl
## =========================
## == Tool dependencies ==
## =========================
${CONTROLLER_GEN}: ${CONTROLLER_GEN}_${CONTROLLER_GEN_VERSION}_${GOVERSION} | ${BIN}
ln -sf $(notdir $<) $@
${CONTROLLER_GEN}_${CONTROLLER_GEN_VERSION}_${GOVERSION}: IMPORT_PATH := sigs.k8s.io/controller-tools/cmd/controller-gen
${CONTROLLER_GEN}_${CONTROLLER_GEN_VERSION}_${GOVERSION}: VERSION := v${CONTROLLER_GEN_VERSION}
${CONTROLLER_GEN}_${CONTROLLER_GEN_VERSION}_${GOVERSION}: | ${BIN}
${go_install_binary}
${ENVTEST_BINARY_ASSETS}: ${ENVTEST_BINARY_ASSETS}_${ENVTEST_K8S_VERSION}
ln -sf $(notdir $<) $@
${ENVTEST_BINARY_ASSETS}_${ENVTEST_K8S_VERSION}: | ${SETUP_ENVTEST} ${ENVTEST_BIN_DIR}
ln -sf $$(${SETUP_ENVTEST} --bin-dir ${ENVTEST_BIN_DIR} use ${ENVTEST_K8S_VERSION} -p path) $@
${GOLANGCI_LINT}: ${GOLANGCI_LINT}_${GOLANGCI_LINT_VERSION}_${GOVERSION} | ${BIN}
ln -sf $(notdir $<) $@
${GOLANGCI_LINT}_${GOLANGCI_LINT_VERSION}_${GOVERSION}: IMPORT_PATH := github.com/golangci/golangci-lint/cmd/golangci-lint
${GOLANGCI_LINT}_${GOLANGCI_LINT_VERSION}_${GOVERSION}: VERSION := v${GOLANGCI_LINT_VERSION}
${GOLANGCI_LINT}_${GOLANGCI_LINT_VERSION}_${GOVERSION}: | ${BIN}
${go_install_binary}
${KIND}: ${KIND}_${KIND_VERSION}_${GOVERSION} | ${BIN}
ln -sf $(notdir $<) $@
${KIND}_${KIND_VERSION}_${GOVERSION}: IMPORT_PATH := sigs.k8s.io/kind
${KIND}_${KIND_VERSION}_${GOVERSION}: VERSION := v${KIND_VERSION}
${KIND}_${KIND_VERSION}_${GOVERSION}: | ${BIN}
${go_install_binary}
${KUBEBUILDER}: ${KUBEBUILDER}_$(KUBEBUILDER_VERSION) | ${BIN}
ln -sf $(notdir $<) $@
${KUBEBUILDER}_$(KUBEBUILDER_VERSION): | ${BIN}
curl -sL https://github.com/kubernetes-sigs/kubebuilder/releases/download/v${KUBEBUILDER_VERSION}/kubebuilder_${OS}_${ARCH} -o $@
chmod +x $@
${LICENSEI}: ${LICENSEI}_${LICENSEI_VERSION}_${GOVERSION} | ${BIN}
ln -sf $(notdir $<) $@
${LICENSEI}_${LICENSEI_VERSION}_${GOVERSION}: IMPORT_PATH := github.com/goph/licensei/cmd/licensei
${LICENSEI}_${LICENSEI_VERSION}_${GOVERSION}: VERSION := v${LICENSEI_VERSION}
${LICENSEI}_${LICENSEI_VERSION}_${GOVERSION}: | ${BIN}
${go_install_binary}
.licensei.cache: ${LICENSEI}
ifndef GITHUB_TOKEN
@>&2 echo "WARNING: building licensei cache without Github token, rate limiting might occur."
@>&2 echo "(Hint: If too many licenses are missing, try specifying a Github token via the environment variable GITHUB_TOKEN.)"
endif
${LICENSEI} cache
${SETUP_ENVTEST}: IMPORT_PATH := sigs.k8s.io/controller-runtime/tools/setup-envtest
${SETUP_ENVTEST}: VERSION := latest
${SETUP_ENVTEST}: | ${BIN}
GOBIN=${BIN} go install ${IMPORT_PATH}@${VERSION}
stern: | ${BIN}
GOBIN=${BIN} go install github.com/stern/stern@latest
${ENVTEST_BIN_DIR}: | ${BIN}
mkdir -p $@
${HELM_DOCS}: ${HELM_DOCS}-${HELM_DOCS_VERSION}
@ln -sf ${HELM_DOCS}-${HELM_DOCS_VERSION} ${HELM_DOCS}
${HELM_DOCS}-${HELM_DOCS_VERSION}:
@mkdir -p bin
curl -L https://github.com/norwoodj/helm-docs/releases/download/v${HELM_DOCS_VERSION}/helm-docs_${HELM_DOCS_VERSION}_$(shell uname)_x86_64.tar.gz | tar -zOxf - helm-docs > ${HELM_DOCS}-${HELM_DOCS_VERSION} && chmod +x ${HELM_DOCS}-${HELM_DOCS_VERSION}
${BIN}:
mkdir -p bin
define go_install_binary
find ${BIN} -name '$(notdir ${IMPORT_PATH})_*' -exec rm {} +
GOBIN=${BIN} go install ${IMPORT_PATH}@${VERSION}
mv ${BIN}/$(notdir ${IMPORT_PATH}) $@
endef
# Self-documenting Makefile
.DEFAULT_GOAL = help
.PHONY: help
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)