Skip to content

Commit 4dbd085

Browse files
Add a Makefile target and start running the API diff linter as part of CI.
1 parent 6601b29 commit 4dbd085

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

.github/workflows/sanity.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ jobs:
1313
runs-on: ubuntu-latest
1414
steps:
1515
- uses: actions/checkout@v6
16+
with:
17+
fetch-depth: 0
1618

1719
- uses: actions/setup-go@v6
1820
with:
@@ -23,6 +25,8 @@ jobs:
2325
runs-on: ubuntu-latest
2426
steps:
2527
- uses: actions/checkout@v6
28+
with:
29+
fetch-depth: 0 # Fetch all history for all branches (needed for API diff)
2630

2731
- uses: actions/setup-go@v6
2832
with:

Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ help-extended: #HELP Display extended help.
118118
#SECTION Development
119119

120120
.PHONY: lint
121-
lint: lint-custom $(GOLANGCI_LINT) #HELP Run golangci linter.
121+
lint: lint-custom lint-api-diff $(GOLANGCI_LINT) #HELP Run golangci linter.
122122
$(GOLANGCI_LINT) run --build-tags $(GO_BUILD_TAGS) $(GOLANGCI_LINT_ARGS)
123123

124124
.PHONY: lint-helm
@@ -149,6 +149,10 @@ custom-linter-build: #EXHELP Build custom linter
149149
lint-custom: custom-linter-build #EXHELP Call custom linter for the project
150150
go vet -tags=$(GO_BUILD_TAGS) -vettool=./bin/custom-linter ./...
151151

152+
.PHONY: lint-api-diff
153+
lint-api-diff: $(GOLANGCI_LINT) #HELP Validate API changes using kube-api-linter with diff-aware analysis
154+
bash hack/api-lint-diff/run.sh
155+
152156
.PHONY: k8s-pin
153157
k8s-pin: #EXHELP Pin k8s staging modules based on k8s.io/kubernetes version (in go.mod or from K8S_IO_K8S_VERSION env var) and run go mod tidy.
154158
K8S_IO_K8S_VERSION='$(K8S_IO_K8S_VERSION)' go run hack/tools/k8smaintainer/main.go
@@ -198,7 +202,7 @@ generate: $(CONTROLLER_GEN) #EXHELP Generate code containing DeepCopy, DeepCopyI
198202
$(CONTROLLER_GEN) --load-build-tags=$(GO_BUILD_TAGS) object:headerFile="hack/boilerplate.go.txt" paths="./..."
199203

200204
.PHONY: verify
201-
verify: k8s-pin kind-verify-versions fmt generate manifests update-tls-profiles crd-ref-docs verify-bingo #HELP Verify all generated code is up-to-date. Runs k8s-pin instead of just tidy.
205+
verify: k8s-pin kind-verify-versions fmt generate manifests update-tls-profiles crd-ref-docs verify-bingo lint-api-diff #HELP Verify all generated code is up-to-date. Runs k8s-pin instead of just tidy.
202206
git diff --exit-code
203207

204208
.PHONY: verify-bingo

hack/api-lint-diff/run.sh

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,36 @@ check_linter_support() {
196196

197197
# Find golangci-lint binary
198198
find_golangci_lint() {
199-
# Check for custom build first
199+
# Check if Variables.mk exists and extract golangci-lint path
200+
if [[ -f ".bingo/Variables.mk" ]]; then
201+
# Extract version from GOLANGCI_LINT variable
202+
# Format: GOLANGCI_LINT := $(GOBIN)/golangci-lint-v2.7.2
203+
local version
204+
version=$(grep '^GOLANGCI_LINT' .bingo/Variables.mk | sed -E 's/.*golangci-lint-(v[0-9]+\.[0-9]+\.[0-9]+).*/\1/')
205+
206+
if [[ -n "${version}" ]]; then
207+
# Use go env to get the actual GOBIN/GOPATH
208+
local gobin
209+
gobin=$(go env GOBIN)
210+
211+
# If GOBIN is empty, use GOPATH/bin
212+
if [[ -z "${gobin}" ]]; then
213+
local gopath
214+
gopath=$(go env GOPATH)
215+
# Take first entry if GOPATH has multiple paths (colon-separated)
216+
gobin="${gopath%%:*}/bin"
217+
fi
218+
219+
# Check if the versioned binary exists
220+
local bingo_path="${gobin}/golangci-lint-${version}"
221+
if [[ -f "${bingo_path}" ]]; then
222+
echo "${bingo_path}"
223+
return 0
224+
fi
225+
fi
226+
fi
227+
228+
# Check for custom build
200229
if [[ -f ".bingo/golangci-lint" ]]; then
201230
echo ".bingo/golangci-lint"
202231
return 0
@@ -216,6 +245,7 @@ find_golangci_lint() {
216245

217246
echo -e "${RED}Error: golangci-lint not found.${NC}" >&2
218247
echo -e "${RED}Searched for:${NC}" >&2
248+
echo -e " - .bingo/Variables.mk (bingo-managed versioned binary)" >&2
219249
echo -e " - .bingo/golangci-lint" >&2
220250
echo -e " - bin/golangci-lint" >&2
221251
echo -e " - golangci-lint on your \$PATH" >&2
@@ -482,6 +512,23 @@ main() {
482512
# Create temporary config
483513
create_temp_config
484514

515+
# Ensure baseline branch is available (important for CI environments like GitHub Actions)
516+
if ! git rev-parse --verify "${BASELINE_BRANCH}" &> /dev/null; then
517+
echo -e "${YELLOW}Baseline branch '${BASELINE_BRANCH}' not found locally. Fetching from origin...${NC}" >&2
518+
519+
# Fetch the baseline branch from origin
520+
if ! git fetch origin "${BASELINE_BRANCH}:${BASELINE_BRANCH}" 2>&1; then
521+
# If direct fetch fails, try fetching with remote tracking
522+
if ! git fetch origin "${BASELINE_BRANCH}" 2>&1; then
523+
echo -e "${RED}Error: Failed to fetch baseline branch '${BASELINE_BRANCH}' from origin${NC}" >&2
524+
echo -e "${RED}Please ensure the branch exists in the remote repository.${NC}" >&2
525+
exit 1
526+
fi
527+
# Use the remote tracking branch
528+
BASELINE_BRANCH="origin/${BASELINE_BRANCH}"
529+
fi
530+
fi
531+
485532
# Get changed files
486533
get_changed_files > "${TEMP_DIR}/changed_files.txt"
487534

0 commit comments

Comments
 (0)