diff --git a/hack/apidiff.sh b/hack/apidiff.sh index fa70631de38..57b47614590 100755 --- a/hack/apidiff.sh +++ b/hack/apidiff.sh @@ -14,10 +14,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -# This script checks the coding style for the Go language files using -# golangci-lint. Which checks are enabled depends on command line flags. The -# default is a minimal set of checks that all existing code passes without -# issues. +# This script analyzes API changes between specified revisions this repository. +# It uses the apidiff tool to detect differences, reports incompatible changes, and optionally +# builds downstream projects to assess the impact of those changes. usage () { cat <&2 @@ -79,36 +78,26 @@ while getopts "r:t:b:" o; do done shift $((OPTIND - 1)) -# Check specific directory or everything. -targets=("$@") -if [ ${#targets[@]} -eq 0 ]; then - # This lists all entries in the go.work file as absolute directory paths. - kube::util::read-array targets < <(go list -f '{{.Dir}}' -m) +# default from prow env if unset from args +# https://docs.prow.k8s.io/docs/jobs/#job-environment-variables +# TODO: handle batch PR testing + +if [[ -z "${target:-}" && -n "${PULL_PULL_SHA:-}" ]]; then + target="${PULL_PULL_SHA}" fi - -# Sanitize paths: -# - We need relative paths because we will invoke apidiff in -# different work trees. -# - Must start with a dot. -for (( i=0; i<${#targets[@]}; i++ )); do - d="${targets[i]}" - d=$(realpath -s --relative-to="$(pwd)" "${d}") - if [ "${d}" != "." ]; then - # sub-directories have to have a leading dot. - d="./${d}" - fi - targets[i]="${d}" -done - -# Must be a something that git can resolve to a commit. +# target must be a something that git can resolve to a commit. # "git rev-parse --verify" checks that and prints a detailed # error. -if [ -n "${target}" ]; then +if [[ -n "${target}" ]]; then target="$(git rev-parse --verify "${target}")" fi -# Determine defaults. -if [ -z "${base}" ]; then +if [[ -z "${base}" && -n "${PULL_BASE_SHA:-}" && -n "${PULL_PULL_SHA:-}" ]]; then + if ! base="$(git merge-base "${PULL_BASE_SHA}" "${PULL_PULL_SHA}")"; then + echo >&2 "Failed to detect base revision correctly with prow environment variables." + exit 1 + fi +elif [[ -z "${base}" ]]; then if ! base="$(git merge-base origin/master "${target:-HEAD}")"; then echo >&2 "Could not determine default base revision. -r must be used explicitly." exit 1 @@ -116,6 +105,20 @@ if [ -z "${base}" ]; then fi base="$(git rev-parse --verify "${base}")" +# Check specific directory or everything. +targets=("$@") +if [ ${#targets[@]} -eq 0 ]; then + shopt -s globstar + # Modules are discovered by looking for go.mod rather than asking go + # to ensure that modules that aren't part of the workspace and/or are + # not dependencies are checked too. + # . and staging are listed explicitly here to avoid _output + for module in ./go.mod ./staging/**/go.mod; do + module="${module%/go.mod}" + targets+=("$module") + done +fi + # Give some information about what's happening. Failures from "git describe" are ignored # silently, that's optional information. describe () { @@ -156,8 +159,18 @@ run () { out="$1" mkdir -p "$out" for d in "${targets[@]}"; do - apidiff -m -w "${out}/$(output_name "${d}")" "${d}" + if ! [ -d "${d}" ]; then + echo "module ${d} does not exist, skipping ..." + continue + fi + # cd to the path for modules that are intree but not part of the go workspace + # per example staging/src/k8s.io/code-generator/examples + ( + cd "${d}" + apidiff -m -w "${out}/$(output_name "${d}")" . + ) & done + wait } # inWorktree checks out a specific revision, then invokes the given @@ -204,12 +217,16 @@ inWorktree "${KUBE_TEMP}/base" "${base}" run "${KUBE_TEMP}/before" # be non-zero if there are incompatible changes. # # The report is Markdown-formatted and can be copied into a PR comment verbatim. -res=0 +failures=() echo compare () { what="$1" before="$2" after="$3" + if [ ! -f "${before}" ] || [ ! -f "${after}" ]; then + echo "can not compare changes, module didn't exist before or after" + return + fi changes=$(apidiff -m "${before}" "${after}" 2>&1 | grep -v -e "^Ignoring internal package") || true echo "## ${what}" if [ -z "$changes" ]; then @@ -218,9 +235,9 @@ compare () { echo "$changes" echo fi - incompatible=$(apidiff -incompatible -m "${before}" "${after}" 2>&1) || true + incompatible=$(apidiff -incompatible -m "${before}" "${after}" 2>&1 | grep -v -e "^Ignoring internal package") || true if [ -n "$incompatible" ]; then - res=1 + failures+=("${what}") fi } @@ -257,7 +274,11 @@ tryBuild () { ) } -if [ $res -ne 0 ]; then +res=0 +if [ ${#failures[@]} -gt 0 ]; then + res=1 + echo "Detected incompatible changes on modules:" + printf '%s\n' "${failures[@]}" cat <