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

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