Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment thread
riadelhajjaji-scality marked this conversation as resolved.
Resolved
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
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |

Expand All @@ -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
Expand Down
20 changes: 13 additions & 7 deletions rpm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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`.
Expand Down
5 changes: 3 additions & 2 deletions rpm/build.Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions rpm/containerd-image-preload.spec
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
65 changes: 57 additions & 8 deletions rpm/sources/containerd-image-preload.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Comment thread
riadelhajjaji-scality marked this conversation as resolved.
Resolved
awk 'NR > 1 && $4 == "complete" && $NF == "true" { print $1, $3 }'
}

Comment thread
riadelhajjaji-scality marked this conversation as resolved.
Resolved
# "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 |
Comment thread
riadelhajjaji-scality marked this conversation as resolved.
Resolved
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
Expand All @@ -49,32 +92,38 @@ 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
Comment thread
riadelhajjaji-scality marked this conversation as resolved.
Resolved
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
2 changes: 2 additions & 0 deletions rpm/sources/containerd-image-preload.timer
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment thread
riadelhajjaji-scality marked this conversation as resolved.
Resolved

[Install]
WantedBy=timers.target
9 changes: 9 additions & 0 deletions rpm/test/package.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand Down
Loading
Loading