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

     1  #!/usr/bin/env bash
     2  # Copyright 2023 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  
    16  set -o errexit
    17  set -o nounset
    18  set -o pipefail
    19  
    20  KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
    21  source "${KUBE_ROOT}/hack/lib/init.sh"
    22  cd "${KUBE_ROOT}"
    23  
    24  # Files larger than 1MB need to be allowed explicitly.
    25  maxsize=$((1 * 1024 * 1024))
    26  
    27  # Sorted list of those exceptions.
    28  allowlist=(
    29      staging/src/k8s.io/kubectl/images/kubectl-logo-full.png
    30  )
    31  
    32  
    33  # Files larger than 1MB get reported and verification fails, unless the file is
    34  # allowed to be larger. Any output or a non-zero exit status indicate a
    35  # failure.
    36  largefiles () {
    37      # --eol adds information that allows detecting binary files:
    38      #    i/-text w/-text attr/text=auto eol=lf 	test/images/sample-device-plugin/sampledeviceplugin
    39      #
    40      # shellcheck disable=SC2034 # Some variables unused and only present to capture the output field.
    41      git ls-files -cm --exclude-standard ':!:vendor/*' --eol | while read -r index tree attr eol file; do
    42          case "$tree" in
    43              w/-text)
    44                  # Only binary files have a size limit.
    45                  size="$(wc -c < "$file")"
    46                  if [ "${size}" -gt "$maxsize" ] &&
    47                         ! kube::util::array_contains "$file" "${allowlist[@]}"; then
    48                      echo    "$file is too large ($size bytes)"
    49                  fi
    50                  ;;
    51              w/|w/lf|w/crlf|w/mixed|w/none)
    52                  # Other files are okay.
    53                  ;;
    54              *)
    55                  echo "unexpected 'git ls-files --eol' output for $file: $tree"
    56                  ;;
    57          esac
    58      done
    59  }
    60  
    61  if ! result="$(largefiles 2>&1)" || [ "${result}" != "" ]; then
    62      # Help "make verify" and Prow identify the failure message through the "ERROR" prefix.
    63      sed -e 's/^/ERROR: /' <<EOF
    64  $result
    65  
    66  Large binary files should not be committed to git. Exceptions need to be listed in
    67  ${BASH_SOURCE[0]}.
    68  EOF
    69      exit 1
    70  fi