inspektor-gadget

Форк
0
/
Makefile 
411 строк · 14.6 Кб
1
TAG := `git describe --tags --always`
2
VERSION :=
3

4
CONTAINER_REPO ?= ghcr.io/inspektor-gadget/inspektor-gadget
5
IMAGE_TAG ?= $(shell ./tools/image-tag branch)
6

7
MINIKUBE ?= minikube
8
KUBERNETES_DISTRIBUTION ?= ""
9
GADGET_TAG ?= $(shell ./tools/image-tag branch)
10
GADGET_REPOSITORY ?= ghcr.io/inspektor-gadget/gadget
11

12
GOHOSTOS ?= $(shell go env GOHOSTOS)
13
GOHOSTARCH ?= $(shell go env GOHOSTARCH)
14

15
KUBERNETES_ARCHITECTURE ?= $(GOHOSTARCH)
16

17
ENABLE_BTFGEN ?= false
18

19
BPFTOOL ?= bpftool
20
ARCH ?= $(shell uname -m | sed 's/x86_64/x86/' | sed 's/aarch64/arm64/' | sed 's/ppc64le/powerpc/' | sed 's/mips.*/mips/')
21

22
# This version number must be kept in sync with CI workflow lint one.
23
LINTER_VERSION ?= v1.54.2
24

25
EBPF_BUILDER ?= ghcr.io/inspektor-gadget/ebpf-builder:latest
26

27
DNSTESTER_IMAGE ?= "ghcr.io/inspektor-gadget/dnstester:latest"
28

29
PLATFORMS ?= "linux/amd64,linux/arm64"
30

31
CLANG_FORMAT ?= clang-format
32

33
# Adds a '-dirty' suffix to version string if there are uncommitted changes
34
changes := $(shell git status --porcelain)
35
ifeq ($(changes),)
36
	VERSION := $(TAG)
37
else
38
	VERSION := $(TAG)-dirty
39
endif
40

41
pvpath := $(shell command -v pv 2>/dev/null || true)
42
ifeq ($(pvpath),)
43
	PV :=
44
else
45
	PV := | $(pvpath)
46
endif
47

48
# export variables that are used in Makefile.btfgen as well.
49
export BPFTOOL ARCH
50

51
include crd.mk
52
include tests.mk
53
include minikube.mk
54

55
LDFLAGS := "-X github.com/inspektor-gadget/inspektor-gadget/cmd/common.version=$(VERSION) \
56
-X main.gadgetimage=$(CONTAINER_REPO):$(IMAGE_TAG) \
57
-extldflags '-static'"
58

59
.DEFAULT_GOAL := build
60
.PHONY: build
61
build: manifests generate kubectl-gadget gadget-container
62

63
.PHONY: all
64
all: build ig
65

66
# make does not allow implicit rules (with '%') to be phony so let's use
67
# the 'phony_explicit' dependency to make implicit rules inherit the phony
68
# attribute
69
.PHONY: phony_explicit
70
phony_explicit:
71

72
ebpf-objects:
73
	docker run --rm --name ebpf-object-builder --user $(shell id -u):$(shell id -g) \
74
		-v $(shell pwd):/work $(EBPF_BUILDER) \
75
		make ebpf-objects-outside-docker
76

77
ebpf-objects-outside-docker:
78
# We need <asm/types.h> and depending on Linux distributions, it is installed
79
# at different paths:
80
#
81
# * Ubuntu, package linux-libc-dev:
82
#   /usr/include/x86_64-linux-gnu/asm/types.h
83
#
84
# * Fedora, package kernel-headers
85
#   /usr/include/asm/types.h
86
#
87
# Since Ubuntu does not install it in a standard path, add a compiler flag for
88
# it.
89
	TARGET=arm64 CFLAGS="-I/usr/include/$(shell uname -m)-linux-gnu -I$(shell pwd)/include/gadget/arm64/ -I$(shell pwd)/include/" go generate ./...
90
	TARGET=amd64 CFLAGS="-I/usr/include/$(shell uname -m)-linux-gnu -I$(shell pwd)/include/gadget/amd64/ -I$(shell pwd)/include/" go generate ./...
91

92
# ig
93

94
IG_TARGETS = \
95
	ig-linux-amd64 \
96
	ig-linux-arm64
97

98
.PHONY: list-ig-targets
99
list-ig-targets:
100
	@echo $(IG_TARGETS)
101

102
.PHONY: ig-all
103
ig-all: $(IG_TARGETS) ig
104

105
ig: ig-$(GOHOSTOS)-$(GOHOSTARCH)
106
	cp ig-$(GOHOSTOS)-$(GOHOSTARCH) ig
107

108
.PHONY: install/ig
109
install/ig: ig
110
	sudo cp ig /usr/local/bin/ig
111

112
ig-%: phony_explicit
113
	echo Building $@
114
	if $(ENABLE_BTFGEN) == "true" ; then \
115
		./tools/getbtfhub.sh && \
116
		$(MAKE) -f Makefile.btfgen \
117
			ARCH=$(subst linux-,,$*) BTFHUB_ARCHIVE=$(HOME)/btfhub-archive/ -j$(nproc); \
118
	fi
119
	docker buildx build --load --platform=$(subst -,/,$*) -t $@ -f Dockerfiles/ig.Dockerfile \
120
		--build-arg VERSION=$(VERSION) --build-arg EBPF_BUILDER=$(EBPF_BUILDER) .
121
	docker create --name ig-$*-container $@
122
	docker cp ig-$*-container:/usr/bin/ig $@
123
	docker rm ig-$*-container
124
	chmod +x $@
125

126
KUBECTL_GADGET_TARGETS = \
127
	kubectl-gadget-linux-amd64 \
128
	kubectl-gadget-linux-arm64 \
129
	kubectl-gadget-darwin-amd64 \
130
	kubectl-gadget-darwin-arm64 \
131
	kubectl-gadget-windows-amd64
132

133
.PHONY: list-kubectl-gadget-targets
134
list-kubectl-gadget-targets:
135
	@echo $(KUBECTL_GADGET_TARGETS)
136

137
.PHONY: kubectl-gadget-all
138
kubectl-gadget-all: $(KUBECTL_GADGET_TARGETS) kubectl-gadget
139

140
kubectl-gadget: kubectl-gadget-$(GOHOSTOS)-$(GOHOSTARCH)
141
	cp kubectl-gadget-$(GOHOSTOS)-$(GOHOSTARCH)$(if $(findstring windows,$*),.exe,) kubectl-gadget$(if $(findstring windows,$*),.exe,)
142

143
kubectl-gadget-%: phony_explicit
144
	export GO111MODULE=on CGO_ENABLED=0 && \
145
	export GOOS=$(shell echo $* | cut -f1 -d-) GOARCH=$(shell echo $* | cut -f2 -d-) && \
146
	go build -ldflags $(LDFLAGS) \
147
		-tags withoutebpf \
148
		-o kubectl-gadget-$${GOOS}-$${GOARCH}$(if $(findstring windows,$*),.exe,) \
149
		github.com/inspektor-gadget/inspektor-gadget/cmd/kubectl-gadget
150

151
.PHONY: install/kubectl-gadget
152
install/kubectl-gadget: kubectl-gadget-$(GOHOSTOS)-$(GOHOSTARCH)
153
	mkdir -p ~/.local/bin/
154
	cp kubectl-gadget-$(GOHOSTOS)-$(GOHOSTARCH) ~/.local/bin/kubectl-gadget
155

156
GADGETCTL_TARGETS = \
157
	gadgetctl-linux-amd64 \
158
	gadgetctl-linux-arm64 \
159
	gadgetctl-darwin-amd64 \
160
	gadgetctl-darwin-arm64 \
161
	gadgetctl-windows-amd64
162

163
.PHONY: list-gadgetctl-targets
164
list-gadgetctl-targets:
165
	@echo $(GADGETCTL_TARGETS)
166

167
.PHONY: gadgetctl-all
168
gadgetctl-all: $(GADGETCTL_TARGETS) gadgetctl
169

170
gadgetctl: gadgetctl-$(GOHOSTOS)-$(GOHOSTARCH)
171
	cp gadgetctl-$(GOHOSTOS)-$(GOHOSTARCH)$(if $(findstring windows,$*),.exe,) gadgetctl$(if $(findstring windows,$*),.exe,)
172

173
gadgetctl-%: phony_explicit
174
	export GO111MODULE=on CGO_ENABLED=0 && \
175
	export GOOS=$(shell echo $* |cut -f1 -d-) GOARCH=$(shell echo $* |cut -f2 -d-) && \
176
	go build -ldflags $(LDFLAGS) \
177
		-tags withoutebpf \
178
		-o gadgetctl-$${GOOS}-$${GOARCH}$(if $(findstring windows,$*),.exe,) \
179
		github.com/inspektor-gadget/inspektor-gadget/cmd/gadgetctl
180

181
.PHONY: install/gadgetctl
182
install/gadgetctl: gadgetctl-$(GOHOSTOS)-$(GOHOSTARCH)
183
	mkdir -p ~/.local/bin/
184
	cp gadgetctl-$(GOHOSTOS)-$(GOHOSTARCH) ~/.local/bin/gadgetctl
185

186
.PHONY: gadget-container
187
gadget-container:
188
	if $(ENABLE_BTFGEN) == "true" ; then \
189
		./tools/getbtfhub.sh && \
190
		$(MAKE) -f Makefile.btfgen \
191
			BTFHUB_ARCHIVE=$(HOME)/btfhub-archive/ -j$(nproc); \
192
	fi
193
	docker buildx build --load -t $(CONTAINER_REPO):$(IMAGE_TAG) \
194
		-f Dockerfiles/gadget.Dockerfile .
195

196
.PHONY: cross-gadget-container
197
cross-gadget-container:
198
	if $(ENABLE_BTFGEN) == "true" ; then \
199
		./tools/getbtfhub.sh && \
200
		$(MAKE) -f Makefile.btfgen \
201
			ARCH=x86 BTFHUB_ARCHIVE=$(HOME)/btfhub-archive/ -j$(nproc) && \
202
		$(MAKE) -f Makefile.btfgen \
203
			ARCH=arm64 BTFHUB_ARCHIVE=$(HOME)/btfhub-archive/ -j$(nproc); \
204
	fi
205
	docker buildx build --platform=$(PLATFORMS) -t $(CONTAINER_REPO):$(IMAGE_TAG) \
206
		--push \
207
		-f Dockerfiles/gadget.Dockerfile .
208

209
push-gadget-container:
210
	docker push $(CONTAINER_REPO):$(IMAGE_TAG)
211

212
# kubectl-gadget container image
213
.PHONY: kubectl-gadget-container
214
kubectl-gadget-container:
215
	docker buildx build --load -t kubectl-gadget -f Dockerfiles/kubectl-gadget.Dockerfile \
216
	--build-arg IMAGE_TAG=$(IMAGE_TAG) .
217

218
.PHONY: cross-kubectl-gadget-container
219
cross-kubectl-gadget-container:
220
	docker buildx build --platform=$(PLATFORMS) -t kubectl-gadget -f Dockerfiles/kubectl-gadget.Dockerfile \
221
	--build-arg IMAGE_TAG=$(IMAGE_TAG) .
222

223
# tests
224
.PHONY: test
225
test:
226
	go test -test.v ./...
227

228
.PHONY: controller-tests
229
controller-tests: kube-apiserver etcd kubectl
230
	ACK_GINKGO_DEPRECATIONS=1.16.4 \
231
	TEST_ASSET_KUBE_APISERVER=$(KUBE_APISERVER_BIN) \
232
	TEST_ASSET_ETCD=$(ETCD_BIN) \
233
	TEST_ASSET_KUBECTL=$(KUBECTL_BIN) \
234
	go test -test.v ./pkg/controllers/... -controller-test
235

236
.PHONY: gadgets-unit-tests
237
gadgets-unit-tests:
238
	go test -test.v -exec sudo ./...
239

240
# Individual tests can be selected with a command such as:
241
# go test -exec sudo -ldflags="-s=false" -bench='^BenchmarkAllGadgetsWithContainers$/^container100$/snapshot-socket' -run=Benchmark ./internal/benchmarks/... -count 10
242
.PHONY: gadgets-benchmarks
243
gadgets-benchmarks:
244
	go test -exec sudo -ldflags="-s=false" -bench=. -run=Benchmark ./pkg/gadgets/... ./internal/benchmarks/...
245

246
.PHONY: ig-tests
247
ig-tests:
248
	# Compile and execute in separate commands because Go might not be
249
	# available in the root environment
250
	go test -c ./pkg/ig-manager
251
	sudo ./ig-manager.test -test.v $$IG_TESTS_PARAMS
252
	rm -f ./ig-manager.test
253

254
# INTEGRATION_TESTS_PARAMS can be used to pass additional parameters locally e.g
255
# INTEGRATION_TESTS_PARAMS="-run TestTraceExec -no-deploy-ig -no-deploy-spo" make integration-tests
256
.PHONY: integration-tests
257
integration-tests: kubectl-gadget
258
	KUBECTL_GADGET="$(shell pwd)/kubectl-gadget" \
259
		go test ./integration/inspektor-gadget/... \
260
			-v \
261
			-integration \
262
			-timeout 30m \
263
			-k8s-distro $(KUBERNETES_DISTRIBUTION) \
264
			-k8s-arch $(KUBERNETES_ARCHITECTURE) \
265
			-image $(CONTAINER_REPO):$(IMAGE_TAG) \
266
			-dnstester-image $(DNSTESTER_IMAGE) \
267
			-gadget-repository $(GADGET_REPOSITORY) \
268
			-gadget-tag $(GADGET_TAG) \
269
			$$INTEGRATION_TESTS_PARAMS
270

271

272
.PHONY: component-tests
273
component-tests:
274
	go test -exec sudo -v ./integration/components/... -integration -timeout 5m --builder-image $(EBPF_BUILDER)
275

276
.PHONY: generate-documentation
277
generate-documentation:
278
	go run -tags docs cmd/gen-doc/gen-doc.go -repo $(shell pwd)
279

280
.PHONY: website-local-update
281
website-local-update:
282
	# Check that the website repository is cloned in the parent directory
283
	# https://github.com/inspektor-gadget/website
284
	# And that "make docs" has been run once
285
	test -d ../website/external-docs/inspektor-gadget.git_mainlatest/
286
	# Replace the documentation
287
	rm -rf ../website/external-docs/inspektor-gadget.git_mainlatest/docs
288
	cp -r docs ../website/external-docs/inspektor-gadget.git_mainlatest/
289

290
lint:
291
	docker build -t linter -f Dockerfiles/linter.Dockerfile --build-arg VERSION=$(LINTER_VERSION) Dockerfiles
292
# XDG_CACHE_HOME is necessary to avoid this type of errors:
293
# ERRO Running error: context loading failed: failed to load packages: failed to load with go/packages: err: exit status 1: stderr: failed to initialize build cache at /.cache/go-build: mkdir /.cache: permission denied
294
# Process 15167 has exited with status 3
295
# While GOLANGCI_LINT_CACHE is used to store golangci-lint cache.
296
	docker run --rm --env XDG_CACHE_HOME=/tmp/xdg_home_cache \
297
		--env GOLANGCI_LINT_CACHE=/tmp/golangci_lint_cache \
298
		--user $(shell id -u):$(shell id -g) -v $(shell pwd):/app -w /app \
299
		linter
300

301
clang-format:
302
	find ./ -type f \( -iname '*.h' ! -iname "vmlinux.h" \) -o -iname '*.c' -execdir $(CLANG_FORMAT) -i {} \;
303

304
# minikube
305
LIVENESS_PROBE ?= true
306
.PHONY: minikube-deploy
307
minikube-deploy: minikube-start gadget-container kubectl-gadget
308
	# Remove all resources created by Inspektor Gadget
309
	./kubectl-gadget undeploy || true
310
	# Remove the image from Minikube
311
	$(MINIKUBE) image rm $(CONTAINER_REPO):$(IMAGE_TAG) || true
312
	@echo "Image on the host:"
313
	docker image list --format "table {{.ID}}\t{{.Repository}}:{{.Tag}}\t{{.Size}}" |grep $(CONTAINER_REPO):$(IMAGE_TAG)
314
	@echo
315
	# Unfortunately, minikube-cache and minikube-image have bugs in older
316
	# versions. And new versions of minikube don't support all eBPF
317
	# features. So we have to keep "docker-save|docker-load" when
318
	# available.
319
	if $(MINIKUBE) docker-env >/dev/null 2>&1 ; then \
320
		docker save $(CONTAINER_REPO):$(IMAGE_TAG) $(PV) | (eval $$($(MINIKUBE) docker-env | grep =) && docker load) ; \
321
	else \
322
		$(MINIKUBE) image load $(CONTAINER_REPO):$(IMAGE_TAG) ; \
323
	fi
324
	@echo "Image in Minikube:"
325
	$(MINIKUBE) image ls --format=table | grep "$(CONTAINER_REPO)\s*|\s*$(IMAGE_TAG)" || \
326
		(echo "Image $(CONTAINER_REPO)\s*|\s*$(IMAGE_TAG) was not correctly loaded into Minikube" && false)
327
	@echo
328
	./kubectl-gadget deploy --liveness-probe=$(LIVENESS_PROBE) \
329
		--image-pull-policy=Never
330
	kubectl rollout status daemonset -n gadget gadget --timeout 30s
331
	@echo "Image used by the gadget pod:"
332
	kubectl get pod -n gadget -o yaml|grep imageID:
333
	@echo "Minikube profile used:"
334
	$(MINIKUBE) profile
335

336
.PHONY: btfgen
337
btfgen:
338
	+make -f Makefile.btfgen
339

340
.PHONY: generate-manifests
341
generate-manifests:
342
	echo "---" > pkg/resources/manifests/deploy.yaml
343
	echo "# This file is generated by 'make generate-manifests'; DO NOT EDIT." >> pkg/resources/manifests/deploy.yaml
344
	cat pkg/resources/manifests/namespace.yaml >> pkg/resources/manifests/deploy.yaml
345
	make -C charts APP_VERSION=latest template
346
	cat charts/bin/deploy.yaml >> pkg/resources/manifests/deploy.yaml
347

348
.PHONY: install-headers
349
install-headers:
350
	cp -r ./include/gadget/ /usr/include/
351

352
.PHONY: remove-headers
353
remove-headers:
354
	rm -rf /usr/include/gadget
355

356
.PHOHY: build-gadgets
357
build-gadgets: install/ig
358
	$(MAKE) -C gadgets/ build
359

360
.PHOHY: push-gadgets
361
push-gadgets: install/ig
362
	$(MAKE) -C gadgets/ push
363

364
.PHONY: testdata
365
testdata:
366
	$(MAKE) -C testdata/
367

368
.PHONY: help
369
help:
370
	@echo  'Building targets:'
371
	@echo  '  all		  		- Build all targets marked with [*]'
372
	@echo  '* ig		  		- Build the ig cli tool'
373
	@echo  '  ig-all	  		- Build the ig cli tool for all architectures'
374
	@echo  '* build		  		- Build all targets marked with [o]'
375
	@echo  'o manifests			- Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects'
376
	@echo  'o generate			- Generate client API code and DeepCopy related code'
377
	@echo  'o kubectl-gadget		- Build the kubectl plugin'
378
	@echo  '  kubectl-gadget-all		- Build the kubectl plugin for all architectures'
379
	@echo  '  kubectl-gadget-container	- Build container for kubectl-gadget'
380
	@echo  'o gadget-container		- Build the gadget container image for the host architecture'
381
	@echo  '  cross-gadget-container	- Build the gadget container image for all supported architectures'
382
	@echo  '  ebpf-objects			- Build eBPF objects file inside docker'
383
	@echo  '  ebpf-objects-outside-docker	- Build eBPF objects file on host'
384
	@echo  '  btfgen			- Build BTF files'
385
	@echo  '  list-ig-targets		- List ig available architectures'
386
	@echo  '  list-kubectl-gadget-targets	- List kubectl plugin available architectures'
387
	@echo  '  build-gadgets			- Build all gadgets'
388
	@echo  '  push-gadgets			- Push all gadgets'
389
	@echo  ''
390
	@echo  'Testing targets:'
391
	@echo  '  test				- Run unit tests'
392
	@echo  '  controller-tests		- Run controllers unit tests'
393
	@echo  '  ig-tests			- Run ig manager unit tests'
394
	@echo  '  gadgets-unit-tests		- Run gadget unit tests'
395
	@echo  '  integration-tests		- Run integration tests'
396
	@echo  ''
397
	@echo  'Installing targets:'
398
	@echo  '  install/kubectl-gadget	- Build kubectl plugin and install it in ~/.local/bin'
399
	@echo  '  install/ig	  		- Build and install ig cli tool in /usr/local/bin'
400
	@echo  ''
401
	@echo  'Development targets:'
402
	@echo  '  clang-format			- Format ebpf source files'
403
	@echo  '  lint				- Lint the Go code'
404
	@echo  '  generate-documentation	- Generate documentation for gadgets and trace CRD'
405
	@echo  '  generate-manifests		- Generate manifests for the gadget deployment'
406
	@echo  '  minikube-start		- Start a kubernetes cluster using minikube with the docker driver'
407
	@echo  '  minikube-deploy		- Build and deploy the gadget container on minikube with docker driver, the cluster is started if it does not exist'
408
	@echo  '  install-headers		- Install headers used to build gadgets in /usr/include/gadget'
409
	@echo  '  remove-headers		- Remove headers installed in /usr/include/gadget'
410
	@echo  '  testdata			- Build testdata'
411
	@echo  '  website-local-update		- Update the documentation in the website repository for testing locally'
412

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.