Make verify-generated-swagger-docs use worktree

Also move a function into these files that was only used in the one
place.
This commit is contained in:
Tim Hockin 2022-09-11 20:36:02 -07:00
parent d2504c94a0
commit b5c5913db0
No known key found for this signature in database
4 changed files with 65 additions and 127 deletions

View File

@ -159,7 +159,9 @@ skipped_dirs = ['third_party', '_gopath', '_output', '.git', 'cluster/env.sh',
# list all the files contain 'DO NOT EDIT', but are not generated
skipped_ungenerated_files = [
'hack/lib/swagger.sh', 'hack/boilerplate/boilerplate.py']
'hack/update-generated-swagger-docs.sh',
'hack/boilerplate/boilerplate.py'
]
def normalize_files(files):

View File

@ -1,68 +0,0 @@
#!/usr/bin/env bash
# Copyright 2016 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Contains swagger related util functions.
#
set -o errexit
set -o nounset
set -o pipefail
# Generates types_swagger_doc_generated file for the given group version.
# $1: Name of the group version
# $2: Path to the directory where types.go for that group version exists. This
# is the directory where the file will be generated.
kube::swagger::gen_types_swagger_doc() {
local group_version=$1
local gv_dir=$2
local TMPFILE
TMPFILE="${TMPDIR:-/tmp}/types_swagger_doc_generated.$(date +%s).go"
echo "Generating swagger type docs for ${group_version} at ${gv_dir}"
{
echo -e "$(cat hack/boilerplate/boilerplate.generatego.txt)\n"
echo "package ${group_version##*/}"
cat <<EOF
// This file contains a collection of methods that can be used from go-restful to
// generate Swagger API documentation for its models. Please read this PR for more
// information on the implementation: https://github.com/emicklei/go-restful/pull/215
//
// TODOs are ignored from the parser (e.g. TODO(andronat):... || TODO:...) if and only if
// they are on one line! For multiple line or blocks that you want to ignore use ---.
// Any context after a --- is ignored.
//
// Those methods can be generated by using hack/update-generated-swagger-docs.sh
// AUTO-GENERATED FUNCTIONS START HERE. DO NOT EDIT.
EOF
} > "${TMPFILE}"
if ! which genswaggertypedocs >/dev/null; then
# build if needed
GO111MODULE=on GOPROXY=off go install k8s.io/kubernetes/cmd/genswaggertypedocs
fi
genswaggertypedocs -s \
"${gv_dir}/types.go" \
-f - \
>> "${TMPFILE}"
echo "// AUTO-GENERATED FUNCTIONS END HERE" >> "${TMPFILE}"
gofmt -w -s "${TMPFILE}"
mv "${TMPFILE}" "${gv_dir}/types_swagger_doc_generated.go"
}

View File

@ -25,9 +25,48 @@ set -o pipefail
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
source "${KUBE_ROOT}/hack/lib/init.sh"
source "${KUBE_ROOT}/hack/lib/swagger.sh"
kube::golang::setup_env
# Generates types_swagger_doc_generated file for the given group version.
# $1: Name of the group version
# $2: Path to the directory where types.go for that group version exists. This
# is the directory where the file will be generated.
gen_types_swagger_doc() {
local group_version=$1
local gv_dir=$2
local TMPFILE
TMPFILE="${TMPDIR:-/tmp}/types_swagger_doc_generated.$(date +%s).go"
echo "Generating swagger type docs for ${group_version} at ${gv_dir}"
{
echo -e "$(cat hack/boilerplate/boilerplate.generatego.txt)\n"
echo "package ${group_version##*/}"
cat <<EOF
// This file contains a collection of methods that can be used from go-restful to
// generate Swagger API documentation for its models. Please read this PR for more
// information on the implementation: https://github.com/emicklei/go-restful/pull/215
//
// TODOs are ignored from the parser (e.g. TODO(andronat):... || TODO:...) if and only if
// they are on one line! For multiple line or blocks that you want to ignore use ---.
// Any context after a --- is ignored.
//
// Those methods can be generated by using hack/update-generated-swagger-docs.sh
// AUTO-GENERATED FUNCTIONS START HERE. DO NOT EDIT.
EOF
} > "${TMPFILE}"
_output/bin/genswaggertypedocs -s \
"${gv_dir}/types.go" \
-f - \
>> "${TMPFILE}"
echo "// AUTO-GENERATED FUNCTIONS END HERE" >> "${TMPFILE}"
gofmt -w -s "${TMPFILE}"
mv "${TMPFILE}" "${gv_dir}/types_swagger_doc_generated.go"
}
IFS=" " read -r -a GROUP_VERSIONS <<< "meta/v1 meta/v1beta1 ${KUBE_AVAILABLE_GROUP_VERSIONS}"
@ -35,8 +74,11 @@ IFS=" " read -r -a GROUP_VERSIONS <<< "meta/v1 meta/v1beta1 ${KUBE_AVAILABLE_GRO
for group_version in "${GROUP_VERSIONS[@]}"; do
rm -f "$(kube::util::group-version-to-pkg-path "${group_version}")/types_swagger_doc_generated.go"
done
# ensure we have the latest genswaggertypedocs built
GO111MODULE=on GOPROXY=off go install k8s.io/kubernetes/cmd/genswaggertypedocs
# Ensure we have the latest genswaggertypedocs built
hack/make-rules/build.sh ./cmd/genswaggertypedocs
# Regenerate files.
for group_version in "${GROUP_VERSIONS[@]}"; do
kube::swagger::gen_types_swagger_doc "${group_version}" "$(kube::util::group-version-to-pkg-path "${group_version}")"
gen_types_swagger_doc "${group_version}" "$(kube::util::group-version-to-pkg-path "${group_version}")"
done

View File

@ -26,59 +26,21 @@ set -o pipefail
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
source "${KUBE_ROOT}/hack/lib/init.sh"
kube::golang::setup_env
kube::util::ensure_clean_working_dir
_tmpdir="$(kube::realpath "$(mktemp -d -t swagger-docs.XXXXXX)")"
function swagger_cleanup {
rm -rf "${_tmpdir}"
}
kube::util::trap_add swagger_cleanup EXIT
_tmpdir="$(kube::realpath "$(mktemp -d -t "$(basename "$0").XXXXXX")")"
git worktree add -f -q "${_tmpdir}" HEAD
kube::util::trap_add "git worktree remove -f ${_tmpdir}" EXIT
cd "${_tmpdir}"
# Copy the contents of the kube directory into the nice clean place
_kubetmp="${_tmpdir}/src/k8s.io"
mkdir -p "${_kubetmp}"
# should create ${_kubetmp}/kubernetes
git archive --format=tar --prefix=kubernetes/ "$(git write-tree)" | (cd "${_kubetmp}" && tar xf -)
_kubetmp="${_kubetmp}/kubernetes"
# Do all our work in the new GOPATH
export GOPATH="${_tmpdir}"
# Update the generated swagger docs
hack/update-generated-swagger-docs.sh
find_files() {
find . -not \( \
\( \
-wholename './output' \
-o -wholename './.git' \
-o -wholename './_output' \
-o -wholename './_gopath' \
-o -wholename './release' \
-o -wholename './target' \
-o -wholename '*/third_party/*' \
-o -wholename '*/vendor/*' \
-o -wholename './staging/src/k8s.io/client-go/*vendor/*' \
\) -prune \
\) -name 'types_swagger_doc_generated.go'
}
while IFS=$'\n' read -r line; do TARGET_FILES+=("$line"); done < <(find_files)
pushd "${_kubetmp}" > /dev/null 2>&1
# Update the generated swagger docs
hack/update-generated-swagger-docs.sh
popd > /dev/null 2>&1
ret=0
pushd "${KUBE_ROOT}" > /dev/null 2>&1
# Test for diffs
_output=""
for file in "${TARGET_FILES[@]}"; do
_output="${_output}$(diff -Naupr -I 'Auto generated by' "${KUBE_ROOT}/${file}" "${_kubetmp}/${file}")" || ret=1
done
if [[ ${ret} -gt 0 ]]; then
echo "Generated swagger type documentation is out of date:" >&2
echo "${_output}" >&2
exit ${ret}
fi
popd > /dev/null 2>&1
echo "Generated swagger type documentation up to date."
# Test for diffs
diffs=$(git status --porcelain | wc -l)
if [[ ${diffs} -gt 0 ]]; then
echo "Generated swagger type documentation is out of date:" >&2
git diff
echo "Please run 'hack/update-generated-swagger-docs.sh'" >&2
exit 1
fi