Skip to content

Commit 6a16c07

Browse files
committed
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.
1 parent aa8cb97 commit 6a16c07

File tree

5 files changed

+78
-98
lines changed

5 files changed

+78
-98
lines changed

hack/lib/verify-generated.sh

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2014 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# Short-circuit if verify-generated.sh has already been sourced.
18+
[[ $(type -t kube::verify::generated::loaded) == function ]] && return 0
19+
20+
source "${KUBE_ROOT}/hack/lib/init.sh"
21+
22+
# This function verifies whether generated files are up-to-date. The first two
23+
# parameters are messages that get printed to stderr when changes are found,
24+
# the rest are the function or command and its parameters for generating files
25+
# in the work tree.
26+
#
27+
# Example: kube::verify::generated "Mock files are out of date" "Please run 'hack/update-mocks.sh'" hack/update-mocks.sh
28+
kube::verify::generated() {
29+
( # a subshell prevents environment changes from leaking out of this function
30+
local failure_header=$1
31+
shift
32+
local failure_tail=$1
33+
shift
34+
35+
kube::util::ensure_clean_working_dir
36+
37+
# This sets up the environment, like GOCACHE, which keeps the worktree cleaner.
38+
kube::golang::setup_env
39+
40+
_tmpdir="$(kube::realpath "$(mktemp -d -t "verify-generated-$(basename "$1").XXXXXX")")"
41+
git worktree add -f -q "${_tmpdir}" HEAD
42+
kube::util::trap_add "git worktree remove -f ${_tmpdir}" EXIT
43+
cd "${_tmpdir}"
44+
45+
# Update generated files.
46+
"$@"
47+
48+
# Test for diffs
49+
diffs=$(git status --porcelain | wc -l)
50+
if [[ ${diffs} -gt 0 ]]; then
51+
if [[ -n "${failure_header}" ]]; then
52+
echo "${failure_header}" >&2
53+
fi
54+
git status >&2
55+
git diff >&2
56+
if [[ -n "${failure_tail}" ]]; then
57+
echo "" >&2
58+
echo "${failure_tail}" >&2
59+
fi
60+
return 1
61+
fi
62+
)
63+
}
64+
65+
# Marker function to indicate verify-generated.sh has been fully sourced.
66+
kube::verify::generated::loaded() {
67+
return 0
68+
}

hack/verify-codegen.sh

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,16 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
# This script verifies whether code update is needed or not against the
18-
# specific sub-projects. The sub-projects are listed below this script(the
19-
# line that starts with `CODEGEN_PKG`).
20-
# Usage: `hack/verify-codegen.sh`.
17+
# This script verifies whether a code update is needed.
18+
# Usage: `hack/verify-codegen.sh <parameters for update-codegen.sh>`.
2119

2220
set -o errexit
2321
set -o nounset
2422
set -o pipefail
2523

2624
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
27-
source "${KUBE_ROOT}/hack/lib/init.sh"
25+
source "${KUBE_ROOT}/hack/lib/verify-generated.sh"
2826

29-
kube::util::ensure_clean_working_dir
30-
31-
# This sets up the environment, like GOCACHE, which keeps the worktree cleaner.
32-
kube::golang::setup_env
33-
34-
_tmpdir="$(kube::realpath "$(mktemp -d -t "$(basename "$0").XXXXXX")")"
35-
git worktree add -f -q "${_tmpdir}" HEAD
36-
kube::util::trap_add "git worktree remove -f ${_tmpdir}" EXIT
37-
cd "${_tmpdir}"
38-
39-
# Update generated code
4027
export UPDATE_API_KNOWN_VIOLATIONS=true
41-
hack/update-codegen.sh "$@"
4228

43-
# Test for diffs
44-
diffs=$(git status --porcelain | wc -l)
45-
if [[ ${diffs} -gt 0 ]]; then
46-
git status >&2
47-
git diff >&2
48-
echo "Generated files need to be updated" >&2
49-
echo "Please run 'hack/update-codegen.sh'" >&2
50-
exit 1
51-
fi
52-
echo "Generated files are up to date"
29+
kube::verify::generated "Generated files need to be updated" "Please run 'hack/update-codegen.sh'" hack/update-codegen.sh "$@"

hack/verify-internal-modules.sh

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,6 @@ set -o nounset
1919
set -o pipefail
2020

2121
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
22-
source "${KUBE_ROOT}/hack/lib/init.sh"
22+
source "${KUBE_ROOT}/hack/lib/verify-generated.sh"
2323

24-
kube::util::ensure_clean_working_dir
25-
# This sets up the environment, like GOCACHE, which keeps the worktree cleaner.
26-
kube::golang::setup_env
27-
28-
_tmpdir="$(kube::realpath "$(mktemp -d -t verify-internal-modules.XXXXXX)")"
29-
kube::util::trap_add "rm -rf ${_tmpdir:?}" EXIT
30-
31-
_tmp_gopath="${_tmpdir}/go"
32-
_tmp_kuberoot="${_tmp_gopath}/src/k8s.io/kubernetes"
33-
git worktree add -f "${_tmp_kuberoot}" HEAD
34-
kube::util::trap_add "git worktree remove -f ${_tmp_kuberoot}" EXIT
35-
36-
pushd "${_tmp_kuberoot}" >/dev/null
37-
./hack/update-internal-modules.sh
38-
popd
39-
40-
git -C "${_tmp_kuberoot}" add -N .
41-
diff=$(git -C "${_tmp_kuberoot}" diff HEAD || true)
42-
43-
if [[ -n "${diff}" ]]; then
44-
echo "${diff}" >&2
45-
echo >&2
46-
echo "Run ./hack/update-internal-modules.sh" >&2
47-
exit 1
48-
fi
24+
kube::verify::generated "" "Run ./hack/update-internal-modules.sh" ./hack/update-internal-modules.sh

hack/verify-mocks.sh

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,6 @@ set -o nounset
2525
set -o pipefail
2626

2727
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
28-
source "${KUBE_ROOT}/hack/lib/init.sh"
28+
source "${KUBE_ROOT}/hack/lib/verify-generated.sh"
2929

30-
# Explicitly opt into go modules, even though we're inside a GOPATH directory
31-
export GO111MODULE=on
32-
33-
kube::util::ensure_clean_working_dir
34-
# This sets up the environment, like GOCACHE, which keeps the worktree cleaner.
35-
kube::golang::setup_env
36-
37-
_tmpdir="$(kube::realpath "$(mktemp -d -t "$(basename "$0").XXXXXX")")"
38-
git worktree add -f -q "${_tmpdir}" HEAD
39-
kube::util::trap_add "git worktree remove -f ${_tmpdir}" EXIT
40-
cd "${_tmpdir}"
41-
42-
# Update the mocks in ${_tmpdir}
43-
hack/update-mocks.sh
44-
45-
# Test for diffs
46-
diffs=$(git status --porcelain | wc -l)
47-
if [[ ${diffs} -gt 0 ]]; then
48-
echo "Mock files are out of date" >&2
49-
git diff
50-
echo "Please run 'hack/update-mocks.sh'" >&2
51-
exit 1
52-
fi
30+
kube::verify::generated "Mock files are out of date" "Please run 'hack/update-mocks.sh'" hack/update-mocks.sh

hack/verify-yamlfmt.sh

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,6 @@ set -o nounset
2525
set -o pipefail
2626

2727
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
28-
source "${KUBE_ROOT}/hack/lib/init.sh"
28+
source "${KUBE_ROOT}/hack/lib/verify-generated.sh"
2929

30-
kube::util::ensure_clean_working_dir
31-
# This sets up the environment, like GOCACHE, which keeps the worktree cleaner.
32-
kube::golang::setup_env
33-
34-
_tmpdir="$(kube::realpath "$(mktemp -d -t "$(basename "$0").XXXXXX")")"
35-
git worktree add -f -q "${_tmpdir}" HEAD
36-
kube::util::trap_add "git worktree remove -f ${_tmpdir}" EXIT
37-
cd "${_tmpdir}"
38-
39-
# Format YAML files
40-
hack/update-yamlfmt.sh
41-
42-
# Test for diffs
43-
diffs=$(git status --porcelain | wc -l)
44-
if [[ ${diffs} -gt 0 ]]; then
45-
echo "YAML files need to be formatted" >&2
46-
git diff
47-
echo "Please run 'hack/update-yamlfmt.sh'" >&2
48-
exit 1
49-
fi
30+
kube::verify::generated "YAML files need to be formatted" "Please run 'hack/update-yamlfmt.sh'" hack/update-yamlfmt.sh

0 commit comments

Comments
 (0)