Skip to content

Commit 54ca5e0

Browse files
test(monitoring-agents): tell releases apart by name and namespace together
Helm release names are unique only within a namespace, so a release of the same name elsewhere can own VictoriaMetrics objects in this namespace. The hook must leave those alone, and the tests stayed green when it matched on the name alone. Assisted-by: LLM Signed-off-by: Mattia Eleuteri <[email protected]>
1 parent 41046f4 commit 54ca5e0

1 file changed

Lines changed: 17 additions & 11 deletions

File tree

‎hack/monitoring-agents-vm-cleanup-hook.bats‎

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
# when the polling condition was inverted. Each of those turns this file red.
1313
#
1414
# What is pinned here:
15-
# * only objects whose Helm annotations name this release are deleted or
16-
# patched: an object of another release, or one Helm does not own, is left
17-
# alone;
15+
# * only objects whose Helm annotations name this release, by name and
16+
# namespace both, are deleted or patched: an object of another release, of
17+
# a same-named release in another namespace, or one Helm does not own, is
18+
# left alone;
1819
# * with the operator gone, the hook waits before touching finalizers, then
1920
# clears them, and nothing of this release is left behind;
2021
# * with the operator alive, it finalizes the objects itself and the hook
@@ -114,14 +115,17 @@ KEOF
114115
}
115116

116117
# write_state <file>
117-
# Two objects of this release in two namespaces, one VMRule of another release
118-
# and one VMServiceScrape that carries no Helm annotations.
118+
# Three objects of this release in two namespaces; a VMRule of another release;
119+
# a VMRule whose release name matches but whose release namespace does not, so
120+
# only the pair of annotations tells it apart; and a VMServiceScrape that
121+
# carries no Helm annotations.
119122
write_state() {
120123
cat > "$1" <<EOF
121124
vmagents|$MA_RELEASE|$MA_NAMESPACE|$MA_NAMESPACE|vmagent
122125
vmrules|$MA_RELEASE|$MA_NAMESPACE|$MA_NAMESPACE|alerts-node
123126
vmservicescrapes|$MA_RELEASE|$MA_NAMESPACE|cozy-victoria-metrics-operator|operator
124127
vmrules|other|$MA_NAMESPACE|$MA_NAMESPACE|foreign-rule
128+
vmrules|$MA_RELEASE|other-namespace|$MA_NAMESPACE|foreign-ns-rule
125129
vmservicescrapes|||$MA_NAMESPACE|unmanaged-scrape
126130
EOF
127131
}
@@ -171,19 +175,20 @@ run_hook() {
171175
done
172176
# The objects of this release must be gone, which the fake only does for a
173177
# patch that really clears the finalizers.
174-
if grep -q "^[a-z]*|$MA_RELEASE|" "$tmp/state"; then
178+
if grep -q "^[a-z]*|$MA_RELEASE|$MA_NAMESPACE|" "$tmp/state"; then
175179
echo "FAIL: objects of this release are still there, Terminating"
176180
cat "$tmp/state" "$tmp/calls"
177181
false
178182
fi
179-
# Another release's object and an object Helm does not own are never touched.
180-
if grep -E -q -- ' (delete|patch) [a-z.]+ (foreign-rule|unmanaged-scrape)( |$)' "$tmp/calls"; then
183+
# Another release's object, one of a same-named release in another
184+
# namespace, and an object Helm does not own are never touched.
185+
if grep -E -q -- ' (delete|patch) [a-z.]+ (foreign-rule|foreign-ns-rule|unmanaged-scrape)( |$)' "$tmp/calls"; then
181186
echo "FAIL: the hook touched an object this release does not own"
182187
cat "$tmp/calls"
183188
false
184189
fi
185-
if [ "$(grep -c '|' "$tmp/state")" -ne 2 ]; then
186-
echo "FAIL: the objects this release does not own should both remain"
190+
if [ "$(grep -c '|' "$tmp/state")" -ne 3 ]; then
191+
echo "FAIL: the three objects this release does not own should remain"
187192
cat "$tmp/state"
188193
false
189194
fi
@@ -287,7 +292,8 @@ run_hook() {
287292
@test "vm cleanup hook exits 0 without deleting anything when the release owns nothing" {
288293
tmp=$(mktemp -d)
289294
write_fakes "$tmp"
290-
printf 'vmrules|other|%s|%s|foreign-rule\n' "$MA_NAMESPACE" "$MA_NAMESPACE" > "$tmp/state"
295+
printf 'vmrules|other|%s|%s|foreign-rule\nvmrules|%s|other-namespace|%s|foreign-ns-rule\n' \
296+
"$MA_NAMESPACE" "$MA_NAMESPACE" "$MA_RELEASE" "$MA_NAMESPACE" > "$tmp/state"
291297
render_hook vm-objects-cleanup "$tmp/hook.sh"
292298
export OPERATOR_LIVE=0 MISSING_KINDS=""
293299
run_hook "$tmp"

0 commit comments

Comments
 (0)