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