diff --git a/DESIGN.md b/DESIGN.md index 77cbcb8..c759a4d 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -24,11 +24,13 @@ 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 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. 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/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..48c0221 100644 --- a/rpm/README.md +++ b/rpm/README.md @@ -69,9 +69,37 @@ 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 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. + +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 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 every archive, names each one it could not import, and then exits non-zero, so @@ -92,9 +120,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..6b69131 100644 --- a/rpm/sources/containerd-image-preload.sh +++ b/rpm/sources/containerd-image-preload.sh @@ -30,15 +30,193 @@ info() { log 6 1 "$@"; } warn() { log 4 2 "$@"; } error() { log 3 2 "$@"; } -scanned=0 +# 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. +# 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. With no +# image at all, it prints nothing, not even the header, and succeeds. +ready_images() { + 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). 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 --no-wildcards -xOf "$1" -- "$2" 2> /dev/null | + head -c "${MEMBER_MAX_BYTES}" +} + +# "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 ' + .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 +} + +# The name containerd gives a docker-archive tag on import, as Docker does. +normalize_name() { + local name="$1" domain path + domain="${name%%/*}" + path="${name#*/}" + # The first component is a registry only if it looks like a host. + 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 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=() + entries="$(archive_member "$1" manifest.json | + jq -r ' + if type == "array" and length > 0 + and all(.[]; (.Config | type) == "string" + 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 + 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}" +} + +# 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 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}")" + [ -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}" ] +} + +# Names of the archive last given to already_imported. +archive_names="" + +# True when containerd already holds every image of the archive. Like +# 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 + 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 + while read -r name digest; do + ready_config "${name}" || return 1 + [ "${config}" = "${digest}" ] || return 1 + done <<< "${images}" + else + return 1 + fi +} + +# 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)' \ + <(printf '%s\n' "${archive_names}") <(printf '%s\n' "${ready}"))" +} + +archives=("${IMAGE_CACHE_DIR}"/*.tar "${IMAGE_CACHE_DIR}"/*/*.tar) +scanned=${#archives[@]} skipped=0 +current=0 failures=0 +# 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" + 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,32 +227,39 @@ 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}" failures=$((failures + 1)) fi + forget_archive_names 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/test/package.bats b/rpm/test/package.bats index 5de9edd..3ca358b 100644 --- a/rpm/test/package.bats +++ b/rpm/test/package.bats @@ -35,6 +35,11 @@ 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 "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..d2647bd 100644 --- a/rpm/test/script.bats +++ b/rpm/test/script.bats @@ -19,16 +19,58 @@ setup() { CTR_LOG="$TMP/ctr.log" CTR_FAIL="$TMP/ctr.fail" CTR_RM="$TMP/ctr.rm" - mkdir -p "$CACHE" "$BIN" + 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" + 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" + : > "$CTR_CONTENT_LOG" : > "$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. `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 + 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 + # 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" # 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 +105,77 @@ 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 +} + +# 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 + 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 $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":[]}' \ + "$(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' \ + "$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 +271,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 +282,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 +306,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 +320,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 +332,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 +355,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 +372,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 +398,627 @@ 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 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 "neither.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 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)" + 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)" + 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)"* ]] +} + +@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)"* ]] +} + +@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" +} + +# 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 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":[]}]' + 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" ] +} + +@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" }