verify-description.sh 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env bash
  2. # Copyright 2014 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 API-related files for missing descriptions and outputs a
  16. # list of structs and fields that are missing descriptions.
  17. # Usage: `hack/verify-description.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. kube::golang::setup_env
  24. make -C "${KUBE_ROOT}" WHAT=cmd/genswaggertypedocs
  25. # Find binary
  26. genswaggertypedocs=$(kube::util::find-binary "genswaggertypedocs")
  27. gen_swagger_result=0
  28. result=0
  29. find_files() {
  30. find . -not \( \
  31. \( \
  32. -wholename './output' \
  33. -o -wholename './_output' \
  34. -o -wholename './_gopath' \
  35. -o -wholename './release' \
  36. -o -wholename './target' \
  37. -o -wholename '*/third_party/*' \
  38. -o -wholename '*/vendor/*' \
  39. \) -prune \
  40. \) \
  41. \( -wholename '*pkg/apis/*/v*/types.go' \
  42. -o -wholename '*pkg/api/unversioned/types.go' \
  43. \)
  44. }
  45. if [[ $# -eq 0 ]]; then
  46. versioned_api_files=$(find_files | grep -E "pkg/.[^/]*/((v.[^/]*)|unversioned)/types\.go") || true
  47. else
  48. versioned_api_files="${*}"
  49. fi
  50. for file in $versioned_api_files; do
  51. $genswaggertypedocs -v -s "${file}" -f - || gen_swagger_result=$?
  52. if [[ "${gen_swagger_result}" -ne "0" ]]; then
  53. echo "API file: ${file} is missing: ${gen_swagger_result} descriptions"
  54. result=1
  55. fi
  56. if grep json: "${file}" | grep -v // | grep description: ; then
  57. echo "API file: ${file} should not contain descriptions in struct tags"
  58. result=1
  59. fi
  60. if grep json: "${file}" | grep -Ee ",[[:space:]]+omitempty|omitempty[[:space:]]+" ; then
  61. echo "API file: ${file} should not contain leading or trailing spaces for omitempty directive"
  62. result=1
  63. fi
  64. done
  65. internal_types_files="${KUBE_ROOT}/pkg/apis/core/types.go ${KUBE_ROOT}/pkg/apis/extensions/types.go"
  66. for internal_types_file in $internal_types_files; do
  67. if [[ ! -e $internal_types_file ]]; then
  68. echo "Internal types file ${internal_types_file} does not exist"
  69. result=1
  70. continue
  71. fi
  72. if grep json: "${internal_types_file}" | grep -v // | grep description: ; then
  73. echo "Internal API types should not contain descriptions"
  74. result=1
  75. fi
  76. done
  77. exit ${result}