pin-dependency.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/env bash
  2. # Copyright 2019 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. set -o errexit
  16. set -o nounset
  17. set -o pipefail
  18. KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
  19. source "${KUBE_ROOT}/hack/lib/init.sh"
  20. # Usage:
  21. # hack/pin-dependency.sh $MODULE $SHA-OR-TAG
  22. #
  23. # Example:
  24. # hack/pin-dependency.sh github.com/docker/docker 501cb131a7b7
  25. # Explicitly opt into go modules, even though we're inside a GOPATH directory
  26. export GO111MODULE=on
  27. # Explicitly clear GOFLAGS, since GOFLAGS=-mod=vendor breaks dependency resolution while rebuilding vendor
  28. export GOFLAGS=
  29. # Detect problematic GOPROXY settings that prevent lookup of dependencies
  30. if [[ "${GOPROXY:-}" == "off" ]]; then
  31. kube::log::error "Cannot run with \$GOPROXY=off"
  32. exit 1
  33. fi
  34. kube::golang::verify_go_version
  35. kube::util::require-jq
  36. dep="${1:-}"
  37. sha="${2:-}"
  38. if [[ -z "${dep}" || -z "${sha}" ]]; then
  39. echo "Usage:"
  40. echo " hack/pin-dependency.sh \$MODULE \$SHA-OR-TAG"
  41. echo ""
  42. echo "Example:"
  43. echo " hack/pin-dependency.sh github.com/docker/docker 501cb131a7b7"
  44. echo ""
  45. exit 1
  46. fi
  47. _tmp="${KUBE_ROOT}/_tmp"
  48. cleanup() {
  49. rm -rf "${_tmp}"
  50. }
  51. trap "cleanup" EXIT SIGINT
  52. cleanup
  53. mkdir -p "${_tmp}"
  54. # Add the require directive
  55. echo "Running: go get ${dep}@${sha}"
  56. go get -m -d "${dep}@${sha}"
  57. # Find the resolved version
  58. rev=$(go mod edit -json | jq -r ".Require[] | select(.Path == \"${dep}\") | .Version")
  59. # No entry in go.mod, we must be using the natural version indirectly
  60. if [[ -z "${rev}" ]]; then
  61. # backup the go.mod file, since go list modifies it
  62. cp go.mod "${_tmp}/go.mod.bak"
  63. # find the revision
  64. rev=$(go list -m -json "${dep}" | jq -r .Version)
  65. # restore the go.mod file
  66. mv "${_tmp}/go.mod.bak" go.mod
  67. fi
  68. # No entry found
  69. if [[ -z "${rev}" ]]; then
  70. echo "Could not resolve ${sha}"
  71. exit 1
  72. fi
  73. echo "Resolved to ${dep}@${rev}"
  74. # Add the replace directive
  75. echo "Running: go mod edit -replace ${dep}=${dep}@${rev}"
  76. go mod edit -replace "${dep}=${dep}@${rev}"
  77. # Propagate pinned version to staging repos that also have that dependency
  78. for repo in $(ls staging/src/k8s.io | sort); do
  79. pushd "staging/src/k8s.io/${repo}" >/dev/null 2>&1
  80. if go mod edit -json | jq -e -r ".Require[] | select(.Path == \"${dep}\")" > /dev/null; then
  81. go mod edit -require "${dep}@${rev}"
  82. go mod edit -replace "${dep}=${dep}@${rev}"
  83. fi
  84. popd >/dev/null 2>&1
  85. done
  86. echo ""
  87. echo "Run hack/update-vendor.sh to rebuild the vendor directory"