Skip to content

Commit 0add10a

Browse files
committed
fix(cdi): stop make update from deleting first-party templates
The update recipes in kubevirt-cdi and kubevirt-cdi-operator opened with `rm -rf templates` and then refetched one upstream manifest each. kubevirt-cdi keeps two more files in templates/ -- the upload-proxy Ingress and TLSRoute -- that are written by hand and never refetched, so the next CDI bump would drop upload-proxy exposure from the chart while `make update` still exited 0. The clean slate bought nothing either way: wget -O truncates the file it writes. Both recipes now create the directory with `mkdir -p` and leave the rest of it alone, which is what kubevirt-operator moved to when it was last bumped. kubevirt-cdi-operator has nothing local to lose today; it changes so the three recipes read the same way, and its one template is rewritten on every run either way. What it gives up is the wipe's incidental cleanup: a .rej from a failed patch run now stays put. The bats case scans the package Makefiles and fails when a recipe that clears its own templates/ leaves behind a tracked file it never names. A removal reaching into another package is outside its range, and the header records the one instance of that in the tree. Assisted-by: LLM Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent 86d62bb commit 0add10a

3 files changed

Lines changed: 269 additions & 4 deletions

File tree

‎hack/package-update-templates.bats‎

Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
#!/usr/bin/env bats
2+
# -----------------------------------------------------------------------------
3+
# Invariant for the package `update` recipes that clear templates/ wholesale.
4+
#
5+
# `update` refetches a package's vendored upstream manifests, and several
6+
# recipes open by deleting templates/ to guarantee a clean slate. That is safe
7+
# only while every file under templates/ comes back from the recipe. templates/
8+
# is an ordinary chart directory, so a package can also keep first-party
9+
# manifests there -- kubevirt-cdi keeps the CDI upload-proxy Ingress and
10+
# TLSRoute -- and the recipe knows nothing about those. The wipe takes them,
11+
# `make update` exits 0, and the loss reaches a cluster as an object that
12+
# silently stopped being rendered at the next version bump, not as a failed
13+
# command.
14+
#
15+
# The invariant: a recipe that clears templates/ must NAME every file tracked
16+
# under it. Both halves are approximations of the shell the recipe would run,
17+
# and each is argued at its helper below: the wipe is a text match over the
18+
# recipe's lines rather than a parse, and "names" means the file's basename
19+
# minus its extension appears somewhere in the Makefile -- a mention, not proof
20+
# of a write. What that catches is the shape the CDI packages had, a file the
21+
# Makefile does not refer to at all: every recipe in the tree writes each path
22+
# it produces, so a name appearing nowhere in the file is a name nothing
23+
# refetches. A recipe that unpacked an archive or split a stream into
24+
# templates/ would produce files it never names and break that reading; none
25+
# does today.
26+
#
27+
# The tree is scanned rather than a list of packages pinned, so a package that
28+
# grows its first first-party manifest under a wiping recipe of its own is
29+
# caught by the commit that adds it. The packages that clear their own
30+
# templates/ today refetch all of it, which is what keeps this green; the two
31+
# CDI packages stopped clearing it, which is what this file was written for.
32+
#
33+
# What that leaves uncovered is a recipe clearing SOMEONE ELSE'S templates/, and
34+
# the tree has one: packages/system/cert-manager/Makefile runs
35+
# `rm -rf ../cert-manager-crds/templates/*` and moves the CRDs it pulled into
36+
# that directory, while cert-manager-crds declares no update target at all. A
37+
# first-party manifest added there is deleted by the next `make update` in
38+
# cert-manager and nothing below reports it. Covering that means resolving the
39+
# removal's target directory rather than reading the wiping package's own, which
40+
# is a second helper; the pattern below is deliberately not widened to accept a
41+
# path prefix, because that alone would fire on cert-manager and then list the
42+
# templates of a directory it does not have -- the removal names the sibling's
43+
# path, so the lookup has to follow it.
44+
#
45+
# The scan alone would also be green against a detector that finds nothing, so
46+
# the control below runs the same two helpers over a fixture that does hold an
47+
# unrefetched file, and over that fixture with the removal taken out.
48+
#
49+
# Constraints from cozytest.sh: it rewrites the closing brace of every function
50+
# in this file into `return 0`, so a helper cannot report through its exit
51+
# status -- both report through stdout -- and it injects `set -e`, so statuses
52+
# are captured explicitly.
53+
#
54+
# Run with: hack/cozytest.sh hack/package-update-templates.bats
55+
# -----------------------------------------------------------------------------
56+
57+
# Prints the recipe line that clears templates/ wholesale, for a package
58+
# Makefile whose `update` target has one. Silent otherwise, including for a
59+
# Makefile with no `update` target at all.
60+
#
61+
# The recipe is read as the run of tab-indented lines after `update:`. make also
62+
# ignores blank lines inside a recipe and this stops at the first one, so such a
63+
# recipe would be read short; none in the tree is written that way.
64+
#
65+
# The match accepts `rm -rf`, `rm -fr` and `rm -r` against `templates` and
66+
# `./templates`, bare, with a trailing slash, or with a `/*` glob -- the three
67+
# spellings that empty the directory. It deliberately does not accept
68+
# `rm -rf templates/one-file.yml`, which takes a single file the recipe goes on
69+
# to refetch (rabbitmq-operator), nor a narrower glob such as
70+
# `rm -rf templates/*.yaml`, which takes a subset the same way.
71+
#
72+
# It is a text match and not a shell parse, so a quoted `rm -rf "templates"`, a
73+
# removal reached through a variable, a `find -delete` and a `cd` into another
74+
# directory first are all invisible to it, as is a path-prefixed target -- the
75+
# cert-manager case in the header is one, and the prefix is the only thing
76+
# keeping it out: the `/*` it ends with is accepted. The enumeration is only as
77+
# complete as the last reader made it, so extend it in the same edit as the
78+
# pattern.
79+
wipe_command() {
80+
awk '/^update:/ { r = 1; next } r && /^\t/ { print; next } r { exit }' "$1" |
81+
grep -E 'rm[[:space:]]+-(rf|fr|r)[[:space:]]+(\./)?templates(/\*?)?([[:space:]]|;|&|$)' || true
82+
}
83+
84+
# Prints every file tracked under a package's templates/ that its Makefile
85+
# never mentions, one path per line. Whether that matters is the caller's call:
86+
# it matters for a package whose recipe clears the directory, and for no other.
87+
#
88+
# The listing is the index, so only a tracked file is at stake -- it is the one
89+
# the next `make update` deletes out from under a reviewer who has no reason to
90+
# look. Reading the working tree instead would fail a package over the
91+
# developer's own litter: an untracked .orig from a hand-applied patch, a
92+
# half-written template, a .DS_Store. A guard that goes red on scratch files is
93+
# a guard that gets switched off.
94+
#
95+
# The extension is stripped before matching, because a recipe may assemble the
96+
# filename from a variable holding the bare stem -- etcd-operator-crds writes
97+
# templates/$$c.yaml from a CRDS list of names without one. A basename whose
98+
# only dot is its first character keeps the whole name instead: stripping there
99+
# leaves the empty string, which grep matches against every Makefile, and the
100+
# file would pass without being looked at. The search covers the whole Makefile
101+
# rather than the recipe alone for the same reason as the stem: the variable is
102+
# assigned above the target. Both make the match generous, so a stem that
103+
# appears for an unrelated reason satisfies it; this reports the files nothing in
104+
# the Makefile refers to, not every file the recipe fails to write.
105+
#
106+
# Comment lines are dropped before the search, so a filename written into one is
107+
# not a mention: a rationale comment above a recipe, naming the very file it is
108+
# about, would otherwise exempt that package from its own guard. Precautionary
109+
# rather than a fix -- no package in the tree is exempted that way today, and
110+
# the one recipe comment that does name a template, in kubevirt-instancetypes,
111+
# names it on a `cp` line as well.
112+
unnamed_templates() {
113+
_mk=$1
114+
_dir=$(dirname "$_mk")
115+
_code=$(grep -vE '^[[:space:]]*@?#' "$_mk") || true
116+
git -C "$_dir" ls-files -- templates | while IFS= read -r _f; do
117+
_base=${_f##*/}
118+
_stem=${_base%.*}
119+
[ -n "$_stem" ] || _stem=$_base
120+
printf '%s\n' "$_code" | grep -Fq -- "$_stem" || printf '%s\n' "$_dir/$_f"
121+
done
122+
}
123+
124+
@test "no package update recipe clears templates/ over a file it never refetches" {
125+
examined=0
126+
offenders=""
127+
128+
for mk in packages/*/*/Makefile; do
129+
[ -f "$mk" ] || continue
130+
examined=$((examined + 1))
131+
[ -n "$(wipe_command "$mk")" ] || continue
132+
found=$(unnamed_templates "$mk")
133+
if [ -n "$found" ]; then
134+
offenders="$offenders
135+
$mk clears templates/ but names none of:
136+
$found"
137+
fi
138+
done
139+
140+
# A glob that matched nothing -- packages/ moved, or the suite run from
141+
# somewhere other than the repository root -- would otherwise report success
142+
# having looked at no Makefile at all.
143+
if [ "$examined" -eq 0 ]; then
144+
echo "no packages/*/*/Makefile found: run this from the repository root" >&2
145+
exit 1
146+
fi
147+
148+
# The index is where the scan reads a package's templates from, so an answer
149+
# of nothing here means it could not have reported anything either.
150+
if [ -z "$(git ls-files -- packages | head -1)" ]; then
151+
echo "git ls-files reported no tracked file under packages/: this test" >&2
152+
echo "cannot see a template directory, so it measured nothing" >&2
153+
exit 1
154+
fi
155+
156+
if [ -n "$offenders" ]; then
157+
echo "a package update recipe deletes templates/ over a file it does not refetch:" >&2
158+
echo "$offenders" >&2
159+
echo "" >&2
160+
echo "templates/ holds first-party manifests alongside the vendored ones and" >&2
161+
echo "'rm -rf templates' takes both, silently: the next 'make update' drops the" >&2
162+
echo "first-party ones and still exits 0. Either keep 'mkdir -p templates'" >&2
163+
echo "without the removal -- wget -O and kustomize > overwrite what they fetch" >&2
164+
echo "anyway -- or write the local file from the recipe too, where the clean" >&2
165+
echo "slate is load-bearing (etcd-operator-crds stages into a tmpdir first)." >&2
166+
exit 1
167+
fi
168+
}
169+
170+
@test "the scan reports a template file that a wiping recipe does not name" {
171+
# Negative control. The test above passes on a tree with no offender and on
172+
# a detector that cannot see one; this separates the two by handing the same
173+
# helpers a package that is broken in exactly the way kubevirt-cdi was.
174+
tmp=$(mktemp -d)
175+
mkdir -p "$tmp/pkg/templates"
176+
: > "$tmp/pkg/templates/fetched.yaml"
177+
: > "$tmp/pkg/templates/local-extra.yaml"
178+
# A tracked dotfile. Stripping its extension leaves nothing and an empty
179+
# pattern matches every Makefile, so without the guard in the helper this
180+
# file is skipped in silence rather than reported.
181+
: > "$tmp/pkg/templates/.gitkeep"
182+
# The comment names the file the recipe does not write, which is what a
183+
# rationale comment above a real recipe looks like. It must not count as the
184+
# recipe naming it.
185+
printf '# local-extra.yaml is written by hand\nupdate:\n\trm -rf templates\n\tmkdir templates\n\twget -O templates/fetched.yaml https://example.invalid/f.yaml\n' \
186+
> "$tmp/pkg/Makefile"
187+
# An index is what the helper reads, so the fixture needs one. Staged rather
188+
# than committed: nothing here needs a commit object, and a commit would want
189+
# an identity and a signature from whoever runs the suite.
190+
git -C "$tmp" init --quiet
191+
git -C "$tmp" add --all
192+
193+
# Untracked litter, written after the staging so it stays untracked. Neither
194+
# file is at stake and neither may be reported.
195+
: > "$tmp/pkg/templates/scratch.yaml"
196+
: > "$tmp/pkg/templates/.DS_Store"
197+
198+
if [ -z "$(wipe_command "$tmp/pkg/Makefile")" ]; then
199+
echo "the matcher did not see 'rm -rf templates' in the fixture recipe" >&2
200+
rm -rf "$tmp"
201+
exit 1
202+
fi
203+
204+
# Exactly the tracked files the recipe does not name. Reporting the fetched
205+
# one too would fail every wiping package in the tree, reporting none of them
206+
# would pass all of them, and reporting the litter would fail a package over
207+
# a file nobody is going to miss.
208+
expected=$(printf '%s\n%s' "$tmp/pkg/templates/.gitkeep" "$tmp/pkg/templates/local-extra.yaml")
209+
found=$(unnamed_templates "$tmp/pkg/Makefile")
210+
if [ "$found" != "$expected" ]; then
211+
echo "expected only the tracked unrefetched files to be reported:" >&2
212+
printf '%s\n' "$expected" >&2
213+
echo "got:" >&2
214+
printf '%s\n' "$found" >&2
215+
rm -rf "$tmp"
216+
exit 1
217+
fi
218+
219+
# The trailing-slash spelling removes exactly as much, so it has to be seen
220+
# as well. Spelled with the leading ./ at the same time, which is the other
221+
# optional half of the pattern; the fixture above carries neither.
222+
printf 'update:\n\trm -rf ./templates/\n\twget -O templates/fetched.yaml https://example.invalid/f.yaml\n' \
223+
> "$tmp/pkg/Makefile"
224+
if [ -z "$(wipe_command "$tmp/pkg/Makefile")" ]; then
225+
echo "the matcher missed 'rm -rf ./templates/', which clears the directory" >&2
226+
rm -rf "$tmp"
227+
exit 1
228+
fi
229+
230+
# And the glob spelling, which empties the directory without naming it as
231+
# the argument. It is what cert-manager's recipe writes, so it is the line a
232+
# package copies when it wants to clear its own templates.
233+
printf 'update:\n\trm -rf templates/*\n\twget -O templates/fetched.yaml https://example.invalid/f.yaml\n' \
234+
> "$tmp/pkg/Makefile"
235+
if [ -z "$(wipe_command "$tmp/pkg/Makefile")" ]; then
236+
echo "the matcher missed 'rm -rf templates/*', which empties the directory" >&2
237+
rm -rf "$tmp"
238+
exit 1
239+
fi
240+
241+
# The other side of that boundary: removing one file inside templates/ is
242+
# rabbitmq-operator's shape, it takes only what the recipe refetches, and a
243+
# pattern loose enough to call it a wipe would report that package forever.
244+
printf 'update:\n\trm -rf templates/fetched.yaml\n\twget -O templates/fetched.yaml https://example.invalid/f.yaml\n' \
245+
> "$tmp/pkg/Makefile"
246+
if [ -n "$(wipe_command "$tmp/pkg/Makefile")" ]; then
247+
echo "the matcher read a single-file removal as clearing the directory" >&2
248+
rm -rf "$tmp"
249+
exit 1
250+
fi
251+
252+
# And the shape the CDI packages moved to switches the check off, so the
253+
# scan is measuring the removal rather than the package.
254+
printf 'update:\n\tmkdir -p templates\n\twget -O templates/fetched.yaml https://example.invalid/f.yaml\n' \
255+
> "$tmp/pkg/Makefile"
256+
if [ -n "$(wipe_command "$tmp/pkg/Makefile")" ]; then
257+
echo "the matcher fired on a recipe that only creates the directory" >&2
258+
rm -rf "$tmp"
259+
exit 1
260+
fi
261+
262+
rm -rf "$tmp"
263+
}

‎packages/system/kubevirt-cdi-operator/Makefile‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ export NAMESPACE=cozy-kubevirt-cdi
44
include ../../../hack/package.mk
55

66
update:
7-
rm -rf templates
8-
mkdir templates
7+
mkdir -p templates
98
export VERSION=$$(basename $$(curl -s -w %{redirect_url} https://github.com/kubevirt/containerized-data-importer/releases/latest)) && \
109
wget https://github.com/kubevirt/containerized-data-importer/releases/download/$$VERSION/cdi-operator.yaml -O templates/cdi-operator.yaml
1110
sed -i 's/namespace: cdi/namespace: cozy-kubevirt-cdi/g' templates/cdi-operator.yaml

‎packages/system/kubevirt-cdi/Makefile‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ export NAMESPACE=cozy-$(NAME)
33

44
include ../../../hack/package.mk
55

6+
# templates/ holds first-party manifests as well as the vendored CR: the
7+
# upload-proxy Ingress and TLSRoute, which nothing below refetches. So this
8+
# recipe must never clear the directory -- wget -O truncates the file it
9+
# fetches and leaves the rest alone.
610
update:
7-
rm -rf templates
8-
mkdir templates
11+
mkdir -p templates
912
export VERSION=$$(basename $$(curl -s -w %{redirect_url} https://github.com/kubevirt/containerized-data-importer/releases/latest)) && \
1013
wget https://github.com/kubevirt/containerized-data-importer/releases/download/$$VERSION/cdi-cr.yaml -O templates/cdi-cr.yaml

0 commit comments

Comments
 (0)