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

     1  #!/usr/bin/env bash
     2  
     3  # Copyright 2014 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 script checks API-related files for missing descriptions and outputs a
    18  # list of structs and fields that are missing descriptions.
    19  # Usage: `hack/verify-description.sh`.
    20  
    21  set -o errexit
    22  set -o nounset
    23  set -o pipefail
    24  
    25  KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
    26  source "${KUBE_ROOT}/hack/lib/init.sh"
    27  source "${KUBE_ROOT}/hack/lib/util.sh"
    28  
    29  kube::golang::setup_env
    30  
    31  GOPROXY=off go install ./cmd/genswaggertypedocs
    32  
    33  find_files() {
    34    find . -not \( \
    35        \( \
    36          -wholename '.git' \
    37          -o -wholename './_output' \
    38          -o -wholename './release' \
    39          -o -wholename './target' \
    40          -o -wholename '*/third_party/*' \
    41          -o -wholename '*/vendor/*' \
    42          -o -wholename './pkg/*' \
    43        \) -prune \
    44      \) \
    45      \( -wholename './staging/src/k8s.io/api/*/v*/types.go' \
    46         -o -wholename './staging/src/k8s.io/kube-aggregator/pkg/apis/*/v*/types.go' \
    47         -o -wholename './staging/src/k8s.io/apiextensions-apiserver/pkg/apis/*/v*/types.go' \
    48      \)
    49  }
    50  
    51  if [[ $# -eq 0 ]]; then
    52    versioned_api_files=$(find_files) || true
    53  else
    54    versioned_api_files="${*}"
    55  fi
    56  
    57  # find_files had incorrect regexes which led to genswaggertypedocs never being invoked.
    58  # This led to many types.go have missing descriptions.
    59  # These types.go files are listed in hack/.descriptions_failures
    60  # Check that the file is in alphabetical order
    61  failure_file="${KUBE_ROOT}/hack/.descriptions_failures"
    62  kube::util::check-file-in-alphabetical-order "${failure_file}"
    63  
    64  failing_files=()
    65  while IFS='' read -r line; do failing_files+=("$line"); done < <(cat "$failure_file")
    66  
    67  result=0
    68  for file in $versioned_api_files; do
    69    if ! kube::util::array_contains "$file" "${failing_files[@]}"; then
    70      gen_swagger_result=0
    71      genswaggertypedocs -v -s "${file}" -f - || gen_swagger_result=$?
    72      if [[ "${gen_swagger_result}" -ne 0 ]]; then
    73        echo "API file: ${file} is missing: ${gen_swagger_result} descriptions"
    74        result=1
    75      fi
    76    fi
    77  
    78    if grep json: "${file}" | grep -v // | grep description: ; then
    79      echo "API file: ${file} should not contain descriptions in struct tags"
    80      result=1
    81    fi
    82    if grep json: "${file}" | grep -Ee ",[[:space:]]+omitempty|omitempty[[:space:]]+" ; then
    83      echo "API file: ${file} should not contain leading or trailing spaces for omitempty directive"
    84      result=1
    85    fi
    86  done
    87  
    88  internal_types_files="${KUBE_ROOT}/pkg/apis/core/types.go ${KUBE_ROOT}/pkg/apis/extensions/types.go"
    89  for internal_types_file in $internal_types_files; do
    90    if [[ ! -e $internal_types_file ]]; then
    91      echo "Internal types file ${internal_types_file} does not exist"
    92      result=1
    93      continue
    94    fi
    95  
    96    if grep json: "${internal_types_file}" | grep -v // | grep description: ; then
    97      echo "Internal API types should not contain descriptions"
    98      result=1
    99    fi
   100  done
   101  
   102  exit ${result}