From ea675172a9beb8bddff2e4b9e9b51ab40cb568ba Mon Sep 17 00:00:00 2001 From: jiaxiao zhou Date: Tue, 25 Jul 2023 23:28:39 +0000 Subject: [PATCH] feat: simplify makefiles and CI this commit largely removes the redundency of writing each command once for each shim this repo supports. It uses foreach syntax to iterate over each item in the shims variable and execute a command for each one. This also applies to the CI. This commit adds a new docker-build-push action that builds and pushes the images to ghcr, abstrating the repeated build and push steps into this reusable workflow. It also adds spin-inbound-redis and spin-outbound-redis images to the release pipeline Signed-off-by: jiaxiao zhou --- .github/workflows/build.yaml | 12 +-- .github/workflows/ci.yaml | 8 +- .github/workflows/docker-build-push.yaml | 29 ++++++ .github/workflows/release.yaml | 43 ++------- Makefile | 115 +++++++---------------- deployments/k3d/Makefile | 37 +++----- 6 files changed, 94 insertions(+), 150 deletions(-) create mode 100644 .github/workflows/docker-build-push.yaml diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 30789538..25c6a268 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -24,9 +24,7 @@ jobs: - uses: Swatinem/rust-cache@v2 with: workspaces: | - "containerd-shim-slight-v1 -> target" - "containerd-shim-spin-v1 -> target" - "containerd-shim-wws-v1 -> target" + "containerd-shim-*-v1 -> target" - name: "Install Rust Wasm targets" run: | make install-rust-targets @@ -36,7 +34,7 @@ jobs: sudo apt-get install protobuf-compiler -y - name: build run: | - make build + VERBOSE=1 make build - name: lowercase the runner OS name shell: bash run: | @@ -45,9 +43,9 @@ jobs: - name: package release assets run: | mkdir _dist - cp containerd-shim-slight-v1/target/${{ matrix.config.arch }}-unknown-linux-musl/release/containerd-shim-*-v1 _dist/ - cp containerd-shim-spin-v1/target/${{ matrix.config.arch }}-unknown-linux-musl/release/containerd-shim-*-v1 _dist/ - cp containerd-shim-wws-v1/target/${{ matrix.config.arch }}-unknown-linux-musl/release/containerd-shim-*-v1 _dist/ + for shim in slight spin wws; do + cp containerd-shim-$shim-v1/target/${{ matrix.config.arch }}-unknown-linux-musl/release/containerd-shim-*-v1 _dist/ + done cd _dist tar czf containerd-wasm-shims-v1-${{ env.RUNNER_OS }}-${{ matrix.config.arch }}.tar.gz containerd-shim-*-v1 - name: upload shim artifacts diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 03e1a53f..ecdafc5e 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -12,9 +12,7 @@ jobs: - uses: Swatinem/rust-cache@v2 with: workspaces: | - "containerd-shim-slight-v1 -> target" - "containerd-shim-spin-v1 -> target" - "containerd-shim-wws-v1 -> target" + "containerd-shim-*-v1 -> target" - name: "Install Rust Wasm targets" run: | make install-rust-targets @@ -36,9 +34,7 @@ jobs: - uses: Swatinem/rust-cache@v2 with: workspaces: | - "containerd-shim-slight-v1 -> target" - "containerd-shim-spin-v1 -> target" - "containerd-shim-wws-v1 -> target" + "containerd-shim-*-v1 -> target" - name: "Install Rust Wasm targets" run: | make install-rust-targets diff --git a/.github/workflows/docker-build-push.yaml b/.github/workflows/docker-build-push.yaml new file mode 100644 index 00000000..17d332a0 --- /dev/null +++ b/.github/workflows/docker-build-push.yaml @@ -0,0 +1,29 @@ +name: 'Build and push Docker images' +on: + workflow_call: + inputs: + images: + required: true + type: string + platforms: + required: true + type: string + secrets: + GITHUB_TOKEN: + required: true +jobs: + build_and_push: + runs-on: ubuntu-latest + strategy: + matrix: + image: ${{fromJson(github.event.inputs.images)}} + steps: + - name: build and push + uses: docker/build-push-action@v3 + with: + push: true + tags: | + ghcr.io/deislabs/containerd-wasm-shims/${{ matrix.image.imageName }}:${{ env.RELEASE_VERSION }} + ghcr.io/deislabs/containerd-wasm-shims/${{ matrix.image.imageName }}:latest + context: ${{ matrix.image.context }} + platforms: ${{ github.event.inputs.platforms }} \ No newline at end of file diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 20b069b5..37872dd4 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -53,41 +53,16 @@ jobs: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - - name: build and push Spin hello world - uses: docker/build-push-action@v3 - with: - push: true - tags: | - ghcr.io/deislabs/containerd-wasm-shims/examples/spin-rust-hello:${{ env.RELEASE_VERSION }} - ghcr.io/deislabs/containerd-wasm-shims/examples/spin-rust-hello:latest - context: images/spin - platforms: wasi/wasm - - name: build and push Spin DotNet - uses: docker/build-push-action@v3 - with: - push: true - tags: | - ghcr.io/deislabs/containerd-wasm-shims/examples/spin-dotnet-hello:${{ env.RELEASE_VERSION }} - ghcr.io/deislabs/containerd-wasm-shims/examples/spin-dotnet-hello:latest - context: images/spin_dotnet - platforms: wasi/wasm - - name: build and push Slight hello world - uses: docker/build-push-action@v3 + - name: Build and push images + uses: ./.github/workflows/build_and_push.yaml with: - push: true - tags: | - ghcr.io/deislabs/containerd-wasm-shims/examples/slight-rust-hello:${{ env.RELEASE_VERSION }} - ghcr.io/deislabs/containerd-wasm-shims/examples/slight-rust-hello:latest - context: images/slight - platforms: wasi/wasm - - name: build and push Wasm Workers Server (wws) hello world (JS) - uses: docker/build-push-action@v3 - with: - push: true - tags: | - ghcr.io/deislabs/containerd-wasm-shims/examples/wws-js-hello:${{ env.RELEASE_VERSION }} - ghcr.io/deislabs/containerd-wasm-shims/examples/wws-js-hello:latest - context: images/wws + images: | + [{"imageName": "examples/spin-rust-hello", "context": "images/spin"}, + {"imageName": "examples/spin-dotnet-hello", "context": "images/spin_dotnet"}, + {"imageName": "examples/slight-rust-hello", "context": "images/slight"}, + {"imageName": "examples/wws-js-hello", "context": "images/wws"}, + {"imageName": "examples/spin-inbound-redis", "context": "images/spin-inbound-redis"}, + {"imageName": "examples/spin-outbound-redis", "context": "images/spin-outbound-redis"}] platforms: wasi/wasm - name: untar x86_64 musl artifacts into ./deployments/k3d/.tmp dir run: | diff --git a/Makefile b/Makefile index c69aa459..556d3343 100644 --- a/Makefile +++ b/Makefile @@ -1,21 +1,27 @@ +SHIMS := slight spin wws +BUILD_TARGETS = $(foreach shim,$(SHIMS),build-$(shim)-cross-$(TARGET)) + PREFIX ?= /usr/local INSTALL ?= install -TEST_IMG_NAME_SPIN ?= wasmtest_spin:latest -TEST_IMG_NAME_SLIGHT ?= wasmtest_slight:latest -TEST_IMG_NAME_WWS ?= wasmtest_wws:latest +TEST_IMG_NAME_spin ?= wasmtest_spin:latest +TEST_IMG_NAME_slight ?= wasmtest_slight:latest +TEST_IMG_NAME_wws ?= wasmtest_wws:latest ARCH ?= x86_64 TARGET ?= $(ARCH)-unknown-linux-musl PYTHON ?= python3 CONTAINERD_NAMESPACE ?= default +ifeq ($(VERBOSE),) +VERBOSE_FLAG := +else +VERBOSE_FLAG := -vvv +endif .PHONY: test test: unit-tests integration-tests .PHONY: unit-tests unit-tests: build - cross test --release --manifest-path=containerd-shim-slight-v1/Cargo.toml --target $(TARGET) - cross test --release --manifest-path=containerd-shim-spin-v1/Cargo.toml --target $(TARGET) - cross test --release --manifest-path=containerd-shim-wws-v1/Cargo.toml --target $(TARGET) + $(foreach shim,$(SHIMS),cross test --release --manifest-path=containerd-shim-$(shim)-v1/Cargo.toml --target $(TARGET);) .PHONY: integration-tests integration-tests: build @@ -25,15 +31,11 @@ integration-tests: build .PHONY: fmt fmt: - cargo fmt --all --manifest-path=containerd-shim-slight-v1/Cargo.toml -- --check - cargo fmt --all --manifest-path=containerd-shim-spin-v1/Cargo.toml -- --check - cargo fmt --all --manifest-path=containerd-shim-wws-v1/Cargo.toml -- --check - cargo clippy --all-targets --all-features --workspace --manifest-path=containerd-shim-slight-v1/Cargo.toml -- -D warnings - cargo clippy --all-targets --all-features --workspace --manifest-path=containerd-shim-spin-v1/Cargo.toml -- -D warnings - cargo clippy --all-targets --all-features --workspace --manifest-path=containerd-shim-wws-v1/Cargo.toml -- -D warnings + $(foreach shim,$(SHIMS),cargo fmt --all --manifest-path=containerd-shim-$(shim)-v1/Cargo.toml -- --check;) + $(foreach shim,$(SHIMS),cargo clippy --all-targets --all-features --workspace --manifest-path=containerd-shim-$(shim)-v1/Cargo.toml -- -D warnings;) .PHONY: build -build: build-spin-cross-$(TARGET) build-slight-cross-$(TARGET) build-wws-cross-$(TARGET) +build: $(foreach shim,$(SHIMS),build-$(shim)-cross-$(TARGET)) echo "Build complete" .PHONY: install-cross @@ -45,85 +47,40 @@ install-rust-targets: rustup target add wasm32-wasi && rustup target add wasm32-unknown-unknown # build-cross can be be used to build any cross supported target (make build-cross-x86_64-unknown-linux-musl) -.PHONY: build-spin-cross-% -build-spin-cross-%: install-cross - cross build --release --target $* --manifest-path=containerd-shim-spin-v1/Cargo.toml -vvv - -.PHONY: build-slight-cross-% -build-slight-cross-%: install-cross - cross build --release --target $* --manifest-path=containerd-shim-slight-v1/Cargo.toml -vvv - -.PHONY: build-wws-cross-% -build-wws-cross-%: install-cross - cross build --release --target $* --manifest-path=containerd-shim-wws-v1/Cargo.toml -vvv - -.PHONY: build-spin -build-spin: install-rust-targets - cargo build --release --manifest-path=containerd-shim-spin-v1/Cargo.toml +.PHONY: $(BUILD_TARGETS) +$(BUILD_TARGETS): SHIM = $(word 2,$(subst -, ,$@)) +$(BUILD_TARGETS): install-cross + cross build --release --target $(TARGET) --manifest-path=containerd-shim-$(SHIM)-v1/Cargo.toml $(VERBOSE_FLAG) -.PHONY: build-slight -build-slight: install-rust-targets - cargo build --release --manifest-path=containerd-shim-slight-v1/Cargo.toml - -.PHONY: build-wws -build-wss: - cargo build --release --manifest-path=containerd-shim-wws-v1/Cargo.toml +.PHONY: build-% +build-%: install-rust-targets + cargo build --release --manifest-path=containerd-shim-$*-v1/Cargo.toml .PHONY: install -install: build-spin build-slight build-wws +install: $(foreach shim,$(SHIMS),build-$(shim)) sudo $(INSTALL) containerd-shim-*/target/release/containerd-shim-*-v1 $(PREFIX)/bin .PHONY: update-deps update-deps: cargo update -test/out_spin/img.tar: images/spin/Dockerfile +test/out_%/img.tar: images/%/Dockerfile mkdir -p $(@D) - docker buildx build --platform=wasi/wasm --load -t $(TEST_IMG_NAME_SPIN) ./images/spin - docker save -o $@ $(TEST_IMG_NAME_SPIN) + docker buildx build --platform=wasi/wasm --load -t $(TEST_IMG_NAME_$*) ./images/$* + docker save -o $@ $(TEST_IMG_NAME_$*) -test/out_slight/img.tar: images/slight/Dockerfile - mkdir -p $(@D) - docker buildx build --platform=wasi/wasm --load -t $(TEST_IMG_NAME_SLIGHT) ./images/slight - docker save -o $@ $(TEST_IMG_NAME_SLIGHT) +load: $(foreach shim,$(SHIMS),test/out_$(shim)/img.tar) + $(foreach shim,$(SHIMS),sudo ctr -n $(CONTAINERD_NAMESPACE) image import test/out_$(shim)/img.tar;) -test/out_wws/img.tar: images/wws/Dockerfile - mkdir -p $(@D) - docker buildx build --platform=wasi/wasm --load -t $(TEST_IMG_NAME_WWS) ./images/wws - docker save -o $@ $(TEST_IMG_NAME_WWS) - -load: test/out_spin/img.tar test/out_slight/img.tar test/out_wws/img.tar - sudo ctr -n $(CONTAINERD_NAMESPACE) image import test/out_spin/img.tar - sudo ctr -n $(CONTAINERD_NAMESPACE) image import test/out_slight/img.tar - sudo ctr -n $(CONTAINERD_NAMESPACE) image import test/out_wws/img.tar - -.PHONY: run_spin -run_spin: install load - sudo ctr run --net-host --rm --runtime=io.containerd.spin.v1 docker.io/library/$(TEST_IMG_NAME_SPIN) testspin - -.PHONY: run_slight -run_slight: install load - sudo ctr run --net-host --rm --runtime=io.containerd.slight.v1 docker.io/library/$(TEST_IMG_NAME_SLIGHT) testslight - -.PHONY: run_wws -run_wws: install load - sudo ctr run --net-host --rm --runtime=io.containerd.wws.v1 docker.io/library/$(TEST_IMG_NAME_WWS) testwws +.PHONY: run_% +run_%: install load + sudo ctr run --net-host --rm --runtime=io.containerd.$*.v1 docker.io/library/$(TEST_IMG_NAME_$*) test$* .PHONY: clean -clean: clean-slight clean-spin clean-wws - test -f $(PREFIX)/bin/containerd-shim-spin-v1 && sudo rm -rf $(PREFIX)/bin/containerd-shim-spin-v1 || true - test -f $(PREFIX)/bin/containerd-shim-slight-v1 && sudo rm -rf $(PREFIX)/bin/containerd-shim-slight-v1 || true - test -f $(PREFIX)/bin/containerd-shim-wws-v1 && sudo rm -rf $(PREFIX)/bin/containerd-shim-wws-v1 || true +clean: $(addprefix clean-,$(SHIMS)) + $(foreach shim,$(SHIMS),test -f $(PREFIX)/bin/containerd-shim-$(shim)-v1 && sudo rm -rf $(PREFIX)/bin/containerd-shim-$(proj)-v1 || true;) test -d ./test && sudo rm -rf ./test || true -.PHONY: clean-spin -clean-spin: - cargo clean --manifest-path containerd-shim-spin-v1/Cargo.toml - -.PHONY: clean-slight -clean-slight: - cargo clean --manifest-path containerd-shim-slight-v1/Cargo.toml - -.PHONY: clean-wws -clean-wss: - cargo clean --manifest-path containerd-shim-wws-v1/Cargo.toml \ No newline at end of file +.PHONY: clean-% +clean-%: + cargo clean --manifest-path containerd-shim-$*-v1/Cargo.toml \ No newline at end of file diff --git a/deployments/k3d/Makefile b/deployments/k3d/Makefile index c77616fb..9c2308cc 100644 --- a/deployments/k3d/Makefile +++ b/deployments/k3d/Makefile @@ -1,26 +1,21 @@ +SHIMS = spin slight wws + IMAGE_NAME ?= k3swithshims CLUSTER_NAME ?= k3s-default PLATFORM ?= linux/amd64 ARCH ?= x86_64 TARGET ?= $(ARCH)-unknown-linux-musl -TEST_IMG_NAME_SPIN ?= wasmtest_spin:latest -TEST_IMG_NAME_SLIGHT ?= wasmtest_slight:latest -TEST_IMG_NAME_WWS ?= wasmtest_wws:latest - -compile-musl-spin: - make build-spin-cross-$(TARGET) -C ../.. +TEST_IMG_NAME_spin ?= wasmtest_spin:latest +TEST_IMG_NAME_slight ?= wasmtest_slight:latest +TEST_IMG_NAME_wws ?= wasmtest_wws:latest -compile-musl-slight: - make build-slight-cross-$(TARGET) -C ../.. +compile-musl-%: + make build-$*-cross-$(TARGET) -C ../.. -compile-musl-wws: - make build-wws-cross-$(TARGET) -C ../.. - -move-musl-to-tmp: compile-musl-spin compile-musl-slight compile-musl-wws +move-musl-to-tmp: $(addprefix compile-musl-,$(SHIMS)) mkdir -p ./.tmp - cp ../../containerd-shim-slight-v1/target/$(TARGET)/release/containerd-shim-*-v1 ./.tmp/ - cp ../../containerd-shim-spin-v1/target/$(TARGET)/release/containerd-shim-*-v1 ./.tmp/ - cp ../../containerd-shim-wws-v1/target/$(TARGET)/release/containerd-shim-*-v1 ./.tmp/ + $(foreach shim,$(SHIMS),cp ../../containerd-shim-$(shim)-v1/target/$(TARGET)/release/containerd-shim-*-v1 ./.tmp/;) + build-multi-k3d-image: move-musl-to-tmp docker buildx build -t $(IMAGE_NAME) --platform linux/amd64,linux/arm64 . @@ -32,23 +27,17 @@ create-k3d: build-dev-k3d-image k3d cluster create $(CLUSTER_NAME) --image $(IMAGE_NAME) --api-port 6550 -p "8081:80@loadbalancer" --agents 1 build-workload-images: - docker buildx build --platform=wasi/wasm --load -t $(TEST_IMG_NAME_SPIN) ../../images/spin - docker buildx build --platform=wasi/wasm --load -t $(TEST_IMG_NAME_SLIGHT) ../../images/slight - docker buildx build --platform=wasi/wasm --load -t $(TEST_IMG_NAME_WWS) ../../images/wws + $(foreach shim,$(SHIMS),docker buildx build --platform=wasi/wasm --load -t $(TEST_IMG_NAME_$(shim)) ../../images/$(shim);) load-workload-images: build-workload-images - k3d image load $(TEST_IMG_NAME_SPIN) - k3d image load $(TEST_IMG_NAME_SLIGHT) - k3d image load $(TEST_IMG_NAME_WWS) + $(foreach shim,$(SHIMS),k3d image load $(TEST_IMG_NAME_$(shim));) up: create-k3d load-workload-images kubectl label nodes k3d-k3s-default-agent-0 spin-enabled=true slight-enabled=true wws-enabled=true kubectl apply -f ./workload test: - curl localhost:8081/spin/hello - curl localhost:8081/slight/hello - curl localhost:8081/wws/hello + $(foreach shim,$(SHIMS),curl localhost:8081/$(shim)/hello;) integration: move-musl-to-tmp cd ../.. && cargo test -- --nocapture