k8s.io/kubernetes@v1.29.3/test/e2e_node/gubernator.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  # Copyright 2016 The Kubernetes Authors.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  # Make bucket and a folder for e2e-node test logs.
    18  # Populate the folder from the logs stored in /tmp/_artifacts/ in the same way as a
    19  # jenkins build would, and then print the URL to view the test results on Gubernator
    20  
    21  set -o errexit
    22  set -o nounset
    23  set -o pipefail
    24  
    25  KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/../..
    26  source "${KUBE_ROOT}/hack/lib/logging.sh"
    27  
    28  
    29  if [[ $# -eq 0 || ! $1 =~ ^[Yy]$ ]]; then
    30    read -r -p "Do you want to run gubernator.sh and upload logs publicly to GCS? [y/n]" yn
    31    echo
    32    if [[ ! $yn =~ ^[Yy]$ ]]; then
    33        exit 1
    34    fi
    35  fi
    36  
    37  # Check that user has gsutil
    38  if [[ $(which gsutil) == "" ]]; then
    39    echo "Could not find gsutil when running \`which gsutil\`"
    40    exit 1
    41  fi
    42  
    43  # Check that user has gcloud
    44  if [[ $(which gcloud) == "" ]]; then
    45    echo "Could not find gcloud when running: \`which gcloud\`"
    46    exit 1
    47  fi
    48  
    49  # Check that user has Credentialed Active account
    50  if ! gcloud auth list | grep -q "ACTIVE"; then
    51    echo "Could not find active account when running: \`gcloud auth list\`"
    52    exit 1
    53  fi
    54  
    55  readonly gcs_acl="public-read"
    56  bucket_name="${USER}-g8r-logs"
    57  echo ""
    58  V=2 kube::log::status "Using bucket ${bucket_name}"
    59  
    60  # Check if the bucket exists
    61  if ! gsutil ls gs:// | grep -q "gs://${bucket_name}/"; then
    62    V=2 kube::log::status "Creating public bucket ${bucket_name}"
    63    gsutil mb "gs://${bucket_name}/"
    64    # Make all files in the bucket publicly readable
    65    gsutil acl ch -u AllUsers:R "gs://${bucket_name}"
    66  else
    67    V=2 kube::log::status "Bucket already exists"
    68  fi
    69  
    70  # Path for e2e-node test results
    71  GCS_JOBS_PATH="gs://${bucket_name}/logs/e2e-node"
    72  
    73  ARTIFACTS=${ARTIFACTS:-"/tmp/_artifacts"}
    74  BUILD_LOG_PATH="${ARTIFACTS}/build-log.txt"
    75  
    76  if [[ ! -e $BUILD_LOG_PATH ]]; then
    77    echo "Could not find build-log.txt at ${BUILD_LOG_PATH}"
    78    exit 1
    79  fi
    80  
    81  # Get start and end timestamps based on build-log.txt file contents
    82  # Line where the actual tests start
    83  start_line=$(grep -n -m 1 "^=" "${BUILD_LOG_PATH}" | sed 's/\([0-9]*\).*/\1/')
    84  # Create text file starting where the tests start
    85  after_start=$(tail -n "+${start_line}" "${BUILD_LOG_PATH}")
    86  echo "${after_start}" >> build-log-cut.txt
    87  # Match the first timestamp
    88  start_time_raw=$(grep -m 1 -o '[0-9][0-9][0-9][0-9][[:blank:]][0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9]*' build-log-cut.txt)
    89  rm build-log-cut.txt
    90  # Make the date readable by date command (ex: 0101 00:00:00.000 -> 01/01 00:00:00.000)
    91  start_time=$(echo "${start_time_raw}" | sed 's/^.\{2\}/&\//')
    92  V=2 kube::log::status "Started at ${start_time}"
    93  # Match the last timestamp in the build-log file
    94  end_time=$(grep -o '[0-9][0-9][0-9][0-9][[:blank:]][0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9]*' "${BUILD_LOG_PATH}" | tail -1 | sed 's/^.\{2\}/&\//')
    95  # Convert to epoch time for Gubernator
    96  start_time_epoch=$(date -d "${start_time}" +%s)
    97  end_time_epoch=$(date -d "${end_time}" +%s)
    98  
    99  # Make folder name for build from timestamp
   100  BUILD_STAMP=$(echo "${start_time}" | sed 's/\///' | sed 's/ /_/')
   101  
   102  GCS_LOGS_PATH="${GCS_JOBS_PATH}/${BUILD_STAMP}"
   103  
   104  # Check if folder for same logs already exists
   105  if gsutil ls "${GCS_JOBS_PATH}" | grep -q "${BUILD_STAMP}"; then
   106    V=2 kube::log::status "Log files already uploaded"
   107    echo "Gubernator linked below:"
   108    echo "k8s-gubernator.appspot.com/build/${GCS_LOGS_PATH}?local=on"
   109    exit
   110  fi
   111  
   112  while IFS= read -r result; do
   113    if [[ $result != "" && $result != "${ARTIFACTS}/results" && $result != "${ARTIFACTS}" ]]; then
   114      mv "${result}/"* "${ARTIFACTS}"
   115    fi
   116  done < <(find "${ARTIFACTS}" -type d -name "results")
   117  
   118  # Upload log files
   119  for upload_attempt in $(seq 3); do
   120    if [[ -d "${ARTIFACTS}" && -n $(ls -A "${ARTIFACTS}") ]]; then
   121      V=2 kube::log::status "Uploading artifacts"
   122      gsutil -m -q -o "GSUtil:use_magicfile=True" cp -a "${gcs_acl}" -r -c \
   123        -z log,xml,json "${ARTIFACTS}" "${GCS_LOGS_PATH}/artifacts" || continue
   124    fi
   125    break
   126  done
   127  for upload_attempt in $(seq 3); do
   128    if [[ -e "${BUILD_LOG_PATH}" ]]; then
   129      V=2 kube::log::status "Uploading build log"
   130      gsutil -q cp -Z -a "${gcs_acl}" "${BUILD_LOG_PATH}" "${GCS_LOGS_PATH}" || continue
   131    fi
   132    break
   133  done
   134  
   135  
   136  # Find the k8s version for started.json
   137  version=""
   138  if [[ -e "version" ]]; then
   139    version=$(cat "version")
   140  elif [[ -e "hack/lib/version.sh" ]]; then
   141    source "hack/lib/version.sh"
   142    kube::version::get_version_vars
   143    version="${KUBE_GIT_VERSION-}"
   144  fi
   145  if [[ -n "${version}" ]]; then
   146    V=2 kube::log::status "Found Kubernetes version: ${version}"
   147  else
   148    V=2 kube::log::status "Could not find Kubernetes version"
   149  fi
   150  
   151  #Find build result from build-log.txt
   152  if grep -Fxq "Test Suite Passed" "${BUILD_LOG_PATH}"
   153    then
   154      build_result="SUCCESS"
   155  else
   156      build_result="FAILURE"
   157  fi
   158  
   159  V=4 kube::log::status "Build result is ${build_result}"
   160  
   161  if [[ -e "${ARTIFACTS}/started.json" ]]; then
   162    rm "${ARTIFACTS}/started.json"
   163  fi
   164  
   165  if [[ -e "${ARTIFACTS}/finished.json" ]]; then
   166    rm "${ARTIFACTS}/finished.json"
   167  fi
   168  
   169  V=2 kube::log::status "Constructing started.json and finished.json files"
   170  cat <<EOF >"${ARTIFACTS}/started.json"
   171  {
   172      "version": "${version}",
   173      "timestamp": ${start_time_epoch},
   174      "jenkins-node": "${NODE_NAME:-}"
   175  }
   176  EOF
   177  
   178  
   179  cat <<EOF >"${ARTIFACTS}/finished.json"
   180  {
   181      "result": "${build_result}",
   182      "timestamp": ${end_time_epoch}
   183  }
   184  EOF
   185  
   186  
   187  # Upload started.json
   188  V=2 kube::log::status "Uploading started.json and finished.json"
   189  V=2 kube::log::status "Run started at ${start_time}"
   190  json_file="${GCS_LOGS_PATH}/started.json"
   191  
   192  for upload_attempt in $(seq 3); do
   193    V=2 kube::log::status "Uploading started.json to ${json_file} (attempt ${upload_attempt})"
   194    gsutil -q -h "Content-Type:application/json" cp -a "${gcs_acl}" "${ARTIFACTS}/started.json" \
   195      "${json_file}" || continue
   196    break
   197  done
   198  
   199  # Upload finished.json
   200  for upload_attempt in $(seq 3); do
   201    V=2 kube::log::status "Uploading finished.json to ${GCS_LOGS_PATH} (attempt ${upload_attempt})"
   202    gsutil -q -h "Content-Type:application/json" cp -a "${gcs_acl}" "${ARTIFACTS}/finished.json" \
   203      "${GCS_LOGS_PATH}/finished.json" || continue
   204    break
   205  done
   206  
   207  
   208  echo "Gubernator linked below:"
   209  echo "k8s-gubernator.appspot.com/build/${bucket_name}/logs/e2e-node/${BUILD_STAMP}"