k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/hack/verify-api-groups.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  # Copyright 2016 The Kubernetes Authors.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  # This scripts locates all API groups by their packages and versions
    18  # Usage: `hack/verify-api-groups.sh`.
    19  
    20  set -o errexit
    21  set -o nounset
    22  set -o pipefail
    23  
    24  KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
    25  source "${KUBE_ROOT}/hack/lib/init.sh"
    26  
    27  register_files=()
    28  while read -r file ; do
    29  	register_files+=("${file}")
    30  done < <(find pkg/apis -name register.go | sort)
    31  
    32  # every register file should contain a GroupName.  Gather the different representations.
    33  # 1. group directory name for client gen
    34  # 2. external group versions for init.sh all APIs list
    35  # 3. install packages for inclusion in import_known_versions files
    36  group_dirnames=()
    37  external_group_versions=()
    38  expected_install_packages=()
    39  for register_file in "${register_files[@]}"; do
    40  	package="${register_file%"/register.go"}"
    41  	group_dirname="${package#"pkg/apis/"}"
    42  	group_dirname="${group_dirname%%"/*"}"
    43  	group_name=""
    44  	if grep -q 'GroupName = "' "${register_file}"; then
    45  		group_name=$(grep 'GroupName = "' "${register_file}" | cut -d\" -f2 -)
    46  	else
    47  		echo "${register_file} is missing \"const GroupName =\""
    48  		exit 1
    49  	fi
    50  
    51  	# If the dirname doesn't have a slash, then it's the internal package.
    52  	# if does have one, then it's versioned (e.g. foobar/v1).
    53  	if [[ "${group_dirname#*'/'}" == "${group_dirname}" ]]; then
    54  		group_dirnames+=("${group_dirname}")
    55  		expected_install_packages+=("k8s.io/kubernetes/${package}")
    56  	else
    57  		version=$(echo "${group_dirname}" | cut -d/ -f2 -)
    58  		external_group_versions+=("${group_name}/${version}")
    59  	fi
    60  done
    61  
    62  
    63  # check to make sure that client gen is getting
    64  # groups_without_codegen is the list of group we EXPECT to not have the client generated for
    65  # them.  This happens for types that aren't served from the API server
    66  groups_without_codegen=(
    67  	"abac"
    68  	"imagepolicy"
    69  	"admission"
    70  )
    71  client_gen_file="${KUBE_ROOT}/staging/src/k8s.io/code-generator/cmd/client-gen/main.go"
    72  
    73  for group_dirname in "${group_dirnames[@]}"; do
    74  	if ! grep -q "${group_dirname}/" "${client_gen_file}" ; then
    75  		found=0
    76  		for group_without_codegen in "${groups_without_codegen[@]}"; do
    77  			if [[ "${group_without_codegen}" == "${group_dirname}" ]]; then
    78  				found=1
    79  			fi
    80  		done
    81  		if [[ "${found}" -ne "1" && -f "${group_dirname}/types.go" ]] ; then
    82  			echo "need to add ${group_dirname}/ to ${client_gen_file}"
    83  			exit 1
    84  		fi
    85  	fi
    86  done
    87  
    88  # import_known_versions checks to be sure we'll get installed
    89  # groups_without_codegen is the list of group we EXPECT to not have the client generated for
    90  # them.  This happens for types that aren't served from the API server
    91  packages_without_install=(
    92  	"k8s.io/kubernetes/pkg/apis/abac"
    93  	"k8s.io/kubernetes/pkg/apis/admission"
    94  	"k8s.io/kubernetes/pkg/apis/apidiscovery"
    95  )
    96  known_version_files=(
    97  	"pkg/controlplane/import_known_versions.go"
    98  )
    99  for expected_install_package in "${expected_install_packages[@]}"; do
   100  	found=0
   101  	for package_without_install in "${packages_without_install[@]}"; do
   102  		if [ "${package_without_install}" == "${expected_install_package}" ]; then
   103  			found=1
   104  		fi
   105  	done
   106  	if [[ "${found}" -eq "1" ]] ; then
   107  		continue
   108  	fi
   109  
   110  	for known_version_file in "${known_version_files[@]}"; do
   111  		if ! grep -q "${expected_install_package}/install" "${known_version_file}" ; then
   112  			echo "missing ${expected_install_package}/install from ${known_version_file}"
   113  			exit 1
   114  		fi
   115  	done
   116  done
   117  
   118  # check all groupversions to make sure they're in the init.sh file.  This isn't perfect, but its slightly
   119  # better than nothing
   120  for external_group_version in "${external_group_versions[@]}"; do
   121  	if ! grep -q "${external_group_version}" "${KUBE_ROOT}/hack/lib/init.sh" ; then
   122  		echo "missing ${external_group_version} from hack/lib/init.sh:/KUBE_AVAILABLE_GROUP_VERSIONS or hack/init.sh:/KUBE_NONSERVER_GROUP_VERSIONS"
   123  		exit 1
   124  	fi
   125  done