k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/hack/verify-vendor.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  # Copyright 2015 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  # This script checks whether fixing of vendor directory or go.mod is needed or
    18  # not. We should run `hack/update-vendor.sh` if actually fixes them.
    19  # Usage: `hack/verify-vendor.sh`.
    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/init.sh"
    27  
    28  kube::golang::setup_env
    29  
    30  # create a nice clean place to put our new vendor tree
    31  _tmpdir="$(kube::realpath "$(mktemp -d -t "$(basename "$0").XXXXXX")")"
    32  
    33  if [[ -z ${KEEP_TMP:-} ]]; then
    34      KEEP_TMP=false
    35  fi
    36  
    37  function cleanup {
    38    # make go module dirs writeable
    39    chmod -R +w "${_tmpdir}"
    40    if [ "${KEEP_TMP}" == "true" ]; then
    41      echo "Leaving ${_tmpdir} for you to examine or copy. Please delete it manually when finished. (rm -rf ${_tmpdir})"
    42    else
    43      echo "Removing ${_tmpdir}"
    44      rm -rf "${_tmpdir}"
    45    fi
    46  }
    47  kube::util::trap_add cleanup EXIT
    48  
    49  # Copy the contents of the kube directory into the nice clean place (which is NOT shaped like a GOPATH)
    50  _kubetmp="${_tmpdir}/kubernetes"
    51  mkdir -p "${_kubetmp}"
    52  tar --exclude=.git --exclude="./_*" -c . | (cd "${_kubetmp}" && tar xf -)
    53  
    54  pushd "${_kubetmp}" > /dev/null 2>&1
    55    # Destroy deps in the copy of the kube tree
    56    rm -rf ./vendor ./LICENSES
    57  
    58    # Recreate the vendor tree using the nice clean set we just downloaded
    59    hack/update-vendor.sh
    60  popd > /dev/null 2>&1
    61  
    62  ret=0
    63  
    64  pushd "${KUBE_ROOT}" > /dev/null 2>&1
    65    # Test for diffs
    66    if ! _out="$(diff -Naupr --ignore-matching-lines='^\s*\"GoVersion\":' go.mod "${_kubetmp}/go.mod")"; then
    67      echo "Your go.mod file is different:" >&2
    68      echo "${_out}" >&2
    69      echo "Vendor Verify failed." >&2
    70      echo "If you're seeing this locally, run the below command to fix your go.mod:" >&2
    71      echo "hack/update-vendor.sh" >&2
    72      ret=1
    73    fi
    74  
    75    if ! _out="$(diff -Naupr -x "AUTHORS*" -x "CONTRIBUTORS*" vendor "${_kubetmp}/vendor")"; then
    76      echo "Your vendored results are different:" >&2
    77      echo "${_out}" >&2
    78      echo "Vendor Verify failed." >&2
    79      echo "${_out}" > vendordiff.patch
    80      echo "If you're seeing this locally, run the below command to fix your directories:" >&2
    81      echo "hack/update-vendor.sh" >&2
    82      ret=1
    83    fi
    84  
    85    # Verify we are pinned to matching levels
    86    hack/lint-dependencies.sh >&2
    87  popd > /dev/null 2>&1
    88  
    89  if [[ ${ret} -gt 0 ]]; then
    90    exit ${ret}
    91  fi
    92  
    93  # Ensure we can tidy every repo using only its recorded versions
    94  for repo in $(kube::util::list_staging_repos); do
    95    pushd "${_kubetmp}/staging/src/k8s.io/${repo}" >/dev/null 2>&1
    96      echo "Tidying k8s.io/${repo}..."
    97      GODEBUG=gocacheverify=1 go mod tidy
    98    popd >/dev/null 2>&1
    99  done
   100  pushd "${_kubetmp}" >/dev/null 2>&1
   101    echo "Tidying k8s.io/kubernetes..."
   102    GODEBUG=gocacheverify=1 go mod tidy
   103  popd >/dev/null 2>&1
   104  
   105  echo "Vendor Verified."
   106  # ex: ts=2 sw=2 et filetype=sh