verify-generated-swagger-docs.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 updating of swagger type documentation is needed or
  16. # not. We should run `hack/update-generated-swagger-docs.sh` if swagger type
  17. # documentation is out of date.
  18. # Usage: `hack/verify-generated-swagger-docs.sh`.
  19. set -o errexit
  20. set -o nounset
  21. set -o pipefail
  22. KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
  23. source "${KUBE_ROOT}/hack/lib/init.sh"
  24. kube::golang::setup_env
  25. make -C "${KUBE_ROOT}" WHAT=cmd/genswaggertypedocs
  26. # Find binary
  27. genswaggertypedocs=$(kube::util::find-binary "genswaggertypedocs")
  28. if [[ ! -x "$genswaggertypedocs" ]]; then
  29. {
  30. echo "It looks as if you don't have a compiled genswaggertypedocs binary"
  31. echo
  32. echo "If you are running from a clone of the git repo, please run"
  33. echo "'make WHAT=cmd/genswaggertypedocs'."
  34. } >&2
  35. exit 1
  36. fi
  37. _tmpdir="$(kube::realpath "$(mktemp -d -t swagger-docs.XXXXXX)")"
  38. function swagger_cleanup {
  39. rm -rf "${_tmpdir}"
  40. }
  41. kube::util::trap_add swagger_cleanup EXIT
  42. # Copy the contents of the kube directory into the nice clean place
  43. _kubetmp="${_tmpdir}/src/k8s.io"
  44. mkdir -p "${_kubetmp}"
  45. # should create ${_kubetmp}/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 the new GOPATH
  49. export GOPATH="${_tmpdir}"
  50. find_files() {
  51. find . -not \( \
  52. \( \
  53. -wholename './output' \
  54. -o -wholename './.git' \
  55. -o -wholename './_output' \
  56. -o -wholename './_gopath' \
  57. -o -wholename './release' \
  58. -o -wholename './target' \
  59. -o -wholename '*/third_party/*' \
  60. -o -wholename '*/vendor/*' \
  61. -o -wholename './staging/src/k8s.io/client-go/*vendor/*' \
  62. \) -prune \
  63. \) -name 'types_swagger_doc_generated.go'
  64. }
  65. while IFS=$'\n' read -r line; do TARGET_FILES+=("$line"); done < <(find_files)
  66. pushd "${_kubetmp}" > /dev/null 2>&1
  67. # Update the generated swagger docs
  68. hack/update-generated-swagger-docs.sh
  69. popd > /dev/null 2>&1
  70. ret=0
  71. pushd "${KUBE_ROOT}" > /dev/null 2>&1
  72. # Test for diffs
  73. _output=""
  74. for file in ${TARGET_FILES[*]}; do
  75. _output="${_output}$(diff -Naupr -I 'Auto generated by' "${KUBE_ROOT}/${file}" "${_kubetmp}/${file}")" || ret=1
  76. done
  77. if [[ ${ret} -gt 0 ]]; then
  78. echo "Generated swagger type documentation is out of date:" >&2
  79. echo "${_output}" >&2
  80. exit ${ret}
  81. fi
  82. popd > /dev/null 2>&1
  83. echo "Generated swagger type documentation up to date."