forked from containerd/runwasi
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Makefile
313 lines (255 loc) · 11.9 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
PREFIX ?= /usr/local
INSTALL ?= install
CARGO ?= cargo
LN ?= ln -sf
TEST_IMG_NAME ?= wasmtest:latest
RUNTIMES ?= wasmedge wasmtime wasmer
CONTAINERD_NAMESPACE ?= default
BUILD_SCRIPT_PATH = $(PWD)/demo/utils/build.rs
HYPER_CLIENT_PATH = demo/hyper/client
HYPER_SERVER_PATH = demo/hyper/server
REQWEST_PATH = demo/reqwest
DB_MYSQL_PATH = demo/db/mysql
DB_MYSQL_ASYNC_PATH = demo/db/mysql_async
MICROSERVICE_DB_PATH = demo/microservice_db
WASINN_PATH = demo/wasinn/pytorch-mobilenet-image/rust
PREOPENS_PATH = demo/rootfs-mounts
LLAMA2_PATH = demo/llama2/simple
LLAMA2_CHAT_PATH = demo/llama2/chat
# We have a bit of fancy logic here to determine the target
# since we support building for gnu and musl
# TARGET must evenutually 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 becuase 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
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 --no-default-features --features generate_bindings $(RELEASE_FLAG)
$(CARGO) build $(TARGET_FLAG) -p containerd-shim-wasm $(FEATURES_wasm) $(RELEASE_FLAG)
build-%:
$(CARGO) build $(TARGET_FLAG) -p containerd-shim-$* $(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 paralellism
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 paralellism
RUST_LOG=trace $(CARGO) test $(TARGET_FLAG) --package containerd-shim-$* $(FEATURES_$*) --lib --verbose $(TEST_ARGS_SEP) --nocapture --test-threads=1
.PHONY: install install-%
install: $(RUNTIMES:%=install-%);
install-%: build-%
mkdir -p $(PREFIX)/bin
$(INSTALL) $(TARGET_DIR)/$(TARGET)/$(OPT_PROFILE)/containerd-shim-$*-v1 $(PREFIX)/bin/
$(LN) ./containerd-shim-$*-v1 $(PREFIX)/bin/containerd-shim-$*d-v1
$(LN) ./containerd-shim-$*-v1 $(PREFIX)/bin/containerd-$*d
.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 build install test-image load
.PHONY: install/oci/all
install/oci/all: test-image/oci/clean build 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
.PHONY: test-image/clean
test-image/clean:
rm -rf target/wasm32-wasi/$(OPT_PROFILE)/
.PHONY: test-image/oci/clean
test-image/oci/clean:
rm -rf target/wasm32-wasi/$(OPT_PROFILE)/img-oci.tar
.PHONY: demo-app
demo-app: target/wasm32-wasi/$(OPT_PROFILE)/wasi-demo-app.wasm
.PHONY: target/wasm32-wasi/$(OPT_PROFILE)/wasi-demo-app.wasm
target/wasm32-wasi/$(OPT_PROFILE)/wasi-demo-app.wasm:
rustup target add wasm32-wasi
cd crates/wasi-demo-app && cargo build $(RELEASE_FLAG)
target/wasm32-wasi/$(OPT_PROFILE)/img.tar: target/wasm32-wasi/$(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-wasi/$(OPT_PROFILE)/img.tar
[ -f $(PWD)/dist/img.tar ] || cp target/wasm32-wasi/$(OPT_PROFILE)/img.tar "$@"
dist/img-oci.tar: target/wasm32-wasi/$(OPT_PROFILE)/img-oci.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
@echo $(CTR_VERSION)\\nv1.7.7 | sort -crV || (echo "containerd version must be 1.7.7+ was $(CTR_VERSION)" && exit 1)
@echo using containerd $(CTR_VERSION)
sudo ctr -n $(CONTAINERD_NAMESPACE) image import --all-platforms $<
.PHONY:
target/wasm32-wasi/$(OPT_PROFILE)/img-oci.tar: target/wasm32-wasi/$(OPT_PROFILE)/wasi-demo-app.wasm
mkdir -p ${CURDIR}/bin/$(OPT_PROFILE)/
cargo run --bin oci-tar-builder -- --name wasi-demo-app --repo ghcr.io/second-state/runwasi-demo --tag wasi-demo-app --module ./target/wasm32-wasi/$(OPT_PROFILE)/wasi-demo-app.wasm -o target/wasm32-wasi/$(OPT_PROFILE)/img-oci.tar
define build_img
@if ! test -f $1/build.rs; then \
echo "Setup build environment for" $1; \
cd $1; \
cp $(BUILD_SCRIPT_PATH) .; \
cargo add --build [email protected] sha256@=1.4.0 [email protected] [email protected] [email protected] [email protected]; \
cargo add --build oci-tar-builder --git https://github.com/containerd/runwasi; \
fi
cd $1 && cargo build --target=wasm32-wasi $(RELEASE_FLAG)
cd $1 && BUILD_IMAGE=TRUE cargo build --target=wasm32-wasi $(RELEASE_FLAG)
endef
.PHONY: demo/%
demo/%:
$(call build_img, $(patsubst %/target/wasm32-wasi/$(OPT_PROFILE)/img.tar,demo/%,$*))
load_demo: $(HYPER_CLIENT_PATH)/target/wasm32-wasi/$(OPT_PROFILE)/img.tar \
$(HYPER_SERVER_PATH)/target/wasm32-wasi/$(OPT_PROFILE)/img.tar \
$(REQWEST_PATH)/target/wasm32-wasi/$(OPT_PROFILE)/img.tar \
$(DB_MYSQL_PATH)/target/wasm32-wasi/$(OPT_PROFILE)/img.tar \
$(DB_MYSQL_ASYNC_PATH)/target/wasm32-wasi/$(OPT_PROFILE)/img.tar \
$(MICROSERVICE_DB_PATH)/target/wasm32-wasi/$(OPT_PROFILE)/img.tar \
$(WASINN_PATH)/target/wasm32-wasi/$(OPT_PROFILE)/img.tar \
$(PREOPENS_PATH)/target/wasm32-wasi/$(OPT_PROFILE)/img.tar \
$(LLAMA2_PATH)/target/wasm32-wasi/$(OPT_PROFILE)/img.tar \
$(LLAMA2_CHAT_PATH)/target/wasm32-wasi/$(OPT_PROFILE)/img.tar
$(foreach var,$^,\
sudo ctr -n $(CONTAINERD_NAMESPACE) image import --all-platforms $(var);\
)
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/cluster-oci-%
test/k8s/cluster-oci-%: dist/img-oci.tar bin/kind test/k8s/_out/img-oci-%
bin/kind create cluster --name $(KIND_CLUSTER_NAME) --image="$(shell cat test/k8s/_out/img-oci-$*)" && \
bin/kind load image-archive --name $(KIND_CLUSTER_NAME) $(<)
.PHONY: test/k8s-%
test/k8s-%: 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=90s
# 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/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 "$*"; 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=120s
# 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
.PHONY: test/k3s/clean
test/k3s/clean: bin/k3s/clean;
.PHONY: clean
clean:
-rm -rf dist
-rm -rf bin
-$(MAKE) test-image/clean
-$(MAKE) test/k8s/clean
-$(MAKE) test/k3s/clean