inspektor-gadget
/
Makefile
411 строк · 14.6 Кб
1TAG := `git describe --tags --always`
2VERSION :=
3
4CONTAINER_REPO ?= ghcr.io/inspektor-gadget/inspektor-gadget
5IMAGE_TAG ?= $(shell ./tools/image-tag branch)
6
7MINIKUBE ?= minikube
8KUBERNETES_DISTRIBUTION ?= ""
9GADGET_TAG ?= $(shell ./tools/image-tag branch)
10GADGET_REPOSITORY ?= ghcr.io/inspektor-gadget/gadget
11
12GOHOSTOS ?= $(shell go env GOHOSTOS)
13GOHOSTARCH ?= $(shell go env GOHOSTARCH)
14
15KUBERNETES_ARCHITECTURE ?= $(GOHOSTARCH)
16
17ENABLE_BTFGEN ?= false
18
19BPFTOOL ?= bpftool
20ARCH ?= $(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.
23LINTER_VERSION ?= v1.54.2
24
25EBPF_BUILDER ?= ghcr.io/inspektor-gadget/ebpf-builder:latest
26
27DNSTESTER_IMAGE ?= "ghcr.io/inspektor-gadget/dnstester:latest"
28
29PLATFORMS ?= "linux/amd64,linux/arm64"
30
31CLANG_FORMAT ?= clang-format
32
33# Adds a '-dirty' suffix to version string if there are uncommitted changes
34changes := $(shell git status --porcelain)
35ifeq ($(changes),)
36VERSION := $(TAG)
37else
38VERSION := $(TAG)-dirty
39endif
40
41pvpath := $(shell command -v pv 2>/dev/null || true)
42ifeq ($(pvpath),)
43PV :=
44else
45PV := | $(pvpath)
46endif
47
48# export variables that are used in Makefile.btfgen as well.
49export BPFTOOL ARCH
50
51include crd.mk
52include tests.mk
53include minikube.mk
54
55LDFLAGS := "-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
61build: manifests generate kubectl-gadget gadget-container
62
63.PHONY: all
64all: 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
70phony_explicit:
71
72ebpf-objects:
73docker run --rm --name ebpf-object-builder --user $(shell id -u):$(shell id -g) \
74-v $(shell pwd):/work $(EBPF_BUILDER) \
75make ebpf-objects-outside-docker
76
77ebpf-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.
89TARGET=arm64 CFLAGS="-I/usr/include/$(shell uname -m)-linux-gnu -I$(shell pwd)/include/gadget/arm64/ -I$(shell pwd)/include/" go generate ./...
90TARGET=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
94IG_TARGETS = \
95ig-linux-amd64 \
96ig-linux-arm64
97
98.PHONY: list-ig-targets
99list-ig-targets:
100@echo $(IG_TARGETS)
101
102.PHONY: ig-all
103ig-all: $(IG_TARGETS) ig
104
105ig: ig-$(GOHOSTOS)-$(GOHOSTARCH)
106cp ig-$(GOHOSTOS)-$(GOHOSTARCH) ig
107
108.PHONY: install/ig
109install/ig: ig
110sudo cp ig /usr/local/bin/ig
111
112ig-%: phony_explicit
113echo Building $@
114if $(ENABLE_BTFGEN) == "true" ; then \
115./tools/getbtfhub.sh && \
116$(MAKE) -f Makefile.btfgen \
117ARCH=$(subst linux-,,$*) BTFHUB_ARCHIVE=$(HOME)/btfhub-archive/ -j$(nproc); \
118fi
119docker buildx build --load --platform=$(subst -,/,$*) -t $@ -f Dockerfiles/ig.Dockerfile \
120--build-arg VERSION=$(VERSION) --build-arg EBPF_BUILDER=$(EBPF_BUILDER) .
121docker create --name ig-$*-container $@
122docker cp ig-$*-container:/usr/bin/ig $@
123docker rm ig-$*-container
124chmod +x $@
125
126KUBECTL_GADGET_TARGETS = \
127kubectl-gadget-linux-amd64 \
128kubectl-gadget-linux-arm64 \
129kubectl-gadget-darwin-amd64 \
130kubectl-gadget-darwin-arm64 \
131kubectl-gadget-windows-amd64
132
133.PHONY: list-kubectl-gadget-targets
134list-kubectl-gadget-targets:
135@echo $(KUBECTL_GADGET_TARGETS)
136
137.PHONY: kubectl-gadget-all
138kubectl-gadget-all: $(KUBECTL_GADGET_TARGETS) kubectl-gadget
139
140kubectl-gadget: kubectl-gadget-$(GOHOSTOS)-$(GOHOSTARCH)
141cp kubectl-gadget-$(GOHOSTOS)-$(GOHOSTARCH)$(if $(findstring windows,$*),.exe,) kubectl-gadget$(if $(findstring windows,$*),.exe,)
142
143kubectl-gadget-%: phony_explicit
144export GO111MODULE=on CGO_ENABLED=0 && \
145export GOOS=$(shell echo $* | cut -f1 -d-) GOARCH=$(shell echo $* | cut -f2 -d-) && \
146go build -ldflags $(LDFLAGS) \
147-tags withoutebpf \
148-o kubectl-gadget-$${GOOS}-$${GOARCH}$(if $(findstring windows,$*),.exe,) \
149github.com/inspektor-gadget/inspektor-gadget/cmd/kubectl-gadget
150
151.PHONY: install/kubectl-gadget
152install/kubectl-gadget: kubectl-gadget-$(GOHOSTOS)-$(GOHOSTARCH)
153mkdir -p ~/.local/bin/
154cp kubectl-gadget-$(GOHOSTOS)-$(GOHOSTARCH) ~/.local/bin/kubectl-gadget
155
156GADGETCTL_TARGETS = \
157gadgetctl-linux-amd64 \
158gadgetctl-linux-arm64 \
159gadgetctl-darwin-amd64 \
160gadgetctl-darwin-arm64 \
161gadgetctl-windows-amd64
162
163.PHONY: list-gadgetctl-targets
164list-gadgetctl-targets:
165@echo $(GADGETCTL_TARGETS)
166
167.PHONY: gadgetctl-all
168gadgetctl-all: $(GADGETCTL_TARGETS) gadgetctl
169
170gadgetctl: gadgetctl-$(GOHOSTOS)-$(GOHOSTARCH)
171cp gadgetctl-$(GOHOSTOS)-$(GOHOSTARCH)$(if $(findstring windows,$*),.exe,) gadgetctl$(if $(findstring windows,$*),.exe,)
172
173gadgetctl-%: phony_explicit
174export GO111MODULE=on CGO_ENABLED=0 && \
175export GOOS=$(shell echo $* |cut -f1 -d-) GOARCH=$(shell echo $* |cut -f2 -d-) && \
176go build -ldflags $(LDFLAGS) \
177-tags withoutebpf \
178-o gadgetctl-$${GOOS}-$${GOARCH}$(if $(findstring windows,$*),.exe,) \
179github.com/inspektor-gadget/inspektor-gadget/cmd/gadgetctl
180
181.PHONY: install/gadgetctl
182install/gadgetctl: gadgetctl-$(GOHOSTOS)-$(GOHOSTARCH)
183mkdir -p ~/.local/bin/
184cp gadgetctl-$(GOHOSTOS)-$(GOHOSTARCH) ~/.local/bin/gadgetctl
185
186.PHONY: gadget-container
187gadget-container:
188if $(ENABLE_BTFGEN) == "true" ; then \
189./tools/getbtfhub.sh && \
190$(MAKE) -f Makefile.btfgen \
191BTFHUB_ARCHIVE=$(HOME)/btfhub-archive/ -j$(nproc); \
192fi
193docker buildx build --load -t $(CONTAINER_REPO):$(IMAGE_TAG) \
194-f Dockerfiles/gadget.Dockerfile .
195
196.PHONY: cross-gadget-container
197cross-gadget-container:
198if $(ENABLE_BTFGEN) == "true" ; then \
199./tools/getbtfhub.sh && \
200$(MAKE) -f Makefile.btfgen \
201ARCH=x86 BTFHUB_ARCHIVE=$(HOME)/btfhub-archive/ -j$(nproc) && \
202$(MAKE) -f Makefile.btfgen \
203ARCH=arm64 BTFHUB_ARCHIVE=$(HOME)/btfhub-archive/ -j$(nproc); \
204fi
205docker buildx build --platform=$(PLATFORMS) -t $(CONTAINER_REPO):$(IMAGE_TAG) \
206--push \
207-f Dockerfiles/gadget.Dockerfile .
208
209push-gadget-container:
210docker push $(CONTAINER_REPO):$(IMAGE_TAG)
211
212# kubectl-gadget container image
213.PHONY: kubectl-gadget-container
214kubectl-gadget-container:
215docker 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
219cross-kubectl-gadget-container:
220docker 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
225test:
226go test -test.v ./...
227
228.PHONY: controller-tests
229controller-tests: kube-apiserver etcd kubectl
230ACK_GINKGO_DEPRECATIONS=1.16.4 \
231TEST_ASSET_KUBE_APISERVER=$(KUBE_APISERVER_BIN) \
232TEST_ASSET_ETCD=$(ETCD_BIN) \
233TEST_ASSET_KUBECTL=$(KUBECTL_BIN) \
234go test -test.v ./pkg/controllers/... -controller-test
235
236.PHONY: gadgets-unit-tests
237gadgets-unit-tests:
238go 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
243gadgets-benchmarks:
244go test -exec sudo -ldflags="-s=false" -bench=. -run=Benchmark ./pkg/gadgets/... ./internal/benchmarks/...
245
246.PHONY: ig-tests
247ig-tests:
248# Compile and execute in separate commands because Go might not be
249# available in the root environment
250go test -c ./pkg/ig-manager
251sudo ./ig-manager.test -test.v $$IG_TESTS_PARAMS
252rm -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
257integration-tests: kubectl-gadget
258KUBECTL_GADGET="$(shell pwd)/kubectl-gadget" \
259go 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
273component-tests:
274go test -exec sudo -v ./integration/components/... -integration -timeout 5m --builder-image $(EBPF_BUILDER)
275
276.PHONY: generate-documentation
277generate-documentation:
278go run -tags docs cmd/gen-doc/gen-doc.go -repo $(shell pwd)
279
280.PHONY: website-local-update
281website-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
285test -d ../website/external-docs/inspektor-gadget.git_mainlatest/
286# Replace the documentation
287rm -rf ../website/external-docs/inspektor-gadget.git_mainlatest/docs
288cp -r docs ../website/external-docs/inspektor-gadget.git_mainlatest/
289
290lint:
291docker 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.
296docker 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 \
299linter
300
301clang-format:
302find ./ -type f \( -iname '*.h' ! -iname "vmlinux.h" \) -o -iname '*.c' -execdir $(CLANG_FORMAT) -i {} \;
303
304# minikube
305LIVENESS_PROBE ?= true
306.PHONY: minikube-deploy
307minikube-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:"
313docker 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.
319if $(MINIKUBE) docker-env >/dev/null 2>&1 ; then \
320docker save $(CONTAINER_REPO):$(IMAGE_TAG) $(PV) | (eval $$($(MINIKUBE) docker-env | grep =) && docker load) ; \
321else \
322$(MINIKUBE) image load $(CONTAINER_REPO):$(IMAGE_TAG) ; \
323fi
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
330kubectl rollout status daemonset -n gadget gadget --timeout 30s
331@echo "Image used by the gadget pod:"
332kubectl get pod -n gadget -o yaml|grep imageID:
333@echo "Minikube profile used:"
334$(MINIKUBE) profile
335
336.PHONY: btfgen
337btfgen:
338+make -f Makefile.btfgen
339
340.PHONY: generate-manifests
341generate-manifests:
342echo "---" > pkg/resources/manifests/deploy.yaml
343echo "# This file is generated by 'make generate-manifests'; DO NOT EDIT." >> pkg/resources/manifests/deploy.yaml
344cat pkg/resources/manifests/namespace.yaml >> pkg/resources/manifests/deploy.yaml
345make -C charts APP_VERSION=latest template
346cat charts/bin/deploy.yaml >> pkg/resources/manifests/deploy.yaml
347
348.PHONY: install-headers
349install-headers:
350cp -r ./include/gadget/ /usr/include/
351
352.PHONY: remove-headers
353remove-headers:
354rm -rf /usr/include/gadget
355
356.PHOHY: build-gadgets
357build-gadgets: install/ig
358$(MAKE) -C gadgets/ build
359
360.PHOHY: push-gadgets
361push-gadgets: install/ig
362$(MAKE) -C gadgets/ push
363
364.PHONY: testdata
365testdata:
366$(MAKE) -C testdata/
367
368.PHONY: help
369help:
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