Skip to content

Commit 82d1f8a

Browse files
ci(api): add codegen drift check (#2463)
## What this PR does - Adds a pre-commit hook and a dedicated GitHub Actions workflow that run `make generate` at the repo root to catch drift in generated API code (CRDs, DeepCopy, clients, RBAC) before it lands on main. - Pre-commit hook is scoped to paths that actually affect codegen (`api/`, `pkg/apis/`, `hack/update-codegen.sh`, `hack/boilerplate.go.txt`) so unrelated commits are not slowed down. - CI workflow (`.github/workflows/codegen-drift.yml`) installs Go from `go.mod`, runs `make generate`, and fails on drift with an error pointing contributors to the local fix. - Includes one drift fix the check surfaced: `RestoreJobSpec.Options` (added in #2437) had no `DeepCopyInto` handling — regenerated. ### Release note ```release-note ci(api): add pre-commit hook and GitHub Actions workflow that verify generated API code (CRDs, deepcopy, clients, RBAC) is in sync with `make generate`. ``` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Chores** * Added a CI workflow that checks generated code during PRs and fails the build if generated artifacts diverge. * Added a pre-commit hook that runs generation checks locally to prevent committing outdated generated files. * Fixed deep-copy behavior for backup restore job specs so nested options are correctly duplicated. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2 parents f1e5641 + 9d552d4 commit 82d1f8a

3 files changed

Lines changed: 77 additions & 0 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
name: Codegen Drift Check
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, reopened]
6+
paths:
7+
- 'api/**'
8+
- 'pkg/apis/**'
9+
- 'pkg/generated/**'
10+
- 'internal/crdinstall/manifests/**'
11+
- 'packages/system/cozystack-controller/definitions/**'
12+
- 'packages/system/application-definition-crd/definition/**'
13+
- 'packages/system/backup-controller/definitions/**'
14+
- 'packages/system/backupstrategy-controller/definitions/**'
15+
- 'hack/update-codegen.sh'
16+
- 'hack/boilerplate.go.txt'
17+
- 'Makefile'
18+
- 'go.mod'
19+
- 'go.sum'
20+
- '.github/workflows/codegen-drift.yml'
21+
22+
concurrency:
23+
group: codegen-drift-${{ github.workflow }}-${{ github.event.pull_request.number }}
24+
cancel-in-progress: true
25+
26+
jobs:
27+
codegen-drift:
28+
name: Verify generated code is up to date
29+
runs-on: ubuntu-22.04
30+
steps:
31+
- name: Checkout code
32+
uses: actions/checkout@v4
33+
34+
- name: Set up Go
35+
uses: actions/setup-go@v5
36+
with:
37+
go-version-file: go.mod
38+
cache: true
39+
40+
- name: Pre-fetch k8s.io/code-generator module
41+
# hack/update-codegen.sh sources kube_codegen.sh from the Go module cache.
42+
# The module is not declared in go.mod, so fetch it explicitly at the
43+
# version pinned in the script.
44+
run: |
45+
version=$(grep -oP 'code-generator@\Kv[0-9.]+' hack/update-codegen.sh)
46+
tmpdir=$(mktemp -d)
47+
cd "$tmpdir"
48+
go mod init codegen-fetch
49+
go get "k8s.io/code-generator@${version}"
50+
51+
- name: Run make generate
52+
run: make generate
53+
54+
- name: Fail on drift
55+
run: |
56+
if [ -n "$(git status --porcelain)" ]; then
57+
echo "::error::'make generate' produced changes. Run 'make generate' locally and commit the result."
58+
git status --short
59+
git diff --color=always
60+
exit 1
61+
fi

‎.pre-commit-config.yaml‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
repos:
22
- repo: local
33
hooks:
4+
- id: run-make-generate-root
5+
name: Run 'make generate' at repo root
6+
entry: |
7+
flock -x .git/pre-commit.lock sh -c '
8+
echo "Running make generate at repo root"
9+
make generate || exit $?
10+
git diff --color=always | cat
11+
'
12+
language: system
13+
files: ^(api/|pkg/apis/|pkg/generated/|internal/crdinstall/manifests/|packages/system/cozystack-controller/definitions/|packages/system/application-definition-crd/definition/|packages/system/backup-controller/definitions/|packages/system/backupstrategy-controller/definitions/|hack/update-codegen\.sh$|hack/boilerplate\.go\.txt$|Makefile$)
14+
pass_filenames: false
415
- id: run-make-generate
516
name: Run 'make generate' in all app directories
617
entry: |

‎api/backups/v1alpha1/zz_generated.deepcopy.go‎

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)