swagger.sh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/bin/bash
  2. # Copyright 2016 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. # Contains swagger related util functions.
  16. #
  17. set -o errexit
  18. set -o nounset
  19. set -o pipefail
  20. # The root of the build/dist directory
  21. KUBE_ROOT="$(cd "$(dirname "${BASH_SOURCE}")/../.." && pwd -P)"
  22. # Generates types_swagger_doc_generated file for the given group version.
  23. # $1: Name of the group version
  24. # $2: Path to the directory where types.go for that group version exists. This
  25. # is the directory where the file will be generated.
  26. kube::swagger::gen_types_swagger_doc() {
  27. local group_version=$1
  28. local gv_dir=$2
  29. local TMPFILE="/tmp/types_swagger_doc_generated.$(date +%s).go"
  30. echo "Generating swagger type docs for ${group_version} at ${gv_dir}"
  31. sed 's/YEAR/2016/' hack/boilerplate/boilerplate.go.txt > "$TMPFILE"
  32. echo "package ${group_version##*/}" >> "$TMPFILE"
  33. cat >> "$TMPFILE" <<EOF
  34. // This file contains a collection of methods that can be used from go-restful to
  35. // generate Swagger API documentation for its models. Please read this PR for more
  36. // information on the implementation: https://github.com/emicklei/go-restful/pull/215
  37. //
  38. // TODOs are ignored from the parser (e.g. TODO(andronat):... || TODO:...) if and only if
  39. // they are on one line! For multiple line or blocks that you want to ignore use ---.
  40. // Any context after a --- is ignored.
  41. //
  42. // Those methods can be generated by using hack/update-generated-swagger-docs.sh
  43. // AUTO-GENERATED FUNCTIONS START HERE
  44. EOF
  45. go run cmd/genswaggertypedocs/swagger_type_docs.go -s \
  46. "${gv_dir}/types.go" \
  47. -f - \
  48. >> "$TMPFILE"
  49. echo "// AUTO-GENERATED FUNCTIONS END HERE" >> "$TMPFILE"
  50. gofmt -w -s "$TMPFILE"
  51. mv "$TMPFILE" ""${gv_dir}"/types_swagger_doc_generated.go"
  52. }
  53. # Generates API reference docs for the given API group versions.
  54. # Required env vars:
  55. # GROUP_VERSIONS: Array of group versions to be included in the reference
  56. # docs.
  57. # GV_DIRS: Array of root directories for those group versions.
  58. # Input vars:
  59. # $1: Root directory path for swagger spec
  60. # $2: Root directory path where the reference docs should be generated.
  61. kube::swagger::gen_api_ref_docs() {
  62. : "${GROUP_VERSIONS?Must set GROUP_VERSIONS env var}"
  63. : "${GV_DIRS?Must set GV_DIRS env var}"
  64. echo "Generating API reference docs for group versions: ${GROUP_VERSIONS[@]}, at dirs: ${GV_DIRS[@]}"
  65. GROUP_VERSIONS=(${GROUP_VERSIONS[@]})
  66. GV_DIRS=(${GV_DIRS[@]})
  67. local swagger_spec_path=${1}
  68. local output_dir=${2}
  69. echo "Reading swagger spec from: ${swagger_spec_path}"
  70. echo "Generating the docs at: ${output_dir}"
  71. # Use REPO_DIR if provided so we can set it to the host-resolvable path
  72. # to the repo root if we are running this script from a container with
  73. # docker mounted in as a volume.
  74. # We pass the host output dir as the source dir to `docker run -v`, but use
  75. # the regular one to compute diff (they will be the same if running this
  76. # test on the host, potentially different if running in a container).
  77. local repo_dir=${REPO_DIR:-"${KUBE_ROOT}"}
  78. local tmp_subpath="_output/generated_html"
  79. local output_tmp_in_host="${repo_dir}/${tmp_subpath}"
  80. local output_tmp="${KUBE_ROOT}/${tmp_subpath}"
  81. echo "Generating api reference docs at ${output_tmp}"
  82. for ver in "${GROUP_VERSIONS[@]}"; do
  83. mkdir -p "${output_tmp}/${ver}"
  84. done
  85. user_flags="-u $(id -u)"
  86. if [[ $(uname) == "Darwin" ]]; then
  87. # mapping in a uid from OS X doesn't make any sense
  88. user_flags=""
  89. fi
  90. for i in "${!GROUP_VERSIONS[@]}"; do
  91. local ver=${GROUP_VERSIONS[i]}
  92. local dir=${GV_DIRS[i]}
  93. local tmp_in_host="${output_tmp_in_host}/${ver}"
  94. local register_file="${dir}/register.go"
  95. local swagger_json_name="$(kube::util::gv-to-swagger-name "${ver}")"
  96. docker run ${user_flags} \
  97. --rm -v "${tmp_in_host}":/output:z \
  98. -v "${swagger_spec_path}":/swagger-source:z \
  99. -v "${register_file}":/register.go:z \
  100. --net=host -e "https_proxy=${KUBERNETES_HTTPS_PROXY:-}" \
  101. gcr.io/google_containers/gen-swagger-docs:v8 \
  102. "${swagger_json_name}"
  103. done
  104. # Check if we actually changed anything
  105. pushd "${output_tmp}" > /dev/null
  106. touch .generated_html
  107. find . -type f | cut -sd / -f 2- | LC_ALL=C sort > .generated_html
  108. popd > /dev/null
  109. while read file; do
  110. if [[ -e "${output_dir}/${file}" && -e "${output_tmp}/${file}" ]]; then
  111. echo "comparing ${output_dir}/${file} with ${output_tmp}/${file}"
  112. # Filter all munges from original content.
  113. original=$(cat "${output_dir}/${file}")
  114. generated=$(cat "${output_tmp}/${file}")
  115. # Filter out meaningless lines with timestamps
  116. original=$(echo "${original}" | grep -v "Last updated" || :)
  117. generated=$(echo "${generated}" | grep -v "Last updated" || :)
  118. # By now, the contents should be normalized and stripped of any
  119. # auto-managed content.
  120. if diff -B >/dev/null <(echo "${original}") <(echo "${generated}"); then
  121. # actual contents same, overwrite generated with original.
  122. cp "${output_dir}/${file}" "${output_tmp}/${file}"
  123. fi
  124. fi
  125. done <"${output_tmp}/.generated_html"
  126. echo "Moving api reference docs from ${output_tmp} to ${output_dir}"
  127. cp -af "${output_tmp}"/* "${output_dir}"
  128. rm -r "${output_tmp}"
  129. }