From 6a16c076e7663994b2fc080e0576b0d130b6d03e Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Wed, 21 Jun 2023 09:50:23 +0200 Subject: [PATCH] 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. --- hack/lib/verify-generated.sh | 68 +++++++++++++++++++++++++++++++++ hack/verify-codegen.sh | 31 ++------------- hack/verify-internal-modules.sh | 28 +------------- hack/verify-mocks.sh | 26 +------------ hack/verify-yamlfmt.sh | 23 +---------- 5 files changed, 78 insertions(+), 98 deletions(-) create mode 100755 hack/lib/verify-generated.sh diff --git a/hack/lib/verify-generated.sh b/hack/lib/verify-generated.sh new file mode 100755 index 00000000000..4770f99ca5c --- /dev/null +++ b/hack/lib/verify-generated.sh @@ -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 +} diff --git a/hack/verify-codegen.sh b/hack/verify-codegen.sh index 2442e2a6d3a..2072e87b62e 100755 --- a/hack/verify-codegen.sh +++ b/hack/verify-codegen.sh @@ -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 `. 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 "$@" diff --git a/hack/verify-internal-modules.sh b/hack/verify-internal-modules.sh index 9b2bef3824e..ed2af762167 100755 --- a/hack/verify-internal-modules.sh +++ b/hack/verify-internal-modules.sh @@ -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 diff --git a/hack/verify-mocks.sh b/hack/verify-mocks.sh index 4fa0db12237..13528756687 100755 --- a/hack/verify-mocks.sh +++ b/hack/verify-mocks.sh @@ -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 diff --git a/hack/verify-yamlfmt.sh b/hack/verify-yamlfmt.sh index 284b318fe24..a6545b5b0e1 100755 --- a/hack/verify-yamlfmt.sh +++ b/hack/verify-yamlfmt.sh @@ -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