Simplify find in codegen

The `find` tool has hard to comprehend syntax and does not consider
things excluded by .gitignore.  I keep tripping over this in my own
repos, where I have __stuff which gets found.

This converts update-codegen to use `git ls-files` in a seemingly
equivalent way (`-cmo --exclude-standard`).  I verified it finds the
same set of files as before.

This also drops some obsolete filtering.

Also hide grep errors for not-found files, which can happen if a file is
removed but git ls-files still knows it.

Re-running update-codegen shows no diffs.

This will make subsequent changes easier.
This commit is contained in:
Tim Hockin 2023-01-04 23:32:44 -08:00
parent 04c80d0a23
commit b201c08cea
No known key found for this signature in database

View File

@ -42,65 +42,40 @@ if [[ "${DBG_CODEGEN}" == 1 ]]; then
kube::log::status "DBG: starting generated_files"
fi
# This is a partial 'find' command. The caller is expected to pass the
# remaining arguments.
#
# Example:
# kfind -type f -name foobar.go
function kfind() {
# We want to include the "special" vendor directories which are actually
# part of the Kubernetes source tree (./staging/*) but we need them to be
# named as their ./vendor/* equivalents. Also, we do not want all of
# ./vendor nor ./hack/tools/vendor nor even all of ./vendor/k8s.io.
find -H . \
\( \
-not \( \
\( \
-name '_*' -o \
-name '.[^.]*' -o \
\( \
-name 'vendor' \
-type d \
\) -o \
\( \
-name 'testdata' \
-type d \
\) \
\) -prune \
\) \
\) \
"$@" \
| sed 's|^./staging/src|vendor|'
function git_find() {
# Similar to find but faster and easier to understand. We want to include
# modified and untracked files because this might be running against code
# which is not tracked by git yet.
git ls-files -cmo --exclude-standard "$@"
}
function find_all_go_dirs() {
kfind -type f -name \*.go \
| sed 's|/[^/]*$||' \
| sed 's|^./||' \
| LC_ALL=C sort -u
function git_grep() {
# We want to include modified and untracked files because this might be
# running against code which is not tracked by git yet.
git grep --untracked "$@"
}
# This variable holds a list of every directory that contains Go files in this
# project. Other rules and variables can use this as a starting point to
# reduce filesystem accesses.
if [[ "${DBG_CODEGEN}" == 1 ]]; then
kube::log::status "DBG: finding all *.go dirs"
fi
ALL_GO_DIRS=()
kube::util::read-array ALL_GO_DIRS < <(find_all_go_dirs)
if [[ "${DBG_CODEGEN}" == 1 ]]; then
kube::log::status "DBG: found ${#ALL_GO_DIRS[@]} *.go dirs"
fi
# Generate a list of all files that have a `+k8s:` comment-tag. This will be
# used to derive lists of files/dirs for generation tools.
#
# We want to include the "special" vendor directories which are actually part
# of the Kubernetes source tree (staging/*) but we need them to be named as
# their vendor/* equivalents. We do not want all of vendor nor
# hack/tools/vendor nor even all of vendor/k8s.io - just the subset that lives
# in staging.
if [[ "${DBG_CODEGEN}" == 1 ]]; then
kube::log::status "DBG: finding all +k8s: tags"
fi
ALL_K8S_TAG_FILES=()
kube::util::read-array ALL_K8S_TAG_FILES < <(
find "${ALL_GO_DIRS[@]}" -maxdepth 1 -type f -name \*.go -print0 \
| xargs -0 grep --color=never -l '^// *+k8s:')
git_grep -l \
-e '^// *+k8s:' `# match +k8s: tags` \
-- \
':!:vendor/*' `# not under vendor` \
':!:*/testdata/*' `# not under any testdata` \
':(glob)**/*.go' `# in any *.go file` \
| sed 's|^staging/src|vendor|' `# see comments above` \
)
if [[ "${DBG_CODEGEN}" == 1 ]]; then
kube::log::status "DBG: found ${#ALL_K8S_TAG_FILES[@]} +k8s: tagged files"
fi
@ -593,13 +568,11 @@ function codegen::applyconfigs() {
local applyconfigurationgen
applyconfigurationgen=$(kube::util::find-binary "applyconfiguration-gen")
# because client-gen doesn't do policy/v1alpha1, we have to skip it too
local ext_apis=()
kube::util::read-array ext_apis < <(
cd "${KUBE_ROOT}/staging/src"
find k8s.io/api -name types.go -print0 \
git_find -z ':(glob)k8s.io/api/**/types.go' \
| xargs -0 -n1 dirname \
| grep -v pkg.apis.policy.v1alpha1 \
| LC_ALL=C sort -u)
ext_apis+=("k8s.io/apimachinery/pkg/apis/meta/v1")
@ -681,7 +654,7 @@ function codegen::listers() {
local ext_apis=()
kube::util::read-array ext_apis < <(
cd "${KUBE_ROOT}/staging/src"
find k8s.io/api -name types.go -print0 \
git_find -z ':(glob)k8s.io/api/**/types.go' \
| xargs -0 -n1 dirname \
| LC_ALL=C sort -u)
@ -712,13 +685,11 @@ function codegen::informers() {
local informergen
informergen=$(kube::util::find-binary "informer-gen")
# because client-gen doesn't do policy/v1alpha1, we have to skip it too
local ext_apis=()
kube::util::read-array ext_apis < <(
cd "${KUBE_ROOT}/staging/src"
find k8s.io/api -name types.go -print0 \
git_find -z ':(glob)k8s.io/api/**/types.go' \
| xargs -0 -n1 dirname \
| grep -v pkg.apis.policy.v1alpha1 \
| LC_ALL=C sort -u)
kube::log::status "Generating informer code for ${#ext_apis[@]} targets"