...

Text file src/k8s.io/kubernetes/hack/verify-api-groups.sh

Documentation: k8s.io/kubernetes/hack

     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
    20set -o errexit
    21set -o nounset
    22set -o pipefail
    23
    24KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
    25source "${KUBE_ROOT}/hack/lib/init.sh"
    26
    27register_files=()
    28while read -r file ; do
    29	register_files+=("${file}")
    30done < <(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
    36group_dirnames=()
    37external_group_versions=()
    38expected_install_packages=()
    39for 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
    60done
    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
    66groups_without_codegen=(
    67	"abac"
    68	"componentconfig"
    69	"imagepolicy"
    70	"admission"
    71)
    72client_gen_file="${KUBE_ROOT}/staging/src/k8s.io/code-generator/cmd/client-gen/main.go"
    73
    74for group_dirname in "${group_dirnames[@]}"; do
    75	if ! grep -q "${group_dirname}/" "${client_gen_file}" ; then
    76		found=0
    77		for group_without_codegen in "${groups_without_codegen[@]}"; do
    78			if [[ "${group_without_codegen}" == "${group_dirname}" ]]; then
    79				found=1
    80			fi
    81		done
    82		if [[ "${found}" -ne "1" && -f "${group_dirname}/types.go" ]] ; then
    83			echo "need to add ${group_dirname}/ to ${client_gen_file}"
    84			exit 1
    85		fi
    86	fi
    87done
    88
    89# import_known_versions checks to be sure we'll get installed
    90# groups_without_codegen is the list of group we EXPECT to not have the client generated for
    91# them.  This happens for types that aren't served from the API server
    92packages_without_install=(
    93	"k8s.io/kubernetes/pkg/apis/abac"
    94	"k8s.io/kubernetes/pkg/apis/admission"
    95	"k8s.io/kubernetes/pkg/apis/apidiscovery"
    96	"k8s.io/kubernetes/pkg/apis/componentconfig" # TODO: Remove this package completely and from this list
    97)
    98known_version_files=(
    99	"pkg/controlplane/import_known_versions.go"
   100)
   101for expected_install_package in "${expected_install_packages[@]}"; do
   102	found=0
   103	for package_without_install in "${packages_without_install[@]}"; do
   104		if [ "${package_without_install}" == "${expected_install_package}" ]; then
   105			found=1
   106		fi
   107	done
   108	if [[ "${found}" -eq "1" ]] ; then
   109		continue
   110	fi
   111
   112	for known_version_file in "${known_version_files[@]}"; do
   113		if ! grep -q "${expected_install_package}/install" "${known_version_file}" ; then
   114			echo "missing ${expected_install_package}/install from ${known_version_file}"
   115			exit 1
   116		fi
   117	done
   118done
   119
   120# check all groupversions to make sure they're in the init.sh file.  This isn't perfect, but its slightly
   121# better than nothing
   122for external_group_version in "${external_group_versions[@]}"; do
   123	if ! grep -q "${external_group_version}" "${KUBE_ROOT}/hack/lib/init.sh" ; then
   124		echo "missing ${external_group_version} from hack/lib/init.sh:/KUBE_AVAILABLE_GROUP_VERSIONS or hack/init.sh:/KUBE_NONSERVER_GROUP_VERSIONS"
   125		exit 1
   126	fi
   127done

View as plain text