From 1bc3266978efd346b99392a843765bd4d9ca016d Mon Sep 17 00:00:00 2001 From: justinsb Date: Fri, 29 Aug 2025 12:09:56 +0000 Subject: [PATCH] chore: make apidiff focus on our APIs We want to check if our public APIs have changed, not if our internal implementation has changed. Focus only on differences in api/ and exp/api/ --- Makefile | 19 +++---------------- hack/verify-apidiff | 41 +++++++++++++++++++++++++++++++++++++++++ scripts/ci-apidiff.sh | 7 ++----- 3 files changed, 46 insertions(+), 21 deletions(-) create mode 100755 hack/verify-apidiff diff --git a/Makefile b/Makefile index c5136e200..ac3acfb15 100644 --- a/Makefile +++ b/Makefile @@ -103,10 +103,6 @@ SETUP_ENVTEST_VER := v0.0.0-20240522175850-2e9781e9fc60 SETUP_ENVTEST_BIN := setup-envtest SETUP_ENVTEST := $(TOOLS_BIN_DIR)/$(SETUP_ENVTEST_BIN) -GO_APIDIFF_VER := v0.8.3 -GO_APIDIFF_BIN := go-apidiff -GO_APIDIFF := $(TOOLS_BIN_DIR)/$(GO_APIDIFF_BIN) - GOTESTSUM_VER := v1.6.4 GOTESTSUM_BIN := gotestsum GOTESTSUM := $(TOOLS_BIN_DIR)/$(GOTESTSUM_BIN) @@ -260,9 +256,6 @@ $(CONVERSION_GEN): ## Build conversion-gen. $(RELEASE_NOTES): ## Build release notes. GOBIN=$(TOOLS_BIN_DIR) $(GO_INSTALL) k8s.io/release/cmd/release-notes $(RELEASE_NOTES_BIN) $(RELEASE_NOTES_VER) -$(GO_APIDIFF): ## Build go-apidiff from tools folder. - GOBIN=$(TOOLS_BIN_DIR) $(GO_INSTALL) github.com/joelanford/go-apidiff $(GO_APIDIFF_BIN) $(GO_APIDIFF_VER) - $(CONVERSION_VERIFIER): go.mod cd $(TOOLS_DIR); go build -tags=tools -o $@ sigs.k8s.io/cluster-api/hack/tools/conversion-verifier @@ -282,9 +275,6 @@ $(KIND): ## Build kind into tools folder .PHONY: $(KUBECTL_BIN) $(KUBECTL_BIN): $(KUBECTL) ## Building kubectl from tools folder -.PHONY: $(GO_APIDIFF_BIN) -$(GO_APIDIFF_BIN): $(GO_APIDIFF) - .PHONY: $(KIND_BIN) $(KIND_BIN): $(KIND) ## Building Kind from tools folder @@ -576,13 +566,10 @@ clean-release: ## Remove the release folder rm -rf $(RELEASE_DIR) .PHONY: apidiff -apidiff: $(GO_APIDIFF) ## Check for API differences. +apidiff: APIDIFF_OLD_COMMIT ?= $(shell git rev-parse origin/main) +apidiff: $(GO_APIDIFF) ## Check for API differences @$(call checkdiff) > /dev/null - @if ($(call checkdiff) | grep "api/"); then \ - $(GO_APIDIFF) $(shell git rev-parse origin/main) --print-compatible; \ - else \ - echo "No changes to 'api/'. Nothing to do."; \ - fi + APIDIFF_OLD_COMMIT="$(APIDIFF_OLD_COMMIT)" hack/verify-apidiff define checkdiff git --no-pager diff --name-only FETCH_HEAD diff --git a/hack/verify-apidiff b/hack/verify-apidiff new file mode 100755 index 000000000..88f12091e --- /dev/null +++ b/hack/verify-apidiff @@ -0,0 +1,41 @@ +#!/usr/bin/env bash + +# Copyright 2025 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. + +set -o errexit +set -o nounset +set -o pipefail + +REPO_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. +cd "${REPO_ROOT}" + +if [[ -z "${APIDIFF_OLD_COMMIT:-}" ]]; then + APIDIFF_OLD_COMMIT=$(git rev-parse origin/main) +fi + + +echo "*** Running go-apidiff ***" + +# Run go-apidiff and filter for changes in api/ or exp/api/ +apidiffs=$(go run github.com/joelanford/go-apidiff@v0.8.3 ${APIDIFF_OLD_COMMIT} || true) +filtered=$(echo "$apidiffs" | grep -E 'cluster-api-provider-gcp/(api/|exp/api/)' || true) + +if [[ -n "$filtered" ]]; then + echo "API differences found in api/ or exp/api/:" + echo "$apidiffs" + exit 1 +else + echo "No differences found in api/ or exp/api/ packages" +fi diff --git a/scripts/ci-apidiff.sh b/scripts/ci-apidiff.sh index 0f1b6b918..3d48c8c4b 100755 --- a/scripts/ci-apidiff.sh +++ b/scripts/ci-apidiff.sh @@ -20,9 +20,6 @@ set -o pipefail REPO_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. -APIDIFF="${REPO_ROOT}/hack/tools/bin/go-apidiff" +cd "${REPO_ROOT}" -cd "${REPO_ROOT}" && make "${APIDIFF##*/}" -echo "*** Running go-apidiff ***" - -${APIDIFF} "${PULL_BASE_SHA}" --print-compatible +APIDIFF_OLD_COMMIT="${PULL_BASE_SHA}" make apidiff \ No newline at end of file