Skip to content

Commit

Permalink
tests: k8s: avoid deleting unrelated pods
Browse files Browse the repository at this point in the history
Delete the debugger pod created during the test, rather than already
existing debugger pods.

Fixes: #9069

Signed-off-by: Dan Mihai <dmihai@microsoft.com>
  • Loading branch information
danmihai1 committed Feb 9, 2024
1 parent e78a951 commit 4a4d917
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion tests/integration/kubernetes/tests_common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ get_one_kata_node() {
echo "${resource_name/"node/"}"
}

# Deletes new_pod it wasn't present in the old_pods array.
delete_pod_if_new() {
new_pod="$1"
old_pods=( "$@:2" )

for old_pod in ${old_pods[@]}
do
[ "${old_pod}" == "${new_pod}" ] && return 0
done

kubectl delete new_pod > /dev/null
}

# Runs a command in the host filesystem.
#
# Parameters:
Expand All @@ -73,6 +86,10 @@ exec_host() {
# `kubectl debug` always returns 0, so we hack it to return the right exit code.
command="${@:2}"
command+='; echo -en \\n$?'

# Get the already existing debugger pods.
old_debugger_pods=( $(kubectl get pods -o name | grep node-debugger) )

# We're trailing the `\r` here due to: https://github.com/kata-containers/kata-containers/issues/8051
# tl;dr: When testing with CRI-O we're facing the foillowing error:
# ```
Expand All @@ -83,7 +100,16 @@ exec_host() {
# bash: line 1: $'\r': command not found
# ```
output="$(kubectl debug -qit "node/${node}" --image=alpine:latest -- chroot /host bash -c "${command}" | tr -d '\r')"
kubectl get pods -o name | grep node-debugger | xargs kubectl delete > /dev/null

# Get the updated list of debugger pods.
new_debugger_pods=( $(kubectl get pods -o name | grep node-debugger) )

# Delete the debugger pod created above.
for new_pod in ${new_debugger_pods[@]}
do
delete_pod_if_new "${new_pod}" ${old_debugger_pods[@]}
done

exit_code="$(echo "${output}" | tail -1)"
echo "$(echo "${output}" | head -n -1)"
return ${exit_code}
Expand Down

0 comments on commit 4a4d917

Please sign in to comment.