K8s: Fix hack/update-codegen (#91867)

This commit is contained in:
Todd Treece
2024-08-14 01:17:05 -04:00
committed by GitHub
parent e90b272299
commit dacf11b048
36 changed files with 296 additions and 670 deletions

View File

@ -45,38 +45,38 @@ function grafana::codegen::gen_openapi() {
while [ "$#" -gt 0 ]; do
case "$1" in
"--input-pkg-single")
in_pkg_single="$2"
shift 2
;;
"--include-common-input-dirs")
if [ "$2" == "true" ]; then
COMMON_INPUT_DIRS='--input-dirs "k8s.io/apimachinery/pkg/apis/meta/v1" --input-dirs "k8s.io/apimachinery/pkg/runtime" --input-dirs "k8s.io/apimachinery/pkg/version"'
else
COMMON_INPUT_DIRS=""
fi
shift 2
;;
"--output-base")
out_base="$2"
shift 2
;;
"--report-filename")
report="$2"
shift 2
;;
"--update-report")
update_report="true"
shift
;;
"--boilerplate")
boilerplate="$2"
shift 2
;;
*)
echo "unknown argument: $1" >&2
return 1
;;
"--input-pkg-single")
in_pkg_single="$2"
shift 2
;;
"--include-common-input-dirs")
if [ "$2" == "true" ]; then
COMMON_INPUT_DIRS='"k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/version"'
else
COMMON_INPUT_DIRS=""
fi
shift 2
;;
"--output-base")
out_base="$2"
shift 2
;;
"--report-filename")
report="$2"
shift 2
;;
"--update-report")
update_report="true"
shift
;;
"--boilerplate")
boilerplate="$2"
shift 2
;;
*)
echo "unknown argument: $1" >&2
return 1
;;
esac
done
@ -103,58 +103,57 @@ function grafana::codegen::gen_openapi() {
openapi-gen
)
# shellcheck disable=2046 # printf word-splitting is intentional
GO111MODULE=on go install $(printf "k8s.io/code-generator/cmd/%s " "${BINS[@]}")
GO111MODULE=on go install $(printf "k8s.io/kube-openapi/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_single}"
root="${in_pkg_single}"
mkdir -p "${root}"
root="$(cd "${root}" && pwd -P)"
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:openapi-gen=' \
":(glob)${root}"/'**/*.go' \
|| true \
) | while read -r -d $'\0' F; do dirname "${F}"; done \
| LC_ALL=C sort -u
(
kube::codegen::internal::grep -l --null \
-e '+k8s:openapi-gen=' \
-r "${root}" \
--include '*.go' ||
true
) | while read -r -d $'\0' F; do dirname "${F}"; done |
LC_ALL=C sort -u
)
local new_report=""
if [ "${#input_pkgs[@]}" != 0 ]; then
echo "Generating openapi code for ${#input_pkgs[@]} targets"
kube::codegen::internal::git_find -z \
":(glob)${root}"/'**/zz_generated.openapi.go' \
| xargs -0 rm -f
local inputs=()
for arg in "${input_pkgs[@]}"; do
inputs+=("--input-dirs" "$arg")
done
new_report="${root}/${report}.tmp"
if [ -n "${update_report}" ]; then
new_report="${root}/${report}"
fi
"${gobin}/openapi-gen" \
-v "${v}" \
-O zz_generated.openapi \
--go-header-file "${boilerplate}" \
--output-base "${out_base}" \
--output-package "${in_pkg_single}" \
--report-filename "${new_report}" \
${COMMON_INPUT_DIRS} \
"${inputs[@]}"
local new_report=""
if [ "${#input_pkgs[@]}" == 0 ]; then
return 0
fi
echo "Generating openapi code for ${#input_pkgs[@]} targets"
kube::codegen::internal::findz \
"${root}" \
-type f \
-name zz_generated.openapi.go \
| xargs -0 rm -f
local new_report
new_report="$(mktemp -t "$(basename "$0").api_violations.XXXXXX")"
if [ -n "${update_report}" ]; then
new_report="${root}/${report}"
fi
"${gobin}/openapi-gen" \
-v "${v}" \
--output-file zz_generated.openapi.go \
--go-header-file "${boilerplate}" \
--output-dir "${root}" \
--output-pkg "${in_pkg_single}" \
--report-filename "${new_report}" \
${COMMON_INPUT_DIRS} \
"${input_pkgs[@]}"
touch "${root}/${report}" # in case it doesn't exist yet
if [[ -z "${new_report}" ]]; then
@ -169,6 +168,6 @@ function grafana::codegen::gen_openapi() {
# if all goes well, remove the temporary reports
if [ -z "${update_report}" ]; then
rm -f "${new_report}"
rm -f "${new_report}"
fi
}