Skip to content

Commit

Permalink
[code-generator] feat: add func for generating register code (kuberne…
Browse files Browse the repository at this point in the history
…tes#124946)

* feat: add func for generating register code

* refactor:remove unused local variable

* fix: make the function name singular

Signed-off-by: Lin Yang <reaver@flomesh.io>

* fix: precisely matching the comment tag for register-gen

Signed-off-by: Lin Yang <reaver@flomesh.io>

---------

Signed-off-by: Lin Yang <reaver@flomesh.io>
  • Loading branch information
reaver-flomesh committed Jun 29, 2024
1 parent 3fdf06a commit 4093c35
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions staging/src/k8s.io/code-generator/kube_codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -665,3 +665,95 @@ function kube::codegen::gen_client() {
"${input_pkgs[@]}"
fi
}

# Generate register code
#
# USAGE: kube::codegen::gen_register [FLAGS] <input-dir>
#
# <input-dir>
# The root directory under which to search for Go files which request code to
# be generated. This must be a local path, not a Go package.
#
# See note at the top about package structure below that.
#
# FLAGS:
#
# --boilerplate <string = path_to_kube_codegen_boilerplate>
# An optional override for the header file to insert into generated files.
#
function kube::codegen::gen_register() {
local in_dir=""
local boilerplate="${KUBE_CODEGEN_ROOT}/hack/boilerplate.go.txt"
local v="${KUBE_VERBOSE:-0}"

while [ "$#" -gt 0 ]; do
case "$1" in
"--boilerplate")
boilerplate="$2"
shift 2
;;
*)
if [[ "$1" =~ ^-- ]]; then
echo "unknown argument: $1" >&2
return 1
fi
if [ -n "$in_dir" ]; then
echo "too many arguments: $1 (already have $in_dir)" >&2
return 1
fi
in_dir="$1"
shift
;;
esac
done

if [ -z "${in_dir}" ]; then
echo "input-dir argument is required" >&2
return 1
fi

(
# To support running this from anywhere, first cd into this directory,
# and then install with forced module mode on and fully qualified name.
cd "${KUBE_CODEGEN_ROOT}"
BINS=(
register-gen
)
# shellcheck disable=2046 # printf word-splitting is intentional
GO111MODULE=on go install $(printf "k8s.io/code-generator/cmd/%s " "${BINS[@]}")
)
# Go installs in $GOBIN if defined, and $GOPATH/bin otherwise
gobin="${GOBIN:-$(go env GOPATH)/bin}"

# Register
#
local input_pkgs=()
while read -r dir; do
pkg="$(cd "${dir}" && GO111MODULE=on go list -find .)"
input_pkgs+=("${pkg}")
done < <(
( kube::codegen::internal::grep -l --null \
-e '^\s*//\s*+groupName' \
-r "${in_dir}" \
--include '*.go' \
|| true \
) | while read -r -d $'\0' F; do dirname "${F}"; done \
| LC_ALL=C sort -u
)

if [ "${#input_pkgs[@]}" != 0 ]; then
echo "Generating register code for ${#input_pkgs[@]} targets"

kube::codegen::internal::findz \
"${in_dir}" \
-type f \
-name zz_generated.register.go \
| xargs -0 rm -f

"${gobin}/register-gen" \
-v "${v}" \
--output-file zz_generated.register.go \
--go-header-file "${boilerplate}" \
"${input_pkgs[@]}"
fi
}

0 comments on commit 4093c35

Please sign in to comment.