-
Notifications
You must be signed in to change notification settings - Fork 24
/
Makefile
553 lines (449 loc) · 16.2 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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
# Copyright 2022 Intel Corporation. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
SHELL := /bin/bash
# Kubernetes version we pull in as modules and our external API versions.
KUBERNETES_VERSION := $(shell grep 'k8s.io/kubernetes ' go.mod | sed 's/^.* //')
IMAGE_VERSION ?= $(shell git describe --tags --dirty 2> /dev/null || echo v0.0.0-unknown)
ifdef IMAGE_REPO
override IMAGE_REPO := $(IMAGE_REPO)/
endif
export IMAGE_VERSION
export IMAGE_PATH
# Determine binary version and buildid.
BUILD_VERSION ?= $(shell scripts/build/get-buildid --version --shell=no)
BUILD_BUILDID ?= $(shell scripts/build/get-buildid --buildid --shell=no)
BUILD_FLAGS ?=
GO_CMD := go
GO_BUILD = $(GO_CMD) build $(BUILD_FLAGS)
GO_INSTALL := $(GO_CMD) install
GO_TEST := $(GO_CMD) test
GO_LINT := golint -set_exit_status
GO_FMT := gofmt
GO_VET := $(GO_CMD) vet
GO_DEPS := $(GO_CMD) list -f '{{ join .Deps "\n" }}'
GO_VERSION ?= 1.22.1
GO_MODULES := $(shell $(GO_CMD) list ./...)
GO_SUBPKGS := $(shell find ./pkg -name go.mod | sed 's:/go.mod::g' | grep -v testdata | \
tr -s ' \t\n' ' ')
GOLANG_CILINT := golangci-lint
GINKGO := ginkgo
TEST_SETUP := test-setup.sh
TEST_CLEANUP := test-cleanup.sh
TEST_PKGS := .
E2E_TESTS ?= policies.test-suite
RUN_E2E_TESTS := cd test/e2e; ./run_tests.sh
BUILD_PATH := $(shell pwd)/build
BIN_PATH := $(BUILD_PATH)/bin
COVERAGE_PATH := $(BUILD_PATH)/coverage
IMAGE_PATH := $(BUILD_PATH)/images
LICENSE_PATH := $(BUILD_PATH)/licenses
DOCKER := docker
DOCKER_BUILD := $(DOCKER) build
# Plugins and other binaries we build.
#
# Notes:
# All plugins have names matching nri-resource-policy-% or nri-%.
# All plugins are in the directory cmd/plugin/$dir, where $dir is
# the name of the plugin with the above mentioned prefixes removed.
# All other binaries are in the directory cmd/$dir, where $dir
# MUST NOT have an nri-% prefix.
#
PLUGINS ?= \
nri-resource-policy-topology-aware \
nri-resource-policy-balloons \
nri-resource-policy-template \
nri-memory-qos \
nri-memtierd \
nri-sgx-epc
BINARIES ?= \
config-manager
OTHER_IMAGE_TARGETS ?= \
nri-plugins-operator-image \
nri-plugins-operator-bundle-image
ifneq ($(V),1)
Q := @
endif
# Git (tagged) version and revisions we'll use to linker-tag our binaries with.
RANDOM_ID := "$(shell head -c20 /dev/urandom | od -An -tx1 | tr -d ' \n')"
ifdef STATIC
STATIC_LDFLAGS := -extldflags=-static
BUILD_TAGS := -tags osusergo,netgo
STATIC_TYPE := "static "
endif
LDFLAGS = \
-ldflags "$(STATIC_LDFLAGS) -X=github.com/containers/nri-plugins/pkg/version.Version=$(BUILD_VERSION) \
-X=github.com/containers/nri-plugins/pkg/version.Build=$(BUILD_BUILDID) \
-B 0x$(RANDOM_ID)"
ifeq ($(DEBUG),1)
GCFLAGS ?= -gcflags "all=-N -l"
DOCKER_BUILD_DEBUG := --build-arg DEBUG=1
ifneq ($(NORACE),1)
BUILD_FLAGS += -race
else
DOCKER_BUILD_DEBUG += --build-arg NORACE=1
endif
endif
# Documentation-related variables
SPHINXOPTS ?= -W
SPHINXBUILD = sphinx-build
SITE_BUILDDIR ?= build/docs
# Docker base command for working with html documentation.
DOCKER_SITE_BUILDER_IMAGE := nri-plugins-site-builder
DOCKER_SITE_CMD := $(DOCKER) run --rm -v "`pwd`:/docs" --user=`id -u`:`id -g` \
-p 8081:8081 \
-e SITE_BUILDDIR=$(SITE_BUILDDIR) -e SPHINXOPTS=$(SPHINXOPTS)
#
# top-level targets
#
all: build
build: build-plugins build-binaries build-check
build-static:
$(MAKE) STATIC=1 build
clean: clean-plugins clean-binaries
allclean: clean clean-cache
test: test-gopkgs
verify: verify-godeps verify-fmt verify-generate verify-build verify-docs
#
# build targets
#
build-plugins: $(foreach bin,$(PLUGINS),$(BIN_PATH)/$(bin))
build-plugins-static:
$(MAKE) STATIC=1 DEBUG=$(DEBUG) NORACE=$(NORACE) build-plugins
build-binaries: $(foreach bin,$(BINARIES),$(BIN_PATH)/$(bin))
build-binaries-static:
$(MAKE) STATIC=1 DEBUG=$(DEBUG) NORACE=$(NORACE) build-binaries
build-images: images
build-check:
$(Q)$(GO_BUILD) -v $(GO_MODULES)
#
# clean targets
#
clean-plugins:
$(Q)echo "Cleaning $(PLUGINS)"; \
for i in $(PLUGINS); do \
rm -f $(BIN_PATH)/$$i; \
done
clean-binaries:
$(Q)echo "Cleaning $(BINARIES)"; \
for i in $(BINARIES); do \
rm -f $(BIN_PATH)/$$i; \
done
clean-images:
$(Q)echo "Cleaning exported images and deployment files."; \
rm -f $(IMAGE_PATH)/*
clean-cache:
$(Q)$(GO_CMD) clean -cache -testcache
#
# plugins build targets
#
$(BIN_PATH)/nri-resource-policy-%: .static.%.$(STATIC)
$(Q)echo "Building $(STATIC_TYPE)$@ (version $(BUILD_VERSION), build $(BUILD_BUILDID))..."; \
src="./cmd/plugins/$(patsubst nri-resource-policy-%,%,$(notdir $@))"; \
mkdir -p $(BIN_PATH); \
cd "$$src" && $(GO_BUILD) $(BUILD_TAGS) $(LDFLAGS) $(GCFLAGS) -o $@
$(BIN_PATH)/nri-%: .static.%.$(STATIC)
$(Q)echo "Building $(STATIC_TYPE)$@ (version $(BUILD_VERSION), build $(BUILD_BUILDID))..."; \
src="./cmd/plugins/$(patsubst nri-%,%,$(notdir $@))"; \
mkdir -p $(BIN_PATH) && \
cd "$$src" && $(GO_BUILD) $(BUILD_TAGS) $(LDFLAGS) $(GCFLAGS) -o $@
$(BIN_PATH)/%: .static.%.$(STATIC)
$(Q)echo "Building $(STATIC_TYPE)$@ (version $(BUILD_VERSION), build $(BUILD_BUILDID))..."; \
src="./cmd/$(notdir $@)"; \
mkdir -p $(BIN_PATH) && \
cd "$$src" && $(GO_BUILD) $(BUILD_TAGS) $(LDFLAGS) $(GCFLAGS) -o $@
.static.%.$(STATIC):
$(Q)if [ ! -f "$@" ]; then \
touch "$@"; \
fi; \
old="$@"; old="$${old%.*}"; \
if [ -n "$(STATIC)" ]; then \
rm -f "$$old."; \
else \
rm -f "$$old.1"; \
fi
.PRECIOUS: $(foreach dir,$(BUILD_DIRS),.static.$(dir).1 .static.$(dir).)
#
# Image building test deployment generation targets
#
images: $(foreach p,$(PLUGINS),image.$(p)) \
$(foreach p,$(BINARIES),image.$(p)) \
$(OTHER_IMAGE_TARGETS)
image.nri-resource-policy-% \
image.nri-% \
image.%:
$(Q)mkdir -p $(IMAGE_PATH); \
case $@ in \
*.nri-resource-policy-*) \
dir=$(patsubst image.nri-resource-policy-%,cmd/plugins/%,$@); \
;; \
*.nri-*) \
dir=$(patsubst image.nri-%,cmd/plugins/%,$@); \
;; \
*.*) \
dir=$(patsubst image.%,cmd/%,$@); \
;; \
esac; \
bin=$(patsubst image.%,%,$@); \
tag=$(patsubst image.%,%,$@); \
$(DOCKER_BUILD) . -f "$$dir/Dockerfile" \
--build-arg GO_VERSION=$(GO_VERSION) \
$(DOCKER_BUILD_DEBUG) \
--build-arg IMAGE_VERSION=$(IMAGE_VERSION) \
--build-arg BUILD_VERSION=$(BUILD_VERSION) \
--build-arg BUILD_BUILDID=$(BUILD_BUILDID) \
-t $(IMAGE_REPO)$$tag:$(IMAGE_VERSION) || exit $$?; \
NRI_IMAGE_INFO=`$(DOCKER) images --filter=reference=$${tag} --format '{{.ID}} {{.Repository}}:{{.Tag}} (created {{.CreatedSince}}, {{.CreatedAt}})' | head -n 1`; \
NRI_IMAGE_ID=`awk '{print $$1}' <<< "$${NRI_IMAGE_INFO}"`; \
NRI_IMAGE_REPOTAG=`awk '{print $$2}' <<< "$${NRI_IMAGE_INFO}"`; \
NRI_IMAGE_TAR=`realpath "$(IMAGE_PATH)/$${tag}-image-$${NRI_IMAGE_ID}.tar"`; \
$(DOCKER) image save "$${NRI_IMAGE_REPOTAG}" > "$${NRI_IMAGE_TAR}";
nri-plugins-operator-image:
$(Q)mkdir -p $(IMAGE_PATH); \
$(MAKE) -C deployment/operator image-save
nri-plugins-operator-bundle-image:
$(Q)mkdir -p $(IMAGE_PATH); \
$(MAKE) -C deployment/operator bundle-save
#
# plugin build dependencies
#
$(BIN_PATH)/nri-resource-policy-topology-aware: \
$(shell for f in cmd/plugins/topology-aware/*.go; do echo $$f; done; \
for dir in $(shell $(GO_DEPS) ./cmd/plugins/topology-aware/... | \
grep -E '(/nri-plugins/)|(cmd/plugins/topology-aware/)' | \
sed 's#github.com/containers/nri-plugins/##g'); do \
find $$dir -name \*.go; \
done | sort | uniq)
$(BIN_PATH)/nri-resource-policy-balloons: \
$(shell for f in cmd/plugins/balloons/*.go; do echo $$f; done; \
for dir in $(shell $(GO_DEPS) ./cmd/plugins/balloons/... | \
grep -E '(/nri-plugins/)|(cmd/plugins/balloons/)' | \
sed 's#github.com/containers/nri-plugins/##g'); do \
find $$dir -name \*.go; \
done | sort | uniq)
$(BIN_PATH)/nri-resource-policy-template: \
$(shell for f in cmd/plugins/template/*.go; do echo $$f; done; \
for dir in $(shell $(GO_DEPS) ./cmd/plugins/template/... | \
grep -E '(/nri-plugins/)|(cmd/plugins/template/)' | \
sed 's#github.com/containers/nri-plugins/##g'); do \
find $$dir -name \*.go; \
done | sort | uniq)
#
# test targets
#
test-gopkgs: ginkgo-test-setup ginkgo-tests ginkgo-subpkgs-tests ginkgo-test-cleanup
ginkgo-test-setup:
$(Q)for i in $$(find $(TEST_PKGS) -name $(TEST_SETUP)); do \
echo "+ Running test setup $$i..."; \
(cd $${i%/*}; \
if [ -x "$(TEST_SETUP)" ]; then \
./$(TEST_SETUP); \
fi); \
done
ginkgo-test-cleanup:
$(Q)for i in $$(find $(TEST_PKGS) -name $(TEST_CLEANUP)); do \
echo "- Running test cleanup $$i..."; \
(cd $${i%/*}; \
if [ -x "$(TEST_CLEANUP)" ]; then \
./$(TEST_CLEANUP); \
fi); \
done
ginkgo-tests:
$(Q)$(GINKGO) run \
--race \
--trace \
--cover \
--covermode atomic \
--output-dir $(COVERAGE_PATH) \
--junit-report junit.xml \
--coverprofile $(COVERAGE_PATH)/coverprofile \
--keep-separate-coverprofiles \
--succinct \
--skip-package $$(echo $(GO_SUBPKGS) | tr -s '\t ' ',') \
-r $(TEST_PKGS); \
$(GO_CMD) tool cover -html=$(COVERAGE_PATH)/coverprofile -o $(COVERAGE_PATH)/coverage.html
ginkgo-subpkgs-tests: # TODO(klihub): coverage
@enabled=""; find $(TEST_PKGS) -type d | while read i; do \
for j in $(GO_SUBPKGS); do \
if [ "$$j" == "$$i" ]; then \
enabled="$$enabled $$j"; \
break; \
fi; \
done; \
done; \
for i in $$enabled; do \
(cd $$i; \
$(GINKGO) run \
--race \
--trace \
--succinct \
-r . || exit 1); \
done
e2e-tests: build images
$(Q)$(RUN_E2E_TESTS) $(E2E_TESTS)
codecov: SHELL := $(shell which bash)
codecov:
bash <(curl -s https://codecov.io/bash) -f $(COVERAGE_PATH)/coverprofile
#
# other validation targets
#
fmt format:
$(Q)$(GO_FMT) -s -d -e .
reformat:
$(Q)$(GO_FMT) -s -d -w $$(git ls-files '*.go')
lint:
$(Q)$(GO_LINT) -set_exit_status ./...
vet:
$(Q)$(GO_VET) ./...
golangci-lint:
$(Q)$(GOLANG_CILINT) run
verify-godeps:
$(Q) $(GO_CMD) mod tidy && git diff --quiet; ec="$$?"; \
if [ "$$ec" != "0" ]; then \
echo "ERROR: go mod dependencies are not up-to-date."; \
echo "ERROR:"; \
git --no-pager diff go.mod go.sum | sed 's/^/ERROR: /g'; \
echo "ERROR:"; \
echo "ERROR: please run 'go mod tidy' and commit these changes."; \
exit "$$ec"; \
fi; \
$(GO_CMD) mod verify
verify-fmt:
$(Q)report=`$(GO_FMT) -s -d -e $$(git ls-files '*.go' | while read f; do grep -q 'Code generated by ' $$f || echo $$f; done)`; \
if [ -n "$$report" ]; then \
echo "ERROR: go formatting errors"; \
echo "$$report" | sed 's/^/ERROR: /g'; \
echo "ERROR: please run make reformat or go fmt by hand and commit any changes."; \
exit 1; \
fi
verify-generate: generate
$(Q) git diff --quiet; ec="$$?"; \
if [ "$$ec" != "0" ]; then \
echo "ERROR: generated artifacts are not up-to-date."; \
echo "ERROR:"; \
git --no-pager diff | sed 's/^/ERROR: /g'; \
echo "ERROR:"; \
echo "ERROR: please run 'make generate' and commit these changes."; \
exit "$$ec"; \
fi
verify-build: build
verify-docs: site-build clean-html
verify-codespell codespell:
$(Q) codespell
#
# targets for installing dependencies
#
install-ginkgo:
$(Q)$(GO_INSTALL) -mod=mod github.com/onsi/ginkgo/v2/ginkgo
report-licenses:
$(Q)mkdir -p $(LICENSE_PATH) && \
LICENSE_PKGS=`find ./cmd -name Dockerfile | xargs -l dirname`; \
go-licenses report $$LICENSE_PKGS \
--ignore github.com/containers/nri-plugins \
> $(LICENSE_PATH)/licenses.csv && \
echo See $(LICENSE_PATH)/licenses.csv for license information
#
# Rules for documentation
#
html: clean-html
$(Q)BUILD_VERSION=$(BUILD_VERSION) \
$(SPHINXBUILD) -c docs . "$(SITE_BUILDDIR)" $(SPHINXOPTS)
cp docs/index.html "$(SITE_BUILDDIR)"
for d in $$(find docs -name figures -type d); do \
mkdir -p $(SITE_BUILDDIR)/$$d && cp $$d/* $(SITE_BUILDDIR)/$$d; \
done
serve-html: html
$(Q)cd $(SITE_BUILDDIR) && python3 -m http.server 8081
clean-html:
rm -rf $(SITE_BUILDDIR)
site-build: .$(DOCKER_SITE_BUILDER_IMAGE).image.stamp
$(Q)$(DOCKER_SITE_CMD) $(DOCKER_SITE_BUILDER_IMAGE) make html
site-serve: .$(DOCKER_SITE_BUILDER_IMAGE).image.stamp
$(Q)$(DOCKER_SITE_CMD) -it $(DOCKER_SITE_BUILDER_IMAGE) make serve-html
.$(DOCKER_SITE_BUILDER_IMAGE).image.stamp: docs/Dockerfile docs/requirements.txt
$(DOCKER_BUILD) -t $(DOCKER_SITE_BUILDER_IMAGE) docs
touch $@
docs: site-build
#
# CRD et. al code generation
#
CRD_BASE_DIR := config/crd/bases
HELM_TOP_DIR := deployment/helm
# Location to install external generation tool binaries
BUILD_AUX ?= $(shell pwd)/build-aux
LOCALBIN ?= $(BUILD_AUX)/bin
$(LOCALBIN):
mkdir -p $(LOCALBIN)
## External generation tool binaries
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
CLIENT_GEN ?= $(LOCALBIN)/client-gen
GIT_CLONE := git clone
# External generation tool versions
CONTROLLER_TOOLS_VERSION ?= v0.16.5
CLIENT_GEN_VERSION ?= v0.28.1
CODE_GENERATOR_REPO ?= https://github.com/kubernetes/code-generator
CODE_GENERATOR_VERSION ?= v0.28.1
GENERATE_GROUPS ?= $(BUILD_AUX)/code-generator/generate-groups.sh
# controller-gen local installation rules
.PHONY: controller-gen
controller-gen: $(CONTROLLER_GEN)
$(CONTROLLER_GEN): $(LOCALBIN)
test -s $@ && $@ --version | grep -q $(CONTROLLER_TOOLS_VERSION) || \
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
# client-gen local installation rules
.PHONY: client-gen
client-gen: $(CLIENT_GEN)
$(CLIENT_GEN): $(LOCALBIN)
test -s $@ && $@ --version | grep -q $(CLIENT_GEN_VERSION) || \
GOBIN=$(LOCALBIN) go install k8s.io/code-generator/cmd/client-gen@$(CLIENT_GEN_VERSION)
# code-generator local installation rules
.PHONY: $(GENERATE_GROUPS)
$(GENERATE_GROUPS):
test -s $@ || \
$(GIT_CLONE) $(CODE_GENERATOR_REPO) -b $(CODE_GENERATOR_VERSION) $(dir $@) && \
cd $(dir $@) && \
for d in ./cmd/*; do go build $$d; done
# CRD generation rules
.PHONY: generate-manifests
generate-manifests: controller-gen
$(CONTROLLER_GEN) rbac:roleName=manager-role crd paths="./pkg/apis/..." output:crd:artifacts:config=$(CRD_BASE_DIR)
# code generation rules for CRD types (DeepCopy, DeepCopyInto, etc.)
.PHONY: generate-types
generate-types: controller-gen
$(CONTROLLER_GEN) object:headerFile="./docs/license-header.go.txt" paths="./pkg/apis/..."
# client generation rules
.PHONY: generate-clients
generate-clients: $(GENERATE_GROUPS)
$(GENERATE_GROUPS) client \
github.com/containers/nri-plugins/pkg/generated \
github.com/containers/nri-plugins/pkg/apis \
"config:v1alpha1" --output-base=. \
--go-header-file=./docs/license-header.go.txt && \
rm -fr pkg/generated && \
mv github.com/containers/nri-plugins/pkg/generated pkg && \
rm -fr github.com
# golang code generation rules
generate-go:
$(Q)$(GO_CMD) generate ./...
# rule to update generated CRDs in our helm charts
.PHONY: update-helm-crds
update-helm-crds:
$(Q)for plugin in $(PLUGINS); do \
plugin="$${plugin#nri-}"; plugin="$${plugin#resource-policy-}"; \
helm_dir=$(HELM_TOP_DIR)/$$plugin/crds; \
if [ ! -d "$$helm_dir" ]; then \
echo "No generated CRD found for $$plugin plugin..."; \
else \
echo "Updating Helm chart CRDs for $$plugin plugin..."; \
cp $(CRD_BASE_DIR)/*_$${plugin//-/}*.yaml $$helm_dir; \
fi; \
done
# top level rule to (re)generate everything we need
generate: generate-go generate-types generate-manifests generate-clients update-helm-crds