verify-generated-swagger-docs.sh 2.8 KB

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