mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-09-24 10:17:21 +00:00
tests: k8s: add container_exec_with_retries()
Add container_exec_with_retries(), useful for retrying if needed commands similar to: kubectl exec <pod_name> -c <container_name> -- <command> Signed-off-by: Dan Mihai <dmihai@microsoft.com>
This commit is contained in:
@@ -454,17 +454,51 @@ grep_pod_exec_output() {
|
|||||||
pod_exec_with_retries() {
|
pod_exec_with_retries() {
|
||||||
local -r pod_name="$1"
|
local -r pod_name="$1"
|
||||||
shift
|
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=""
|
local cmd_out=""
|
||||||
|
|
||||||
for _ in {1..10}; do
|
for _ in {1..10}; do
|
||||||
bats_unbuffered_info "Executing in pod ${pod_name}: $*"
|
if [[ -n "${container_name}" ]]; then
|
||||||
cmd_out=$(kubectl exec "${pod_name}" -- "$@") || (bats_unbuffered_info "kubectl exec failed" ; cmd_out="")
|
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
|
if [[ -n "${cmd_out}" ]]; then
|
||||||
bats_unbuffered_info "command output: ${cmd_out}"
|
bats_unbuffered_info "command output: ${cmd_out}"
|
||||||
break
|
break
|
||||||
|
else
|
||||||
|
bats_unbuffered_info "Warning: empty output from kubectl exec"
|
||||||
|
sleep 1
|
||||||
fi
|
fi
|
||||||
bats_unbuffered_info "Warning: empty output from kubectl exec"
|
|
||||||
sleep 1
|
|
||||||
done
|
done
|
||||||
|
|
||||||
echo "${cmd_out}"
|
echo "${cmd_out}"
|
||||||
|
Reference in New Issue
Block a user