3232# the hook skips keeps its brokers running after the release is gone;
3333# * a KafkaNodePool CRD that is absent exits 0, and any other failure of
3434# that delete exits non-zero, so the uninstall is retried rather than
35- # finishing with the pools still in place.
35+ # finishing with the pools still in place;
36+ # * the release's StrimziPodSets are deleted after the pools, with the same
37+ # exit codes. Garbage collection does not take them on a cluster upgraded
38+ # from Strimzi 0.x until kube-controller-manager restarts
39+ # (kubernetes/kubernetes#102641), so the hook is what stops the brokers.
3640#
3741# Run from the repo root with make bats-unit-tests. Direct status checks
3842# retain compatibility with cozytest, which has no Bats run/setup helpers.
@@ -65,7 +69,8 @@ render_hook() {
6569# The KafkaNodePool delete answers from POOL_OUT / POOL_RC instead, so a test
6670# can fail one call without failing the other. POOL_RC defaults to a clean
6771# delete, because the topic-path tests above are about the topic call; every
68- # pool-path test sets it explicitly.
72+ # pool-path test sets it explicitly. The StrimziPodSet delete answers from
73+ # SPS_OUT / SPS_RC the same way.
6974# No line below is a bare column-0 `}`, so cozytest.sh's parser leaves it alone.
7075write_fake_kubectl () {
7176 cat > " $1 /kubectl" << 'KEOF '
@@ -75,6 +80,9 @@ case "$*" in
7580 *kafkanodepools*)
7681 printf '%s\n' "${POOL_OUT:-kafkanodepool.kafka.strimzi.io \"b-0\" deleted}"
7782 exit "${POOL_RC:-0}" ;;
83+ *strimzipodsets*)
84+ printf '%s\n' "${SPS_OUT:-strimzipodset.core.strimzi.io \"kafka-test-b-0\" deleted}"
85+ exit "${SPS_RC:-0}" ;;
7886esac
7987if [ -z "${DELETE_RC:-}" ]; then
8088 echo "fake kubectl: DELETE_RC unset; the test would assert nothing" >&2
@@ -284,6 +292,11 @@ KEOF
284292 cat " $tmp /calls"
285293 false
286294 fi
295+ if ! grep -q -- " delete strimzipodsets.core.strimzi.io -l strimzi.io/cluster=kafka-test " " $tmp /calls" ; then
296+ echo " FAIL: the StrimziPodSets were skipped after the topic wait timed out"
297+ cat " $tmp /calls"
298+ false
299+ fi
287300 rm -rf " $tmp "
288301}
289302
@@ -342,3 +355,96 @@ KEOF
342355 fi
343356 rm -rf " $tmp "
344357}
358+
359+ @test " kafka pre-delete hook deletes this release's StrimziPodSets after its node pools" {
360+ tmp=$( mktemp -d)
361+ write_fake_kubectl " $tmp "
362+ render_hook " $KAFKA_CHART " kafka-test tenant-test kafka-test-pre-delete " $tmp /hook.sh"
363+
364+ export KLOG=" $tmp /calls"
365+ export DELETE_RC=0
366+ export POOL_RC=0
367+ export SPS_RC=0
368+ rc=0
369+ PATH=" $tmp :$PATH " sh " $tmp /hook.sh" > " $tmp /out" 2> " $tmp /err" || rc=$?
370+ if [ " $rc " -ne 0 ]; then
371+ echo " FAIL: hook exited $rc on a clean delete"
372+ cat " $tmp /out" " $tmp /err"
373+ false
374+ fi
375+ # Anchored like the selectors above.
376+ sps_line=$( grep -n -- " delete strimzipodsets.core.strimzi.io -l strimzi.io/cluster=kafka-test " " $tmp /calls" | cut -d: -f1)
377+ pool_line=$( grep -n -- " delete kafkanodepools.kafka.strimzi.io -l strimzi.io/cluster=kafka-test " " $tmp /calls" | cut -d: -f1)
378+ if [ -z " $sps_line " ] || [ -z " $pool_line " ]; then
379+ echo " FAIL: expected one release-scoped delete of each kind"
380+ cat " $tmp /calls"
381+ false
382+ fi
383+ # Deleting the sets while their pools exist lets the operator recreate them.
384+ if [ " $sps_line " -le " $pool_line " ]; then
385+ echo " FAIL: the StrimziPodSets were deleted before the node pools"
386+ cat " $tmp /calls"
387+ false
388+ fi
389+ if ! grep -q -- " strimzipodsets.* --wait=false" " $tmp /calls" ; then
390+ echo " FAIL: the StrimziPodSet delete waits, which spends the Job's deadline"
391+ cat " $tmp /calls"
392+ false
393+ fi
394+ rm -rf " $tmp "
395+ }
396+
397+ @test " kafka pre-delete hook exits 0 when the StrimziPodSet CRD is absent" {
398+ for msg in ' error: the server doesn' " '" ' t have a resource type "strimzipodsets"' \
399+ ' error: no matches for kind "StrimziPodSet" in version "core.strimzi.io/v1"' ; do
400+ tmp=$( mktemp -d)
401+ write_fake_kubectl " $tmp "
402+ render_hook " $KAFKA_CHART " kafka-test tenant-test kafka-test-pre-delete " $tmp /hook.sh"
403+
404+ export KLOG=" $tmp /calls"
405+ export DELETE_RC=0
406+ export POOL_RC=0
407+ export SPS_RC=1
408+ export SPS_OUT=" $msg "
409+ rc=0
410+ PATH=" $tmp :$PATH " sh " $tmp /hook.sh" > " $tmp /out" 2> " $tmp /err" || rc=$?
411+ if [ " $rc " -ne 0 ]; then
412+ echo " FAIL: hook exited $rc on an absent StrimziPodSet CRD, wedging the uninstall"
413+ cat " $tmp /out" " $tmp /err"
414+ false
415+ fi
416+ if ! grep -q ' StrimziPodSet CRD absent' " $tmp /out" ; then
417+ echo " FAIL: the absent CRD was not reported as such"
418+ cat " $tmp /out"
419+ false
420+ fi
421+ rm -rf " $tmp "
422+ done
423+ }
424+
425+ @test " kafka pre-delete hook exits non-zero when the StrimziPodSet delete fails" {
426+ # A set left here keeps its brokers running after the release is gone, and
427+ # Helm never runs this hook for the release again.
428+ tmp=$( mktemp -d)
429+ write_fake_kubectl " $tmp "
430+ render_hook " $KAFKA_CHART " kafka-test tenant-test kafka-test-pre-delete " $tmp /hook.sh"
431+
432+ export KLOG=" $tmp /calls"
433+ export DELETE_RC=0
434+ export POOL_RC=0
435+ export SPS_RC=1
436+ export SPS_OUT=' Error from server (Forbidden): strimzipodsets.core.strimzi.io is forbidden'
437+ rc=0
438+ PATH=" $tmp :$PATH " sh " $tmp /hook.sh" > " $tmp /out" 2> " $tmp /err" || rc=$?
439+ if [ " $rc " -eq 0 ]; then
440+ echo " FAIL: hook exited 0 on a Forbidden, so the uninstall would finish and leave the brokers"
441+ cat " $tmp /out" " $tmp /err"
442+ false
443+ fi
444+ if ! grep -q ' ^ERROR: deleting StrimziPodSets failed' " $tmp /err" ; then
445+ echo " FAIL: the failure was not reported as an error"
446+ cat " $tmp /err"
447+ false
448+ fi
449+ rm -rf " $tmp "
450+ }
0 commit comments