k8s.io/test-infra@v0.0.0-20240520184403-27c6b4c223d8/images/krte/wrapper.sh (about) 1 #!/usr/bin/env bash 2 # Copyright 2019 The Kubernetes Authors. 3 # 4 # Licensed under the Apache License, Version 2.0 (the "License"); 5 # you may not use this file except in compliance with the License. 6 # You may obtain a copy of the License at 7 # 8 # http://www.apache.org/licenses/LICENSE-2.0 9 # 10 # Unless required by applicable law or agreed to in writing, software 11 # distributed under the License is distributed on an "AS IS" BASIS, 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 # See the License for the specific language governing permissions and 14 # limitations under the License. 15 16 # wrapper.sh handles setting up things before / after the test command $@ 17 # 18 # usage: wrapper.sh my-test-command [my-test-args] 19 # 20 # Things wrapper.sh handles: 21 # - starting / stopping docker-in-docker 22 # -- configuring the docker daemon for IPv6 23 # - activating GCP service account credentials 24 # - ensuring GOPATH/bin is in PATH 25 # 26 # After handling these things / before cleanup, my-test-command will be invoked, 27 # and the exit code of my-test-command will be preserved by wrapper.sh 28 29 set -o errexit 30 set -o pipefail 31 set -o nounset 32 33 >&2 echo "wrapper.sh] [INFO] Wrapping Test Command: \`$*\`" 34 >&2 echo "wrapper.sh] [INFO] Running in: ${KRTE_IMAGE}" 35 >&2 echo "wrapper.sh] [INFO] See: https://github.com/kubernetes/test-infra/blob/master/images/krte/wrapper.sh" 36 printf '%0.s=' {1..80} >&2; echo >&2 37 >&2 echo "wrapper.sh] [SETUP] Performing pre-test setup ..." 38 39 cleanup(){ 40 if [[ "${DOCKER_IN_DOCKER_ENABLED:-false}" == "true" ]]; then 41 >&2 echo "wrapper.sh] [CLEANUP] Waiting 30 seconds for pods stopped with terminationGracePeriod:30" 42 sleep 30 43 >&2 echo "wrapper.sh] [CLEANUP] Cleaning up after Docker in Docker ..." 44 docker ps -aq | xargs -r docker rm -f || true 45 >&2 echo "wrapper.sh] [CLEANUP] Waiting for docker to stop for 30 seconds" 46 timeout 30 service docker stop || true 47 >&2 echo "wrapper.sh] [CLEANUP] Done cleaning up after Docker in Docker." 48 fi 49 } 50 51 early_exit_handler() { 52 >&2 echo "wrapper.sh] [EARLY EXIT] Interrupted, entering handler ..." 53 # if we got here before the wrapped command exited, skip, otherwise signal and wait 54 if [ -n "${WRAPPED_COMMAND_PID:-}" ] && [ -z "${EXIT_VALUE:-}" ]; then 55 kill -TERM "$WRAPPED_COMMAND_PID" || true 56 wait $WRAPPED_COMMAND_PID 57 EXIT_VALUE=$? 58 >&2 echo "wrapper.sh] [EARLY EXIT] Exit code was ${EXIT_VALUE}, not preserving due to interrupt signal" 59 # else if we have an exit code because we already waited on the process then debug it 60 elif [ -n "${EXIT_VALUE:-}" ]; then 61 >&2 echo "wrapper.sh] [EARLY EXIT] Original exit code was ${EXIT_VALUE}, not preserving due to interrupt signal" 62 fi 63 cleanup 64 >&2 echo "wrapper.sh] [EARLY EXIT] Completed handler ..." 65 exit 1 66 } 67 68 trap early_exit_handler TERM INT 69 70 # optionally enable ipv6 docker 71 export DOCKER_IN_DOCKER_IPV6_ENABLED=${DOCKER_IN_DOCKER_IPV6_ENABLED:-false} 72 if [[ "${DOCKER_IN_DOCKER_IPV6_ENABLED}" == "true" ]]; then 73 >&2 echo "wrapper.sh] [SETUP] Enabling IPv6 in Docker config ..." 74 # enable ipv6 75 sysctl net.ipv6.conf.all.disable_ipv6=0 76 sysctl net.ipv6.conf.all.forwarding=1 77 # enable ipv6 iptables 78 modprobe -v ip6table_nat 79 >&2 echo "wrapper.sh] [SETUP] Done enabling IPv6 in Docker config." 80 fi 81 82 # optionally enable iptables-nft 83 export DOCKER_IN_DOCKER_NFT_ENABLED=${DOCKER_IN_DOCKER_NFT_ENABLED:-false} 84 if [[ "${DOCKER_IN_DOCKER_NFT_ENABLED}" == "true" ]]; then 85 >&2 echo "wrapper.sh] [SETUP] Enabling iptables-nft ..." 86 # enable iptables-nft 87 update-alternatives --set iptables /usr/sbin/iptables-nft 88 update-alternatives --set ip6tables /usr/sbin/ip6tables-nft 89 # enable nft iptables module 90 modprobe -v nf_tables 91 >&2 echo "wrapper.sh] [SETUP] Done enabling iptables-nft by default." 92 fi 93 94 # Check if the job has opted-in to docker-in-docker 95 export DOCKER_IN_DOCKER_ENABLED=${DOCKER_IN_DOCKER_ENABLED:-false} 96 if [[ "${DOCKER_IN_DOCKER_ENABLED}" == "true" ]]; then 97 >&2 echo "wrapper.sh] [SETUP] Docker in Docker enabled, initializing ..." 98 # Fix ulimit issue 99 sed -i 's|ulimit -Hn|ulimit -n|' /etc/init.d/docker || true 100 # If we have opted in to docker in docker, start the docker daemon, 101 service docker start 102 # the service can be started but the docker socket not ready, wait for ready 103 WAIT_N=0 104 while true; do 105 # docker ps -q should only work if the daemon is ready 106 docker ps -q > /dev/null 2>&1 && break 107 if [[ ${WAIT_N} -lt 5 ]]; then 108 WAIT_N=$((WAIT_N+1)) 109 echo "wrapper.sh] [SETUP] Waiting for Docker to be ready, sleeping for ${WAIT_N} seconds ..." 110 sleep ${WAIT_N} 111 else 112 echo "wrapper.sh] [SETUP] Reached maximum attempts, not waiting any longer ..." 113 break 114 fi 115 done 116 echo "wrapper.sh] [SETUP] Done setting up Docker in Docker." 117 fi 118 119 # add $GOPATH/bin to $PATH 120 export GOPATH="${GOPATH:-${HOME}/go}" 121 export PATH="${GOPATH}/bin:${PATH}" 122 mkdir -p "${GOPATH}/bin" 123 124 # Authenticate gcloud, allow failures 125 if [[ -n "${GOOGLE_APPLICATION_CREDENTIALS:-}" ]]; then 126 >&2 echo "wrapper.sh] activating service account from GOOGLE_APPLICATION_CREDENTIALS ..." 127 gcloud auth activate-service-account --key-file="${GOOGLE_APPLICATION_CREDENTIALS}" || true 128 fi 129 130 if git rev-parse --is-inside-work-tree >/dev/null; then 131 >&2 echo "wrapper.sh] [SETUP] Setting SOURCE_DATE_EPOCH for build reproducibility ..." 132 # Use a reproducible build date based on the most recent git commit timestamp. 133 SOURCE_DATE_EPOCH=$(git log -1 --pretty=%ct) 134 export SOURCE_DATE_EPOCH 135 >&2 echo "wrapper.sh] [SETUP] exported SOURCE_DATE_EPOCH=${SOURCE_DATE_EPOCH}" 136 fi 137 138 # actually run the user supplied command 139 printf '%0.s=' {1..80}; echo 140 >&2 echo "wrapper.sh] [TEST] Running Test Command: \`$*\` ..." 141 set +o errexit 142 "$@" & 143 WRAPPED_COMMAND_PID=$! 144 wait $WRAPPED_COMMAND_PID 145 EXIT_VALUE=$? 146 set -o errexit 147 >&2 echo "wrapper.sh] [TEST] Test Command exit code: ${EXIT_VALUE}" 148 149 # cleanup 150 cleanup 151 152 # preserve exit value from user supplied command 153 printf '%0.s=' {1..80} >&2; echo >&2 154 >&2 echo "wrapper.sh] Exiting ${EXIT_VALUE}" 155 exit ${EXIT_VALUE}