From 620a9301433217e95aec790d1af2a58476a10e69 Mon Sep 17 00:00:00 2001 From: riad Date: Thu, 12 Nov 2026 15:07:15 +0100 Subject: [PATCH 1/9] fix(rpm): skip the archives whose images containerd already has The timer re-imported every archive every 10 minutes, reading and writing about twice the cache size even when containerd had it all. The script now compares each archive's OCI index.json (names and digests) with what `ctr images check` reports complete and unpacked, and skips the archives that match. Anything it cannot compare is imported as before. The package now requires jq and tar. The timer gets RandomizedDelaySec=1min so nodes that boot together drift apart. Refs: MK8S-475 --- DESIGN.md | 11 +- README.md | 4 +- rpm/README.md | 20 +- rpm/build.Dockerfile | 5 +- rpm/containerd-image-preload.spec | 2 + rpm/sources/containerd-image-preload.sh | 65 ++++++- rpm/sources/containerd-image-preload.timer | 2 + rpm/test/package.bats | 9 + rpm/test/script.bats | 203 ++++++++++++++++++++- 9 files changed, 288 insertions(+), 33 deletions(-) diff --git a/DESIGN.md b/DESIGN.md index 77cbcb8..4c03990 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -24,11 +24,12 @@ Restoring the cache and filling it are separate problems with separate lifetimes, so they are separate components: - **`containerd-image-preload`** (an RPM) answers *restore*. A systemd timer - runs `ctr images import` over every tarball of the cache directory, on boot - and periodically. It is deliberately dumb: a shell script, no daemon, no - Kubernetes client, no registry access. It works on a node whose cluster is - down, and it is idempotent: re-importing what containerd already has - replaces nothing and breaks nothing. + runs `ctr images import` over every tarball of the cache directory whose + images containerd is missing, on boot and periodically. It is deliberately + dumb: a shell script, no daemon, no Kubernetes client, no registry access. + It works on a node whose cluster is down. Archives whose `index.json` names + and digests all show as complete in `ctr images check` are skipped, since + re-importing them costs about twice their size in I/O. - **`image-cache-agent`** (a Kubernetes DaemonSet) answers *fill*. It watches `ImageCache` custom resources, pulls the images they point at, extracts the diff --git a/README.md b/README.md index 1ba332d..95f83db 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ The project is three pieces that meet on a directory: | Component | What it does | | --- | --- | -| [`containerd-image-preload`](rpm/) (RPM) | A systemd service and timer that import every tarball of the cache directory into containerd. Runs on boot and every 10 minutes. Needs no Kubernetes, no registry, no network. | +| [`containerd-image-preload`](rpm/) (RPM) | A systemd service and timer that import into containerd the tarballs of the cache directory whose images containerd is missing. Runs on boot and every 10 minutes. Needs no Kubernetes, no registry, no network. | | [`image-cache-agent`](agent/) (DaemonSet) | A Kubernetes agent that fills the cache: it pulls the images declared by `ImageCache` custom resources, extracts the tarballs they carry into the cache directory, garbage-collects what is no longer declared, and reports per-node progress as node labels. | | [`imagecachectl`](agent/cmd/imagecachectl/) (command, RPM) | The same fill, once, for a node that has no Kubernetes yet. Reads a registry or a docker archive and writes the tarballs where the agent would have. | @@ -45,7 +45,7 @@ cache up to date on a cluster that imports it some other way. │ reads *.tar ▼ ┌──────────────────────────────────────┐ - │ containerd-image-preload (systemd) │ ctr images import, every 10 min + │ containerd-image-preload (systemd) │ ctr images import of what is missing, every 10 min └──────────────────┬───────────────────┘ ▼ containerd diff --git a/rpm/README.md b/rpm/README.md index ecc0849..c32c915 100644 --- a/rpm/README.md +++ b/rpm/README.md @@ -30,7 +30,8 @@ systemctl enable --now containerd-image-preload.timer Installing does not enable anything: the package ships the units and stops there, so the provisioning layer decides when they start. The timer fires once at boot (`OnBootSec=0`) and every ten minutes after each run -(`OnUnitActiveSec=10min`). To import right away: +(`OnUnitActiveSec=10min`), up to a minute late (`RandomizedDelaySec=1min`) so +nodes that boot together drift apart. To import right away: ```console systemctl start containerd-image-preload.service @@ -69,9 +70,14 @@ below it: Nothing deeper, and nothing under a hidden directory. Each file goes in with `ctr -n k8s.io images import --platform "$IMAGE_PLATFORM"`, into the `k8s.io` -namespace, which is where the kubelet looks. Re-importing an image containerd -already has replaces nothing and breaks nothing, which is what makes running -the timer on a short period harmless. +namespace, which is where the kubelet looks. + +An archive is skipped when every image in its OCI `index.json` +(`io.containerd.image.name` and digest) is reported complete and unpacked by +`ctr -n k8s.io images check`, which saves re-reading and re-writing it every +run. Otherwise it is imported, including when the archive has no OCI index, +an unnamed image or an index over 1 MiB, and when `ctr images check` fails. +This needs `jq` and GNU `tar`, which the package requires. A tarball that fails to import does not stop the ones after it. The run tries every archive, names each one it could not import, and then exits non-zero, so @@ -92,9 +98,9 @@ exits zero: deciding that an empty cache is wrong is the caller's call, not this script's. A pipeline that reads "wrote to stderr" as "failed" will see that line, so read the exit status. -Every run ends with one line that says what it did. Under systemd, each line -carries its syslog priority: failures are `err`, a cache with nothing left is -`warning`, the rest is `info`. So `journalctl -u containerd-image-preload -p err` +Every run ends with one line that says what it did. Under systemd, each line carries its syslog priority: failures are +`err`, a cache with nothing left and a containerd that could not list its +images are `warning`, the rest is `info`. So `journalctl -u containerd-image-preload -p err` shows only the failed imports. The journal adds the timestamps. Run by hand, the script prints plain lines with no priority prefix. Lines that `ctr` writes itself stay at `info`. diff --git a/rpm/build.Dockerfile b/rpm/build.Dockerfile index c42b5db..1f9773e 100644 --- a/rpm/build.Dockerfile +++ b/rpm/build.Dockerfile @@ -1,12 +1,13 @@ # Build image for the containerd-image-preload RPM. # Provides the RHEL-family RPM build toolchain (rpm-build + rpmlint) plus -# shellcheck and systemd to validate the shipped script and unit files. +# shellcheck and systemd to validate the shipped script and unit files, and +# jq and tar for the script's tests. # The base image (Rocky Linux 8 or 9) is provided by the caller; see the Makefile. ARG BUILD_IMAGE FROM ${BUILD_IMAGE} RUN dnf install -y --setopt=skip_missing_names_on_install=False \ - rpm-build rpmlint rpmdevtools systemd && \ + rpm-build rpmlint rpmdevtools systemd jq tar && \ dnf install -y epel-release && \ dnf install -y ShellCheck bats && \ dnf clean all diff --git a/rpm/containerd-image-preload.spec b/rpm/containerd-image-preload.spec index 535971b..cb0d392 100644 --- a/rpm/containerd-image-preload.spec +++ b/rpm/containerd-image-preload.spec @@ -12,6 +12,8 @@ Source3: containerd-image-preload.sysconfig BuildArch: noarch Requires: containerd +Requires: jq +Requires: tar %description A systemd service and timer that periodically import container image tarballs diff --git a/rpm/sources/containerd-image-preload.sh b/rpm/sources/containerd-image-preload.sh index e9c17fe..7794389 100644 --- a/rpm/sources/containerd-image-preload.sh +++ b/rpm/sources/containerd-image-preload.sh @@ -30,15 +30,58 @@ info() { log 6 1 "$@"; } warn() { log 4 2 "$@"; } error() { log 3 2 "$@"; } -scanned=0 +# Larger index.json files are not trusted; the archive is imported. +INDEX_MAX_BYTES=1048576 + +# "name digest" of each image containerd holds complete and unpacked. +# STATUS and SIZE contain spaces, so UNPACKED is read as the last field. +ready_images() { + ctr -n k8s.io images check | + awk 'NR > 1 && $4 == "complete" && $NF == "true" { print $1, $3 }' +} + +# "name digest" of each image in the archive's index.json. Fails when there +# is no index, an image is unnamed, or the index is too large. +archive_images() { + tar --seek --occurrence=1 -xOf "$1" index.json 2> /dev/null | + head -c "${INDEX_MAX_BYTES}" | + jq -r ' + .manifests + | if length > 0 + and all(.[]; (.annotations["io.containerd.image.name"] // "") != "") + then .[] | "\(.annotations["io.containerd.image.name"]) \(.digest)" + else error("not every image is named") end + ' 2> /dev/null +} + +# True when containerd already holds every image of the archive. +already_imported() { + local images line + [ -n "${ready}" ] || return 1 + images="$(archive_images "$1")" || return 1 + [ -n "${images}" ] || return 1 + while IFS= read -r line; do + grep -qxF -- "${line}" <<< "${ready}" || return 1 + done <<< "${images}" +} + +archives=("${IMAGE_CACHE_DIR}"/*.tar "${IMAGE_CACHE_DIR}"/*/*.tar) +scanned=${#archives[@]} skipped=0 +current=0 failures=0 +# If containerd cannot list its images, every archive is imported. +ready="" +if [ "${scanned}" -ne 0 ] && ! ready="$(ready_images)"; then + warn "Could not list the images containerd has, importing every archive" + ready="" +fi + # Tars live flat (provisioning) or in per-resource subdirectories (agent). # An archive containerd cannot read must not keep the others out, so every # archive is tried and each failure is named as it happens. -for tar in "${IMAGE_CACHE_DIR}"/*.tar "${IMAGE_CACHE_DIR}"/*/*.tar; do - scanned=$((scanned + 1)) +for tar in "${archives[@]}"; do # The glob expanded before the first import, and the agent takes a # resource's directory away to collect it and again to swap a fresh # extraction in. An archive that is already gone when its turn comes is @@ -49,6 +92,11 @@ for tar in "${IMAGE_CACHE_DIR}"/*.tar "${IMAGE_CACHE_DIR}"/*/*.tar; do skipped=$((skipped + 1)) continue fi + if already_imported "${tar}"; then + info "Skipping ${tar}, already in containerd" + current=$((current + 1)) + continue + fi info "Importing ${tar}" if ! ctr -n k8s.io images import --platform "${IMAGE_PLATFORM}" "${tar}"; then error "Failed to import ${tar}" @@ -56,25 +104,26 @@ for tar in "${IMAGE_CACHE_DIR}"/*.tar "${IMAGE_CACHE_DIR}"/*/*.tar; do fi done -imported=$((scanned - skipped - failures)) +imported=$((scanned - skipped - current - failures)) if [ "${failures}" -ne 0 ]; then # Say what became of all of them: one torn archive out of forty reads # nothing like a containerd that is down, and neither reads like a cache # the agent emptied while the run was walking it. error "Failed to import ${failures} of the ${scanned} cached archives" \ - "(${imported} imported, ${skipped} no longer there)" + "(${imported} imported, ${current} already in containerd," \ + "${skipped} no longer there)" exit 1 fi if [ "${scanned}" -eq 0 ]; then info "No cached archives to import in ${IMAGE_CACHE_DIR}" -elif [ "${imported}" -eq 0 ]; then - # Nothing failed, yet nothing made it in either. A whole cache going away +elif [ "${skipped}" -eq "${scanned}" ]; then + # Nothing failed, yet nothing was there either. A whole cache going away # under the run is legitimate (every resource collected at once) and is not # this script's call to make, but it must not pass for a quiet success. warn "None of the ${scanned} cached archives were still there to import" else info "Imported ${imported} of the ${scanned} cached archives" \ - "(${skipped} no longer there)" + "(${current} already in containerd, ${skipped} no longer there)" fi diff --git a/rpm/sources/containerd-image-preload.timer b/rpm/sources/containerd-image-preload.timer index 76a4f8c..ac2c1db 100644 --- a/rpm/sources/containerd-image-preload.timer +++ b/rpm/sources/containerd-image-preload.timer @@ -4,6 +4,8 @@ Description=Periodically pre-load container images from the local cache into con [Timer] OnBootSec=0 OnUnitActiveSec=10min +# Spread nodes that boot together. Also delays the boot run (systemd 239). +RandomizedDelaySec=1min [Install] WantedBy=timers.target diff --git a/rpm/test/package.bats b/rpm/test/package.bats index 5de9edd..556c483 100644 --- a/rpm/test/package.bats +++ b/rpm/test/package.bats @@ -35,6 +35,15 @@ assert_linter_clean() { rpm -qpR "$RPM" | grep -qx "containerd" } +@test "package requires jq and tar, which the script reads archive indexes with" { + rpm -qpR "$RPM" | grep -qx "jq" + rpm -qpR "$RPM" | grep -qx "tar" +} + +@test "timer spreads the runs of nodes that boot together" { + grep -qx "RandomizedDelaySec=1min" "/usr/lib/systemd/system/$NAME.timer" +} + @test "package ships the expected files" { run rpm -qlp "$RPM" [ "$status" -eq 0 ] diff --git a/rpm/test/script.bats b/rpm/test/script.bats index 425141e..f83d09d 100644 --- a/rpm/test/script.bats +++ b/rpm/test/script.bats @@ -19,16 +19,32 @@ setup() { CTR_LOG="$TMP/ctr.log" CTR_FAIL="$TMP/ctr.fail" CTR_RM="$TMP/ctr.rm" + CTR_IMAGES="$TMP/ctr.images" + CTR_CHECK_LOG="$TMP/ctr.check.log" + CTR_CHECK_FAIL="$TMP/ctr.check.fail" mkdir -p "$CACHE" "$BIN" : > "$CTR_LOG" : > "$CTR_FAIL" : > "$CTR_RM" - # The stub logs every call, and exits non-zero on the archives listed in + : > "$CTR_IMAGES" + : > "$CTR_CHECK_LOG" + # The stub logs every import, and exits non-zero on the archives listed in # $CTR_FAIL, the way containerd reports one it cannot read. The archive is # the last argument, and is matched by its path inside the cache: the same - # file name legitimately lives flat and in a subdirectory. + # file name legitimately lives flat and in a subdirectory. `images check` + # prints $CTR_IMAGES and logs to $CTR_CHECK_LOG. cat > "$BIN/ctr" <> "$CTR_CHECK_LOG" + if [ -e "$CTR_CHECK_FAIL" ]; then + echo "ctr: connection refused" >&2 + exit 1 + fi + printf 'REF\tTYPE\tDIGEST\tSTATUS\tSIZE\tUNPACKED\n' + cat "$CTR_IMAGES" + exit 0 +fi echo "\$*" >> "$CTR_LOG" # Stand in for an agent garbage collecting a resource mid-run, so the archives # the loop has not reached yet are gone when their turn comes. @@ -63,6 +79,44 @@ vanish_on_first_import() { printf '%s\n' "$@" > "$CTR_RM" } +# A digest made of one repeated hex digit, enough to tell images apart. +digest() { + printf 'sha256:%064d' 0 | tr 0 "$1" +} + +# Write an OCI archive at $1 holding the given "name digest" pairs. +make_archive() { + local archive="$1" dir manifests="" sep="" + shift + dir="$(mktemp -d "$TMP/archive.XXXXXX")" + while [ "$#" -ge 2 ]; do + manifests+="${sep}{\"mediaType\":\"application/vnd.oci.image.index.v1+json\",\"digest\":\"$2\",\"size\":1,\"annotations\":{\"io.containerd.image.name\":\"$1\",\"org.opencontainers.image.ref.name\":\"1\"}}" + sep="," + shift 2 + done + write_index "$dir" "{\"schemaVersion\":2,\"manifests\":[${manifests}]}" + tar_archive "$archive" "$dir" +} + +# Lay out an OCI directory around an index.json given verbatim. +write_index() { + mkdir -p "$1/blobs/sha256" + head -c 1048576 /dev/zero > "$1/blobs/sha256/$(digest a | cut -d: -f2)" + printf '%s' '{"imageLayoutVersion":"1.0.0"}' > "$1/oci-layout" + printf '%s' "$2" > "$1/index.json" +} + +tar_archive() { + mkdir -p "$(dirname "$1")" + tar -cf "$1" -C "$2" blobs oci-layout index.json +} + +# Add an image to the `ctr images check` output (healthy by default). +containerd_has() { + printf '%s\tapplication/vnd.oci.image.index.v1+json\t%s\t%s\t%s\t%s\n' \ + "$1" "$2" "${3:-complete (3/3)}" "2.1 MiB/2.1 MiB" "${4:-true}" >> "$CTR_IMAGES" +} + # Run the script against the stubbed `ctr`, keeping stdout and stderr apart so # a test can tell what the unit reports from what it merely logs. Extra # `NAME=value` arguments override the environment, the last one winning. @@ -158,7 +212,7 @@ run_preload_on_journal() { [ "$status" -eq 1 ] [[ "$stderr" == *"Failed to import $CACHE/flat-bad.tar"* ]] [[ "$stderr" == *"Failed to import $CACHE/worker-134-0-0/nested-bad.tar"* ]] - [[ "$stderr" == *"Failed to import 2 of the 4 cached archives (2 imported, 0 no longer there)"* ]] + [[ "$stderr" == *"Failed to import 2 of the 4 cached archives (2 imported, 0 already in containerd, 0 no longer there)"* ]] grep -q "flat-good.tar" "$CTR_LOG" grep -q "worker-134-0-0/nested-good.tar" "$CTR_LOG" } @@ -169,7 +223,7 @@ run_preload_on_journal() { : > "$CACHE/b.tar" run_preload [ "$status" -eq 1 ] - [[ "$stderr" == *"Failed to import 2 of the 2 cached archives (0 imported, 0 no longer there)"* ]] + [[ "$stderr" == *"Failed to import 2 of the 2 cached archives (0 imported, 0 already in containerd, 0 no longer there)"* ]] } @test "a cache with nothing left to import says so instead of passing quietly" { @@ -193,7 +247,7 @@ run_preload_on_journal() { fail_on bad.tar run_preload [ "$status" -eq 1 ] - [[ "$stderr" == *"Failed to import 1 of the 3 cached archives (1 imported, 1 no longer there)"* ]] + [[ "$stderr" == *"Failed to import 1 of the 3 cached archives (1 imported, 0 already in containerd, 1 no longer there)"* ]] } @test "an archive that fails and then disappears is still a failure" { @@ -207,7 +261,7 @@ run_preload_on_journal() { run_preload [ "$status" -eq 1 ] [[ "$stderr" == *"Failed to import $CACHE/bad.tar"* ]] - [[ "$stderr" == *"Failed to import 1 of the 2 cached archives (1 imported, 0 no longer there)"* ]] + [[ "$stderr" == *"Failed to import 1 of the 2 cached archives (1 imported, 0 already in containerd, 0 no longer there)"* ]] } @test "a run that fails and skips does not also claim it found nothing" { @@ -219,7 +273,7 @@ run_preload_on_journal() { fail_on bad-one.tar bad-two.tar run_preload [ "$status" -eq 1 ] - [[ "$stderr" == *"Failed to import 2 of the 3 cached archives (0 imported, 1 no longer there)"* ]] + [[ "$stderr" == *"Failed to import 2 of the 3 cached archives (0 imported, 0 already in containerd, 1 no longer there)"* ]] [[ "$stderr" != *"None of the"* ]] } @@ -242,7 +296,7 @@ run_preload_on_journal() { : > "$CACHE/b.tar" run_preload [ "$status" -eq 0 ] - [[ "$output" == *"Imported 2 of the 2 cached archives (0 no longer there)"* ]] + [[ "$output" == *"Imported 2 of the 2 cached archives (0 already in containerd, 0 no longer there)"* ]] } @test "an empty cache says there was nothing to import" { @@ -259,7 +313,7 @@ run_preload_on_journal() { [ "$status" -eq 1 ] grep -qxF "<6>Importing $CACHE/good.tar" "$TMP/journal" grep -qxF "<3>Failed to import $CACHE/bad.tar" "$TMP/journal" - grep -qxF "<3>Failed to import 1 of the 2 cached archives (1 imported, 0 no longer there)" "$TMP/journal" + grep -qxF "<3>Failed to import 1 of the 2 cached archives (1 imported, 0 already in containerd, 0 no longer there)" "$TMP/journal" } @test "on the journal, a cache with nothing left is a warning" { @@ -285,4 +339,135 @@ run_preload_on_journal() { [ "$status" -eq 0 ] # Neither is reachable, so `ctr` is never called at all. [ ! -s "$CTR_LOG" ] + [ ! -s "$CTR_CHECK_LOG" ] +} + +@test "an archive containerd already holds is not imported again" { + make_archive "$CACHE/worker-134-0-0/etcd.tar" registry.example/etcd:3.5 "$(digest 1)" + containerd_has registry.example/etcd:3.5 "$(digest 1)" + run_preload + [ "$status" -eq 0 ] + [ -z "$stderr" ] + [ ! -s "$CTR_LOG" ] + [[ "$output" == *"Skipping $CACHE/worker-134-0-0/etcd.tar, already in containerd"* ]] + [[ "$output" == *"Imported 0 of the 1 cached archives (1 already in containerd, 0 no longer there)"* ]] +} + +@test "containerd is asked once per run, in the kubelet's namespace" { + make_archive "$CACHE/a.tar" registry.example/a:1 "$(digest 1)" + make_archive "$CACHE/b.tar" registry.example/b:1 "$(digest 2)" + run_preload + [ "$status" -eq 0 ] + [ "$(grep -c . "$CTR_CHECK_LOG")" -eq 1 ] + grep -qx -- "-n k8s.io images check" "$CTR_CHECK_LOG" +} + +@test "an empty cache does not ask containerd anything" { + run_preload + [ "$status" -eq 0 ] + [ ! -s "$CTR_CHECK_LOG" ] +} + +@test "an image containerd lost is imported again" { + make_archive "$CACHE/etcd.tar" registry.example/etcd:3.5 "$(digest 1)" + containerd_has registry.example/other:1 "$(digest 1)" + run_preload + [ "$status" -eq 0 ] + grep -q "etcd.tar" "$CTR_LOG" + [[ "$output" == *"Imported 1 of the 1 cached archives (0 already in containerd, 0 no longer there)"* ]] +} + +@test "an archive replaced with another digest is imported" { + make_archive "$CACHE/etcd.tar" registry.example/etcd:3.5 "$(digest 2)" + containerd_has registry.example/etcd:3.5 "$(digest 1)" + run_preload + [ "$status" -eq 0 ] + grep -q "etcd.tar" "$CTR_LOG" +} + +@test "an image containerd holds incomplete is imported" { + make_archive "$CACHE/etcd.tar" registry.example/etcd:3.5 "$(digest 1)" + containerd_has registry.example/etcd:3.5 "$(digest 1)" "incomplete (2/3)" + run_preload + [ "$status" -eq 0 ] + grep -q "etcd.tar" "$CTR_LOG" +} + +@test "an image containerd holds but has not unpacked is imported" { + make_archive "$CACHE/etcd.tar" registry.example/etcd:3.5 "$(digest 1)" + containerd_has registry.example/etcd:3.5 "$(digest 1)" "complete (3/3)" false + run_preload + [ "$status" -eq 0 ] + grep -q "etcd.tar" "$CTR_LOG" +} + +@test "an archive is imported when containerd lacks one of its images" { + make_archive "$CACHE/control-plane.tar" \ + registry.example/apiserver:1 "$(digest 1)" \ + registry.example/scheduler:1 "$(digest 2)" + containerd_has registry.example/apiserver:1 "$(digest 1)" + run_preload + [ "$status" -eq 0 ] + grep -q "control-plane.tar" "$CTR_LOG" +} + +@test "an archive with an unnamed image is always imported" { + local dir="$TMP/unnamed" + write_index "$dir" "{\"schemaVersion\":2,\"manifests\":[{\"digest\":\"$(digest 1)\",\"annotations\":{\"org.opencontainers.image.ref.name\":\"3.5\"}}]}" + tar_archive "$CACHE/etcd.tar" "$dir" + containerd_has 3.5 "$(digest 1)" + run_preload + [ "$status" -eq 0 ] + grep -q "etcd.tar" "$CTR_LOG" +} + +@test "an archive with no OCI index is always imported" { + mkdir -p "$TMP/legacy" + printf '[]' > "$TMP/legacy/manifest.json" + tar -cf "$CACHE/legacy.tar" -C "$TMP/legacy" manifest.json + containerd_has registry.example/etcd:3.5 "$(digest 1)" + run_preload + [ "$status" -eq 0 ] + grep -q "legacy.tar" "$CTR_LOG" +} + +@test "an oversized index is not trusted to skip the import" { + local dir="$TMP/huge" padding + padding="$(head -c 1100000 /dev/zero | tr '\0' x)" + write_index "$dir" "{\"schemaVersion\":2,\"pad\":\"${padding}\",\"manifests\":[{\"digest\":\"$(digest 1)\",\"annotations\":{\"io.containerd.image.name\":\"registry.example/etcd:3.5\"}}]}" + tar_archive "$CACHE/etcd.tar" "$dir" + containerd_has registry.example/etcd:3.5 "$(digest 1)" + run_preload + [ "$status" -eq 0 ] + grep -q "etcd.tar" "$CTR_LOG" +} + +@test "a containerd that cannot list its images gets every archive imported" { + make_archive "$CACHE/etcd.tar" registry.example/etcd:3.5 "$(digest 1)" + containerd_has registry.example/etcd:3.5 "$(digest 1)" + : > "$CTR_CHECK_FAIL" + run_preload + [ "$status" -eq 0 ] + grep -q "etcd.tar" "$CTR_LOG" + [[ "$stderr" == *"Could not list the images containerd has, importing every archive"* ]] +} + +@test "a run where everything is already in containerd is no warning" { + make_archive "$CACHE/a.tar" registry.example/a:1 "$(digest 1)" + containerd_has registry.example/a:1 "$(digest 1)" + run_preload_on_journal + [ "$status" -eq 0 ] + grep -qxF "<6>Skipping $CACHE/a.tar, already in containerd" "$TMP/journal" + run ! grep -q "^<[0-4]>" "$TMP/journal" +} + +@test "a failure is counted apart from the archives already in containerd" { + make_archive "$CACHE/current.tar" registry.example/a:1 "$(digest 1)" + containerd_has registry.example/a:1 "$(digest 1)" + : > "$CACHE/bad.tar" + : > "$CACHE/good.tar" + fail_on bad.tar + run_preload + [ "$status" -eq 1 ] + [[ "$stderr" == *"Failed to import 1 of the 3 cached archives (1 imported, 1 already in containerd, 0 no longer there)"* ]] } From 86753ed77fcd37f5d23b1f2961c18edde1ec5b34 Mon Sep 17 00:00:00 2001 From: riadel Date: Wed, 18 Nov 2026 16:25:55 +0100 Subject: [PATCH 2/9] fix: fix design Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- DESIGN.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESIGN.md b/DESIGN.md index 4c03990..8baecf5 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -28,7 +28,7 @@ lifetimes, so they are separate components: images containerd is missing, on boot and periodically. It is deliberately dumb: a shell script, no daemon, no Kubernetes client, no registry access. It works on a node whose cluster is down. Archives whose `index.json` names - and digests all show as complete in `ctr images check` are skipped, since + and digests all show as complete and unpacked in `ctr images check` are skipped, since re-importing them costs about twice their size in I/O. - **`image-cache-agent`** (a Kubernetes DaemonSet) answers *fill*. It watches From 8759055add4d29954eff79f5e61bc1fc269bbac9 Mon Sep 17 00:00:00 2001 From: riad Date: Wed, 18 Nov 2026 17:57:47 +0100 Subject: [PATCH 3/9] fix(rpm): keep the images check table when one image fails its check --- rpm/sources/containerd-image-preload.sh | 11 ++++++++--- rpm/test/script.bats | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/rpm/sources/containerd-image-preload.sh b/rpm/sources/containerd-image-preload.sh index 7794389..004d90e 100644 --- a/rpm/sources/containerd-image-preload.sh +++ b/rpm/sources/containerd-image-preload.sh @@ -35,9 +35,14 @@ INDEX_MAX_BYTES=1048576 # "name digest" of each image containerd holds complete and unpacked. # STATUS and SIZE contain spaces, so UNPACKED is read as the last field. +# ctr exits non-zero as soon as one image of the namespace cannot be checked, +# any image, not only cached ones, yet still prints every row. So its exit +# code is ignored: only a table with no header means containerd did not answer. ready_images() { - ctr -n k8s.io images check | - awk 'NR > 1 && $4 == "complete" && $NF == "true" { print $1, $3 }' + local table + table="$(ctr -n k8s.io images check 2> /dev/null)" || true + [[ "${table}" == REF* ]] || return 1 + awk 'NR > 1 && $4 == "complete" && $NF == "true" { print $1, $3 }' <<< "${table}" } # "name digest" of each image in the archive's index.json. Fails when there @@ -71,7 +76,7 @@ skipped=0 current=0 failures=0 -# If containerd cannot list its images, every archive is imported. +# If containerd does not answer at all, every archive is imported. ready="" if [ "${scanned}" -ne 0 ] && ! ready="$(ready_images)"; then warn "Could not list the images containerd has, importing every archive" diff --git a/rpm/test/script.bats b/rpm/test/script.bats index f83d09d..aac6a27 100644 --- a/rpm/test/script.bats +++ b/rpm/test/script.bats @@ -22,6 +22,7 @@ setup() { CTR_IMAGES="$TMP/ctr.images" CTR_CHECK_LOG="$TMP/ctr.check.log" CTR_CHECK_FAIL="$TMP/ctr.check.fail" + CTR_CHECK_PARTIAL="$TMP/ctr.check.partial" mkdir -p "$CACHE" "$BIN" : > "$CTR_LOG" : > "$CTR_FAIL" @@ -43,6 +44,12 @@ if [ "\$3 \$4" = "images check" ]; then fi printf 'REF\tTYPE\tDIGEST\tSTATUS\tSIZE\tUNPACKED\n' cat "$CTR_IMAGES" + # Like the real ctr: every row printed, then a non-zero exit when one + # image could not be checked. + if [ -e "$CTR_CHECK_PARTIAL" ]; then + echo "ctr: unable to check registry.example/broken:1" >&2 + exit 1 + fi exit 0 fi echo "\$*" >> "$CTR_LOG" @@ -452,6 +459,21 @@ run_preload_on_journal() { [[ "$stderr" == *"Could not list the images containerd has, importing every archive"* ]] } +@test "one image containerd cannot check does not stop the others from being skipped" { + make_archive "$CACHE/etcd.tar" registry.example/etcd:3.5 "$(digest 1)" + make_archive "$CACHE/broken.tar" registry.example/broken:1 "$(digest 2)" + containerd_has registry.example/etcd:3.5 "$(digest 1)" + containerd_has registry.example/broken:1 "$(digest 2)" "unavailable (1/?)" false + : > "$CTR_CHECK_PARTIAL" + run_preload + [ "$status" -eq 0 ] + [[ "$output" == *"Skipping $CACHE/etcd.tar, already in containerd"* ]] + [[ "$stderr" != *"Could not list the images containerd has"* ]] + # The image that failed its check is not taken as present. + grep -q "broken.tar" "$CTR_LOG" + run ! grep -q "etcd.tar" "$CTR_LOG" +} + @test "a run where everything is already in containerd is no warning" { make_archive "$CACHE/a.tar" registry.example/a:1 "$(digest 1)" containerd_has registry.example/a:1 "$(digest 1)" From 60ef578d5858dd797b54f09012303202a1a40d28 Mon Sep 17 00:00:00 2001 From: riad Date: Wed, 18 Nov 2026 18:00:14 +0100 Subject: [PATCH 4/9] fix(rpm): drop the random delay from the preload timer `RandomizedDelaySec=1min` was there to keep nodes whose disks sit on the same storage from importing at the same time. Now that a run skips the archives containerd already has, a run on a node that is up to date only reads the archive indexes and writes nothing, so there is little left to spread out. The delay still held back the timer's boot run by up to a minute. Back to the timer of main: `OnBootSec=0` and `OnUnitActiveSec=10min`. --- rpm/README.md | 3 +-- rpm/sources/containerd-image-preload.timer | 2 -- rpm/test/package.bats | 4 ---- 3 files changed, 1 insertion(+), 8 deletions(-) diff --git a/rpm/README.md b/rpm/README.md index c32c915..d14e058 100644 --- a/rpm/README.md +++ b/rpm/README.md @@ -30,8 +30,7 @@ systemctl enable --now containerd-image-preload.timer Installing does not enable anything: the package ships the units and stops there, so the provisioning layer decides when they start. The timer fires once at boot (`OnBootSec=0`) and every ten minutes after each run -(`OnUnitActiveSec=10min`), up to a minute late (`RandomizedDelaySec=1min`) so -nodes that boot together drift apart. To import right away: +(`OnUnitActiveSec=10min`). To import right away: ```console systemctl start containerd-image-preload.service diff --git a/rpm/sources/containerd-image-preload.timer b/rpm/sources/containerd-image-preload.timer index ac2c1db..76a4f8c 100644 --- a/rpm/sources/containerd-image-preload.timer +++ b/rpm/sources/containerd-image-preload.timer @@ -4,8 +4,6 @@ Description=Periodically pre-load container images from the local cache into con [Timer] OnBootSec=0 OnUnitActiveSec=10min -# Spread nodes that boot together. Also delays the boot run (systemd 239). -RandomizedDelaySec=1min [Install] WantedBy=timers.target diff --git a/rpm/test/package.bats b/rpm/test/package.bats index 556c483..3ca358b 100644 --- a/rpm/test/package.bats +++ b/rpm/test/package.bats @@ -40,10 +40,6 @@ assert_linter_clean() { rpm -qpR "$RPM" | grep -qx "tar" } -@test "timer spreads the runs of nodes that boot together" { - grep -qx "RandomizedDelaySec=1min" "/usr/lib/systemd/system/$NAME.timer" -} - @test "package ships the expected files" { run rpm -qlp "$RPM" [ "$status" -eq 0 ] From b780441b7f61f9eb99f82e12145553854cb102e4 Mon Sep 17 00:00:00 2001 From: riad Date: Mon, 23 Nov 2026 15:32:36 +0100 Subject: [PATCH 5/9] fix(rpm): also skip the docker-archives whose images containerd already has An archive with no index.json, as skopeo docker-archive: and docker save before Docker 25 write, was imported at every run. containerd writes its own manifest for such an image, so it is matched on each tag of manifest.json, named the way containerd names it on import, and on the digest of its image configuration, hashed from the archive and read from containerd with `ctr content get`. Like containerd, an archive with an index.json is still read as OCI. --- DESIGN.md | 8 +- rpm/README.md | 28 ++- rpm/sources/containerd-image-preload.sh | 122 ++++++++-- rpm/test/script.bats | 295 +++++++++++++++++++++++- 4 files changed, 423 insertions(+), 30 deletions(-) diff --git a/DESIGN.md b/DESIGN.md index 8baecf5..98cfff5 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -27,9 +27,11 @@ lifetimes, so they are separate components: runs `ctr images import` over every tarball of the cache directory whose images containerd is missing, on boot and periodically. It is deliberately dumb: a shell script, no daemon, no Kubernetes client, no registry access. - It works on a node whose cluster is down. Archives whose `index.json` names - and digests all show as complete and unpacked in `ctr images check` are skipped, since - re-importing them costs about twice their size in I/O. + It works on a node whose cluster is down. Archives whose images all show as + complete and unpacked in `ctr images check` are skipped, since re-importing + them costs about twice their size in I/O. An OCI archive's images are + matched by the name and manifest digest of its `index.json`, a + docker-archive's by the tags and configuration digest of its `manifest.json`. - **`image-cache-agent`** (a Kubernetes DaemonSet) answers *fill*. It watches `ImageCache` custom resources, pulls the images they point at, extracts the diff --git a/rpm/README.md b/rpm/README.md index d14e058..e1c9b75 100644 --- a/rpm/README.md +++ b/rpm/README.md @@ -71,12 +71,28 @@ Nothing deeper, and nothing under a hidden directory. Each file goes in with `ctr -n k8s.io images import --platform "$IMAGE_PLATFORM"`, into the `k8s.io` namespace, which is where the kubelet looks. -An archive is skipped when every image in its OCI `index.json` -(`io.containerd.image.name` and digest) is reported complete and unpacked by -`ctr -n k8s.io images check`, which saves re-reading and re-writing it every -run. Otherwise it is imported, including when the archive has no OCI index, -an unnamed image or an index over 1 MiB, and when `ctr images check` fails. -This needs `jq` and GNU `tar`, which the package requires. +An archive is skipped when containerd already holds every image in it, +complete and unpacked as `ctr -n k8s.io images check` reports, which saves +re-reading and re-writing it every run. How an image is matched depends on +the archive's format, read the way containerd reads it: + +- **OCI archive** (an `index.json`, as `docker save` writes from Docker 25 on, + even next to a `manifest.json`): its `io.containerd.image.name` and + manifest digest, which containerd keeps as is. +- **docker-archive** (a `manifest.json` and no `index.json`, as `skopeo copy + … docker-archive:` and older `docker save` write): each of its `RepoTags`, + named the way containerd names it on import (`pause:3.10` becomes + `docker.io/library/pause:3.10`), and the digest of its image configuration. + containerd writes a manifest of its own for such an image, so the + configuration, copied byte for byte, is what both have in common. The + script hashes it from the archive, and reads the one containerd holds from + the manifest with `ctr content get`, once per image per run. + +Otherwise the archive is imported: when it has neither file, an unnamed or +untagged image, an `index.json`, `manifest.json` or configuration over 1 MiB, +when the name points at something other than a single manifest, and when +`ctr` cannot answer. This needs `jq` and GNU `tar`, which the package +requires. A tarball that fails to import does not stop the ones after it. The run tries every archive, names each one it could not import, and then exits non-zero, so diff --git a/rpm/sources/containerd-image-preload.sh b/rpm/sources/containerd-image-preload.sh index 004d90e..df91957 100644 --- a/rpm/sources/containerd-image-preload.sh +++ b/rpm/sources/containerd-image-preload.sh @@ -30,8 +30,9 @@ info() { log 6 1 "$@"; } warn() { log 4 2 "$@"; } error() { log 3 2 "$@"; } -# Larger index.json files are not trusted; the archive is imported. -INDEX_MAX_BYTES=1048576 +# Larger index.json, manifest.json or image configurations are not trusted; +# the archive is imported. +MEMBER_MAX_BYTES=1048576 # "name digest" of each image containerd holds complete and unpacked. # STATUS and SIZE contain spaces, so UNPACKED is read as the last field. @@ -45,11 +46,18 @@ ready_images() { awk 'NR > 1 && $4 == "complete" && $NF == "true" { print $1, $3 }' <<< "${table}" } -# "name digest" of each image in the archive's index.json. Fails when there -# is no index, an image is unnamed, or the index is too large. -archive_images() { - tar --seek --occurrence=1 -xOf "$1" index.json 2> /dev/null | - head -c "${INDEX_MAX_BYTES}" | +# One file at the root of an archive, cut at MEMBER_MAX_BYTES. Fails when +# the archive does not hold it. A file cut short fails whatever reads it: it +# is no longer valid JSON, and it no longer hashes to its digest. +archive_member() { + tar --seek --occurrence=1 -xOf "$1" "$2" 2> /dev/null | + head -c "${MEMBER_MAX_BYTES}" +} + +# "name digest" of each image in an OCI archive's index.json, the manifest +# digest that containerd keeps as is. Fails when an image is unnamed. +oci_archive_images() { + archive_member "$1" index.json | jq -r ' .manifests | if length > 0 @@ -59,15 +67,101 @@ archive_images() { ' 2> /dev/null } -# True when containerd already holds every image of the archive. +# The name containerd gives a docker-archive tag on import: docker.io and +# library/ added to a short name, latest to an untagged one. +normalize_name() { + local name="$1" domain path + domain="${name%%/*}" + path="${name#*/}" + # As in Docker, a first component is a registry only when it looks like + # a host: a dot, a port, localhost, or an upper case letter, which a + # repository cannot have. + if [[ "${name}" != */* ]] || + [[ "${domain}" != *[.:[:upper:]]* && "${domain}" != localhost ]]; then + domain=docker.io + path="${name}" + fi + if [ "${domain}" = index.docker.io ]; then + domain=docker.io + fi + if [ "${domain}" = docker.io ] && [[ "${path}" != */* ]]; then + path="library/${path}" + fi + if [[ "${path##*/}" != *:* ]]; then + path="${path}:latest" + fi + printf '%s/%s\n' "${domain}" "${path}" +} + +# "name digest" of each tag in a docker-archive's manifest.json, with the +# digest of the image configuration. containerd writes a manifest of its own +# on import, so the configuration, copied byte for byte, is what the archive +# and containerd have in common. Its digest is the hash of the file, not its +# name. Fails when an image has no tag. +docker_archive_images() { + local entries name config sum + local -A config_digests=() + entries="$(archive_member "$1" manifest.json | + jq -r ' + if type == "array" and length > 0 + and all(.[]; (.Config | type) == "string" + and ((.RepoTags // []) | length) > 0) + then .[] | .Config as $config | .RepoTags[] | "\(.) \($config)" + else error("not every image is tagged") end + ' 2> /dev/null)" || return 1 + while read -r name config; do + if [ -z "${config_digests[${config}]+set}" ]; then + sum="$(archive_member "$1" "${config}" | sha256sum)" || return 1 + config_digests[${config}]="sha256:${sum%% *}" + fi + printf '%s %s\n' "$(normalize_name "${name}")" "${config_digests[${config}]}" + done <<< "${entries}" +} + +# Configuration digest of each manifest looked up so far, empty when it could +# not be read. Kept across archives, so an image is read once per run. +declare -A ready_configs=() + +# Sets config to the configuration digest of the image containerd holds +# ready under the name $1. Fails when it holds none, or when the name points +# at an index rather than at a single manifest. +ready_config() { + local manifest + manifest="$(name="$1" awk '$1 == ENVIRON["name"] { print $2; exit }' <<< "${ready}")" + [ -n "${manifest}" ] || return 1 + if [ -z "${ready_configs[${manifest}]+set}" ]; then + ready_configs[${manifest}]="$(ctr -n k8s.io content get "${manifest}" 2> /dev/null | + head -c "${MEMBER_MAX_BYTES}" | + jq -er '.config.digest | strings' 2> /dev/null)" || + ready_configs[${manifest}]="" + fi + config="${ready_configs[${manifest}]}" + [ -n "${config}" ] +} + +# True when containerd already holds every image of the archive. Like +# containerd, it reads an archive with an index.json as OCI, even when it also +# has the manifest.json that Docker 25 and later write next to it. already_imported() { - local images line + local members images line name digest config [ -n "${ready}" ] || return 1 - images="$(archive_images "$1")" || return 1 - [ -n "${images}" ] || return 1 - while IFS= read -r line; do - grep -qxF -- "${line}" <<< "${ready}" || return 1 - done <<< "${images}" + members="$(tar --seek -tf "$1" 2> /dev/null)" || return 1 + if grep -qxF index.json <<< "${members}"; then + images="$(oci_archive_images "$1")" || return 1 + [ -n "${images}" ] || return 1 + while IFS= read -r line; do + grep -qxF -- "${line}" <<< "${ready}" || return 1 + done <<< "${images}" + elif grep -qxF manifest.json <<< "${members}"; then + images="$(docker_archive_images "$1")" || return 1 + [ -n "${images}" ] || return 1 + while read -r name digest; do + ready_config "${name}" || return 1 + [ "${config}" = "${digest}" ] || return 1 + done <<< "${images}" + else + return 1 + fi } archives=("${IMAGE_CACHE_DIR}"/*.tar "${IMAGE_CACHE_DIR}"/*/*.tar) diff --git a/rpm/test/script.bats b/rpm/test/script.bats index aac6a27..63c8971 100644 --- a/rpm/test/script.bats +++ b/rpm/test/script.bats @@ -23,7 +23,10 @@ setup() { CTR_CHECK_LOG="$TMP/ctr.check.log" CTR_CHECK_FAIL="$TMP/ctr.check.fail" CTR_CHECK_PARTIAL="$TMP/ctr.check.partial" - mkdir -p "$CACHE" "$BIN" + CTR_CONTENT="$TMP/ctr.content" + CTR_CONTENT_LOG="$TMP/ctr.content.log" + mkdir -p "$CACHE" "$BIN" "$CTR_CONTENT" + : > "$CTR_CONTENT_LOG" : > "$CTR_LOG" : > "$CTR_FAIL" : > "$CTR_RM" @@ -33,9 +36,20 @@ setup() { # $CTR_FAIL, the way containerd reports one it cannot read. The archive is # the last argument, and is matched by its path inside the cache: the same # file name legitimately lives flat and in a subdirectory. `images check` - # prints $CTR_IMAGES and logs to $CTR_CHECK_LOG. + # prints $CTR_IMAGES and logs to $CTR_CHECK_LOG. `content get` prints the + # blob stored under its digest in $CTR_CONTENT, fails on any other, and + # logs to $CTR_CONTENT_LOG. cat > "$BIN/ctr" <> "$CTR_CONTENT_LOG" + if [ -f "$CTR_CONTENT/\$5" ]; then + cat "$CTR_CONTENT/\$5" + exit 0 + fi + echo "ctr: \$5: not found" >&2 + exit 1 +fi if [ "\$3 \$4" = "images check" ]; then echo "\$*" >> "$CTR_CHECK_LOG" if [ -e "$CTR_CHECK_FAIL" ]; then @@ -118,6 +132,43 @@ tar_archive() { tar -cf "$1" -C "$2" blobs oci-layout index.json } +# Write a docker-archive at $1, the way skopeo and Docker before 25 write +# one: a manifest.json and no index.json. Each image is a "tag[,tag...]" and +# a configuration given verbatim, stored under the file name of its hash. +# Prints nothing; the digest of a configuration is $(config_digest CONTENT). +make_docker_archive() { + local archive="$1" dir entries="" sep="" tags config file + shift + dir="$(mktemp -d "$TMP/docker-archive.XXXXXX")" + while [ "$#" -ge 2 ]; do + tags="$(jq -cn --arg t "$1" '$t | split(",")')" + config="$2" + file="$(config_digest "${config}" | cut -d: -f2).json" + printf '%s' "${config}" > "$dir/$file" + entries+="${sep}{\"Config\":\"$file\",\"RepoTags\":${tags},\"Layers\":[\"layer.tar\"]}" + sep="," + shift 2 + done + head -c 1048576 /dev/zero > "$dir/layer.tar" + printf '[%s]' "${entries}" > "$dir/manifest.json" + mkdir -p "$(dirname "$archive")" + (cd "$dir" && tar -cf "$archive" -- *) +} + +# The digest of an image configuration, given verbatim. +config_digest() { + printf 'sha256:%s' "$(printf '%s' "$1" | sha256sum | cut -d' ' -f1)" +} + +# Have containerd hold an image imported from a docker-archive: its row in +# `ctr images check`, under the manifest digest $2 that containerd wrote, and +# that manifest in the content store, naming the configuration $3 verbatim. +containerd_has_config() { + containerd_has "$1" "$2" "${4:-complete (3/3)}" "${5:-true}" + printf '{"schemaVersion":2,"mediaType":"application/vnd.docker.distribution.manifest.v2+json","config":{"mediaType":"application/vnd.docker.container.image.v1+json","digest":"%s","size":1},"layers":[]}' \ + "$(config_digest "$3")" > "$CTR_CONTENT/$2" +} + # Add an image to the `ctr images check` output (healthy by default). containerd_has() { printf '%s\tapplication/vnd.oci.image.index.v1+json\t%s\t%s\t%s\t%s\n' \ @@ -428,14 +479,14 @@ run_preload_on_journal() { grep -q "etcd.tar" "$CTR_LOG" } -@test "an archive with no OCI index is always imported" { - mkdir -p "$TMP/legacy" - printf '[]' > "$TMP/legacy/manifest.json" - tar -cf "$CACHE/legacy.tar" -C "$TMP/legacy" manifest.json +@test "an archive with neither an index.json nor a manifest.json is always imported" { + mkdir -p "$TMP/neither" + : > "$TMP/neither/layer.tar" + tar -cf "$CACHE/neither.tar" -C "$TMP/neither" layer.tar containerd_has registry.example/etcd:3.5 "$(digest 1)" run_preload [ "$status" -eq 0 ] - grep -q "legacy.tar" "$CTR_LOG" + grep -q "neither.tar" "$CTR_LOG" } @test "an oversized index is not trusted to skip the import" { @@ -493,3 +544,233 @@ run_preload_on_journal() { [ "$status" -eq 1 ] [[ "$stderr" == *"Failed to import 1 of the 3 cached archives (1 imported, 1 already in containerd, 0 no longer there)"* ]] } + +@test "a docker-archive whose configuration containerd holds under its tag is not imported again" { + make_docker_archive "$CACHE/worker-134-0-0/pause.tar" registry.example/pause:3.10 '{"pause":1}' + containerd_has_config registry.example/pause:3.10 "$(digest 1)" '{"pause":1}' + run_preload + [ "$status" -eq 0 ] + [ -z "$stderr" ] + [ ! -s "$CTR_LOG" ] + grep -qxF "$(digest 1)" "$CTR_CONTENT_LOG" + [[ "$output" == *"Skipping $CACHE/worker-134-0-0/pause.tar, already in containerd"* ]] + [[ "$output" == *"Imported 0 of the 1 cached archives (1 already in containerd, 0 no longer there)"* ]] +} + +@test "a docker-archive with another configuration under the same tag is imported" { + make_docker_archive "$CACHE/pause.tar" registry.example/pause:3.10 '{"pause":2}' + containerd_has_config registry.example/pause:3.10 "$(digest 1)" '{"pause":1}' + run_preload + [ "$status" -eq 0 ] + grep -q "pause.tar" "$CTR_LOG" +} + +@test "a docker-archive whose configuration containerd holds under another tag only is imported" { + make_docker_archive "$CACHE/pause.tar" registry.example/pause:3.10 '{"pause":1}' + containerd_has_config registry.example/other:1 "$(digest 1)" '{"pause":1}' + run_preload + [ "$status" -eq 0 ] + grep -q "pause.tar" "$CTR_LOG" +} + +@test "a docker-archive image containerd holds incomplete or not unpacked is imported" { + make_docker_archive "$CACHE/incomplete.tar" registry.example/a:1 '{"a":1}' + make_docker_archive "$CACHE/packed.tar" registry.example/b:1 '{"b":1}' + containerd_has_config registry.example/a:1 "$(digest 1)" '{"a":1}' "incomplete (2/3)" + containerd_has_config registry.example/b:1 "$(digest 2)" '{"b":1}' "complete (3/3)" false + run_preload + [ "$status" -eq 0 ] + grep -q "incomplete.tar" "$CTR_LOG" + grep -q "packed.tar" "$CTR_LOG" +} + +@test "a docker-archive is imported when containerd lacks one of its images" { + make_docker_archive "$CACHE/control-plane.tar" \ + registry.example/apiserver:1 '{"apiserver":1}' \ + registry.example/scheduler:1 '{"scheduler":1}' + containerd_has_config registry.example/apiserver:1 "$(digest 1)" '{"apiserver":1}' + run_preload + [ "$status" -eq 0 ] + grep -q "control-plane.tar" "$CTR_LOG" +} + +@test "a docker-archive is skipped only when containerd holds every tag of an image" { + make_docker_archive "$CACHE/pause.tar" registry.example/pause:3.10,registry.example/pause:stable '{"pause":1}' + containerd_has_config registry.example/pause:3.10 "$(digest 1)" '{"pause":1}' + run_preload + [ "$status" -eq 0 ] + grep -q "pause.tar" "$CTR_LOG" + containerd_has_config registry.example/pause:stable "$(digest 1)" '{"pause":1}' + : > "$CTR_LOG" + run_preload + [ "$status" -eq 0 ] + [ ! -s "$CTR_LOG" ] +} + +@test "docker-archive tags are matched under the names containerd gives them" { + # Each name on the right is what containerd 1.6, 1.7 and 2.4 list after + # importing a docker-archive tagged with the name on the left. + local pair tag name n=0 + for pair in \ + "pause:short docker.io/library/pause:short" \ + "myorg/pause:1 docker.io/myorg/pause:1" \ + "myorg/pause docker.io/myorg/pause:latest" \ + "pause docker.io/library/pause:latest" \ + "docker.io/pause:6 docker.io/library/pause:6" \ + "index.docker.io/library/pause:4 docker.io/library/pause:4" \ + "localhost/pause:2 localhost/pause:2" \ + "reg:5000/pause:3 reg:5000/pause:3" \ + "reg:5000/pause reg:5000/pause:latest" \ + "Upper/pause:5 Upper/pause:5" \ + "a.example/deep/path/p:1 a.example/deep/path/p:1"; do + tag="${pair% *}" + name="${pair#* }" + n=$((n + 1)) + make_docker_archive "$CACHE/image-$n.tar" "$tag" "{\"n\":$n}" + containerd_has_config "$name" "$(printf 'sha256:%064d' "$n")" "{\"n\":$n}" + done + run_preload + [ "$status" -eq 0 ] + [ ! -s "$CTR_LOG" ] + [[ "$output" == *"Imported 0 of the 11 cached archives (11 already in containerd, 0 no longer there)"* ]] +} + +@test "a docker-archive tag is not matched under a name containerd would not give it" { + make_docker_archive "$CACHE/pause.tar" pause:3.10 '{"pause":1}' + containerd_has_config pause:3.10 "$(digest 1)" '{"pause":1}' + run_preload + [ "$status" -eq 0 ] + grep -q "pause.tar" "$CTR_LOG" +} + +@test "a docker-archive configuration is identified by its hash, not by its file name" { + # The file is named after the configuration containerd holds, but holds + # another one: containerd would import this other one. + local dir="$TMP/misnamed" held='{"pause":1}' + mkdir -p "$dir" + printf '%s' '{"pause":2}' > "$dir/$(config_digest "$held" | cut -d: -f2).json" + printf '[{"Config":"%s.json","RepoTags":["registry.example/pause:3.10"],"Layers":[]}]' \ + "$(config_digest "$held" | cut -d: -f2)" > "$dir/manifest.json" + (cd "$dir" && tar -cf "$CACHE/pause.tar" -- *) + containerd_has_config registry.example/pause:3.10 "$(digest 1)" "$held" + run_preload + [ "$status" -eq 0 ] + grep -q "pause.tar" "$CTR_LOG" +} + +@test "a docker-archive whose configuration file is missing is imported" { + local dir="$TMP/noconfig" + mkdir -p "$dir" + printf '[{"Config":"%s.json","RepoTags":["registry.example/pause:3.10"],"Layers":[]}]' \ + "$(config_digest '{"pause":1}' | cut -d: -f2)" > "$dir/manifest.json" + tar -cf "$CACHE/pause.tar" -C "$dir" manifest.json + containerd_has_config registry.example/pause:3.10 "$(digest 1)" '{"pause":1}' + run_preload + [ "$status" -eq 0 ] + grep -q "pause.tar" "$CTR_LOG" +} + +@test "a docker-archive with an untagged image is always imported" { + make_docker_archive "$CACHE/pause.tar" registry.example/pause:3.10 '{"pause":1}' "" '{"other":1}' + containerd_has_config registry.example/pause:3.10 "$(digest 1)" '{"pause":1}' + run_preload + [ "$status" -eq 0 ] + grep -q "pause.tar" "$CTR_LOG" +} + +@test "a docker-archive with an empty or broken manifest.json is always imported" { + mkdir -p "$TMP/empty" "$TMP/broken" + printf '[]' > "$TMP/empty/manifest.json" + printf '[{"Config":' > "$TMP/broken/manifest.json" + tar -cf "$CACHE/empty.tar" -C "$TMP/empty" manifest.json + tar -cf "$CACHE/broken.tar" -C "$TMP/broken" manifest.json + containerd_has registry.example/etcd:3.5 "$(digest 1)" + run_preload + [ "$status" -eq 0 ] + grep -q "empty.tar" "$CTR_LOG" + grep -q "broken.tar" "$CTR_LOG" +} + +@test "an oversized manifest.json is not trusted to skip the import" { + local dir="$TMP/huge" padding file + padding="$(head -c 1100000 /dev/zero | tr '\0' x)" + file="$(config_digest '{"pause":1}' | cut -d: -f2).json" + mkdir -p "$dir" + printf '%s' '{"pause":1}' > "$dir/$file" + printf '[{"Config":"%s","RepoTags":["registry.example/pause:3.10"],"Pad":"%s"}]' \ + "$file" "$padding" > "$dir/manifest.json" + (cd "$dir" && tar -cf "$CACHE/pause.tar" -- *) + containerd_has_config registry.example/pause:3.10 "$(digest 1)" '{"pause":1}' + run_preload + [ "$status" -eq 0 ] + grep -q "pause.tar" "$CTR_LOG" +} + +@test "a docker-archive tag that points at an index in containerd is imported" { + # Pulled from a registry rather than imported: the name points at an + # index, which has no configuration of its own. + make_docker_archive "$CACHE/pause.tar" registry.example/pause:3.10 '{"pause":1}' + containerd_has registry.example/pause:3.10 "$(digest 1)" + printf '{"schemaVersion":2,"mediaType":"application/vnd.oci.image.index.v1+json","manifests":[]}' \ + > "$CTR_CONTENT/$(digest 1)" + run_preload + [ "$status" -eq 0 ] + grep -q "pause.tar" "$CTR_LOG" +} + +@test "a docker-archive is imported when containerd cannot give the manifest" { + make_docker_archive "$CACHE/pause.tar" registry.example/pause:3.10 '{"pause":1}' + containerd_has registry.example/pause:3.10 "$(digest 1)" + run_preload + [ "$status" -eq 0 ] + grep -q "pause.tar" "$CTR_LOG" + grep -qxF "$(digest 1)" "$CTR_CONTENT_LOG" +} + +@test "containerd is asked for each manifest once per run" { + make_docker_archive "$CACHE/a.tar" registry.example/pause:3.10 '{"pause":1}' + mkdir -p "$CACHE/worker-134-0-0" + make_docker_archive "$CACHE/worker-134-0-0/b.tar" registry.example/pause:3.10 '{"pause":1}' + containerd_has_config registry.example/pause:3.10 "$(digest 1)" '{"pause":1}' + run_preload + [ "$status" -eq 0 ] + [ ! -s "$CTR_LOG" ] + [ "$(grep -c . "$CTR_CONTENT_LOG")" -eq 1 ] +} + +@test "an archive with an index.json is read as OCI even when it has a manifest.json" { + # Docker 25 and later write both, and containerd imports from index.json. + local dir="$TMP/both" config='{"pause":1}' + write_index "$dir" "{\"schemaVersion\":2,\"manifests\":[{\"digest\":\"$(digest 2)\",\"annotations\":{\"io.containerd.image.name\":\"registry.example/pause:3.10\"}}]}" + printf '%s' "$config" > "$dir/config.json" + printf '[{"Config":"config.json","RepoTags":["registry.example/pause:3.10"],"Layers":[]}]' > "$dir/manifest.json" + tar -cf "$CACHE/pause.tar" -C "$dir" blobs oci-layout index.json manifest.json config.json + # The manifest.json would match, but not the index.json. + containerd_has_config registry.example/pause:3.10 "$(digest 1)" "$config" + run_preload + [ "$status" -eq 0 ] + grep -q "pause.tar" "$CTR_LOG" + [ ! -s "$CTR_CONTENT_LOG" ] +} + +@test "an OCI archive does not ask containerd for manifests" { + make_archive "$CACHE/etcd.tar" registry.example/etcd:3.5 "$(digest 1)" + containerd_has registry.example/etcd:3.5 "$(digest 1)" + run_preload + [ "$status" -eq 0 ] + [ ! -s "$CTR_LOG" ] + [ ! -s "$CTR_CONTENT_LOG" ] +} + +@test "OCI archives and docker-archives are skipped side by side" { + make_archive "$CACHE/etcd.tar" registry.example/etcd:3.5 "$(digest 1)" + make_docker_archive "$CACHE/pause.tar" registry.example/pause:3.10 '{"pause":1}' + make_docker_archive "$CACHE/nginx.tar" registry.example/nginx:1 '{"nginx":1}' + containerd_has registry.example/etcd:3.5 "$(digest 1)" + containerd_has_config registry.example/pause:3.10 "$(digest 2)" '{"pause":1}' + run_preload + [ "$status" -eq 0 ] + [ "$(grep -c . "$CTR_LOG")" -eq 1 ] + grep -q "nginx.tar" "$CTR_LOG" + [[ "$output" == *"Imported 1 of the 3 cached archives (2 already in containerd, 0 no longer there)"* ]] +} From 9dbc6deef619aeabc99cf5c853a25b6fad4e0984 Mon Sep 17 00:00:00 2001 From: riad Date: Mon, 23 Nov 2026 16:12:12 +0100 Subject: [PATCH 6/9] fix(rpm): import again a name an earlier archive of the run moved Two archives can hold the same image name with different images. The images containerd holds are listed once, before the first import, so after the first archive moved the name, the second was still taken as present and skipped. The name then switched between the two images at every run. Once an archive is imported, whether the import worked or not, its names are no longer taken as present. A later archive holding one of them is imported too, and the last one wins, as when nothing is skipped. --- rpm/README.md | 6 ++ rpm/sources/containerd-image-preload.sh | 21 +++++ rpm/test/script.bats | 105 ++++++++++++++++++++++++ 3 files changed, 132 insertions(+) diff --git a/rpm/README.md b/rpm/README.md index e1c9b75..bd04fb8 100644 --- a/rpm/README.md +++ b/rpm/README.md @@ -94,6 +94,12 @@ when the name points at something other than a single manifest, and when `ctr` cannot answer. This needs `jq` and GNU `tar`, which the package requires. +When two archives hold the same image name with different images, the last +one imported wins, as when nothing is skipped. Once the run imports an +archive, it no longer takes that archive's names as present, so a later +archive holding one of them is imported too. Both are then imported at every +run: give each name to one archive only to have them skipped. + A tarball that fails to import does not stop the ones after it. The run tries every archive, names each one it could not import, and then exits non-zero, so the unit still reports the failure while the node gets every image the cache diff --git a/rpm/sources/containerd-image-preload.sh b/rpm/sources/containerd-image-preload.sh index df91957..877b9c9 100644 --- a/rpm/sources/containerd-image-preload.sh +++ b/rpm/sources/containerd-image-preload.sh @@ -139,22 +139,29 @@ ready_config() { [ -n "${config}" ] } +# The image names already_imported read from the archive it was last given, +# empty when it could not read them. +archive_names="" + # True when containerd already holds every image of the archive. Like # containerd, it reads an archive with an index.json as OCI, even when it also # has the manifest.json that Docker 25 and later write next to it. already_imported() { local members images line name digest config + archive_names="" [ -n "${ready}" ] || return 1 members="$(tar --seek -tf "$1" 2> /dev/null)" || return 1 if grep -qxF index.json <<< "${members}"; then images="$(oci_archive_images "$1")" || return 1 [ -n "${images}" ] || return 1 + archive_names="$(cut -d' ' -f1 <<< "${images}")" while IFS= read -r line; do grep -qxF -- "${line}" <<< "${ready}" || return 1 done <<< "${images}" elif grep -qxF manifest.json <<< "${members}"; then images="$(docker_archive_images "$1")" || return 1 [ -n "${images}" ] || return 1 + archive_names="$(cut -d' ' -f1 <<< "${images}")" while read -r name digest; do ready_config "${name}" || return 1 [ "${config}" = "${digest}" ] || return 1 @@ -164,6 +171,19 @@ already_imported() { fi } +# Drops the names of the archive just imported from ready. Two archives can +# hold the same name with different images: ready was listed before the +# first one moved the name, so it would still show the second as present, +# and the name would switch between the two at every run. Without them, the +# later archive is imported too, and the last one wins, as it does when +# nothing is skipped. This holds whether the import worked or not: one that +# failed may still have moved a name. +forget_archive_names() { + [ -n "${archive_names}" ] || return 0 + ready="$(awk 'NR == FNR { names[$1]; next } !($1 in names)' \ + <(printf '%s\n' "${archive_names}") <(printf '%s\n' "${ready}"))" +} + archives=("${IMAGE_CACHE_DIR}"/*.tar "${IMAGE_CACHE_DIR}"/*/*.tar) scanned=${#archives[@]} skipped=0 @@ -201,6 +221,7 @@ for tar in "${archives[@]}"; do error "Failed to import ${tar}" failures=$((failures + 1)) fi + forget_archive_names done imported=$((scanned - skipped - current - failures)) diff --git a/rpm/test/script.bats b/rpm/test/script.bats index 63c8971..00ef708 100644 --- a/rpm/test/script.bats +++ b/rpm/test/script.bats @@ -774,3 +774,108 @@ run_preload_on_journal() { grep -q "nginx.tar" "$CTR_LOG" [[ "$output" == *"Imported 1 of the 3 cached archives (2 already in containerd, 0 no longer there)"* ]] } + +@test "a later OCI archive holding a name the run just imported is imported too" { + # containerd holds the name at b's image, as the last run left it. a moves + # it, so b must go in again, or the name would switch at every run. + make_archive "$CACHE/a.tar" registry.example/postgres:17 "$(digest 1)" + make_archive "$CACHE/b.tar" registry.example/postgres:17 "$(digest 2)" + containerd_has registry.example/postgres:17 "$(digest 2)" + run_preload + [ "$status" -eq 0 ] + [ "$(grep -c . "$CTR_LOG")" -eq 2 ] + [ "$(sed -n 1p "$CTR_LOG" | grep -c "/a.tar$")" -eq 1 ] + [ "$(sed -n 2p "$CTR_LOG" | grep -c "/b.tar$")" -eq 1 ] +} + +@test "a later docker-archive holding a name the run just imported is imported too" { + make_docker_archive "$CACHE/a.tar" registry.example/postgres:17 '{"postgres":1}' + make_docker_archive "$CACHE/b.tar" registry.example/postgres:17 '{"postgres":2}' + containerd_has_config registry.example/postgres:17 "$(digest 2)" '{"postgres":2}' + run_preload + [ "$status" -eq 0 ] + grep -q "/a.tar$" "$CTR_LOG" + grep -q "/b.tar$" "$CTR_LOG" +} + +@test "a name imported from an OCI archive is not taken as present for a later docker-archive" { + # Short tag: the docker-archive is matched under its normalized name. + make_archive "$CACHE/a.tar" docker.io/library/postgres:17 "$(digest 1)" + make_docker_archive "$CACHE/b.tar" postgres:17 '{"postgres":2}' + containerd_has_config docker.io/library/postgres:17 "$(digest 2)" '{"postgres":2}' + run_preload + [ "$status" -eq 0 ] + grep -q "/a.tar$" "$CTR_LOG" + grep -q "/b.tar$" "$CTR_LOG" +} + +@test "a name imported from a docker-archive is not taken as present for a later OCI archive" { + make_docker_archive "$CACHE/a.tar" registry.example/postgres:17 '{"postgres":1}' + make_archive "$CACHE/b.tar" registry.example/postgres:17 "$(digest 2)" + containerd_has registry.example/postgres:17 "$(digest 2)" + run_preload + [ "$status" -eq 0 ] + grep -q "/a.tar$" "$CTR_LOG" + grep -q "/b.tar$" "$CTR_LOG" +} + +@test "an import only forgets the names of the archive it imported" { + make_archive "$CACHE/a.tar" registry.example/postgres:17 "$(digest 1)" + make_archive "$CACHE/b.tar" registry.example/redis:7 "$(digest 2)" + make_docker_archive "$CACHE/c.tar" registry.example/pause:3.10 '{"pause":1}' + containerd_has registry.example/redis:7 "$(digest 2)" + containerd_has_config registry.example/pause:3.10 "$(digest 3)" '{"pause":1}' + run_preload + [ "$status" -eq 0 ] + [ "$(grep -c . "$CTR_LOG")" -eq 1 ] + grep -q "/a.tar$" "$CTR_LOG" + [[ "$output" == *"Imported 1 of the 3 cached archives (2 already in containerd, 0 no longer there)"* ]] +} + +@test "a skipped archive does not make a later one with the same name imported" { + # Same name, same image in both: nothing moved it, both stay skipped. + make_archive "$CACHE/a.tar" registry.example/postgres:17 "$(digest 1)" + make_archive "$CACHE/b.tar" registry.example/postgres:17 "$(digest 1)" + containerd_has registry.example/postgres:17 "$(digest 1)" + run_preload + [ "$status" -eq 0 ] + [ ! -s "$CTR_LOG" ] +} + +@test "a failed import forgets its names too" { + # A failed import may still have moved a name before it gave up. + make_archive "$CACHE/a.tar" registry.example/postgres:17 "$(digest 1)" + make_archive "$CACHE/b.tar" registry.example/postgres:17 "$(digest 2)" + containerd_has registry.example/postgres:17 "$(digest 2)" + fail_on a.tar + run_preload + [ "$status" -eq 1 ] + grep -q "/b.tar$" "$CTR_LOG" +} + +@test "an archive whose names cannot be read does not keep the others from being skipped" { + mkdir -p "$TMP/neither" + : > "$TMP/neither/layer.tar" + tar -cf "$CACHE/a.tar" -C "$TMP/neither" layer.tar + make_archive "$CACHE/b.tar" registry.example/redis:7 "$(digest 2)" + containerd_has registry.example/redis:7 "$(digest 2)" + run_preload + [ "$status" -eq 0 ] + grep -q "/a.tar$" "$CTR_LOG" + run ! grep -q "/b.tar$" "$CTR_LOG" +} + +@test "an archive whose names cannot be read forgets none of the names read before it" { + # a is skipped; importing b, whose names are unknown, must not drop a's + # names, so c, which holds the same image as a, is still skipped. + make_archive "$CACHE/a.tar" registry.example/postgres:17 "$(digest 1)" + mkdir -p "$TMP/neither" + : > "$TMP/neither/layer.tar" + tar -cf "$CACHE/b.tar" -C "$TMP/neither" layer.tar + make_archive "$CACHE/c.tar" registry.example/postgres:17 "$(digest 1)" + containerd_has registry.example/postgres:17 "$(digest 1)" + run_preload + [ "$status" -eq 0 ] + [ "$(grep -c . "$CTR_LOG")" -eq 1 ] + grep -q "/b.tar$" "$CTR_LOG" +} From aca4c98ba0aa41c40966188cbeb18ee19c2217dc Mon Sep 17 00:00:00 2001 From: riad Date: Mon, 23 Nov 2026 16:54:57 +0100 Subject: [PATCH 7/9] fix(rpm): never read a docker-archive's configuration path as a tar option The configuration path comes from the archive's manifest.json, and GNU tar reads an option even after the archive: `--` now keeps it a file name. Only `--occurrence`, which then had no file name, kept such a path from running anything. manifest.json is also refused, and the archive imported, when a configuration path is empty, starts with a dash or holds white space, or when its tags are not a list of strings with no white space. An empty path logged "bad array subscript" at every run. --- rpm/sources/containerd-image-preload.sh | 15 ++++-- rpm/test/script.bats | 66 +++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 4 deletions(-) diff --git a/rpm/sources/containerd-image-preload.sh b/rpm/sources/containerd-image-preload.sh index 877b9c9..79e5ce2 100644 --- a/rpm/sources/containerd-image-preload.sh +++ b/rpm/sources/containerd-image-preload.sh @@ -48,9 +48,11 @@ ready_images() { # One file at the root of an archive, cut at MEMBER_MAX_BYTES. Fails when # the archive does not hold it. A file cut short fails whatever reads it: it -# is no longer valid JSON, and it no longer hashes to its digest. +# is no longer valid JSON, and it no longer hashes to its digest. The name +# can come from the archive itself, and GNU tar reads an option even after +# the archive, so `--` keeps it a name. archive_member() { - tar --seek --occurrence=1 -xOf "$1" "$2" 2> /dev/null | + tar --seek --occurrence=1 -xOf "$1" -- "$2" 2> /dev/null | head -c "${MEMBER_MAX_BYTES}" } @@ -97,7 +99,9 @@ normalize_name() { # digest of the image configuration. containerd writes a manifest of its own # on import, so the configuration, copied byte for byte, is what the archive # and containerd have in common. Its digest is the hash of the file, not its -# name. Fails when an image has no tag. +# name. Fails when an image has no tag. A configuration path or tag that is +# empty or holds white space would not survive the "name config" lines, and +# a path starting with a dash would read as an option, so those fail too. docker_archive_images() { local entries name config sum local -A config_digests=() @@ -105,7 +109,10 @@ docker_archive_images() { jq -r ' if type == "array" and length > 0 and all(.[]; (.Config | type) == "string" - and ((.RepoTags // []) | length) > 0) + and (.Config | test("^[^-\\s][^\\s]*$")) + and ((.RepoTags // []) | type) == "array" + and ((.RepoTags // []) | length) > 0 + and all(.RepoTags[]; type == "string" and test("^[^\\s]+$"))) then .[] | .Config as $config | .RepoTags[] | "\(.) \($config)" else error("not every image is tagged") end ' 2> /dev/null)" || return 1 diff --git a/rpm/test/script.bats b/rpm/test/script.bats index 00ef708..105d074 100644 --- a/rpm/test/script.bats +++ b/rpm/test/script.bats @@ -879,3 +879,69 @@ run_preload_on_journal() { [ "$(grep -c . "$CTR_LOG")" -eq 1 ] grep -q "/b.tar$" "$CTR_LOG" } + +# Write a docker-archive at $1 whose manifest.json is $2 verbatim, next to a +# configuration file, and have containerd hold its tag ready. +make_raw_docker_archive() { + local dir + dir="$(mktemp -d "$TMP/raw.XXXXXX")" + printf '%s' '{"pause":1}' > "$dir/config.json" + printf '%s' "$2" > "$dir/manifest.json" + (cd "$dir" && tar -cf "$1" -- *) + containerd_has_config registry.example/pause:3.10 "$(digest 1)" '{"pause":1}' +} + +@test "a docker-archive configuration path is never read as a tar option" { + make_raw_docker_archive "$CACHE/evil.tar" \ + "[{\"Config\":\"--to-command=touch $TMP/pwned\",\"RepoTags\":[\"registry.example/pause:3.10\"],\"Layers\":[]}]" + run_preload + [ "$status" -eq 0 ] + [ ! -e "$TMP/pwned" ] + [ -z "$stderr" ] + grep -q "evil.tar" "$CTR_LOG" +} + +@test "a docker-archive with an empty configuration path is imported, quietly" { + make_raw_docker_archive "$CACHE/empty.tar" \ + '[{"Config":"","RepoTags":["registry.example/pause:3.10"],"Layers":[]}]' + run_preload + [ "$status" -eq 0 ] + [ -z "$stderr" ] + grep -q "empty.tar" "$CTR_LOG" +} + +@test "a docker-archive with white space in a configuration path or a tag is imported" { + make_raw_docker_archive "$CACHE/space.tar" \ + '[{"Config":"config .json","RepoTags":["registry.example/pause:3.10"],"Layers":[]}]' + make_raw_docker_archive "$CACHE/newline.tar" \ + '[{"Config":"config.json\nregistry.example/pause:3.10 x","RepoTags":["registry.example/pause:3.10"],"Layers":[]}]' + make_raw_docker_archive "$CACHE/tag.tar" \ + '[{"Config":"config.json","RepoTags":["registry.example/pause:3.10 sha256:00"],"Layers":[]}]' + run_preload + [ "$status" -eq 0 ] + [ -z "$stderr" ] + grep -q "space.tar" "$CTR_LOG" + grep -q "newline.tar" "$CTR_LOG" + grep -q "tag.tar" "$CTR_LOG" +} + +@test "a docker-archive with tags that are not a list of strings is imported" { + make_raw_docker_archive "$CACHE/string.tar" \ + '[{"Config":"config.json","RepoTags":"registry.example/pause:3.10","Layers":[]}]' + make_raw_docker_archive "$CACHE/number.tar" \ + '[{"Config":"config.json","RepoTags":[1],"Layers":[]}]' + run_preload + [ "$status" -eq 0 ] + [ -z "$stderr" ] + grep -q "string.tar" "$CTR_LOG" + grep -q "number.tar" "$CTR_LOG" +} + +@test "a well-formed raw docker-archive is still skipped" { + # Guards the stricter filter: the helper itself must still give a skip. + make_raw_docker_archive "$CACHE/good.tar" \ + '[{"Config":"config.json","RepoTags":["registry.example/pause:3.10"],"Layers":[]}]' + run_preload + [ "$status" -eq 0 ] + [ ! -s "$CTR_LOG" ] +} From 0527b97e47a63a8fc84a050f3bfd8e5f73def4ba Mon Sep 17 00:00:00 2001 From: riad Date: Tue, 24 Nov 2026 09:11:37 +0100 Subject: [PATCH 8/9] fix(rpm): forget every name an imported archive gives, even when not skippable An archive with one unnamed or untagged image, or one malformed entry, cannot be skipped, so its names were never read, and its import forgot none of them. A later archive holding one of those names was then skipped while the import had moved it, and the name switched between the two images at every run. The names are now read on their own, leniently: every name containerd can give an image from the archive, docker-archive tags normalized as for the skip. An archive whose index cannot be read at all still forgets nothing. DESIGN.md now links to rpm/README.md for how images are matched instead of repeating it. --- DESIGN.md | 5 +- rpm/README.md | 5 +- rpm/sources/containerd-image-preload.sh | 90 +++++++++++++------------ rpm/test/script.bats | 63 +++++++++++++++-- 4 files changed, 108 insertions(+), 55 deletions(-) diff --git a/DESIGN.md b/DESIGN.md index 98cfff5..c759a4d 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -29,9 +29,8 @@ lifetimes, so they are separate components: dumb: a shell script, no daemon, no Kubernetes client, no registry access. It works on a node whose cluster is down. Archives whose images all show as complete and unpacked in `ctr images check` are skipped, since re-importing - them costs about twice their size in I/O. An OCI archive's images are - matched by the name and manifest digest of its `index.json`, a - docker-archive's by the tags and configuration digest of its `manifest.json`. + them costs about twice their size in I/O. See + [rpm/README.md](rpm/README.md#what-it-imports) for how images are matched. - **`image-cache-agent`** (a Kubernetes DaemonSet) answers *fill*. It watches `ImageCache` custom resources, pulls the images they point at, extracts the diff --git a/rpm/README.md b/rpm/README.md index bd04fb8..48c0221 100644 --- a/rpm/README.md +++ b/rpm/README.md @@ -96,8 +96,9 @@ requires. When two archives hold the same image name with different images, the last one imported wins, as when nothing is skipped. Once the run imports an -archive, it no longer takes that archive's names as present, so a later -archive holding one of them is imported too. Both are then imported at every +archive, it no longer takes the names it could read from that archive as +present, even from one it could not have skipped, so a later archive +holding one of them is imported too. Both are then imported at every run: give each name to one archive only to have them skipped. A tarball that fails to import does not stop the ones after it. The run tries diff --git a/rpm/sources/containerd-image-preload.sh b/rpm/sources/containerd-image-preload.sh index 79e5ce2..871ea3d 100644 --- a/rpm/sources/containerd-image-preload.sh +++ b/rpm/sources/containerd-image-preload.sh @@ -30,15 +30,12 @@ info() { log 6 1 "$@"; } warn() { log 4 2 "$@"; } error() { log 3 2 "$@"; } -# Larger index.json, manifest.json or image configurations are not trusted; -# the archive is imported. +# Archive metadata larger than this is not trusted: the archive is imported. MEMBER_MAX_BYTES=1048576 # "name digest" of each image containerd holds complete and unpacked. -# STATUS and SIZE contain spaces, so UNPACKED is read as the last field. -# ctr exits non-zero as soon as one image of the namespace cannot be checked, -# any image, not only cached ones, yet still prints every row. So its exit -# code is ignored: only a table with no header means containerd did not answer. +# ctr exits non-zero when any image fails its check but still prints every +# row, so only a missing header means containerd did not answer. ready_images() { local table table="$(ctr -n k8s.io images check 2> /dev/null)" || true @@ -46,18 +43,15 @@ ready_images() { awk 'NR > 1 && $4 == "complete" && $NF == "true" { print $1, $3 }' <<< "${table}" } -# One file at the root of an archive, cut at MEMBER_MAX_BYTES. Fails when -# the archive does not hold it. A file cut short fails whatever reads it: it -# is no longer valid JSON, and it no longer hashes to its digest. The name -# can come from the archive itself, and GNU tar reads an option even after -# the archive, so `--` keeps it a name. +# One file of an archive, cut at MEMBER_MAX_BYTES (a cut file fails to parse +# or to hash). `--` stops GNU tar from reading a name as an option. archive_member() { tar --seek --occurrence=1 -xOf "$1" -- "$2" 2> /dev/null | head -c "${MEMBER_MAX_BYTES}" } -# "name digest" of each image in an OCI archive's index.json, the manifest -# digest that containerd keeps as is. Fails when an image is unnamed. +# "name manifest-digest" of each image in an OCI archive. Fails when an image +# is unnamed. oci_archive_images() { archive_member "$1" index.json | jq -r ' @@ -69,15 +63,12 @@ oci_archive_images() { ' 2> /dev/null } -# The name containerd gives a docker-archive tag on import: docker.io and -# library/ added to a short name, latest to an untagged one. +# The name containerd gives a docker-archive tag on import, as Docker does. normalize_name() { local name="$1" domain path domain="${name%%/*}" path="${name#*/}" - # As in Docker, a first component is a registry only when it looks like - # a host: a dot, a port, localhost, or an upper case letter, which a - # repository cannot have. + # The first component is a registry only if it looks like a host. if [[ "${name}" != */* ]] || [[ "${domain}" != *[.:[:upper:]]* && "${domain}" != localhost ]]; then domain=docker.io @@ -95,13 +86,10 @@ normalize_name() { printf '%s/%s\n' "${domain}" "${path}" } -# "name digest" of each tag in a docker-archive's manifest.json, with the -# digest of the image configuration. containerd writes a manifest of its own -# on import, so the configuration, copied byte for byte, is what the archive -# and containerd have in common. Its digest is the hash of the file, not its -# name. Fails when an image has no tag. A configuration path or tag that is -# empty or holds white space would not survive the "name config" lines, and -# a path starting with a dash would read as an option, so those fail too. +# "name config-digest" of each tag in a docker-archive. containerd writes its +# own manifest on import, so the configuration is what both share; its digest +# is the hash of the file. Fails on an untagged image, or on a tag or path +# that is empty, holds white space, or (for a path) starts with a dash. docker_archive_images() { local entries name config sum local -A config_digests=() @@ -125,13 +113,34 @@ docker_archive_images() { done <<< "${entries}" } -# Configuration digest of each manifest looked up so far, empty when it could -# not be read. Kept across archives, so an image is read once per run. +# Every name an import of the archive can set, read leniently: an archive +# rejected for a skip still moves the names it does have. +oci_archive_names() { + archive_member "$1" index.json | + jq -r ' + .manifests | arrays | .[] | objects + | .annotations | objects | .["io.containerd.image.name"] + | strings | select(test("^[^\\s]+$")) + ' 2> /dev/null +} + +docker_archive_names() { + local tag + archive_member "$1" manifest.json | + jq -r ' + arrays | .[] | objects | .RepoTags | arrays | .[] + | strings | select(test("^[^\\s]+$")) + ' 2> /dev/null | + while IFS= read -r tag; do + normalize_name "${tag}" + done +} + +# Configuration digest per manifest, cached for the run (empty if unreadable). declare -A ready_configs=() -# Sets config to the configuration digest of the image containerd holds -# ready under the name $1. Fails when it holds none, or when the name points -# at an index rather than at a single manifest. +# Sets config to the configuration digest of the ready image named $1. Fails +# when there is none, or when the name points at an index. ready_config() { local manifest manifest="$(name="$1" awk '$1 == ENVIRON["name"] { print $2; exit }' <<< "${ready}")" @@ -146,29 +155,27 @@ ready_config() { [ -n "${config}" ] } -# The image names already_imported read from the archive it was last given, -# empty when it could not read them. +# Names of the archive last given to already_imported. archive_names="" # True when containerd already holds every image of the archive. Like -# containerd, it reads an archive with an index.json as OCI, even when it also -# has the manifest.json that Docker 25 and later write next to it. +# containerd, it prefers index.json when manifest.json is there too. already_imported() { local members images line name digest config archive_names="" [ -n "${ready}" ] || return 1 members="$(tar --seek -tf "$1" 2> /dev/null)" || return 1 if grep -qxF index.json <<< "${members}"; then + archive_names="$(oci_archive_names "$1")" || true images="$(oci_archive_images "$1")" || return 1 [ -n "${images}" ] || return 1 - archive_names="$(cut -d' ' -f1 <<< "${images}")" while IFS= read -r line; do grep -qxF -- "${line}" <<< "${ready}" || return 1 done <<< "${images}" elif grep -qxF manifest.json <<< "${members}"; then + archive_names="$(docker_archive_names "$1")" || true images="$(docker_archive_images "$1")" || return 1 [ -n "${images}" ] || return 1 - archive_names="$(cut -d' ' -f1 <<< "${images}")" while read -r name digest; do ready_config "${name}" || return 1 [ "${config}" = "${digest}" ] || return 1 @@ -178,13 +185,10 @@ already_imported() { fi } -# Drops the names of the archive just imported from ready. Two archives can -# hold the same name with different images: ready was listed before the -# first one moved the name, so it would still show the second as present, -# and the name would switch between the two at every run. Without them, the -# later archive is imported too, and the last one wins, as it does when -# nothing is skipped. This holds whether the import worked or not: one that -# failed may still have moved a name. +# Drops the names of the archive just imported (or failed: it may have moved +# some) from ready, so a later archive holding the same name with another +# image is imported too and the last one wins, instead of the name switching +# at every run. forget_archive_names() { [ -n "${archive_names}" ] || return 0 ready="$(awk 'NR == FNR { names[$1]; next } !($1 in names)' \ diff --git a/rpm/test/script.bats b/rpm/test/script.bats index 105d074..0d5a25c 100644 --- a/rpm/test/script.bats +++ b/rpm/test/script.bats @@ -132,10 +132,8 @@ tar_archive() { tar -cf "$1" -C "$2" blobs oci-layout index.json } -# Write a docker-archive at $1, the way skopeo and Docker before 25 write -# one: a manifest.json and no index.json. Each image is a "tag[,tag...]" and -# a configuration given verbatim, stored under the file name of its hash. -# Prints nothing; the digest of a configuration is $(config_digest CONTENT). +# Write a docker-archive at $1 (manifest.json, no index.json). Each image is +# a "tag[,tag...]" and a configuration given verbatim. make_docker_archive() { local archive="$1" dir entries="" sep="" tags config file shift @@ -160,9 +158,7 @@ config_digest() { printf 'sha256:%s' "$(printf '%s' "$1" | sha256sum | cut -d' ' -f1)" } -# Have containerd hold an image imported from a docker-archive: its row in -# `ctr images check`, under the manifest digest $2 that containerd wrote, and -# that manifest in the content store, naming the configuration $3 verbatim. +# Have containerd hold $1 ready under manifest $2, whose configuration is $3. containerd_has_config() { containerd_has "$1" "$2" "${4:-complete (3/3)}" "${5:-true}" printf '{"schemaVersion":2,"mediaType":"application/vnd.docker.distribution.manifest.v2+json","config":{"mediaType":"application/vnd.docker.container.image.v1+json","digest":"%s","size":1},"layers":[]}' \ @@ -945,3 +941,56 @@ make_raw_docker_archive() { [ "$status" -eq 0 ] [ ! -s "$CTR_LOG" ] } + +@test "an OCI archive with an unnamed image still forgets the names it does give" { + # a cannot be skipped (one image unnamed), but its import still moves + # postgres:17, so b, which holds that name too, must go in again. + local dir="$TMP/partly" + write_index "$dir" "{\"schemaVersion\":2,\"manifests\":[{\"digest\":\"$(digest 1)\",\"annotations\":{\"io.containerd.image.name\":\"registry.example/postgres:17\"}},{\"digest\":\"$(digest 3)\"}]}" + tar_archive "$CACHE/a.tar" "$dir" + make_archive "$CACHE/b.tar" registry.example/postgres:17 "$(digest 2)" + containerd_has registry.example/postgres:17 "$(digest 2)" + run_preload + [ "$status" -eq 0 ] + grep -q "/a.tar$" "$CTR_LOG" + grep -q "/b.tar$" "$CTR_LOG" +} + +@test "a docker-archive with an untagged image still forgets the tags it does give" { + # Short tag: forgotten under the name containerd gives it. + make_docker_archive "$CACHE/a.tar" postgres:17 '{"postgres":1}' "" '{"other":1}' + make_archive "$CACHE/b.tar" docker.io/library/postgres:17 "$(digest 2)" + containerd_has docker.io/library/postgres:17 "$(digest 2)" + run_preload + [ "$status" -eq 0 ] + grep -q "/a.tar$" "$CTR_LOG" + grep -q "/b.tar$" "$CTR_LOG" +} + +@test "a docker-archive with a malformed entry still forgets the tags it does give" { + local dir="$TMP/malformed" + mkdir -p "$dir" + printf '[{"Config":"","RepoTags":["registry.example/postgres:17"]},{"Config":"c.json","RepoTags":[1,"bad tag"]}]' \ + > "$dir/manifest.json" + tar -cf "$CACHE/a.tar" -C "$dir" manifest.json + make_archive "$CACHE/b.tar" registry.example/postgres:17 "$(digest 2)" + containerd_has registry.example/postgres:17 "$(digest 2)" + run_preload + [ "$status" -eq 0 ] + [ -z "$stderr" ] + grep -q "/a.tar$" "$CTR_LOG" + grep -q "/b.tar$" "$CTR_LOG" +} + +@test "an archive with a broken index forgets nothing, and stays quiet" { + mkdir -p "$TMP/broken" + write_index "$TMP/broken" '{"manifests":[{"annotations":' + tar_archive "$CACHE/a.tar" "$TMP/broken" + make_archive "$CACHE/b.tar" registry.example/redis:7 "$(digest 2)" + containerd_has registry.example/redis:7 "$(digest 2)" + run_preload + [ "$status" -eq 0 ] + [ -z "$stderr" ] + grep -q "/a.tar$" "$CTR_LOG" + run ! grep -q "/b.tar$" "$CTR_LOG" +} From b751713a5ef388e2ecaaed53b781db853d7affbd Mon Sep 17 00:00:00 2001 From: riad Date: Wed, 25 Nov 2026 17:44:25 +0100 Subject: [PATCH 9/9] fix(rpm): no glob in archive member names, no warning on an empty containerd A docker-archive's configuration path comes from the archive, and GNU tar matched it as a pattern on extraction: "*" read the first member instead. --no-wildcards keeps it literal. With no image in k8s.io, `ctr images check` prints nothing and succeeds, so a fresh node warned that containerd could not list its images. An empty, successful answer now means nothing is ready. --- rpm/sources/containerd-image-preload.sh | 15 ++++++++----- rpm/test/script.bats | 28 +++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/rpm/sources/containerd-image-preload.sh b/rpm/sources/containerd-image-preload.sh index 871ea3d..6b69131 100644 --- a/rpm/sources/containerd-image-preload.sh +++ b/rpm/sources/containerd-image-preload.sh @@ -35,18 +35,23 @@ MEMBER_MAX_BYTES=1048576 # "name digest" of each image containerd holds complete and unpacked. # ctr exits non-zero when any image fails its check but still prints every -# row, so only a missing header means containerd did not answer. +# row, so only a missing header means containerd did not answer. With no +# image at all, it prints nothing, not even the header, and succeeds. ready_images() { - local table - table="$(ctr -n k8s.io images check 2> /dev/null)" || true + local table status=0 + table="$(ctr -n k8s.io images check 2> /dev/null)" || status=$? + if [ -z "${table}" ] && [ "${status}" -eq 0 ]; then + return 0 + fi [[ "${table}" == REF* ]] || return 1 awk 'NR > 1 && $4 == "complete" && $NF == "true" { print $1, $3 }' <<< "${table}" } # One file of an archive, cut at MEMBER_MAX_BYTES (a cut file fails to parse -# or to hash). `--` stops GNU tar from reading a name as an option. +# or to hash). The name can come from the archive: `--` stops GNU tar from +# reading it as an option, --no-wildcards from matching `*` or `[` as a glob. archive_member() { - tar --seek --occurrence=1 -xOf "$1" -- "$2" 2> /dev/null | + tar --seek --occurrence=1 --no-wildcards -xOf "$1" -- "$2" 2> /dev/null | head -c "${MEMBER_MAX_BYTES}" } diff --git a/rpm/test/script.bats b/rpm/test/script.bats index 0d5a25c..d2647bd 100644 --- a/rpm/test/script.bats +++ b/rpm/test/script.bats @@ -23,6 +23,7 @@ setup() { CTR_CHECK_LOG="$TMP/ctr.check.log" CTR_CHECK_FAIL="$TMP/ctr.check.fail" CTR_CHECK_PARTIAL="$TMP/ctr.check.partial" + CTR_CHECK_EMPTY="$TMP/ctr.check.empty" CTR_CONTENT="$TMP/ctr.content" CTR_CONTENT_LOG="$TMP/ctr.content.log" mkdir -p "$CACHE" "$BIN" "$CTR_CONTENT" @@ -56,6 +57,10 @@ if [ "\$3 \$4" = "images check" ]; then echo "ctr: connection refused" >&2 exit 1 fi + # Like the real ctr on a namespace with no image: no header, success. + if [ -e "$CTR_CHECK_EMPTY" ]; then + exit 0 + fi printf 'REF\tTYPE\tDIGEST\tSTATUS\tSIZE\tUNPACKED\n' cat "$CTR_IMAGES" # Like the real ctr: every row printed, then a non-zero exit when one @@ -506,6 +511,15 @@ run_preload_on_journal() { [[ "$stderr" == *"Could not list the images containerd has, importing every archive"* ]] } +@test "a containerd with no image yet gets every archive imported, with no warning" { + make_archive "$CACHE/etcd.tar" registry.example/etcd:3.5 "$(digest 1)" + : > "$CTR_CHECK_EMPTY" + run_preload + [ "$status" -eq 0 ] + grep -q "etcd.tar" "$CTR_LOG" + [ -z "$stderr" ] +} + @test "one image containerd cannot check does not stop the others from being skipped" { make_archive "$CACHE/etcd.tar" registry.example/etcd:3.5 "$(digest 1)" make_archive "$CACHE/broken.tar" registry.example/broken:1 "$(digest 2)" @@ -897,6 +911,20 @@ make_raw_docker_archive() { grep -q "evil.tar" "$CTR_LOG" } +@test "a docker-archive configuration path is never matched as a glob" { + # Read as patterns, both would match config.json, the configuration + # containerd holds, and the archives would be skipped. + make_raw_docker_archive "$CACHE/star.tar" \ + '[{"Config":"*","RepoTags":["registry.example/pause:3.10"],"Layers":[]}]' + make_raw_docker_archive "$CACHE/bracket.tar" \ + '[{"Config":"[c]onfig.json","RepoTags":["registry.example/pause:3.10"],"Layers":[]}]' + run_preload + [ "$status" -eq 0 ] + [ -z "$stderr" ] + grep -q "star.tar" "$CTR_LOG" + grep -q "bracket.tar" "$CTR_LOG" +} + @test "a docker-archive with an empty configuration path is imported, quietly" { make_raw_docker_archive "$CACHE/empty.tar" \ '[{"Config":"","RepoTags":["registry.example/pause:3.10"],"Layers":[]}]'