From fd15ad282eb72d8ad6187ff92eb131c99b018fbd Mon Sep 17 00:00:00 2001 From: Maxim Belyy Date: Sat, 14 Nov 2026 19:51:20 +0700 Subject: [PATCH 1/5] fix(ci): take the main ref for a bare image ref in a drifted file A file whose configuration drifted from the cozystack-packages:main artifact keeps its committed refs. For a digest-pinned ref that runs an older image; for the bare : a new image is first committed with, it names a tag that was never pushed, so the component cannot pull. The merge commit CI tests is fixed when the run starts, so a PR whose merge commit predates a config change to observability-proxy's values.yaml installed observability-proxy:v0.0.0 and failed E2E. In a drifted values file, a bare ref under an image key now takes the artifact line with the same key, the same image name and a digest, when exactly one exists. The rest of the file stays as the PR has it. Assisted-by: LLM Signed-off-by: Maxim Belyy --- hack/overlay-main-images.sh | 89 ++++++++++++++++++++++++++++-- hack/overlay-main-images_test.bats | 85 ++++++++++++++++++++++++++++ 2 files changed, 170 insertions(+), 4 deletions(-) diff --git a/hack/overlay-main-images.sh b/hack/overlay-main-images.sh index 2c13e34e1..b35be70ba 100755 --- a/hack/overlay-main-images.sh +++ b/hack/overlay-main-images.sh @@ -42,9 +42,13 @@ # a change: `yq -i`, which every `make image` uses to stamp the ref, drops one. # If any OTHER line differs — the PR branched from a # main whose config for that file differs from the artifact's base — the file is -# left on its committed ref (safe degradation, logged, never fatal). CI checks -# out the pull_request merge commit (current main + PR), so for an unbuilt file -# the config already matches current main and only ref lines differ. +# left on its committed ref (safe degradation, logged, never fatal), except +# that a bare digest-less ref in it, which no registry serves, still takes the +# artifact's ref (see replace_bare_refs). CI checks out the pull_request merge +# commit (main + PR), so for an unbuilt file the config matches main as of +# that merge commit. It is fixed when the run starts, so a rerun, or a PR not +# pushed to since, can be older than the artifact and drift on files it never +# touched. # # Usage: hack/overlay-main-images.sh [touched-pkg-dirs] # = extracted cozystack-packages:main tree; its root holds @@ -123,8 +127,76 @@ img_line='(@sha256:|^[[:space:]]*(- )?(image|repository|registry|tag|digest):|^[ # match branch for this same shape and for this same reason. bare_ref_line='^[^[:space:]#][^[:space:]]*/[^[:space:]]+$' +# A drifted file keeps its committed refs. For a ref pinned by digest that is a +# pullable older image, which is the safe answer. A bare `:` with no +# digest is different: it is the first commit of a new image +# (docs/agents/image-refs.md), its tag was never pushed, and keeping it makes the +# component fail to pull instead of run an older image. The merge commit CI +# checks out is fixed when the run starts, so a PR whose merge commit predates a +# config change to such a file drifts on it, and every new image is exposed +# until a release stamps its ref (#5328). +# +# replace_bare_refs rewrites, in only, each line whose value is +# such a bare ref under an image key, taking the line from that has the +# same key and indentation, the same image name and an @sha256: digest. Exactly +# one such line must exist; two are ambiguous and none means build-main did not +# stamp it, so there is nothing more pullable to take. Every other line of +# stays as the PR has it. The current image may expect configuration the PR's +# older chart lacks, but the committed ref cannot run at all. Prints the number +# of lines replaced. +replace_bare_refs() { + tmp="$1.overlay-bare.$$" + n=$(awk -v newf="$2" -v out="$tmp" ' + function key_prefix(s) { + if (match(s, /^[[:space:]]*(- )?([A-Za-z]+[Ii]mage|image):[[:space:]]*/)) return substr(s, 1, RLENGTH) + return "" + } + function ref_value(s, p, v) { + v = substr(s, length(p) + 1) + sub(/[[:space:]]+$/, "", v) + gsub(/"/, "", v) + return v + } + function image_name(v) { + sub(/@.*/, "", v) + sub(/.*\//, "", v) + sub(/:.*/, "", v) + return v + } + BEGIN { + while ((getline l < newf) > 0) { + p = key_prefix(l) + if (p == "" || index(l, "@sha256:") == 0) continue + k = p SUBSEP image_name(ref_value(l, p)) + cnt[k]++ + line[k] = l + } + close(newf) + } + { + p = key_prefix($0) + if (p != "") { + v = ref_value($0, p) + if (v ~ /^[^@[:space:]]+\/[^@[:space:]\/]+:[^@[:space:]\/]+$/) { + k = p SUBSEP image_name(v) + if (cnt[k] == 1) { print line[k] > out; r++; next } + } + } + print > out + } + END { close(out); print r + 0 } + ' "$1") + if [ "$n" -gt 0 ]; then + mv "$tmp" "$1" + else + rm -f "$tmp" + fi + echo "$n" +} + overlaid=0 same=0 +bare=0 skipped=0 drift=0 drift_files="" @@ -164,6 +236,15 @@ for new in $(find "$MAINPKGS" -type d -name charts -prune -o \ | grep -vE '^[[:space:]]*$' | grep -qvE "$line_pat"; then echo "drift (non-ref change) in $cur -> keeping committed ref" drift=$((drift + 1)) + case "$cur" in + *.yaml) + r=$(replace_bare_refs "$cur" "$new") || r=0 + if [ "$r" -gt 0 ]; then + echo "bare ref in $cur -> current-main ($r line(s))" + bare=$((bare + r)) + fi + ;; + esac # Name the PACKAGE, not the directory the file happens to sit in: an # images/*.tag drift would otherwise read as "/images", which is not # something anyone can go and look at. @@ -181,7 +262,7 @@ for new in $(find "$MAINPKGS" -type d -name charts -prune -o \ fi done -echo "Overlay current-main images: overlaid=$overlaid same=$same skipped(rebuilt/owned/edited)=$skipped drift=$drift failed=$failed" +echo "Overlay current-main images: overlaid=$overlaid same=$same skipped(rebuilt/owned/edited)=$skipped drift=$drift bare-refs=$bare failed=$failed" # Keeping the committed ref is the safe answer to a config that does not match # the artifact, but it is not a neutral one: that component then runs its last diff --git a/hack/overlay-main-images_test.bats b/hack/overlay-main-images_test.bats index 7cff2d842..7b22b00ff 100644 --- a/hack/overlay-main-images_test.bats +++ b/hack/overlay-main-images_test.bats @@ -502,3 +502,88 @@ load test_helper cd "$root" rm -rf "$w" } + +# A drifted file keeps its committed refs, and for a digest-pinned ref that is +# a pullable older image. A bare : is the first commit of a new image +# (docs/agents/image-refs.md) and names a tag no registry serves, so keeping it +# turns drift into ImagePullBackOff. That hit every fork PR whose merge commit +# predated a config change to observability-proxy's values.yaml (#5328). Only +# such a bare line takes the artifact's ref; the rest of the file stays as the +# PR has it. +@test "a drifted file still takes the artifact ref for a bare (digest-less) ref line" { + root=$(pwd) + w=$(mktemp -d) + mkdir -p "$w/packages/system/proxy" "$w/main/system/proxy" + printf 'proxy:\n image: "ghcr.io/cozystack/cozystack/proxy:v0.0.0"\n lookback: 1h\n' \ + > "$w/packages/system/proxy/values.yaml" + printf 'proxy:\n image: "iad.ocir.io/x/cozystack/proxy:main@sha256:bbbb"\n lookback: 1h\n prefix: cozy-\n' \ + > "$w/main/system/proxy/values.yaml" + ( cd "$w" && "$root/hack/overlay-main-images.sh" main '[]' ) > "$w/out.txt" + grep -q 'drift (non-ref change) in packages/system/proxy/values.yaml' "$w/out.txt" + grep -q ' image: "iad.ocir.io/x/cozystack/proxy:main@sha256:bbbb"' "$w/packages/system/proxy/values.yaml" + # The config stays the PR's: the new key is not introduced. + if grep -q 'prefix:' "$w/packages/system/proxy/values.yaml"; then + echo "FAIL: a config line was taken from the artifact" >&2; exit 1 + fi + grep -q 'bare ref in packages/system/proxy/values.yaml -> current-main' "$w/out.txt" + grep -q 'bare-refs=1' "$w/out.txt" + rm -rf "$w" +} + +@test "a drifted file replaces only its bare ref, not a digest-pinned sibling" { + root=$(pwd) + w=$(mktemp -d) + mkdir -p "$w/packages/system/pair" "$w/main/system/pair" + printf 'tuning: old\nimage: ghcr.io/cozystack/cozystack/pair:v1.5.0@sha256:aaaa\nclientImage: ghcr.io/cozystack/cozystack/pair-client:latest\n' \ + > "$w/packages/system/pair/values.yaml" + printf 'tuning: new\nimage: iad.ocir.io/x/cozystack/pair:main@sha256:bbbb\nclientImage: iad.ocir.io/x/cozystack/pair-client:main@sha256:cccc\n' \ + > "$w/main/system/pair/values.yaml" + ( cd "$w" && "$root/hack/overlay-main-images.sh" main '[]' ) > "$w/out.txt" + grep -q '^image: ghcr.io/cozystack/cozystack/pair:v1.5.0@sha256:aaaa$' "$w/packages/system/pair/values.yaml" + grep -q '^clientImage: iad.ocir.io/x/cozystack/pair-client:main@sha256:cccc$' "$w/packages/system/pair/values.yaml" + grep -q '^tuning: old$' "$w/packages/system/pair/values.yaml" + rm -rf "$w" +} + +@test "a bare ref is not replaced by an artifact line for a different image" { + # The same key can hold another image in the artifact, e.g. after a rename. + # Matching on the key alone would swap in an image the chart never asked for. + root=$(pwd) + w=$(mktemp -d) + mkdir -p "$w/packages/system/ren" "$w/main/system/ren" + printf 'tuning: old\nimage: ghcr.io/cozystack/cozystack/old-name:v0.0.0\n' > "$w/packages/system/ren/values.yaml" + printf 'tuning: new\nimage: iad.ocir.io/x/cozystack/new-name:main@sha256:bbbb\n' > "$w/main/system/ren/values.yaml" + ( cd "$w" && "$root/hack/overlay-main-images.sh" main '[]' ) > "$w/out.txt" + grep -q '^image: ghcr.io/cozystack/cozystack/old-name:v0.0.0$' "$w/packages/system/ren/values.yaml" + grep -q 'bare-refs=0' "$w/out.txt" + rm -rf "$w" +} + +@test "a bare ref is kept when the artifact carries no digest for it either" { + # A ref build-main does not stamp (third-party, or not built yet) has no + # digest in the artifact, so there is nothing more pullable to take. + root=$(pwd) + w=$(mktemp -d) + mkdir -p "$w/packages/system/tp" "$w/main/system/tp" + printf 'tuning: old\nimage: docker.io/library/busybox:1.37.0\n' > "$w/packages/system/tp/values.yaml" + printf 'tuning: new\nimage: docker.io/library/busybox:1.38.0\n' > "$w/main/system/tp/values.yaml" + ( cd "$w" && "$root/hack/overlay-main-images.sh" main '[]' ) > "$w/out.txt" + grep -q '^image: docker.io/library/busybox:1.37.0$' "$w/packages/system/tp/values.yaml" + grep -q 'bare-refs=0' "$w/out.txt" + rm -rf "$w" +} + +@test "a bare ref is kept when the artifact has two candidate lines for it" { + # Two lines with the same key and image name (e.g. one per component in a + # list) cannot be told apart, so neither is taken. + root=$(pwd) + w=$(mktemp -d) + mkdir -p "$w/packages/system/dup" "$w/main/system/dup" + printf 'tuning: old\na:\n image: ghcr.io/cozystack/cozystack/dup:v0.0.0\n' > "$w/packages/system/dup/values.yaml" + printf 'tuning: new\na:\n image: iad.ocir.io/x/cozystack/dup:main@sha256:bbbb\nb:\n image: iad.ocir.io/x/cozystack/dup:main@sha256:cccc\n' \ + > "$w/main/system/dup/values.yaml" + ( cd "$w" && "$root/hack/overlay-main-images.sh" main '[]' ) > "$w/out.txt" + grep -q '^ image: ghcr.io/cozystack/cozystack/dup:v0.0.0$' "$w/packages/system/dup/values.yaml" + grep -q 'bare-refs=0' "$w/out.txt" + rm -rf "$w" +} From 54b76fcdf1ff61402a695eb790381dbf1428e46f Mon Sep 17 00:00:00 2001 From: Maxim Belyy Date: Sat, 14 Nov 2026 19:51:20 +0700 Subject: [PATCH 2/5] docs(agents): say how PR CI stamps a bare ref in a drifted file Assisted-by: LLM Signed-off-by: Maxim Belyy --- docs/agents/image-refs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/agents/image-refs.md b/docs/agents/image-refs.md index 9c9bb3ca6..764b560c2 100644 --- a/docs/agents/image-refs.md +++ b/docs/agents/image-refs.md @@ -42,7 +42,7 @@ If a package stamps a reference somewhere the two globs do not cover, add that f ## Invariants -- **Every first-party reference is tag+digest.** A bare tag with no digest is not reproducible and must not be committed. The one exception is the first commit of a new image: it has no digest until it is built, so it lands as a bare `:` that the producing `make image` replaces. PR CI and release preparation both stamp it before anything pulls it, but a tree applied without a build pulls a tag that does not exist. +- **Every first-party reference is tag+digest.** A bare tag with no digest is not reproducible and must not be committed. The one exception is the first commit of a new image: it has no digest until it is built, so it lands as a bare `:` that the producing `make image` replaces. PR CI and release preparation both stamp it before anything pulls it, but a tree applied without a build pulls a tag that does not exist. In PR CI the stamp comes from `hack/overlay-main-images.sh` when the PR does not rebuild the package, and it takes a bare ref even from a file whose configuration drifted from the artifact, because keeping a tag that was never pushed cannot install. - **Exactly one producer stamps each reference.** A reference no `Makefile` writes will never be refreshed by anything, including release preparation. - **A package that stamps a reference is in the root `Makefile` `build:` list.** `build-matrix.sh`, the main build and release preparation all iterate that one list, so a package outside it is never rebuilt by any path and its pin freezes permanently. `hack/build-matrix_test.bats` computes the set of packages whose Makefile runs `docker buildx build` and fails on any the list misses. - **Every consumer of references reads all three storage shapes**, via `hack/lib/image-refs.sh`. From 854a3f40e40dbcd8b2bdb8c0c8beec6e590b96b5 Mon Sep 17 00:00:00 2001 From: Maxim Belyy Date: Sat, 14 Nov 2026 19:54:31 +0700 Subject: [PATCH 3/5] fix(ci): limit the drifted-file bare-ref overlay to first-party images Main can bump a third-party image together with its config, so a PR behind it must keep the committed pair rather than take main's image alone. Only ghcr.io/cozystack/cozystack refs, matched against an artifact line for the same first-party image, are replaced now. State the trade-off for first-party refs that do exist, and name the packages that took main's images in their own annotation. Assisted-by: LLM Signed-off-by: Maxim Belyy --- docs/agents/image-refs.md | 2 +- hack/overlay-main-images.sh | 63 +++++++++++++++++++++--------- hack/overlay-main-images_test.bats | 45 +++++++++++++++++---- 3 files changed, 83 insertions(+), 27 deletions(-) diff --git a/docs/agents/image-refs.md b/docs/agents/image-refs.md index 764b560c2..191bc62c2 100644 --- a/docs/agents/image-refs.md +++ b/docs/agents/image-refs.md @@ -42,7 +42,7 @@ If a package stamps a reference somewhere the two globs do not cover, add that f ## Invariants -- **Every first-party reference is tag+digest.** A bare tag with no digest is not reproducible and must not be committed. The one exception is the first commit of a new image: it has no digest until it is built, so it lands as a bare `:` that the producing `make image` replaces. PR CI and release preparation both stamp it before anything pulls it, but a tree applied without a build pulls a tag that does not exist. In PR CI the stamp comes from `hack/overlay-main-images.sh` when the PR does not rebuild the package, and it takes a bare ref even from a file whose configuration drifted from the artifact, because keeping a tag that was never pushed cannot install. +- **Every first-party reference is tag+digest.** A bare tag with no digest is not reproducible and must not be committed. The one exception is the first commit of a new image: it has no digest until it is built, so it lands as a bare `:` that the producing `make image` replaces. PR CI and release preparation both stamp it before anything pulls it, but a tree applied without a build pulls a tag that does not exist. In PR CI the stamp comes from `hack/overlay-main-images.sh` when the PR does not rebuild the package, and it takes a bare first-party ref even from a file whose configuration drifted from the artifact, because a tag that was never pushed cannot install. The cost is that any other digest-less first-party ref in such a file also runs main's image with the PR's older config; third-party refs in it keep their committed pair. - **Exactly one producer stamps each reference.** A reference no `Makefile` writes will never be refreshed by anything, including release preparation. - **A package that stamps a reference is in the root `Makefile` `build:` list.** `build-matrix.sh`, the main build and release preparation all iterate that one list, so a package outside it is never rebuilt by any path and its pin freezes permanently. `hack/build-matrix_test.bats` computes the set of packages whose Makefile runs `docker buildx build` and fails on any the list misses. - **Every consumer of references reads all three storage shapes**, via `hack/lib/image-refs.sh`. diff --git a/hack/overlay-main-images.sh b/hack/overlay-main-images.sh index b35be70ba..adb6b8638 100755 --- a/hack/overlay-main-images.sh +++ b/hack/overlay-main-images.sh @@ -43,7 +43,7 @@ # If any OTHER line differs — the PR branched from a # main whose config for that file differs from the artifact's base — the file is # left on its committed ref (safe degradation, logged, never fatal), except -# that a bare digest-less ref in it, which no registry serves, still takes the +# that a bare first-party ref in it, one with no digest, still takes the # artifact's ref (see replace_bare_refs). CI checks out the pull_request merge # commit (main + PR), so for an unbuilt file the config matches main as of # that merge commit. It is fixed when the run starts, so a rerun, or a PR not @@ -127,23 +127,33 @@ img_line='(@sha256:|^[[:space:]]*(- )?(image|repository|registry|tag|digest):|^[ # match branch for this same shape and for this same reason. bare_ref_line='^[^[:space:]#][^[:space:]]*/[^[:space:]]+$' -# A drifted file keeps its committed refs. For a ref pinned by digest that is a -# pullable older image, which is the safe answer. A bare `:` with no -# digest is different: it is the first commit of a new image -# (docs/agents/image-refs.md), its tag was never pushed, and keeping it makes the -# component fail to pull instead of run an older image. The merge commit CI -# checks out is fixed when the run starts, so a PR whose merge commit predates a -# config change to such a file drifts on it, and every new image is exposed -# until a release stamps its ref (#5328). +# A drifted file keeps its committed refs. For a ref pinned by digest that is an +# older image which pulls and matches the PR's config, so it is the safe answer. +# A first-party ref with no digest is not: docs/agents/image-refs.md forbids it +# except as the first commit of a new image, whose tag was never pushed, and +# keeping that makes the component fail to pull. The merge commit CI checks out +# is fixed when the run starts, so a PR whose merge commit predates a config +# change to such a file drifts on it, and a new image fails every such PR until +# a release stamps its ref (#5328). # # replace_bare_refs rewrites, in only, each line whose value is -# such a bare ref under an image key, taking the line from that has the -# same key and indentation, the same image name and an @sha256: digest. Exactly -# one such line must exist; two are ambiguous and none means build-main did not -# stamp it, so there is nothing more pullable to take. Every other line of -# stays as the PR has it. The current image may expect configuration the PR's -# older chart lacks, but the committed ref cannot run at all. Prints the number -# of lines replaced. +# a bare first-party ref (ghcr.io/cozystack/cozystack/:, no digest) +# under an image key, taking the line from that has the same key and +# indentation, an @sha256: digest and the same first-party image, i.e. a path +# ending in /cozystack/. Exactly one such line must exist; two are +# ambiguous and none means build-main did not stamp it. Every other line of +# stays as the PR has it. +# +# The trade-off is deliberate. A bare first-party ref that does exist (an older +# release tag or a moving `latest`, #4264) also takes main's image, so it runs +# with the PR's older config rather than as committed. It is the same component +# built from the tree, and a digest-less ref is not reproducible anyway; the +# alternative is a ref that may not pull at all. Third-party refs are never +# touched: main can bump one alongside its config, and the committed pair is +# the one known to work together. The split shape (`repository:` + `tag:` +# without `digest:`) is not handled; no package uses it for a bare ref today. +# +# Prints the number of lines replaced. replace_bare_refs() { tmp="$1.overlay-bare.$$" n=$(awk -v newf="$2" -v out="$tmp" ' @@ -167,7 +177,10 @@ replace_bare_refs() { while ((getline l < newf) > 0) { p = key_prefix(l) if (p == "" || index(l, "@sha256:") == 0) continue - k = p SUBSEP image_name(ref_value(l, p)) + v = ref_value(l, p) + name = image_name(v) + if (index(v, "/cozystack/" name ":") == 0) continue + k = p SUBSEP name cnt[k]++ line[k] = l } @@ -177,7 +190,7 @@ replace_bare_refs() { p = key_prefix($0) if (p != "") { v = ref_value($0, p) - if (v ~ /^[^@[:space:]]+\/[^@[:space:]\/]+:[^@[:space:]\/]+$/) { + if (v ~ /^ghcr\.io\/cozystack\/cozystack\/[^@[:space:]\/]+:[^@[:space:]\/]+$/) { k = p SUBSEP image_name(v) if (cnt[k] == 1) { print line[k] > out; r++; next } } @@ -197,6 +210,7 @@ replace_bare_refs() { overlaid=0 same=0 bare=0 +bare_files="" skipped=0 drift=0 drift_files="" @@ -240,8 +254,10 @@ for new in $(find "$MAINPKGS" -type d -name charts -prune -o \ *.yaml) r=$(replace_bare_refs "$cur" "$new") || r=0 if [ "$r" -gt 0 ]; then - echo "bare ref in $cur -> current-main ($r line(s))" + echo "bare first-party ref in $cur -> current-main ($r line(s)); its other refs stay committed" bare=$((bare + r)) + d=$(dirname "${cur#packages/}") + bare_files="$bare_files $d" fi ;; esac @@ -277,3 +293,12 @@ if [ "$drift" -gt 0 ]; then "$(printf '%s\n' "$drift_pkgs" | wc -l | tr -d ' ')" \ "$(printf '%s' "$drift_pkgs" | tr '\n' ' ' | sed 's/ $//')" fi +# Among those, a package whose bare first-party ref took main's image runs that +# image with the PR's older config, not its committed ref; name it separately +# so the warning above does not read as the whole story for it. +if [ "$bare" -gt 0 ]; then + bare_pkgs=$(echo "$bare_files" | tr ' ' '\n' | grep -v '^$' | sort -u) + printf '::warning title=Image overlay bare refs::%s drifted package(s) took current-main images for bare first-party refs, with the config committed in this PR: %s\n' \ + "$(printf '%s\n' "$bare_pkgs" | wc -l | tr -d ' ')" \ + "$(printf '%s' "$bare_pkgs" | tr '\n' ' ' | sed 's/ $//')" +fi diff --git a/hack/overlay-main-images_test.bats b/hack/overlay-main-images_test.bats index 7b22b00ff..28f04526f 100644 --- a/hack/overlay-main-images_test.bats +++ b/hack/overlay-main-images_test.bats @@ -525,7 +525,8 @@ load test_helper if grep -q 'prefix:' "$w/packages/system/proxy/values.yaml"; then echo "FAIL: a config line was taken from the artifact" >&2; exit 1 fi - grep -q 'bare ref in packages/system/proxy/values.yaml -> current-main' "$w/out.txt" + grep -q 'bare first-party ref in packages/system/proxy/values.yaml -> current-main' "$w/out.txt" + grep -q '::warning title=Image overlay bare refs::1 drifted package(s) .*: system/proxy$' "$w/out.txt" grep -q 'bare-refs=1' "$w/out.txt" rm -rf "$w" } @@ -560,15 +561,45 @@ load test_helper } @test "a bare ref is kept when the artifact carries no digest for it either" { - # A ref build-main does not stamp (third-party, or not built yet) has no - # digest in the artifact, so there is nothing more pullable to take. + # A ref build-main does not stamp has no digest in the artifact, so there is + # nothing more pullable to take. root=$(pwd) w=$(mktemp -d) - mkdir -p "$w/packages/system/tp" "$w/main/system/tp" - printf 'tuning: old\nimage: docker.io/library/busybox:1.37.0\n' > "$w/packages/system/tp/values.yaml" - printf 'tuning: new\nimage: docker.io/library/busybox:1.38.0\n' > "$w/main/system/tp/values.yaml" + mkdir -p "$w/packages/system/nd" "$w/main/system/nd" + printf 'tuning: old\nimage: ghcr.io/cozystack/cozystack/nd:v0.0.0\n' > "$w/packages/system/nd/values.yaml" + printf 'tuning: new\nimage: iad.ocir.io/x/cozystack/nd:main\n' > "$w/main/system/nd/values.yaml" ( cd "$w" && "$root/hack/overlay-main-images.sh" main '[]' ) > "$w/out.txt" - grep -q '^image: docker.io/library/busybox:1.37.0$' "$w/packages/system/tp/values.yaml" + grep -q '^image: ghcr.io/cozystack/cozystack/nd:v0.0.0$' "$w/packages/system/nd/values.yaml" + grep -q 'bare-refs=0' "$w/out.txt" + rm -rf "$w" +} + +@test "a bare third-party ref in a drifted file is kept even when main pins one" { + # Main can bump a third-party image together with its config in one file. A + # PR behind it has the older pair, which is the one known to work together; + # taking main's image alone would run it with the config it replaced. + root=$(pwd) + w=$(mktemp -d) + mkdir -p "$w/packages/system/kc" "$w/main/system/kc" + printf 'features: old\nimage: quay.io/keycloak/keycloak:26.6.3\n' > "$w/packages/system/kc/values.yaml" + printf 'features: new\nimage: quay.io/keycloak/keycloak:27.0.0@sha256:abcd\n' > "$w/main/system/kc/values.yaml" + ( cd "$w" && "$root/hack/overlay-main-images.sh" main '[]' ) > "$w/out.txt" + grep -q '^image: quay.io/keycloak/keycloak:26.6.3$' "$w/packages/system/kc/values.yaml" + grep -q 'bare-refs=0' "$w/out.txt" + if grep -q 'Image overlay bare refs' "$w/out.txt"; then + echo "FAIL: a bare-refs annotation with nothing replaced" >&2; exit 1 + fi + rm -rf "$w" +} + +@test "a bare first-party ref is not replaced by a third-party image of the same name" { + root=$(pwd) + w=$(mktemp -d) + mkdir -p "$w/packages/system/same" "$w/main/system/same" + printf 'tuning: old\nimage: ghcr.io/cozystack/cozystack/redis:v0.0.0\n' > "$w/packages/system/same/values.yaml" + printf 'tuning: new\nimage: docker.io/library/redis:8.0@sha256:abcd\n' > "$w/main/system/same/values.yaml" + ( cd "$w" && "$root/hack/overlay-main-images.sh" main '[]' ) > "$w/out.txt" + grep -q '^image: ghcr.io/cozystack/cozystack/redis:v0.0.0$' "$w/packages/system/same/values.yaml" grep -q 'bare-refs=0' "$w/out.txt" rm -rf "$w" } From 2468b309eea6e6b8e6ec3bbd2ea89b3285fb9b8c Mon Sep 17 00:00:00 2001 From: Maxim Belyy Date: Sat, 14 Nov 2026 19:58:33 +0700 Subject: [PATCH 4/5] fix(ci): count a failed bare-ref rewrite and word the drift log by result A rewrite that cannot replace the file now counts as failed and leaves no temporary file behind, instead of reporting lines as replaced. A drifted file whose bare refs took main's images no longer logs that it kept its committed ref. Assisted-by: LLM Signed-off-by: Maxim Belyy --- hack/overlay-main-images.sh | 36 +++++++++++++++++++----------- hack/overlay-main-images_test.bats | 20 +++++++++++++++++ 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/hack/overlay-main-images.sh b/hack/overlay-main-images.sh index adb6b8638..2e6c4a1c4 100755 --- a/hack/overlay-main-images.sh +++ b/hack/overlay-main-images.sh @@ -151,7 +151,10 @@ bare_ref_line='^[^[:space:]#][^[:space:]]*/[^[:space:]]+$' # alternative is a ref that may not pull at all. Third-party refs are never # touched: main can bump one alongside its config, and the committed pair is # the one known to work together. The split shape (`repository:` + `tag:` -# without `digest:`) is not handled; no package uses it for a bare ref today. +# without `digest:`) is not handled, nor is a single-quoted value or one +# followed by a `# comment`; such a line stays as committed, and no package +# writes a bare first-party ref that way today. Prints nothing and fails when +# the file could not be rewritten, so the caller counts it as failed. # # Prints the number of lines replaced. replace_bare_refs() { @@ -199,11 +202,11 @@ replace_bare_refs() { } END { close(out); print r + 0 } ' "$1") - if [ "$n" -gt 0 ]; then - mv "$tmp" "$1" - else - rm -f "$tmp" - fi + case "$n" in + ''|*[!0-9]*) rm -f "$tmp"; return 1 ;; + 0) rm -f "$tmp" ;; + *) mv "$tmp" "$1" || { rm -f "$tmp"; return 1; } ;; + esac echo "$n" } @@ -248,19 +251,26 @@ for new in $(find "$MAINPKGS" -type d -name charts -prune -o \ if diff "$cur" "$new" | sed -n 's/^[<>] //p' \ | grep -vE '^[[:space:]]*$' | grep -qvE "$line_pat"; then - echo "drift (non-ref change) in $cur -> keeping committed ref" drift=$((drift + 1)) + r=0 case "$cur" in *.yaml) - r=$(replace_bare_refs "$cur" "$new") || r=0 - if [ "$r" -gt 0 ]; then - echo "bare first-party ref in $cur -> current-main ($r line(s)); its other refs stay committed" - bare=$((bare + r)) - d=$(dirname "${cur#packages/}") - bare_files="$bare_files $d" + if ! r=$(replace_bare_refs "$cur" "$new"); then + echo "WARN: rewriting bare refs failed for $cur (keeping committed refs)" + failed=$((failed + 1)) + r=0 fi ;; esac + if [ "$r" -eq 0 ]; then + echo "drift (non-ref change) in $cur -> keeping committed ref" + else + echo "drift (non-ref change) in $cur -> keeping committed refs except bare first-party ones" + echo "bare first-party ref in $cur -> current-main ($r line(s)); its other refs stay committed" + bare=$((bare + r)) + d=$(dirname "${cur#packages/}") + bare_files="$bare_files $d" + fi # Name the PACKAGE, not the directory the file happens to sit in: an # images/*.tag drift would otherwise read as "/images", which is not # something anyone can go and look at. diff --git a/hack/overlay-main-images_test.bats b/hack/overlay-main-images_test.bats index 28f04526f..4059f9ea1 100644 --- a/hack/overlay-main-images_test.bats +++ b/hack/overlay-main-images_test.bats @@ -618,3 +618,23 @@ load test_helper grep -q 'bare-refs=0' "$w/out.txt" rm -rf "$w" } + +@test "a bare-ref rewrite that cannot write the file is counted as failed, not replaced" { + # Mode bits do not stop root from writing, and the POSIX runner has no + # `skip`, so there is nothing to observe as root. + if [ "$(id -u)" -eq 0 ]; then return 0; fi + root=$(pwd) + w=$(mktemp -d) + mkdir -p "$w/packages/system/ro" "$w/main/system/ro" + printf 'tuning: old\nimage: ghcr.io/cozystack/cozystack/ro:v0.0.0\n' > "$w/packages/system/ro/values.yaml" + printf 'tuning: new\nimage: iad.ocir.io/x/cozystack/ro:main@sha256:bbbb\n' > "$w/main/system/ro/values.yaml" + chmod a-w "$w/packages/system/ro" + ( cd "$w" && "$root/hack/overlay-main-images.sh" main '[]' ) > "$w/out.txt" 2>&1 + chmod u+w "$w/packages/system/ro" + grep -q '^image: ghcr.io/cozystack/cozystack/ro:v0.0.0$' "$w/packages/system/ro/values.yaml" + grep -q 'bare-refs=0 failed=1' "$w/out.txt" + if ls "$w/packages/system/ro" | grep -q 'overlay-bare'; then + echo "FAIL: temp file left behind" >&2; exit 1 + fi + rm -rf "$w" +} From 1597bb609842d961d7d01cf3803dd0ae96f0cb9f Mon Sep 17 00:00:00 2001 From: Maxim Belyy Date: Sat, 14 Nov 2026 20:02:29 +0700 Subject: [PATCH 5/5] fix(ci): keep the drift annotation true when bare refs took main's images Assisted-by: LLM Signed-off-by: Maxim Belyy --- hack/overlay-main-images.sh | 10 +++++++--- hack/overlay-main-images_test.bats | 1 + 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/hack/overlay-main-images.sh b/hack/overlay-main-images.sh index 2e6c4a1c4..6e8feafd2 100755 --- a/hack/overlay-main-images.sh +++ b/hack/overlay-main-images.sh @@ -141,7 +141,9 @@ bare_ref_line='^[^[:space:]#][^[:space:]]*/[^[:space:]]+$' # under an image key, taking the line from that has the same key and # indentation, an @sha256: digest and the same first-party image, i.e. a path # ending in /cozystack/. Exactly one such line must exist; two are -# ambiguous and none means build-main did not stamp it. Every other line of +# ambiguous, and none means build-main did not stamp it, or the key moved +# (another indentation or parent) or the image was renamed on main since the +# PR's merge commit. Every other line of # stays as the PR has it. # # The trade-off is deliberate. A bare first-party ref that does exist (an older @@ -299,8 +301,10 @@ echo "Overlay current-main images: overlaid=$overlaid same=$same skipped(rebuilt # cost three unrelated PRs, one round each. if [ "$drift" -gt 0 ]; then drift_pkgs=$(echo "$drift_files" | tr ' ' '\n' | grep -v '^$' | sort -u) - printf '::warning title=Image overlay drift::%s package(s) kept committed release refs, so they run release images in this lane rather than current main: %s\n' \ - "$(printf '%s\n' "$drift_pkgs" | wc -l | tr -d ' ')" \ + except="" + [ "$bare" -eq 0 ] || except=" (bare first-party refs excepted, see Image overlay bare refs)" + printf '::warning title=Image overlay drift::%s package(s) kept committed release refs%s, so they run release images in this lane rather than current main: %s\n' \ + "$(printf '%s\n' "$drift_pkgs" | wc -l | tr -d ' ')" "$except" \ "$(printf '%s' "$drift_pkgs" | tr '\n' ' ' | sed 's/ $//')" fi # Among those, a package whose bare first-party ref took main's image runs that diff --git a/hack/overlay-main-images_test.bats b/hack/overlay-main-images_test.bats index 4059f9ea1..68438ffa5 100644 --- a/hack/overlay-main-images_test.bats +++ b/hack/overlay-main-images_test.bats @@ -527,6 +527,7 @@ load test_helper fi grep -q 'bare first-party ref in packages/system/proxy/values.yaml -> current-main' "$w/out.txt" grep -q '::warning title=Image overlay bare refs::1 drifted package(s) .*: system/proxy$' "$w/out.txt" + grep -q '::warning title=Image overlay drift::1 package(s) kept committed release refs (bare first-party refs excepted, see Image overlay bare refs), .*: system/proxy$' "$w/out.txt" grep -q 'bare-refs=1' "$w/out.txt" rm -rf "$w" }