From 9fa4e2ad51412abfc784f12cd2c3beea21b8a98e Mon Sep 17 00:00:00 2001 From: chiourung_huang Date: Fri, 23 Aug 2024 08:10:53 +0000 Subject: [PATCH] JIRA-SONIC-9824: Add retry for pulling k8s when building the SONiC image Pulling universal k8s images is not stable. It sometimes fails. Add retry to fix build image failure. --- .../build_templates/sonic_debian_extension.j2 | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/files/build_templates/sonic_debian_extension.j2 b/files/build_templates/sonic_debian_extension.j2 index 75ca31025..f98bf10eb 100644 --- a/files/build_templates/sonic_debian_extension.j2 +++ b/files/build_templates/sonic_debian_extension.j2 @@ -798,8 +798,33 @@ sudo LANG=C DOCKER_HOST="$DOCKER_HOST" chroot $FILESYSTEM_ROOT /usr/local/bin/ge {% if include_kubernetes == "y" %} ## Pull in kubernetes docker images echo "pulling universal k8s images ..." -sudo https_proxy=$https_proxy LANG=C chroot $FILESYSTEM_ROOT docker $SONIC_NATIVE_DOCKERD_FOR_DOCKERFS pull k8s.gcr.io/pause:${K8s_GCR_IO_PAUSE_VERSION} -echo "docker images pull complete" + +MAX_ATTEMPTS=5 +RETRY_INTERVAL=10 +attempt=1 + +# skip Exit immediately due to error retry +set +e +while [ $attempt -le $MAX_ATTEMPTS ]; do + output=$(sudo https_proxy=$https_proxy LANG=C chroot $FILESYSTEM_ROOT docker $SONIC_NATIVE_DOCKERD_FOR_DOCKERFS pull k8s.gcr.io/pause:${K8s_GCR_IO_PAUSE_VERSION} 2>&1) + exit_status=$? + if [ $exit_status -eq 0 ]; then + echo "docker images pull complete" + break + else + echo "Failed to pull Docker image. Error message:" + echo "$output" + echo "Retrying in $RETRY_INTERVAL seconds..." + sleep $RETRY_INTERVAL + fi + + attempt=$(( $attempt + 1 )) +done +set -e +if [ $attempt -gt $MAX_ATTEMPTS ]; then + echo "Failed to pull Docker image after $MAX_ATTEMPTS attempts." + exit 1 +fi {% endif %} {% if include_kubernetes_master == "y" %}