verify-no-vendor-cycles.sh 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env bash
  2. # Copyright 2017 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 packages under `vendor` directory have cyclic
  16. # dependencies on `main` or `staging` repositories.
  17. # Usage: `hack/verify-no-vendor-cycles.sh`.
  18. set -o errexit
  19. set -o nounset
  20. set -o pipefail
  21. set -x;
  22. KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
  23. source "${KUBE_ROOT}/hack/lib/init.sh"
  24. export GO111MODULE=auto
  25. staging_repos=()
  26. kube::util::read-array staging_repos < <(kube::util::list_staging_repos)
  27. staging_repos_pattern=$(IFS="|"; echo "${staging_repos[*]}")
  28. cd "${KUBE_ROOT}"
  29. failed=false
  30. while IFS= read -r dir; do
  31. deps=$(go list -f '{{range .Deps}}{{.}}{{"\n"}}{{end}}' "${dir}" 2> /dev/null || echo "")
  32. deps_on_main=$(echo "${deps}" | grep -v "k8s.io/kubernetes/vendor/" | grep "k8s.io/kubernetes" || echo "")
  33. if [ -n "${deps_on_main}" ]; then
  34. echo "Package ${dir} has a cyclic dependency on the main repository."
  35. failed=true
  36. fi
  37. deps_on_staging=$(echo "${deps}" | grep "k8s.io/kubernetes/vendor/k8s.io" | grep -E "k8s.io\/${staging_repos_pattern}\>" || echo "")
  38. if [ -n "${deps_on_staging}" ]; then
  39. echo "Package ${dir} has a cyclic dependency on staging repository packages: ${deps_on_staging}"
  40. failed=true
  41. fi
  42. done < <(find ./vendor -type d)
  43. if [[ "${failed}" == "true" ]]; then
  44. exit 1
  45. fi