-
Notifications
You must be signed in to change notification settings - Fork 96
/
Makefile
369 lines (298 loc) · 14.7 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
PREFIX ?= /usr/local
INSTALL ?= install
CARGO ?= cargo
TEST_IMG_NAME ?= wasmtest:latest
RUNTIMES ?= wasmedge wasmtime wasmer wamr
CONTAINERD_NAMESPACE ?= default
RUSTC ?= rustc
SHELL=/bin/bash -o pipefail
# We have a bit of fancy logic here to determine the target
# since we support building for gnu and musl
# TARGET must eventually match one of the values in the cross.toml
HOST_TARGET = $(shell $(RUSTC) --version -v | sed -En 's/host: (.*)/\1/p')
# if TARGET is not set and we are using cross
# default to musl to facilitate easier use shim on other distros because of the static build
# otherwise use the host target
ifeq ($(TARGET),)
ifeq ($(CARGO),cross)
override TARGET = $(shell uname -m)-unknown-linux-musl
else
override TARGET = $(HOST_TARGET)
endif
endif
# always use cross when the target is not the host target
ifneq ($(TARGET),$(HOST_TARGET))
override CARGO = cross
endif
ifeq ($(CARGO),cross)
override TARGET_DIR := $(or $(TARGET_DIR),./target/build/$(TARGET)/)
# When using `cross` we need to run the tests outside the `cross` container.
# We stop `cargo test` from running the tests with the `--no-run` flag.
# We then need to run the generate test binary manually.
# For that we use `--message-format=json` and `jq` to find the name of the binary, `xargs` and execute it.
TEST_ARGS_SEP= --no-run --color=always --message-format=json | \
jq -R '. as $$line | try (fromjson | .executable | strings) catch ($$line+"\n" | stderr | empty)' -r | \
sed -E 's|^/target|$(TARGET_DIR)|' | \
xargs -I_ ./scripts/test-runner.sh ./_
else
override TARGET_DIR := $(or $(TARGET_DIR),./target/)
TEST_ARGS_SEP= --
endif
TARGET_FLAG = --target=$(TARGET) --target-dir=$(TARGET_DIR)
OPT_PROFILE ?= debug
RELEASE_FLAG :=
ifeq ($(OPT_PROFILE),release)
RELEASE_FLAG = --release
endif
FEATURES_wasmedge =
WARNINGS = -D warnings
ifeq ($(OS), Windows_NT)
# need to turn off static/standalone for wasm-edge
FEATURES_wasmedge = --no-default-features
# turn of warnings until windows is fully supported #49
WARNINGS =
endif
# turn opentelemetry feature on
FEATURES_wasm ?= -F opentelemetry
DOCKER_BUILD ?= docker buildx build
KIND_CLUSTER_NAME ?= containerd-wasm
export
.PHONY: build build-common build-wasm build-%
build: build-wasm $(RUNTIMES:%=build-%);
build-common: build-wasm;
build-wasm:
$(CARGO) build $(TARGET_FLAG) -p containerd-shim-wasm $(FEATURES_wasm) $(RELEASE_FLAG)
build-%:
$(CARGO) build $(TARGET_FLAG) -p containerd-shim-$* $(FEATURES_$*) $(RELEASE_FLAG)
build-oci-tar-builder:
$(CARGO) build $(TARGET_FLAG) -p oci-tar-builder $(FEATURES_$*) $(RELEASE_FLAG)
.PHONY: check check-common check-wasm check-%
check: check-wasm $(RUNTIMES:%=check-%);
check-common: check-wasm;
check-wasm:
# clear CARGO envvar as it otherwise interferes with rustfmt
CARGO= $(CARGO) +nightly fmt -p oci-tar-builder -p wasi-demo-app -p containerd-shim-wasm -p containerd-shim-wasm-test-modules -- --check
$(CARGO) clippy $(TARGET_FLAG) $(FEATURES_wasm) -p oci-tar-builder -p wasi-demo-app -p containerd-shim-wasm -p containerd-shim-wasm-test-modules -- $(WARNINGS)
check-%:
# clear CARGO envvar as it otherwise interferes with rustfmt
CARGO= $(CARGO) +nightly fmt -p containerd-shim-$* -- --check
$(CARGO) clippy $(TARGET_FLAG) $(FEATURES_$*) -p containerd-shim-$* -- $(WARNINGS)
.PHONY: fix fix-common fix-wasm fix-%
fix: fix-wasm $(RUNTIMES:%=fix-%);
fix-common: fix-wasm;
fix-wasm:
# clear CARGO envvar as it otherwise interferes with rustfmt
CARGO= $(CARGO) +nightly fmt -p oci-tar-builder -p wasi-demo-app -p containerd-shim-wasm -p containerd-shim-wasm-test-modules
$(CARGO) clippy $(TARGET_FLAG) $(FEATURES_wasm) --fix -p oci-tar-builder -p wasi-demo-app -p containerd-shim-wasm -p containerd-shim-wasm-test-modules -- $(WARNINGS)
fix-%:
# clear CARGO envvar as it otherwise interferes with rustfmt
CARGO= $(CARGO) +nightly fmt -p containerd-shim-$*
$(CARGO) clippy $(TARGET_FLAG) $(FEATURES_$*) --fix -p containerd-shim-$* -- $(WARNINGS)
.PHONY: test test-common test-wasm test-wasmedge test-%
test: test-wasm $(RUNTIMES:%=test-%);
test-common: test-wasm;
test-wasm:
# oci-tar-builder and wasi-demo-app have no tests
RUST_LOG=trace $(CARGO) test $(TARGET_FLAG) --package containerd-shim-wasm $(FEATURES_wasm) --verbose $(TEST_ARGS_SEP) --nocapture --test-threads=1
test-wasmedge:
# run tests in one thread to prevent parallelism
RUST_LOG=trace $(CARGO) test $(TARGET_FLAG) --package containerd-shim-wasmedge $(FEATURES_wasmedge) --lib --verbose $(TEST_ARGS_SEP) --nocapture --test-threads=1
ifneq ($(OS), Windows_NT)
ifneq ($(patsubst %-musl,,xx_$(TARGET)),)
# run wasmedge test without the default `static` feature
RUST_LOG=trace $(CARGO) test $(TARGET_FLAG) --package containerd-shim-wasmedge --no-default-features --features standalone --lib --verbose $(TEST_ARGS_SEP) --nocapture --test-threads=1
endif
endif
test-%:
# run tests in one thread to prevent parallelism
RUST_LOG=trace $(CARGO) test $(TARGET_FLAG) --package containerd-shim-$* $(FEATURES_$*) --lib --verbose $(TEST_ARGS_SEP) --nocapture --test-threads=1
test-doc:
RUST_LOG=trace $(CARGO) test --doc -- --test-threads=1
generate-doc:
RUST_LOG=trace $(CARGO) doc --workspace --all-features --no-deps --document-private-items --exclude wasi-demo-app
test-oci-tar-builder:
RUST_LOG=trace $(CARGO) test $(TARGET_FLAG) --package oci-tar-builder $(FEATURES_$*) --verbose $(TEST_ARGS_SEP) --nocapture --test-threads=1
.PHONY: install install-%
install: $(RUNTIMES:%=install-%);
install-%:
mkdir -p $(PREFIX)/bin
$(INSTALL) $(TARGET_DIR)$(if $(TARGET),$(TARGET)/,)$(OPT_PROFILE)/containerd-shim-$*-v1 $(PREFIX)/bin/
install-oci-tar-builder:
mkdir -p $(PREFIX)/bin
$(INSTALL) $(TARGET_DIR)$(if $(TARGET),$(TARGET)/,)$(OPT_PROFILE)/oci-tar-builder $(PREFIX)/bin/
.PHONY: dist dist-%
dist: $(RUNTIMES:%=dist-%);
dist-%:
[ -f $(PWD)/dist/bin/containerd-shim-$*-v1 ] || $(MAKE) install-$* CARGO=$(CARGO) PREFIX="$(PWD)/dist" OPT_PROFILE="$(OPT_PROFILE)"
.PHONY: dist/clean
dist/clean:
rm -rf dist
.PHONY: install/all
install/all: test-image/clean install test-image load
.PHONY: install/oci/all
install/oci/all: test-image/oci/clean install test-image/oci load/oci
.PHONY: test-image
test-image: dist/img.tar
.PHONY: test-image/oci
test-image/oci: dist/img-oci.tar dist/img-oci-artifact.tar
.PHONY: test-image/http
test-image/http: dist/http-img-oci.tar
.PHONY: test-image/clean
test-image/clean:
rm -rf target/wasm32-wasip1/$(OPT_PROFILE)/
.PHONY: test-image/oci/clean
test-image/oci/clean:
rm -rf target/wasm32-wasip1/$(OPT_PROFILE)/img-oci.tar
rm -rf target/wasm32-wasip1/$(OPT_PROFILE)/img-oci-artifact.tar
.PHONY: demo-app
demo-app: target/wasm32-wasip1/$(OPT_PROFILE)/wasi-demo-app.wasm
.PHONY: target/wasm32-wasip1/$(OPT_PROFILE)/wasi-demo-app.wasm
target/wasm32-wasip1/$(OPT_PROFILE)/wasi-demo-app.wasm:
rustup target add wasm32-wasip1
cd crates/wasi-demo-app && cargo build $(RELEASE_FLAG)
target/wasm32-wasip1/$(OPT_PROFILE)/img.tar: target/wasm32-wasip1/$(OPT_PROFILE)/wasi-demo-app.wasm
cd crates/wasi-demo-app && cargo build $(RELEASE_FLAG) --features oci-v1-tar
.PHONY: dist/img.tar
dist/img.tar:
@mkdir -p "dist/"
[ -f $(PWD)/dist/img.tar ] || $(MAKE) target/wasm32-wasip1/$(OPT_PROFILE)/img.tar
[ -f $(PWD)/dist/img.tar ] || cp target/wasm32-wasip1/$(OPT_PROFILE)/img.tar "$@"
dist/img-oci.tar: target/wasm32-wasip1/$(OPT_PROFILE)/img-oci.tar
@mkdir -p "dist/"
cp "$<" "$@"
dist/img-oci-artifact.tar: target/wasm32-wasip1/$(OPT_PROFILE)/img-oci-artifact.tar
@mkdir -p "dist/"
cp "$<" "$@"
load: dist/img.tar
sudo ctr -n $(CONTAINERD_NAMESPACE) image import --all-platforms $<
CTR_VERSION := $(shell sudo ctr version | sed -n -e '/Version/ {s/.*: *//p;q;}')
load/oci: dist/img-oci.tar dist/img-oci-artifact.tar
@echo $(CTR_VERSION)\\nv1.7.7 | sort -crV || @echo $(CTR_VERSION)\\nv1.6.25 | sort -crV || (echo "containerd version must be 1.7.7+ or 1.6.25+ was $(CTR_VERSION)" && exit 1)
@echo using containerd $(CTR_VERSION)
sudo ctr -n $(CONTAINERD_NAMESPACE) image import --all-platforms $<
sudo ctr -n $(CONTAINERD_NAMESPACE) image import --all-platforms dist/img-oci-artifact.tar
.PHONY: load/http
load/http: dist/http-img-oci.tar
sudo ctr -n $(CONTAINERD_NAMESPACE) image import --all-platforms $<
target/wasm32-wasip1/$(OPT_PROFILE)/img-oci.tar: target/wasm32-wasip1/$(OPT_PROFILE)/wasi-demo-app.wasm
mkdir -p ${CURDIR}/bin/$(OPT_PROFILE)/
cargo run --bin oci-tar-builder -- --name wasi-demo-oci --repo ghcr.io/containerd/runwasi --tag latest --module ./target/wasm32-wasip1/$(OPT_PROFILE)/wasi-demo-app.wasm -o target/wasm32-wasip1/$(OPT_PROFILE)/img-oci.tar
.PHONY:
target/wasm32-wasip1/$(OPT_PROFILE)/img-oci-artifact.tar: target/wasm32-wasip1/$(OPT_PROFILE)/wasi-demo-app.wasm
mkdir -p ${CURDIR}/bin/$(OPT_PROFILE)/
cargo run --bin oci-tar-builder -- --name wasi-demo-oci-artifact --as-artifact --repo ghcr.io/containerd/runwasi --tag latest --module ./target/wasm32-wasip1/$(OPT_PROFILE)/wasi-demo-app.wasm -o target/wasm32-wasip1/$(OPT_PROFILE)/img-oci-artifact.tar
.PHONY:
dist/http-img-oci.tar: crates/containerd-shim-wasm-test-modules/src/modules/hello_wasi_http.wasm
@mkdir -p "dist/"
cargo run --bin oci-tar-builder -- \
--name wasi-http \
--repo ghcr.io/containerd/runwasi \
--tag latest \
--module $< \
-o $@
bin/kind: test/k8s/Dockerfile
$(DOCKER_BUILD) --output=bin/ -f test/k8s/Dockerfile --target=kind .
# Use a static build of the shims for better compatibility.
# Using cross with no target defaults to <arch>-unknown-linux-musl, which creates a static binary.
test/k8s/_out/img-%: override CARGO=cross TARGET= TARGET_DIR=
test/k8s/_out/img-%: test/k8s/Dockerfile dist-%
mkdir -p $(@D) && $(DOCKER_BUILD) -f test/k8s/Dockerfile --build-arg="RUNTIME=$*" --iidfile=$(@) --load .
test/k8s/_out/img-oci-%: test/k8s/Dockerfile.oci dist-%
mkdir -p $(@D) && $(DOCKER_BUILD) -f test/k8s/Dockerfile.oci --build-arg="RUNTIME=$*" --iidfile=$(@) --load .
.PHONY: test/nginx
test/nginx:
docker pull docker.io/nginx:latest
mkdir -p $@/out && docker save -o $@/out/img.tar docker.io/nginx:latest
.PHONY: test/k8s/cluster-%
test/k8s/cluster-%: dist/img.tar bin/kind test/k8s/_out/img-%
bin/kind create cluster --name $(KIND_CLUSTER_NAME) --image="$(shell cat test/k8s/_out/img-$*)" && \
bin/kind load image-archive --name $(KIND_CLUSTER_NAME) $(<)
.PHONY: test/k8s/deploy-workload-%
test/k8s/deploy-workload-%: test/k8s/clean test/k8s/cluster-%
kubectl --context=kind-$(KIND_CLUSTER_NAME) apply -f test/k8s/deploy.yaml
kubectl --context=kind-$(KIND_CLUSTER_NAME) wait deployment wasi-demo --for condition=Available=True --timeout=300s
# verify that we are still running after some time
sleep 5s
kubectl --context=kind-$(KIND_CLUSTER_NAME) wait deployment wasi-demo --for condition=Available=True --timeout=5s
.PHONY: test/k8s/deploy-workload-oci-%
test/k8s/deploy-workload-oci-%: test/k8s/clean test/k8s/cluster-% dist/img-oci.tar dist/img-oci-artifact.tar test/k8s/cluster-%
bin/kind load image-archive --name $(KIND_CLUSTER_NAME) dist/img-oci.tar
bin/kind load image-archive --name $(KIND_CLUSTER_NAME) dist/img-oci-artifact.tar
kubectl --context=kind-$(KIND_CLUSTER_NAME) apply -f test/k8s/deploy.oci.yaml
kubectl --context=kind-$(KIND_CLUSTER_NAME) wait deployment wasi-demo --for condition=Available=True --timeout=300s
# verify that we are still running after some time
sleep 5s
kubectl --context=kind-$(KIND_CLUSTER_NAME) wait deployment wasi-demo --for condition=Available=True --timeout=5s
@if [ "$*" = "wasmtime" ]; then \
set -e; \
echo "checking for pre-compiled labels and ensuring can scale after pre-compile"; \
docker exec $(KIND_CLUSTER_NAME)-control-plane ctr -n k8s.io content ls | grep "runwasi.io/precompiled"; \
kubectl --context=kind-$(KIND_CLUSTER_NAME) scale deployment wasi-demo --replicas=4; \
kubectl --context=kind-$(KIND_CLUSTER_NAME) wait deployment wasi-demo --for condition=Available=True --timeout=5s; \
fi
.PHONY: test/k8s-%
test/k8s-%: test/k8s/deploy-workload-%
# verify that we are able to delete the deployment
kubectl --context=kind-$(KIND_CLUSTER_NAME) delete -f test/k8s/deploy.yaml
kubectl --context=kind-$(KIND_CLUSTER_NAME) wait deployment wasi-demo --for delete --timeout=60s
.PHONY: test/k8s-oci-%
test/k8s-oci-%: test/k8s/deploy-workload-oci-%
# verify that we are able to delete the deployment
kubectl --context=kind-$(KIND_CLUSTER_NAME) delete -f test/k8s/deploy.oci.yaml
kubectl --context=kind-$(KIND_CLUSTER_NAME) wait deployment wasi-demo --for delete --timeout=60s
.PHONY: test/k8s/clean
test/k8s/clean: bin/kind
bin/kind delete cluster --name $(KIND_CLUSTER_NAME)
.PHONY: bin/k3s
bin/k3s:
mkdir -p bin && \
curl -sfL https://get.k3s.io | INSTALL_K3S_BIN_DIR=$(PWD)/bin INSTALL_K3S_SYMLINK=skip INSTALL_K3S_NAME=runwasi sh -
.PHONY: bin/k3s/clean
bin/k3s/clean:
bin/k3s-runwasi-uninstall.sh
.PHONY: test/k3s-%
test/k3s-%: dist/img.tar bin/k3s dist-%
sudo bash -c -- 'while ! timeout 40 test/k3s/bootstrap.sh "$*" dist/img.tar; do $(MAKE) bin/k3s/clean bin/k3s; done'
sudo bin/k3s kubectl get pods --all-namespaces
sudo bin/k3s kubectl apply -f test/k8s/deploy.yaml
sudo bin/k3s kubectl get pods --all-namespaces
sudo bin/k3s kubectl wait deployment wasi-demo --for condition=Available=True --timeout=300s
# verify that we are still running after some time
sleep 5s
sudo bin/k3s kubectl wait deployment wasi-demo --for condition=Available=True --timeout=5s
sudo bin/k3s kubectl get pods -o wide
sudo bin/k3s kubectl delete -f test/k8s/deploy.yaml
sudo bin/k3s kubectl wait deployment wasi-demo --for delete --timeout=60s
.PHONY: test/k3s-oci-%
test/k3s-oci-%: dist/img-oci.tar bin/k3s dist-%
sudo bash -c -- 'while ! timeout 40 test/k3s/bootstrap.sh "$*" dist/img-oci.tar; do $(MAKE) bin/k3s/clean bin/k3s; done'
sudo bin/k3s kubectl get pods --all-namespaces
sudo bin/k3s kubectl apply -f test/k8s/deploy.oci.yaml
sudo bin/k3s kubectl get pods --all-namespaces
sudo bin/k3s kubectl wait deployment wasi-demo --for condition=Available=True --timeout=300s
# verify that we are still running after some time
sleep 5s
sudo bin/k3s kubectl wait deployment wasi-demo --for condition=Available=True --timeout=5s
sudo bin/k3s kubectl get pods -o wide
@if [ "$*" = "wasmtime" ]; then \
set -e; \
echo "checking for pre-compiled labels and ensuring can scale"; \
sudo bin/k3s ctr -n k8s.io content ls | grep "runwasi.io/precompiled"; \
sudo bin/k3s kubectl scale deployment wasi-demo --replicas=4; \
sudo bin/k3s kubectl wait deployment wasi-demo --for condition=Available=True --timeout=5s; \
fi
sudo bin/k3s kubectl delete -f test/k8s/deploy.oci.yaml
sudo bin/k3s kubectl wait deployment wasi-demo --for delete --timeout=60s
.PHONY: test/k3s/clean
test/k3s/clean: bin/k3s/clean;
.PHONY: bench
bench:
$(CARGO) bench -p containerd-shim-benchmarks
.PHONY: clean
clean:
-rm -rf dist
-rm -rf bin
-rm -rf test/k8s/_out
-$(MAKE) test-image/clean
-$(MAKE) test/k8s/clean
-$(MAKE) test/k3s/clean