Skip to content

ci(api): add codegen drift check - #2463

Merged
myasnikovdaniil merged 4 commits into
mainfrom
ci/codegen-drift-precommit
Apr 28, 2026
Merged

myasnikovdaniil merged 4 commits into
mainfrom
ci/codegen-drift-precommit

Conversation

@myasnikovdaniil

@myasnikovdaniil myasnikovdaniil commented Apr 23, 2026 •

Copy link
Copy Markdown
Contributor

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 feat(dashboard): add RestoreJob list, add page, and sidebar link #2437) had no DeepCopyInto handling — regenerated.

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`.

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.

Run root 'make generate' as a pre-commit hook and as a dedicated
CI workflow so missed codegen updates (CRDs, deepcopy, clients, RBAC)
are caught instead of merging stale generated files.

Pre-commit hook is scoped to files that actually affect codegen
(api/, pkg/apis/, hack/update-codegen.sh, hack/boilerplate.go.txt)
so unrelated commits are not slowed down. CI job sets up Go from
go.mod, runs make generate, and fails on drift with a pointer to
the local fix.

Assisted-By: Claude <[email protected]>
Signed-off-by: Myasnikov Daniil <[email protected]>
The Options field was added to RestoreJobSpec without re-running
'make generate', leaving zz_generated.deepcopy.go out of sync.
Regenerated to include the missing DeepCopyInto handling for the
runtime.RawExtension pointer.

Assisted-By: Claude <[email protected]>
Signed-off-by: Myasnikov Daniil <[email protected]>
@dosubot dosubot Bot added size/XS This PR changes 0-9 lines, ignoring generated files kind/feature Categorizes issue or PR as related to a new feature labels Apr 23, 2026
@coderabbitai

coderabbitai Bot commented Apr 23, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 0ed63d9c-634e-4c59-966b-112f8da4e87f

📥 Commits

Reviewing files that changed from the base of the PR and between 9222b6f and 9d552d4.

📒 Files selected for processing (2)
  • .github/workflows/codegen-drift.yml
  • .pre-commit-config.yaml
🚧 Files skipped from review as they are similar to previous changes (2)
  • .github/workflows/codegen-drift.yml
  • .pre-commit-config.yaml

📝 Walkthrough

Walkthrough

Adds a GitHub Actions workflow and a pre-commit hook that run code generation and fail when generated artifacts drift; also updates generated deepcopy logic to deep-copy RestoreJobSpec.Options when non-nil.

Changes

Cohort / File(s) Summary
CI Workflow
\.github/workflows/codegen-drift.yml
New GitHub Actions workflow triggered on PRs touching codegen-related paths; checks out repo, sets Go from go.mod, fetches the pinned k8s.io/code-generator version (via temporary go mod init + go get), runs make generate, and fails if git diff shows changes.
Pre-commit Hook
.pre-commit-config.yaml
Adds a local pre-commit hook that conditionally runs make generate when specified files/dirs change, uses an exclusive lock at .git/pre-commit.lock, exits non-zero on failure, and prints git diff after running.
Generated Code
api/backups/v1alpha1/zz_generated.deepcopy.go
Modifies DeepCopyInto for RestoreJobSpec to allocate and deep-copy Options (runtime.RawExtension) when non-nil instead of leaving it unchanged.

Sequence Diagram(s)

sequenceDiagram
    participant Dev as Developer (PR)
    participant GH as GitHub Actions
    participant Repo as Repository (checkout)
    participant Go as Go toolchain
    participant KG as k8s/code-generator (fetched)
    participant Make as make generate
    participant Git as git diff

    Dev->>GH: Open PR touching codegen inputs
    GH->>Repo: actions/checkout
    GH->>Go: setup-go (version from go.mod)
    GH->>KG: extract pinned version from hack/update-codegen.sh\nand run go get in temp module
    GH->>Make: run make generate
    Make->>Repo: write generated files
    GH->>Git: git diff --exit-code
    alt no diff
        Git-->>GH: exit 0 (success)
    else diff detected
        Git-->>GH: exit non-zero (fail), print git status & git diff
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 I hop through lines with tiny feet,
I run the maker, keep files neat,
I copy deep where options hide,
CI and hooks keep drift outside,
A tidy repo — my little treat.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'ci(api): add codegen drift check' directly summarizes the main change: adding a CI workflow to detect drift in generated code. It accurately reflects the primary intent of the PR.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch ci/codegen-drift-precommit

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request establishes robust mechanisms to maintain the consistency of generated API code within the repository. By integrating automated checks both locally via a pre-commit hook and in the continuous integration pipeline, it proactively identifies and prevents discrepancies in critical components like CRDs and DeepCopy implementations, thereby enhancing code reliability and developer workflow efficiency.

Highlights

  • Automated Code Generation Drift Check: Introduced a new pre-commit hook and a dedicated GitHub Actions workflow to automatically run "make generate" at the repository root, ensuring generated API code (CRDs, DeepCopy, clients, RBAC) remains in sync.
  • Scoped Pre-commit Hook: The pre-commit hook is intelligently scoped to only trigger when changes occur in relevant paths ("api/", "pkg/apis/", "hack/update-codegen.sh", "hack/boilerplate.go.txt"), preventing unnecessary slowdowns for unrelated commits.
  • Drift Fix Included: Addressed an existing code drift where "RestoreJobSpec.Options" lacked "DeepCopyInto" handling, which was surfaced and fixed by this new check.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Ignored Files
  • Ignored by pattern: **/zz_generated.*.go (1)
    • api/backups/v1alpha1/zz_generated.deepcopy.go
  • Ignored by pattern: .github/workflows/** (1)
    • .github/workflows/codegen-drift.yml
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment Gemini (@gemini-code-assist) Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on Gemini (@gemini-code-assist) comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new pre-commit hook, run-make-generate-root, which automates the execution of 'make generate' at the repository root when changes occur in API definitions or codegen scripts. Feedback suggests expanding the file trigger regex to include the Makefile and generated output directories like pkg/generated/ and internal/crdinstall/manifests/ to ensure a more comprehensive drift check.

Comment thread .pre-commit-config.yaml Outdated
git diff --color=always | cat
'
language: system
files: ^(api/|pkg/apis/|hack/update-codegen\.sh$|hack/boilerplate\.go\.txt$)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The files filter should include the generated output directories and the Makefile to ensure that manual edits to generated files are caught and that changes to the build logic trigger a re-generation check. This ensures the drift check is comprehensive and prevents inconsistent manual changes from being committed.

Consider adding pkg/generated/, internal/crdinstall/manifests/, and Makefile to the regex.

      files: ^(api/|pkg/apis/|pkg/generated/|internal/crdinstall/manifests/|hack/update-codegen\.sh$|hack/boilerplate\.go\.txt$|Makefile$)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call — extended the files: regex on the root hook to include the generated-output directories (pkg/generated/, internal/crdinstall/manifests/, and the four packages/system/*/definitions/ dirs) plus Makefile, so manual edits to generated artifacts or changes to the root generate target also re-run make generate through pre-commit. Commit: 9d552d4.

hack/update-codegen.sh sources kube_codegen.sh from the Go module
cache at ~/go/pkg/mod/k8s.io/[email protected]/, but the module
is not declared in go.mod so a fresh runner has nothing to source
from. Add a workflow step that parses the pinned version out of the
script and pulls the module into the cache before running
make generate.

Assisted-By: Claude <[email protected]>
Signed-off-by: Myasnikov Daniil <[email protected]>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (1)
.pre-commit-config.yaml (1)

13-13: Consider running the local hook for generated-output paths too.

Line 13 only matches generator inputs. Since hack/update-codegen.sh also writes generated clients/OpenAPI/CRDs under pkg/generated, internal/crdinstall/manifests, and packages/system/.../definitions, staged edits to those generated outputs can bypass the local drift check.

Suggested path coverage expansion
-      files: ^(api/|pkg/apis/|hack/update-codegen\.sh$|hack/boilerplate\.go\.txt$)
+      files: ^(api/|pkg/apis/|pkg/generated/|internal/crdinstall/manifests/|packages/system/(cozystack-controller/definitions/|application-definition-crd/definition/|backup-controller/definitions/|backupstrategy-controller/definitions/)|go\.mod$|go\.sum$|hack/update-codegen\.sh$|hack/boilerplate\.go\.txt$)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In @.pre-commit-config.yaml at line 13, Update the files pattern under the
pre-commit hook so it also matches generated-output paths (not just generator
inputs): modify the files entry (the regex currently
`^(api/|pkg/apis/|hack/update-codegen\.sh$)`) to include the generated output
directories created by hack/update-codegen.sh such as `pkg/generated`,
`internal/crdinstall/manifests` and `packages/system/.*/definitions` (or
equivalent glob/regex for those trees) so staged edits to generated
clients/OpenAPI/CRDs cannot bypass the local drift check.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In @.github/workflows/codegen-drift.yml:
- Around line 36-42: The current check uses git diff --exit-code which misses
untracked generated files; replace that conditional in the "Fail on drift" step
so it fails when there are any modifications or untracked files (e.g., run git
status --porcelain --untracked-files=all and exit non-zero if it prints
anything, or use git diff --exit-code || git diff --no-index --quiet /dev/null
generated_files || git status --porcelain to detect untracked files). Locate the
existing conditional that calls git diff --exit-code and update it to run git
status --porcelain --untracked-files=all (or equivalent) and exit 1 when output
is non-empty so new generated CRD/client files cause the job to fail.
- Around line 6-13: The workflow's paths list in
.github/workflows/codegen-drift.yml misses generated output directories so PRs
that only change generated clients/CRDs/OpenAPI can skip the check; update the
paths section (the paths: block in that workflow) to include the generated
outputs such as pkg/generated/** and the config/ or output directories where
CRDs/OpenAPI/clients are emitted (for example config/crd/**, config/openapi/**,
pkg/generated/**) so that changes to generated artifacts trigger the workflow.
- Around line 27-34: The CI can fail on cold runners because
hack/update-codegen.sh sources kube_codegen.sh from the module cache before go
run can populate it; add a pre-generation step in the workflow (after
actions/setup-go@v5 and before the "Run make generate" step) that ensures the
k8s code-generator module is downloaded into the module cache (for example by
running a go install or go get for k8s.io/[email protected]), so
kube_codegen.sh is present when hack/update-codegen.sh sources it; reference the
actions/setup-go step, the "Run make generate" step, and hack/update-codegen.sh
/ kube_codegen.sh when adding this new download step.

---

Nitpick comments:
In @.pre-commit-config.yaml:
- Line 13: Update the files pattern under the pre-commit hook so it also matches
generated-output paths (not just generator inputs): modify the files entry (the
regex currently `^(api/|pkg/apis/|hack/update-codegen\.sh$)`) to include the
generated output directories created by hack/update-codegen.sh such as
`pkg/generated`, `internal/crdinstall/manifests` and
`packages/system/.*/definitions` (or equivalent glob/regex for those trees) so
staged edits to generated clients/OpenAPI/CRDs cannot bypass the local drift
check.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 6d1a399e-9483-43ae-94bd-7d7d56dc3dac

📥 Commits

Reviewing files that changed from the base of the PR and between a92dc76 and 860f431.

📒 Files selected for processing (3)
  • .github/workflows/codegen-drift.yml
  • .pre-commit-config.yaml
  • api/backups/v1alpha1/zz_generated.deepcopy.go

Comment thread .github/workflows/codegen-drift.yml
Comment thread .github/workflows/codegen-drift.yml
Comment thread .github/workflows/codegen-drift.yml
@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Apr 23, 2026
- Add generated-output dirs (pkg/generated, internal/crdinstall/manifests,
  packages/system/*/definitions) and Makefile to the workflow paths: filter
  so PRs that modify only generated artifacts still trigger the drift check.
- Mirror the same paths in the root pre-commit hook's files: regex so
  manual edits to generated files or changes to the root generate target
  re-run make generate through pre-commit.
- Switch drift detection in the workflow from `git diff --exit-code` to
  `git status --porcelain` so new untracked files produced by make generate
  (e.g. generated YAML/Go for a new API type) also fail the job; dump
  `git diff --color=always` on failure for easier debugging.

Signed-off-by: Myasnikov Daniil <[email protected]>
@myasnikovdaniil
myasnikovdaniil merged commit 82d1f8a into main Apr 28, 2026
8 checks passed
@myasnikovdaniil
myasnikovdaniil deleted the ci/codegen-drift-precommit branch April 28, 2026 07:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/feature Categorizes issue or PR as related to a new feature lgtm This PR has been approved by a maintainer size/XS This PR changes 0-9 lines, ignoring generated files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants