pin-dependency.sh 3.0 KB

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