Skip to content

Commit

Permalink
Replace shell code with golang tooling
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed Jan 19, 2024
1 parent 2831fdd commit 0615e85
Showing 1 changed file with 3 additions and 166 deletions.
169 changes: 3 additions & 166 deletions staging/src/k8s.io/code-generator/kube_codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,172 +55,9 @@ function kube::codegen::internal::git_grep() {
# directories to consider during conversion generation.
#
function kube::codegen::gen_helpers() {
local in_pkg_root=""
local out_base="" # gengo needs the output dir must be $out_base/$out_pkg_root
local boilerplate="${KUBE_CODEGEN_ROOT}/hack/boilerplate.go.txt"
local v="${KUBE_VERBOSE:-0}"
local extra_peers=()

while [ "$#" -gt 0 ]; do
case "$1" in
"--input-pkg-root")
in_pkg_root="$2"
shift 2
;;
"--output-base")
out_base="$2"
shift 2
;;
"--boilerplate")
boilerplate="$2"
shift 2
;;
"--extra-peer-dir")
extra_peers+=("$2")
shift 2
;;
*)
echo "unknown argument: $1" >&2
return 1
;;
esac
done

if [ -z "${in_pkg_root}" ]; then
echo "--input-pkg-root is required" >&2
return 1
fi
if [ -z "${out_base}" ]; then
echo "--output-base 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=(
conversion-gen
deepcopy-gen
defaulter-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}"

# These tools all assume out-dir == in-dir.
root="${out_base}/${in_pkg_root}"
mkdir -p "${root}"
root="$(cd "${root}" && pwd -P)"

# Deepcopy
#
local input_pkgs=()
while read -r dir; do
pkg="$(cd "${dir}" && GO111MODULE=on go list -find .)"
input_pkgs+=("${pkg}")
done < <(
( kube::codegen::internal::git_grep -l --null \
-e '+k8s:deepcopy-gen=' \
":(glob)${root}"/'**/*.go' \
|| true \
) | while read -r -d $'\0' F; do dirname "${F}"; done \
| LC_ALL=C sort -u
)

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

kube::codegen::internal::git_find -z \
":(glob)${root}"/'**/zz_generated.deepcopy.go' \
| xargs -0 rm -f

local input_args=()
for arg in "${input_pkgs[@]}"; do
input_args+=("--input-dirs" "$arg")
done
"${gobin}/deepcopy-gen" \
-v "${v}" \
--output-file-base zz_generated.deepcopy \
--go-header-file "${boilerplate}" \
--output-base "${out_base}" \
"${input_args[@]}"
fi

# Defaults
#
local input_pkgs=()
while read -r dir; do
pkg="$(cd "${dir}" && GO111MODULE=on go list -find .)"
input_pkgs+=("${pkg}")
done < <(
( kube::codegen::internal::git_grep -l --null \
-e '+k8s:defaulter-gen=' \
":(glob)${root}"/'**/*.go' \
|| true \
) | while read -r -d $'\0' F; do dirname "${F}"; done \
| LC_ALL=C sort -u
)

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

kube::codegen::internal::git_find -z \
":(glob)${root}"/'**/zz_generated.defaults.go' \
| xargs -0 rm -f

local input_args=()
for arg in "${input_pkgs[@]}"; do
input_args+=("--input-dirs" "$arg")
done
"${gobin}/defaulter-gen" \
-v "${v}" \
--output-file-base zz_generated.defaults \
--go-header-file "${boilerplate}" \
--output-base "${out_base}" \
"${input_args[@]}"
fi

# Conversions
#
local input_pkgs=()
while read -r dir; do
pkg="$(cd "${dir}" && GO111MODULE=on go list -find .)"
input_pkgs+=("${pkg}")
done < <(
( kube::codegen::internal::git_grep -l --null \
-e '+k8s:conversion-gen=' \
":(glob)${root}"/'**/*.go' \
|| true \
) | while read -r -d $'\0' F; do dirname "${F}"; done \
| LC_ALL=C sort -u
)

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

kube::codegen::internal::git_find -z \
":(glob)${root}"/'**/zz_generated.conversion.go' \
| xargs -0 rm -f

local input_args=()
for arg in "${input_pkgs[@]}"; do
input_args+=("--input-dirs" "$arg")
done
local extra_peer_args=()
for arg in "${extra_peers[@]:+"${extra_peers[@]}"}"; do
extra_peer_args+=("--extra-peer-dirs" "$arg")
done
"${gobin}/conversion-gen" \
-v "${v}" \
--output-file-base zz_generated.conversion \
--go-header-file "${boilerplate}" \
--output-base "${out_base}" \
"${extra_peer_args[@]:+"${extra_peer_args[@]}"}" \
"${input_args[@]}"
fi
# TODO: Add deprecation message, when other functions in this file are
# rewritten to use the new go-native code-generator.
go run k8s.io/code-generator gen-helpers "$@"
}

# Generate openapi code
Expand Down

0 comments on commit 0615e85

Please sign in to comment.