diff --git a/Makefile b/Makefile index af4862a..a01ac38 100644 --- a/Makefile +++ b/Makefile @@ -73,7 +73,7 @@ tests/collect-debug-logs: install-k3s: ./scripts/install-k3s.sh build-and-push-images: - ./tests/k3s/build-and-push-images.sh + ./scripts/build-and-push-images.sh --spin .PHONY: test/k3s test/k3s: install-k3s build-and-push-images @@ -132,7 +132,7 @@ run-%: install load .PHONY: up move-bins deploy-workloads-pushed-using-docker-build-push deploy-workloads-pushed-using-spin-registry-push pod-terminates-test prepare-cluster-and-images up: - ./scripts/up.sh + ./scripts/k3d-up.sh move-bins: ./scripts/move-bins.sh $(BIN_DIR) diff --git a/scripts/build-and-push-images.sh b/scripts/build-and-push-images.sh new file mode 100755 index 0000000..81c4fc9 --- /dev/null +++ b/scripts/build-and-push-images.sh @@ -0,0 +1,69 @@ +#!/bin/bash + +set -euo pipefail + +RUN_SPIN=false +RUN_DOCKER=false + +cluster_name="test-cluster" +OUT_DIRS=("test/out_spin" "test/out_spin_keyvalue" "test/out_spin_outbound_redis" "test/out_spin_multi_trigger_app" "test/out_spin_static_assets" "test/out_spin_mqtt_message_logger") +IMAGES=("spin-hello-world" "spin-keyvalue" "spin-outbound-redis" "spin-multi-trigger-app" "spin-static-assets" "spin-mqtt-message-logger") + + +spin_build_and_push() { + local i=$1 + spin build -f "./images/${IMAGES[$i]}/spin.toml" + if [ "${IMAGES[$i]}" == "spin-static-assets" ]; then + export SPIN_OCI_ARCHIVE_LAYERS=1 + fi + spin registry push "localhost:5000/spin-registry-push/${IMAGES[$i]}:latest" -f "./images/${IMAGES[$i]}/spin.toml" -k +} + +docker_build_and_push() { + local image="$1" + local out_dir="$2" + + docker buildx build -t "${image}:latest" "./images/${image}" --load + mkdir -p "${out_dir}" + docker save -o "${out_dir}/img.tar" "${image}:latest" + k3d image import "${out_dir}/img.tar" -c "$cluster_name" +} + +while [[ "$#" -gt 0 ]]; do + case "$1" in + --spin) RUN_SPIN=true ;; + --docker) RUN_DOCKER=true ;; + --both) RUN_SPIN=true; RUN_DOCKER=true ;; + *) echo "Unknown option: $1"; exit 1 ;; + esac + shift +done + +if ! $RUN_SPIN && ! $RUN_DOCKER; then + echo "Error: At least one of --spin, --docker, or --both must be specified." + exit 1 +fi + + +if $RUN_SPIN; then + echo "Running Spin builds and pushes..." + if ! docker ps | grep -q test-registry; then + docker run -d -p 5000:5000 --name test-registry registry:2 + fi + for i in "${!IMAGES[@]}"; do + spin_build_and_push "$i" & + done +fi + +if $RUN_DOCKER; then + echo "Running Docker builds and pushes..." + for i in "${!IMAGES[@]}"; do + docker_build_and_push "${IMAGES[$i]}" "${OUT_DIRS[$i]}" & + done +fi + +# Wait for all background jobs to finish +wait + +sleep 5 +echo "Images are ready" \ No newline at end of file diff --git a/scripts/k3d-up.sh b/scripts/k3d-up.sh new file mode 100755 index 0000000..3997855 --- /dev/null +++ b/scripts/k3d-up.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +set -euo pipefail + +cluster_name="test-cluster" +dockerfile_path="deployments/k3d" + +docker build -t k3d-shim-test "$dockerfile_path" + +k3d cluster create "$cluster_name" \ + --image k3d-shim-test --api-port 6551 -p '8082:80@loadbalancer' --agents 2 \ + --registry-create test-registry:0.0.0.0:5000 \ + --k3s-arg '--kubelet-arg=eviction-hard=imagefs.available<1%,nodefs.available<1%@agent:*' \ + --k3s-arg '--kubelet-arg=eviction-minimum-reclaim=imagefs.available=1%,nodefs.available=1%@agent:*' + +kubectl wait --for=condition=ready node --all --timeout=120s + +echo "Running Spin and Docker builds and pushes..." +./scripts/spin-build-and-push-images.sh --both + +echo ">>> Cluster setup and image builds/pushes complete!" \ No newline at end of file diff --git a/scripts/up.sh b/scripts/up.sh deleted file mode 100755 index 3ffd047..0000000 --- a/scripts/up.sh +++ /dev/null @@ -1,43 +0,0 @@ -#!/bin/bash - -set -euo pipefail - -cluster_name="test-cluster" # name of the k3d cluster -dockerfile_path="deployments/k3d" # path to the Dockerfile - -DOCKER_IMAGES=("spin" "spin-keyvalue" "spin-outbound-redis" "spin-multi-trigger-app" "spin-static-assets" "spin-mqtt-message-logger") -OUT_DIRS=("test/out_spin" "test/out_spin_keyvalue" "test/out_spin_outbound_redis" "test/out_spin_multi_trigger_app" "test/out_spin_static_assets" "test/out_spin_mqtt_message_logger") -IMAGES=("spin-hello-world" "spin-keyvalue" "spin-outbound-redis" "spin-multi-trigger-app" "spin-static-assets" "spin-mqtt-message-logger") - -# build the Docker image for the k3d cluster -docker build -t k3d-shim-test "$dockerfile_path" - -k3d cluster create "$cluster_name" \ - --image k3d-shim-test --api-port 6551 -p '8082:80@loadbalancer' --agents 2 \ - --registry-create test-registry:0.0.0.0:5000 \ - --k3s-arg '--kubelet-arg=eviction-hard=imagefs.available<1%,nodefs.available<1%@agent:*' \ - --k3s-arg '--kubelet-arg=eviction-minimum-reclaim=imagefs.available=1%,nodefs.available=1%@agent:*' - -kubectl wait --for=condition=ready node --all --timeout=120s - -# Iterate through the Docker images and build them -for i in "${!DOCKER_IMAGES[@]}"; do - docker buildx build -t "${IMAGES[$i]}:latest" "./images/${DOCKER_IMAGES[$i]}" --load - mkdir -p "${OUT_DIRS[$i]}" - docker save -o "${OUT_DIRS[$i]}/img.tar" "${IMAGES[$i]}:latest" - k3d image import "${OUT_DIRS[$i]}/img.tar" -c "$cluster_name" - - ## also do spin builds and spin registry push - ## images pushed as localhost:5000//: - ## can be pulled as registry:5000//: from within k3d cluster - spin build -f "./images/${DOCKER_IMAGES[$i]}/spin.toml" - ## For the spin-static-assets app, use archive layers to test this functionality in the shim - if [ "${i}" == "spin-static-assets" ]; then - export SPIN_OCI_ARCHIVE_LAYERS=1 - fi - spin registry push "localhost:5000/spin-registry-push/${IMAGES[$i]}:latest" -f "./images/${DOCKER_IMAGES[$i]}/spin.toml" -k -done - -sleep 5 - -echo ">>> cluster is ready" diff --git a/tests/k3s/build-and-push-images.sh b/tests/k3s/build-and-push-images.sh deleted file mode 100644 index 1183a15..0000000 --- a/tests/k3s/build-and-push-images.sh +++ /dev/null @@ -1,31 +0,0 @@ -#!/bin/bash - -set -euo pipefail - -DOCKER_IMAGES=("spin" "spin-keyvalue" "spin-outbound-redis" "spin-multi-trigger-app" "spin-static-assets" "spin-mqtt-message-logger") -OUT_DIRS=("test/out_spin" "test/out_spin_keyvalue" "test/out_spin_outbound_redis" "test/out_spin_multi_trigger_app" "test/out_spin_static_assets" "test/out_spin_mqtt_message_logger") -IMAGES=("spin-hello-world" "spin-keyvalue" "spin-outbound-redis" "spin-multi-trigger-app" "spin-static-assets" "spin-mqtt-message-logger") - -# start a local registry at localhost:5000 -docker run -d -p 5000:5000 --name test-registry registry:2 - -build_and_push() { - local i=$1 - spin build -f "./images/${DOCKER_IMAGES[$i]}/spin.toml" - if [ "${DOCKER_IMAGES[$i]}" == "spin-static-assets" ]; then - export SPIN_OCI_ARCHIVE_LAYERS=1 - fi - spin registry push "localhost:5000/spin-registry-push/${IMAGES[$i]}:latest" -f "./images/${DOCKER_IMAGES[$i]}/spin.toml" -k -} - -# Iterate through the Docker images and build them in parallel -for i in "${!DOCKER_IMAGES[@]}"; do - build_and_push "$i" & -done - -# Wait for all background jobs to finish -wait - -sleep 5 - -echo ">>> images are ready" \ No newline at end of file