hack: move common "verify generated" shell code into function

Several verify scripts used the same pattern of "check for clean working tree,
generated files, check for diffs". The code for that is now in
kube::verify::generated, defined in hack/lib/verify-generated.sh, and those
scripts just source that.
This commit is contained in:
Patrick Ohly 2023-06-21 09:50:23 +02:00
parent aa8cb97f65
commit 6a16c076e7
5 changed files with 78 additions and 98 deletions

68
hack/lib/verify-generated.sh Executable file
View File

@ -0,0 +1,68 @@
#!/usr/bin/env bash
# Copyright 2014 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.
# Short-circuit if verify-generated.sh has already been sourced.
[[ $(type -t kube::verify::generated::loaded) == function ]] && return 0
source "${KUBE_ROOT}/hack/lib/init.sh"
# This function verifies whether generated files are up-to-date. The first two
# parameters are messages that get printed to stderr when changes are found,
# the rest are the function or command and its parameters for generating files
# in the work tree.
#
# Example: kube::verify::generated "Mock files are out of date" "Please run 'hack/update-mocks.sh'" hack/update-mocks.sh
kube::verify::generated() {
( # a subshell prevents environment changes from leaking out of this function
local failure_header=$1
shift
local failure_tail=$1
shift
kube::util::ensure_clean_working_dir
# This sets up the environment, like GOCACHE, which keeps the worktree cleaner.
kube::golang::setup_env
_tmpdir="$(kube::realpath "$(mktemp -d -t "verify-generated-$(basename "$1").XXXXXX")")"
git worktree add -f -q "${_tmpdir}" HEAD
kube::util::trap_add "git worktree remove -f ${_tmpdir}" EXIT
cd "${_tmpdir}"
# Update generated files.
"$@"
# Test for diffs
diffs=$(git status --porcelain | wc -l)
if [[ ${diffs} -gt 0 ]]; then
if [[ -n "${failure_header}" ]]; then
echo "${failure_header}" >&2
fi
git status >&2
git diff >&2
if [[ -n "${failure_tail}" ]]; then
echo "" >&2
echo "${failure_tail}" >&2
fi
return 1
fi
)
}
# Marker function to indicate verify-generated.sh has been fully sourced.
kube::verify::generated::loaded() {
return 0
}

View File

@ -14,39 +14,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# This script verifies whether code update is needed or not against the
# specific sub-projects. The sub-projects are listed below this script(the
# line that starts with `CODEGEN_PKG`).
# Usage: `hack/verify-codegen.sh`.
# This script verifies whether a code update is needed.
# Usage: `hack/verify-codegen.sh <parameters for update-codegen.sh>`.
set -o errexit
set -o nounset
set -o pipefail
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
source "${KUBE_ROOT}/hack/lib/init.sh"
source "${KUBE_ROOT}/hack/lib/verify-generated.sh"
kube::util::ensure_clean_working_dir
# This sets up the environment, like GOCACHE, which keeps the worktree cleaner.
kube::golang::setup_env
_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}"
# Update generated code
export UPDATE_API_KNOWN_VIOLATIONS=true
hack/update-codegen.sh "$@"
# Test for diffs
diffs=$(git status --porcelain | wc -l)
if [[ ${diffs} -gt 0 ]]; then
git status >&2
git diff >&2
echo "Generated files need to be updated" >&2
echo "Please run 'hack/update-codegen.sh'" >&2
exit 1
fi
echo "Generated files are up to date"
kube::verify::generated "Generated files need to be updated" "Please run 'hack/update-codegen.sh'" hack/update-codegen.sh "$@"

View File

@ -19,30 +19,6 @@ set -o nounset
set -o pipefail
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
source "${KUBE_ROOT}/hack/lib/init.sh"
source "${KUBE_ROOT}/hack/lib/verify-generated.sh"
kube::util::ensure_clean_working_dir
# This sets up the environment, like GOCACHE, which keeps the worktree cleaner.
kube::golang::setup_env
_tmpdir="$(kube::realpath "$(mktemp -d -t verify-internal-modules.XXXXXX)")"
kube::util::trap_add "rm -rf ${_tmpdir:?}" EXIT
_tmp_gopath="${_tmpdir}/go"
_tmp_kuberoot="${_tmp_gopath}/src/k8s.io/kubernetes"
git worktree add -f "${_tmp_kuberoot}" HEAD
kube::util::trap_add "git worktree remove -f ${_tmp_kuberoot}" EXIT
pushd "${_tmp_kuberoot}" >/dev/null
./hack/update-internal-modules.sh
popd
git -C "${_tmp_kuberoot}" add -N .
diff=$(git -C "${_tmp_kuberoot}" diff HEAD || true)
if [[ -n "${diff}" ]]; then
echo "${diff}" >&2
echo >&2
echo "Run ./hack/update-internal-modules.sh" >&2
exit 1
fi
kube::verify::generated "" "Run ./hack/update-internal-modules.sh" ./hack/update-internal-modules.sh

View File

@ -25,28 +25,6 @@ set -o nounset
set -o pipefail
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
source "${KUBE_ROOT}/hack/lib/init.sh"
source "${KUBE_ROOT}/hack/lib/verify-generated.sh"
# Explicitly opt into go modules, even though we're inside a GOPATH directory
export GO111MODULE=on
kube::util::ensure_clean_working_dir
# This sets up the environment, like GOCACHE, which keeps the worktree cleaner.
kube::golang::setup_env
_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}"
# Update the mocks in ${_tmpdir}
hack/update-mocks.sh
# Test for diffs
diffs=$(git status --porcelain | wc -l)
if [[ ${diffs} -gt 0 ]]; then
echo "Mock files are out of date" >&2
git diff
echo "Please run 'hack/update-mocks.sh'" >&2
exit 1
fi
kube::verify::generated "Mock files are out of date" "Please run 'hack/update-mocks.sh'" hack/update-mocks.sh

View File

@ -25,25 +25,6 @@ set -o nounset
set -o pipefail
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
source "${KUBE_ROOT}/hack/lib/init.sh"
source "${KUBE_ROOT}/hack/lib/verify-generated.sh"
kube::util::ensure_clean_working_dir
# This sets up the environment, like GOCACHE, which keeps the worktree cleaner.
kube::golang::setup_env
_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}"
# Format YAML files
hack/update-yamlfmt.sh
# Test for diffs
diffs=$(git status --porcelain | wc -l)
if [[ ${diffs} -gt 0 ]]; then
echo "YAML files need to be formatted" >&2
git diff
echo "Please run 'hack/update-yamlfmt.sh'" >&2
exit 1
fi
kube::verify::generated "YAML files need to be formatted" "Please run 'hack/update-yamlfmt.sh'" hack/update-yamlfmt.sh