lint-dependencies.sh 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 checks version dependencies of modules. It checks whether all
  16. # pinned versions of checked dependencies match their preferred version or not.
  17. # Usage: `hack/lint-dependencies.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. # 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. case "${1:-}" in
  35. "--all")
  36. echo "Checking all dependencies"
  37. filter=''
  38. ;;
  39. "-a")
  40. echo "Checking all dependencies"
  41. filter=''
  42. ;;
  43. "")
  44. # by default, skip checking golang.org/x/... dependencies... we pin to levels that match our go version for those
  45. echo "Skipping golang.org/x/... dependencies, pass --all to include"
  46. filter='select(.Path | startswith("golang.org/x/") | not) |'
  47. ;;
  48. *)
  49. kube::log::error "Unrecognized arg: ${1}"
  50. exit 1
  51. ;;
  52. esac
  53. outdated=$(go list -m -json all | jq -r "
  54. select(.Replace.Version != null) |
  55. select(.Version != .Replace.Version) |
  56. ${filter}
  57. select(.Path) |
  58. \"\(.Path)
  59. pinned: \(.Replace.Version)
  60. preferred: \(.Version)
  61. hack/pin-dependency.sh \(.Path) \(.Version)\"
  62. ")
  63. if [[ -n "${outdated}" ]]; then
  64. echo "These modules are pinned to versions different than the minimal preferred version."
  65. echo "That means that without replace directives, a different version would be selected,"
  66. echo "which breaks consumers of our published modules."
  67. echo "1. Use hack/pin-dependency.sh to switch to the preferred version for each module"
  68. echo "2. Run hack/update-vendor.sh to rebuild the vendor directory"
  69. echo "3. Run hack/lint-dependencies.sh to verify no additional changes are required"
  70. echo ""
  71. echo "${outdated}"
  72. fi
  73. unused=$(comm -23 \
  74. <(go mod edit -json | jq -r '.Replace[] | select(.New.Version != null) | .Old.Path' | sort) \
  75. <(go list -m -json all | jq -r .Path | sort))
  76. if [[ -n "${unused}" ]]; then
  77. echo ""
  78. echo "Use the given commands to remove pinned module versions that aren't actually used:"
  79. echo "${unused}" | xargs -L 1 echo 'GO111MODULE=on go mod edit -dropreplace'
  80. fi
  81. if [[ -n "${unused}${outdated}" ]]; then
  82. exit 1
  83. fi
  84. echo "All pinned versions of checked dependencies match their preferred version."
  85. exit 0