verify-api-groups.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/env 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. # This scripts locates all API groups by their packages and versions
  16. # Usage: `hack/verify-api-groups.sh`.
  17. set -o errexit
  18. set -o nounset
  19. set -o pipefail
  20. KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
  21. source "${KUBE_ROOT}/hack/lib/init.sh"
  22. prefix="${KUBE_ROOT%"k8s.io/kubernetes"}"
  23. register_files=()
  24. while IFS= read -d $'\0' -r file ; do
  25. register_files+=("${file}")
  26. done < <(find "${KUBE_ROOT}"/pkg/apis -name register.go -print0)
  27. # every register file should contain a GroupName. Gather the different representations.
  28. # 1. group directory name for client gen
  29. # 2. external group versions for init.sh all APIs list
  30. # 3. install packages for inclusion in import_known_versions files
  31. group_dirnames=()
  32. external_group_versions=()
  33. expected_install_packages=()
  34. for register_file in "${register_files[@]}"; do
  35. package="${register_file#"${prefix}"}"
  36. package="${package%"/register.go"}"
  37. group_dirname="${package#"k8s.io/kubernetes/pkg/apis/"}"
  38. group_dirname="${group_dirname%%"/*"}"
  39. group_name=""
  40. if grep -q 'GroupName = "' "${register_file}"; then
  41. group_name=$(grep 'GroupName = "' "${register_file}" | cut -d\" -f2 -)
  42. else
  43. echo "${register_file} is missing \"const GroupName =\""
  44. exit 1
  45. fi
  46. # does the dirname doesn't have a slash, then it's the internal package.
  47. # if does have one, then its an external
  48. if [[ "${group_dirname#*'/'}" == "${group_dirname}" ]]; then
  49. group_dirnames+=("${group_dirname}")
  50. expected_install_packages+=("${package}")
  51. else
  52. version=$(echo "${group_dirname}" | cut -d/ -f2 -)
  53. external_group_versions+=("${group_name}/${version}")
  54. fi
  55. done
  56. # check to make sure that client gen is getting
  57. # groups_without_codegen is the list of group we EXPECT to not have the client generated for
  58. # them. This happens for types that aren't served from the API server
  59. groups_without_codegen=(
  60. "abac"
  61. "componentconfig"
  62. "imagepolicy"
  63. "admission"
  64. )
  65. client_gen_file="${KUBE_ROOT}/vendor/k8s.io/code-generator/cmd/client-gen/main.go"
  66. for group_dirname in "${group_dirnames[@]}"; do
  67. if ! grep -q "${group_dirname}/" "${client_gen_file}" ; then
  68. found=0
  69. for group_without_codegen in "${groups_without_codegen[@]}"; do
  70. if [[ "${group_without_codegen}" == "${group_dirname}" ]]; then
  71. found=1
  72. fi
  73. done
  74. if [[ "${found}" -ne "1" && -f "${group_dirname}/types.go" ]] ; then
  75. echo "need to add ${group_dirname}/ to ${client_gen_file}"
  76. exit 1
  77. fi
  78. fi
  79. done
  80. # import_known_versions checks to be sure we'll get installed
  81. # groups_without_codegen is the list of group we EXPECT to not have the client generated for
  82. # them. This happens for types that aren't served from the API server
  83. packages_without_install=(
  84. "k8s.io/kubernetes/pkg/apis/abac"
  85. "k8s.io/kubernetes/pkg/apis/admission"
  86. "k8s.io/kubernetes/pkg/apis/componentconfig" # TODO: Remove this package completely and from this list
  87. )
  88. known_version_files=(
  89. "pkg/master/import_known_versions.go"
  90. )
  91. for expected_install_package in "${expected_install_packages[@]}"; do
  92. found=0
  93. for package_without_install in "${packages_without_install[@]}"; do
  94. if [ "${package_without_install}" == "${expected_install_package}" ]; then
  95. found=1
  96. fi
  97. done
  98. if [[ "${found}" -eq "1" ]] ; then
  99. continue
  100. fi
  101. for known_version_file in "${known_version_files[@]}"; do
  102. if ! grep -q "${expected_install_package}/install" "${known_version_file}" ; then
  103. echo "missing ${expected_install_package}/install from ${known_version_file}"
  104. exit 1
  105. fi
  106. done
  107. done
  108. # check all groupversions to make sure they're in the init.sh file. This isn't perfect, but its slightly
  109. # better than nothing
  110. for external_group_version in "${external_group_versions[@]}"; do
  111. if ! grep -q "${external_group_version}" "${KUBE_ROOT}/hack/lib/init.sh" ; then
  112. echo "missing ${external_group_version} from hack/lib/init.sh:/KUBE_AVAILABLE_GROUP_VERSIONS or hack/init.sh:/KUBE_NONSERVER_GROUP_VERSIONS"
  113. exit 1
  114. fi
  115. done