verify-vendor.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env bash
  2. # Copyright 2015 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. # This script checks whether fixing of vendor directory or go.mod is needed or
  16. # not. We should run `hack/update-vendor.sh` if actually fixes them.
  17. # Usage: `hack/verify-vendor.sh`.
  18. set -o errexit
  19. set -o nounset
  20. set -o pipefail
  21. KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
  22. source "${KUBE_ROOT}/hack/lib/init.sh"
  23. # create a nice clean place to put our new licenses
  24. # must be in the user dir (e.g. KUBE_ROOT) in order for the docker volume mount
  25. # to work with docker-machine on macs
  26. mkdir -p "${KUBE_ROOT}/_tmp"
  27. _tmpdir="$(mktemp -d "${KUBE_ROOT}/_tmp/kube-vendor.XXXXXX")"
  28. if [[ -z ${KEEP_TMP:-} ]]; then
  29. KEEP_TMP=false
  30. fi
  31. function cleanup {
  32. # make go module dirs writeable
  33. chmod -R +w "${_tmpdir}"
  34. if [ "${KEEP_TMP}" == "true" ]; then
  35. echo "Leaving ${_tmpdir} for you to examine or copy. Please delete it manually when finished. (rm -rf ${_tmpdir})"
  36. else
  37. echo "Removing ${_tmpdir}"
  38. rm -rf "${_tmpdir}"
  39. fi
  40. }
  41. kube::util::trap_add cleanup EXIT
  42. # Copy the contents of the kube directory into the nice clean place (which is NOT shaped like a GOPATH)
  43. _kubetmp="${_tmpdir}"
  44. mkdir -p "${_kubetmp}"
  45. # should create ${_kubectmp}/kubernetes
  46. git archive --format=tar --prefix=kubernetes/ "$(git write-tree)" | (cd "${_kubetmp}" && tar xf -)
  47. _kubetmp="${_kubetmp}/kubernetes"
  48. # Do all our work in module mode
  49. export GO111MODULE=on
  50. pushd "${_kubetmp}" > /dev/null 2>&1
  51. # Destroy deps in the copy of the kube tree
  52. rm -rf ./Godeps/LICENSES ./vendor
  53. # Recreate the vendor tree using the nice clean set we just downloaded
  54. hack/update-vendor.sh
  55. popd > /dev/null 2>&1
  56. ret=0
  57. pushd "${KUBE_ROOT}" > /dev/null 2>&1
  58. # Test for diffs
  59. if ! _out="$(diff -Naupr --ignore-matching-lines='^\s*\"GoVersion\":' go.mod "${_kubetmp}/go.mod")"; then
  60. echo "Your go.mod file is different:" >&2
  61. echo "${_out}" >&2
  62. echo "Vendor Verify failed." >&2
  63. echo "If you're seeing this locally, run the below command to fix your go.mod:" >&2
  64. echo "hack/update-vendor.sh" >&2
  65. ret=1
  66. fi
  67. if ! _out="$(diff -Naupr -x "BUILD" -x "AUTHORS*" -x "CONTRIBUTORS*" vendor "${_kubetmp}/vendor")"; then
  68. echo "Your vendored results are different:" >&2
  69. echo "${_out}" >&2
  70. echo "Vendor Verify failed." >&2
  71. echo "${_out}" > vendordiff.patch
  72. echo "If you're seeing this locally, run the below command to fix your directories:" >&2
  73. echo "hack/update-vendor.sh" >&2
  74. ret=1
  75. fi
  76. # Verify we are pinned to matching levels
  77. hack/lint-dependencies.sh >&2
  78. popd > /dev/null 2>&1
  79. if [[ ${ret} -gt 0 ]]; then
  80. exit ${ret}
  81. fi
  82. echo "Vendor Verified."
  83. # ex: ts=2 sw=2 et filetype=sh