k8s.io/test-infra@v0.0.0-20240520184403-27c6b4c223d8/images/bootstrap/runner.sh (about)

     1  #!/usr/bin/env bash
     2  # Copyright 2018 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  # generic runner script, handles DIND, bazelrc for caching, etc.
    17  
    18  # Check if the job has opted-in to bazel remote caching and if so generate 
    19  # .bazelrc entries pointing to the remote cache
    20  export BAZEL_REMOTE_CACHE_ENABLED=${BAZEL_REMOTE_CACHE_ENABLED:-false}
    21  if [[ "${BAZEL_REMOTE_CACHE_ENABLED}" == "true" ]]; then
    22      echo "Bazel remote cache is enabled, generating .bazelrcs ..."
    23      /usr/local/bin/create_bazel_cache_rcs.sh
    24  fi
    25  
    26  
    27  # runs custom docker data root cleanup binary and debugs remaining resources
    28  cleanup_dind() {
    29      if [[ "${DOCKER_IN_DOCKER_ENABLED:-false}" == "true" ]]; then
    30          echo "Waiting 30 seconds for pods stopped with terminationGracePeriod:30"
    31          sleep 30
    32          echo "Cleaning up after docker"
    33          docker ps -aq | xargs -r docker rm -f || true
    34          echo "Waiting for docker to stop for 30 seconds"
    35          timeout 30 service docker stop || true
    36      fi
    37  }
    38  
    39  early_exit_handler() {
    40      if [ -n "${WRAPPED_COMMAND_PID:-}" ]; then
    41          kill -TERM "$WRAPPED_COMMAND_PID" || true
    42      fi
    43      cleanup_dind
    44  }
    45  
    46  # optionally enable ipv6 docker
    47  export DOCKER_IN_DOCKER_IPV6_ENABLED=${DOCKER_IN_DOCKER_IPV6_ENABLED:-false}
    48  if [[ "${DOCKER_IN_DOCKER_IPV6_ENABLED}" == "true" ]]; then
    49      echo "Enabling IPV6 for Docker."
    50      # configure the daemon with ipv6
    51      mkdir -p /etc/docker/
    52      cat <<EOF >/etc/docker/daemon.json
    53  {
    54    "ipv6": true,
    55    "fixed-cidr-v6": "fc00:db8:1::/64"
    56  }
    57  EOF
    58      # enable ipv6
    59      sysctl net.ipv6.conf.all.disable_ipv6=0
    60      sysctl net.ipv6.conf.all.forwarding=1
    61      # enable ipv6 iptables
    62      modprobe -v ip6table_nat
    63  fi
    64  
    65  # Check if the job has opted-in to docker-in-docker availability.
    66  export DOCKER_IN_DOCKER_ENABLED=${DOCKER_IN_DOCKER_ENABLED:-false}
    67  if [[ "${DOCKER_IN_DOCKER_ENABLED}" == "true" ]]; then
    68      echo "Docker in Docker enabled, initializing..."
    69      printf '=%.0s' {1..80}; echo
    70      # Fix ulimit issue
    71      sed -i 's|ulimit -Hn|ulimit -n|' /etc/init.d/docker || true
    72      # If we have opted in to docker in docker, start the docker daemon,
    73      service docker start
    74      # the service can be started but the docker socket not ready, wait for ready
    75      WAIT_N=0
    76      MAX_WAIT=5
    77      while true; do
    78          # docker ps -q should only work if the daemon is ready
    79          docker ps -q > /dev/null 2>&1 && break
    80          if [[ ${WAIT_N} -lt ${MAX_WAIT} ]]; then
    81              WAIT_N=$((WAIT_N+1))
    82              echo "Waiting for docker to be ready, sleeping for ${WAIT_N} seconds."
    83              sleep ${WAIT_N}
    84          else
    85              echo "Reached maximum attempts, not waiting any longer..."
    86              break
    87          fi
    88      done
    89      printf '=%.0s' {1..80}; echo
    90      echo "Done setting up docker in docker."
    91  
    92      # Workaround for https://github.com/kubernetes/test-infra/issues/23741
    93      # Instead of removing, disabled by default in case we need to address again
    94      if [[ "${BOOTSTRAP_MTU_WORKAROUND:-"false"}" == "true" ]]; then
    95          echo "configure iptables to set MTU"
    96          iptables -t mangle -A POSTROUTING -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
    97      fi
    98  fi
    99  
   100  trap early_exit_handler INT TERM
   101  
   102  # disable error exit so we can run post-command cleanup
   103  set +o errexit
   104  
   105  # add $GOPATH/bin to $PATH
   106  export PATH="${GOPATH}/bin:${PATH}"
   107  mkdir -p "${GOPATH}/bin"
   108  # Authenticate gcloud, allow failures
   109  if [[ -n "${GOOGLE_APPLICATION_CREDENTIALS:-}" ]]; then
   110    gcloud auth activate-service-account --key-file="${GOOGLE_APPLICATION_CREDENTIALS}" || true
   111  fi
   112  
   113  # Use a reproducible build date based on the most recent git commit timestamp.
   114  SOURCE_DATE_EPOCH=$(git log -1 --pretty=%ct || true)
   115  export SOURCE_DATE_EPOCH
   116  
   117  # actually start bootstrap and the job
   118  set -o xtrace
   119  "$@" &
   120  WRAPPED_COMMAND_PID=$!
   121  wait $WRAPPED_COMMAND_PID
   122  EXIT_VALUE=$?
   123  set +o xtrace
   124  
   125  # cleanup after job
   126  if [[ "${DOCKER_IN_DOCKER_ENABLED}" == "true" ]]; then
   127      echo "Cleaning up after docker in docker."
   128      printf '=%.0s' {1..80}; echo
   129      cleanup_dind
   130      printf '=%.0s' {1..80}; echo
   131      echo "Done cleaning up after docker in docker."
   132  fi
   133  
   134  # preserve exit value from job / bootstrap
   135  exit ${EXIT_VALUE}