diff --git a/.github/workflows/pull-requests.yaml b/.github/workflows/pull-requests.yaml index 92379083c..11d01dc15 100644 --- a/.github/workflows/pull-requests.yaml +++ b/.github/workflows/pull-requests.yaml @@ -482,6 +482,8 @@ jobs: buildkitd-config: hack/buildkitd.toml env: DOCKER_CONFIG: ${{ runner.temp }}/.docker + - name: Record image references before the build + run: sh -c '. hack/lib/image-refs.sh; collect_image_refs packages' | sort -u > "$RUNNER_TEMP/image-refs-before" - name: Build ${{ matrix.pkg }} run: | mkdir -p _out/oci @@ -511,6 +513,18 @@ jobs: # against the package dir — the archive would land in /_out/oci and # miss the repo-root _out/oci the upload reads. OCI_EXPORT_DIR: ${{ github.event.pull_request.head.repo.fork && format('{0}/_out/oci', github.workspace) || '' }} + # Fork PR images exist only as OCI archives at this point, so there is + # nothing to pull. + - name: Smoke-test the images this job built + if: ${{ !github.event.pull_request.head.repo.fork }} + env: + DOCKER_CONFIG: ${{ runner.temp }}/.docker + COZYTEST_TRACE: '0' + run: | + sh -c '. hack/lib/image-refs.sh; collect_image_refs packages' | sort -u > "$RUNNER_TEMP/image-refs-after" + comm -13 "$RUNNER_TEMP/image-refs-before" "$RUNNER_TEMP/image-refs-after" > "$RUNNER_TEMP/image-refs-built" + cat "$RUNNER_TEMP/image-refs-built" + IMAGE_SMOKE_REFS_FILE="$RUNNER_TEMP/image-refs-built" hack/cozytest.sh hack/image-smoke.bats - name: Capture digest patch fragment id: frag env: diff --git a/docs/agents/image-refs.md b/docs/agents/image-refs.md index 191bc62c2..9dec7b155 100644 --- a/docs/agents/image-refs.md +++ b/docs/agents/image-refs.md @@ -62,6 +62,7 @@ If a package stamps a reference somewhere the two globs do not cover, add that f | `hack/stitch-multiarch.sh` | all three shapes | join each amd64 image and its `-arm64` twin into one index, each side an image or, built with `SBOM=1`, an index of the image and its attestation that the result keeps, repin the tree on it, then fail if any old digest survives | | `hack/retention-packages.sh` | all three shapes, in the published nightly tree | name the GHCR packages the retention workflow prunes (those linked to another repository are then skipped); a ref it cannot see is a package whose nightlies are never pruned | | `hack/verify-multiarch.sh` | all three shapes, first- and third-party | fail a release unless every pinned image is an index with `linux/amd64` and `linux/arm64`, attestation entries not counted, bar the repositories in `hack/multiarch-allowlist` | +| PR build job, image smoke step | all three shapes | list the references a package build stamped, by diffing the tree before and after it, for `hack/image-smoke.bats` to run | `hack/multiarch-allowlist` is the only bypass for that gate, and it is for images that cannot exist for arm64, not for images nobody has rebuilt yet. An entry is a repository with no tag or digest, so bumping an allowed image needs no edit, and it must carry a reason after `#` on the same line: the script refuses an entry without one. Allowed refs are still printed, under their own "allowed single-arch" heading, and in tree mode an entry that matches no ref that failed the check is reported as stale without changing the exit status, so it gets removed once the image is multi-arch or gone. `--refs-file` reports no stale entries, since a list of pulled images omits everything the run did not pull. diff --git a/hack/build-matrix.sh b/hack/build-matrix.sh index d15267631..4c9e29ec3 100755 --- a/hack/build-matrix.sh +++ b/hack/build-matrix.sh @@ -96,4 +96,14 @@ if grep -qE '^packages/system/seaweedfs/' "$CHANGED"; then esac fi +# the kubevirt-csi-node sidecars run on tenant vCPUs and are ISA-checked by +# hack/image-smoke.bats in the job that builds kubevirt-csi-driver, so a +# kubevirt-csi-node-only diff must still run the kubernetes build. +if grep -qE '^packages/system/kubevirt-csi-node/' "$CHANGED"; then + case " $selected " in + *" packages/apps/kubernetes "*) : ;; + *) selected="$selected packages/apps/kubernetes" ;; + esac +fi + emit_json "$selected" diff --git a/hack/build-matrix_test.bats b/hack/build-matrix_test.bats index c11f2707f..05673ec25 100644 --- a/hack/build-matrix_test.bats +++ b/hack/build-matrix_test.bats @@ -139,6 +139,22 @@ load test_helper rm -f "$tmp" } +@test "kubevirt-csi-node change fans out to the kubernetes build" { + tmp=$(mktemp) + echo "packages/system/kubevirt-csi-node/templates/deploy.yaml" > "$tmp" + out=$(hack/build-matrix.sh "$tmp") + [ "$out" = '["packages/apps/kubernetes"]' ] + rm -f "$tmp" +} + +@test "kubevirt-csi-node change does not duplicate an already-selected kubernetes build" { + tmp=$(mktemp) + printf 'packages/system/kubevirt-csi-node/templates/deploy.yaml\npackages/apps/kubernetes/values.yaml\n' > "$tmp" + out=$(hack/build-matrix.sh "$tmp") + [ "$out" = '["packages/apps/kubernetes"]' ] + rm -f "$tmp" +} + @test "cozy-lib change forces the full matrix" { tmp=$(mktemp) echo "packages/library/cozy-lib/templates/_helpers.tpl" > "$tmp" diff --git a/hack/image-smoke.bats b/hack/image-smoke.bats new file mode 100644 index 000000000..d496ebf73 --- /dev/null +++ b/hack/image-smoke.bats @@ -0,0 +1,160 @@ +#!/usr/bin/env bats +# ----------------------------------------------------------------------------- +# Smoke checks for images whose runtime depends on tools their base image or a +# build stage installs: a base or tool bump can drop a package or change its +# major version with nothing failing at build time. +# +# IMAGE_SMOKE_REFS_FILE names a file with one image reference per line, the +# images built by the current CI job. A test whose image is not listed passes +# without running, so the unit lane, which builds nothing, runs no containers. +# +# Run with: IMAGE_SMOKE_REFS_FILE= hack/cozytest.sh hack/image-smoke.bats +# ----------------------------------------------------------------------------- + +load test_helper + +# Prints the first listed reference whose repository is named $1. +smoke_ref() { + [ -n "${IMAGE_SMOKE_REFS_FILE:-}" ] || return 0 + # The pipeline takes head's status, so a grep that cannot read the file + # would look like an image the job did not build. + [ -r "$IMAGE_SMOKE_REFS_FILE" ] || { echo "$IMAGE_SMOKE_REFS_FILE: cannot read the refs file" >&2; return 1; } + grep -E "/$1([:@]|\$)" "$IMAGE_SMOKE_REFS_FILE" | head -n 1 +} + +smoke_run() { + docker run --rm --network none --entrypoint sh "$1" -c "$2" +} + +# readelf and go come from the golang builder the CSI image already pins, since +# the checked images carry neither. It is pulled through mirror.gcr.io, as the +# image builds pull docker.io: anonymous Docker Hub pulls hit its rate limit. +READELF_IMAGE=$(sed -n 's|^ARG builder_image=docker.io/|mirror.gcr.io/|p' "$PWD/packages/apps/kubernetes/images/kubevirt-csi-driver/Dockerfile") + +# Fails when any of the given files, copied out of image $1, requires an x86 +# ISA level above x86-64-v2: a C object through its GNU property note, a Go +# binary through the GOAMD64 it was built with, which leaves no note. glibc +# refuses to load such an object on an older CPU and a Go binary dies at start, +# so the container is scheduled and every exec of it fails. v2 is the floor the +# AlmaLinux 9 userland already sets. A path prefixed with ? is skipped when the +# image does not have it. +isa_baseline() { + isa_img=$1 + shift + isa_dir=$(mktemp -d "${TMPDIR:-/tmp}/image-smoke-isa.XXXXXX") + isa_cid=$(docker create "$isa_img" true) || { rm -rf "$isa_dir"; return 1; } + isa_rc=0 + for isa_f in "$@"; do + case "$isa_f" in + \?*) docker cp -L "$isa_cid:${isa_f#?}" "$isa_dir/$(basename "$isa_f")" 2>/dev/null || true ;; + *) docker cp -L "$isa_cid:$isa_f" "$isa_dir/$(basename "$isa_f")" || isa_rc=1 ;; + esac + done + docker rm "$isa_cid" >/dev/null + isa_out= + if [ "$isa_rc" -eq 0 ]; then + # A file that yields neither line was not judged at all, so it fails too. + isa_out=$(docker run --rm --network none -v "$isa_dir:/x:ro" --entrypoint sh "$READELF_IMAGE" \ + -c 'command -v readelf >/dev/null && command -v go >/dev/null || exit 1 + for f in /x/*; do + l=$(readelf -n "$f" | sed -n "s|.*x86 ISA needed: *|$f: |p" + go version -m "$f" 2>/dev/null | sed -n "s|.*GOAMD64=|$f: GOAMD64=|p") + [ -n "$l" ] || { echo "$f: no ISA level recorded"; exit 1; } + echo "$l" + done') || isa_rc=1 + fi + rm -rf "$isa_dir" + printf '%s\n' "$isa_out" + [ "$isa_rc" -eq 0 ] || return 1 + if printf '%s\n' "$isa_out" | grep -E 'x86-64-v[34]|GOAMD64=v[34]'; then + return 1 + fi +} + +@test "a refs file that cannot be read fails the lookup instead of skipping" { + if IMAGE_SMOKE_REFS_FILE=/nonexistent/image-refs smoke_ref kubevirt-csi-driver; then + echo "FAIL: smoke_ref succeeded on a missing refs file" >&2 + false + fi +} + +@test "kubevirt-csi-driver carries the filesystem and NFS tools" { + ref=$(smoke_ref kubevirt-csi-driver) + [ -n "$ref" ] || return 0 + smoke_run "$ref" 'mkfs.ext4 -V && mkfs.xfs -V && mount.nfs -V' +} + +# The node service runs inside tenant VMs, whose vCPUs may not offer +# x86-64-v3. +@test "kubevirt-csi-driver runs its filesystem tools without x86-64-v3" { + ref=$(smoke_ref kubevirt-csi-driver) + [ -n "$ref" ] || return 0 + paths=$(smoke_run "$ref" 'for b in mkfs.ext4 mkfs.xfs fsck.ext4 blkid mount umount; do p=$(command -v "$b") || { echo "$b: not found" >&2; exit 1; }; readlink -f "$p"; done; readlink -f /usr/lib64/libc.so.6') + [ "$(docker image inspect --format '{{.Architecture}}' "$ref")" = amd64 ] || return 0 + # shellcheck disable=SC2086 + isa_baseline "$ref" $paths +} + +# The CSI sidecars run beside the driver in the same pods, so they are checked +# in its job; hack/build-matrix.sh runs that job for a kubevirt-csi-node change +# too. The node sidecars are required by name in the node manifest: a +# reference the grep stops matching there would otherwise go unchecked. +@test "the KubeVirt CSI sidecars run without x86-64-v3" { + ref=$(smoke_ref kubevirt-csi-driver) + [ -n "$ref" ] || return 0 + sidecars=$(grep -ohE 'registry\.k8s\.io/sig-storage/[^"[:space:]]+' \ + "$PWD/packages/apps/kubernetes/templates/csi/deploy.yaml" \ + "$PWD/packages/system/kubevirt-csi-node/templates/deploy.yaml" | sort -u) + for want in csi-node-driver-registrar livenessprobe; do + grep -qE "registry\.k8s\.io/sig-storage/$want:" "$PWD/packages/system/kubevirt-csi-node/templates/deploy.yaml" \ + || { echo "$want: no reference found in kubevirt-csi-node" >&2; return 1; } + done + for sidecar in $sidecars; do + docker pull -q --platform linux/amd64 "$sidecar" >/dev/null + entry=$(docker image inspect --format '{{index .Config.Entrypoint 0}}' "$sidecar") + isa_baseline "$sidecar" "$entry" '?/lib/x86_64-linux-gnu/libc.so.6' '?/usr/lib64/libc.so.6' + done +} + +@test "linstor-csi carries the filesystem and NFS tools and an mtab link" { + ref=$(smoke_ref linstor-csi) + [ -n "$ref" ] || return 0 + smoke_run "$ref" 'mkfs.ext4 -V && mkfs.xfs -V && mount.nfs -V && test -L /etc/mtab' +} + +@test "piraeus-server carries drbd, lvm, zfs, java and the linstor client" { + ref=$(smoke_ref piraeus-server) + [ -n "$ref" ] || return 0 + # zfs and zpool exit non-zero without the kernel module, so only presence is checked. + smoke_run "$ref" 'drbdadm --version && command -v lvm && command -v zfs && command -v zpool && java -version && linstor --version' +} + +@test "multus-cni starts and ships the CNI plugins" { + ref=$(smoke_ref multus-cni) + [ -n "$ref" ] || return 0 + smoke_run "$ref" '/usr/src/multus-cni/bin/multus-daemon --help >/dev/null 2>&1 && for p in bridge host-local macvlan portmap tuning; do test -x /cni-plugins/$p || exit 1; done' +} + +@test "harbor nginx-photon runs nginx as uid 10000" { + ref=$(smoke_ref nginx-photon) + [ -n "$ref" ] || return 0 + smoke_run "$ref" 'nginx -v && test "$(id -u nginx)" = 10000' +} + +@test "harbor trivy-adapter-photon carries rpm and trivy for scanner uid 10000" { + ref=$(smoke_ref trivy-adapter-photon) + [ -n "$ref" ] || return 0 + smoke_run "$ref" 'rpm --version && trivy --version && test "$(id -u scanner)" = 10000' +} + +@test "harbor-core runs as harbor uid 10000" { + ref=$(smoke_ref harbor-core) + [ -n "$ref" ] || return 0 + smoke_run "$ref" 'test "$(id -u harbor)" = 10000 && test -x /harbor/harbor_core' +} + +@test "redis-backup carries redis 8 or newer and curl" { + ref=$(smoke_ref redis-backup) + [ -n "$ref" ] || return 0 + smoke_run "$ref" 'major=$(redis-server --version | sed -n "s/^Redis server v=\([0-9]*\)\..*/\1/p") && test "${major:-0}" -ge 8 && curl --version' +} diff --git a/hack/lib/image-refs.sh b/hack/lib/image-refs.sh index 791067f05..4b8732388 100644 --- a/hack/lib/image-refs.sh +++ b/hack/lib/image-refs.sh @@ -3,8 +3,9 @@ # # Sourced by hack/promote-rewrite-tags.sh, hack/promote-retag.sh, # hack/nightly-mirror.sh, hack/verify-promoted-packages.sh, -# hack/stitch-multiarch.sh, hack/verify-multiarch.sh and -# hack/retention-packages.sh. It exists because the +# hack/stitch-multiarch.sh, hack/verify-multiarch.sh, +# hack/retention-packages.sh and the image-reference record and smoke steps +# of the PR build job. It exists because the # first three call sites each grew their own idea of where a ref can live, and # drifted: promote-retag and nightly-mirror scanned only the depth-2 # values.yaml, while the promote workflow's tag rewrite scanned those plus