NoN-CTF/k8s/build-images.sh
2026-07-12 20:32:52 +01:00

49 lines
1.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# Build all web challenge images and make them available to k3s.
#
# k3s uses containerd, not the Docker daemon, so a locally-built Docker image
# is NOT visible to k3s until it is imported into containerd (or pushed to a
# registry). This script builds each image and imports it directly.
#
# Run this ON the k3s node (needs docker + the k3s binary).
#
# For a MULTI-NODE cluster, import-per-node does not scale: set REGISTRY to a
# registry reachable by every node, e.g.
# REGISTRY=registry.entysec.com/ctf ./build-images.sh
# then change imagePullPolicy to IfNotPresent (already set) and prefix the
# image names in the manifests with $REGISTRY/.
set -euo pipefail
# Repo root = parent of this script's directory.
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
REGISTRY="${REGISTRY:-}" # empty => build + import into local k3s containerd
# image-tag -> build context (relative to repo root)
declare -a CHALLENGES=(
"ctf-second-track:latest|Web/Easy/Second Track"
"ctf-second-track-aftermath:latest|Web/Medium/Second Track Aftermath"
"ctf-second-track-reborn:latest|Web/Hard/Second Track Reborn"
"ctf-big-software-foundation:latest|Web/Easy/Big Software Foundation"
"ctf-elfs-blog:latest|Web/Hard/Elfs' Blog"
)
for entry in "${CHALLENGES[@]}"; do
tag="${entry%%|*}"
ctx="${entry#*|}"
image="${REGISTRY:+$REGISTRY/}$tag"
echo "==> Building $image (context: $ctx)"
docker build -t "$image" "$ROOT/$ctx"
if [ -n "$REGISTRY" ]; then
echo "==> Pushing $image"
docker push "$image"
else
echo "==> Importing $image into k3s containerd"
# Pipe the Docker image straight into k3s' containerd image store.
docker save "$image" | sudo k3s ctr images import -
fi
done
echo "==> Done. Images ready for k3s."