diff --git a/tests/integration/kubernetes/tests_common.sh b/tests/integration/kubernetes/tests_common.sh index 5b79908690..7d254d7d75 100644 --- a/tests/integration/kubernetes/tests_common.sh +++ b/tests/integration/kubernetes/tests_common.sh @@ -454,17 +454,51 @@ grep_pod_exec_output() { pod_exec_with_retries() { local -r pod_name="$1" shift + local -r container_name="" + + container_exec_with_retries "${pod_name}" "${container_name}" "$@" +} + +# Execute a command in a pod's container and echo kubectl's output to stdout. +# +# If the caller specifies an empty container name as parameter, the command is executed in pod's default container, +# or in pod's first container if there is no default. +# +# This function retries "kubectl exec" several times, if: +# - kubectl returns a failure exit code, or +# - kubectl exits successfully but produces empty console output. +# These retries are an attempt to work around issues similar to https://github.com/kubernetes/kubernetes/issues/124571. +# +# Parameters: +# $1 - pod name +# $2 - container name +# $3+ - the command to execute using "kubectl exec" +# +# Exit code: +# 0 +container_exec_with_retries() { + local -r pod_name="$1" + shift + local -r container_name="$1" + shift local cmd_out="" for _ in {1..10}; do - bats_unbuffered_info "Executing in pod ${pod_name}: $*" - cmd_out=$(kubectl exec "${pod_name}" -- "$@") || (bats_unbuffered_info "kubectl exec failed" ; cmd_out="") + if [[ -n "${container_name}" ]]; then + bats_unbuffered_info "Executing in pod ${pod_name}, container ${container_name}: $*" + cmd_out=$(kubectl exec "${pod_name}" -c "${container_name}" -- "$@") || (bats_unbuffered_info "kubectl exec failed" ; cmd_out="") + else + bats_unbuffered_info "Executing in pod ${pod_name}: $*" + cmd_out=$(kubectl exec "${pod_name}" -- "$@") || (bats_unbuffered_info "kubectl exec failed" ; cmd_out="") + fi + if [[ -n "${cmd_out}" ]]; then bats_unbuffered_info "command output: ${cmd_out}" break + else + bats_unbuffered_info "Warning: empty output from kubectl exec" + sleep 1 fi - bats_unbuffered_info "Warning: empty output from kubectl exec" - sleep 1 done echo "${cmd_out}"