Skip to content

Use codegen to produce CRDs#28

Open
booxter wants to merge 1 commit intok8snetworkplumbingwg:masterfrom
booxter:codegen-crds
Open

Use codegen to produce CRDs#28
booxter wants to merge 1 commit intok8snetworkplumbingwg:masterfrom
booxter:codegen-crds

Conversation

@booxter
Copy link
Copy Markdown

@booxter booxter commented Jan 26, 2026

I tried to keep the difference in descriptions of fields to the minimum.
I advise to review the change to CRD file with (-w, ignore whitespace)
option enabled.

I tried to keep the difference in descriptions of fields to the minimum.
I advise to review the change to CRD file with (-w, ignore whitespace)
option enabled.

Signed-off-by: Ihar Hrachyshka <ihrachyshka@nvidia.com>
Assisted-By: Claude Code; opus (claude-opus-4-5-20251101)
@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello @booxter, 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 refactors the Custom Resource Definition (CRD) generation process by integrating controller-gen. This change automates the creation of CRD schemas directly from Go type definitions, ensuring that the CRD manifests are always in sync with the Go code. It also significantly improves the clarity and detail of the MultiNetworkPolicy CRD's documentation through extensive kubebuilder annotations and comments, making the API more understandable and maintainable.

Highlights

  • Automated CRD Generation: Introduced the use of controller-gen for automatically generating Custom Resource Definition (CRD) schemas from Go type definitions, enhancing consistency and reducing manual maintenance.
  • New CRD Generation Script: Added a new shell script, hack/update-crdgen.sh, to automate the process of installing controller-gen and generating the scheme.yml CRD manifest.
  • Enhanced CRD Type Definitions: Updated MultiNetworkPolicy and related types in v1beta1 and v1beta2 API versions with kubebuilder markers and comprehensive documentation comments, aligning them with standard Kubernetes NetworkPolicy descriptions.
  • Documentation Update: The README.md file has been updated to include a new 'Development' section, providing clear instructions on how to regenerate CRD schemas and Go client code.
Ignored Files
  • Ignored by pattern: .github/workflows/** (1)
    • .github/workflows/build.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-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-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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

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.

Copy link
Copy Markdown

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

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 code generation for the CRD schema using controller-gen, which is a significant improvement for maintainability. The changes include adding a generation script, updating Go type definitions with kubebuilder markers and detailed comments, and updating the README.md. My review focuses on making the code generation process more robust and ensuring the CRD versioning is correctly configured. I've identified a critical issue with the storage version configuration that could impact cluster operations and data persistence. Additionally, I've suggested improvements to the generation script to use pinned tool versions for reproducible builds, and enhanced some code comments for better clarity.

Comment on lines +9 to +30
# Determine GOBIN
GOBIN="${GOBIN:-$(go env GOPATH)/bin}"

# Install controller-gen if not present
if ! command -v controller-gen &> /dev/null && [ ! -f "${GOBIN}/controller-gen" ]; then
echo "Installing controller-gen..."
go install sigs.k8s.io/controller-tools/cmd/controller-gen@latest
fi

# Use controller-gen from GOBIN if not in PATH
CONTROLLER_GEN="${GOBIN}/controller-gen"
if command -v controller-gen &> /dev/null; then
CONTROLLER_GEN="controller-gen"
fi

cd "${SCRIPT_ROOT}"

# Generate CRD manifests
echo "Generating CRD manifests..."
"${CONTROLLER_GEN}" crd \
paths="./pkg/apis/k8s.cni.cncf.io/..." \
output:crd:artifacts:config=.
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

To ensure reproducible builds, it's better to use the vendored version of controller-gen via go run instead of installing the @latest version. This avoids unexpected changes in the generated code due to tool updates. This change assumes controller-gen has been added to tools.go as suggested in a separate comment.

Suggested change
# Determine GOBIN
GOBIN="${GOBIN:-$(go env GOPATH)/bin}"
# Install controller-gen if not present
if ! command -v controller-gen &> /dev/null && [ ! -f "${GOBIN}/controller-gen" ]; then
echo "Installing controller-gen..."
go install sigs.k8s.io/controller-tools/cmd/controller-gen@latest
fi
# Use controller-gen from GOBIN if not in PATH
CONTROLLER_GEN="${GOBIN}/controller-gen"
if command -v controller-gen &> /dev/null; then
CONTROLLER_GEN="controller-gen"
fi
cd "${SCRIPT_ROOT}"
# Generate CRD manifests
echo "Generating CRD manifests..."
"${CONTROLLER_GEN}" crd \
paths="./pkg/apis/k8s.cni.cncf.io/..." \
output:crd:artifacts:config=.
cd "${SCRIPT_ROOT}"
# Generate CRD manifests
echo "Generating CRD manifests..."
go run sigs.k8s.io/controller-tools/cmd/controller-gen crd \
paths="./pkg/apis/k8s.cni.cncf.io/..." \
output:crd:artifacts:config=.

Comment on lines +6 to +12
_ "k8s.io/code-generator"
_ "k8s.io/code-generator/cmd/client-gen"
_ "k8s.io/code-generator/cmd/deepcopy-gen"
_ "k8s.io/code-generator/cmd/defaulter-gen"
_ "k8s.io/code-generator/cmd/informer-gen"
_ "k8s.io/code-generator/cmd/lister-gen"
_ "k8s.io/kube-openapi/cmd/openapi-gen"
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

For reproducible builds, it's best to vendor the controller-gen tool and pin its version in go.mod. Please add it to your tool dependencies. This will allow using go run in the generation script, avoiding the need to install it globally or fetch @latest.

	_ "k8s.io/code-generator"
	_ "k8s.io/code-generator/cmd/client-gen"
	_ "k8s.io/code-generator/cmd/deepcopy-gen"
	_ "k8s.io/code-generator/cmd/defaulter-gen"
	_ "k8s.io/code-generator/cmd/informer-gen"
	_ "k8s.io/code-generator/cmd/lister-gen"
	_ "k8s.io/kube-openapi/cmd/openapi-gen"
	_ "sigs.k8s.io/controller-tools/cmd/controller-gen"

@booxter
Copy link
Copy Markdown
Author

booxter commented Jan 26, 2026

@dougbtv @s1061123 thoughts on using codegen to produce the CRDs here?

Copy link
Copy Markdown
Member

@zeeke zeeke left a comment

Choose a reason for hiding this comment

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

LGTM

I prefer to have another opinion here, as the project is used by multiple implementations (iptables, nftables, ovnk, ...)
@dougbtv @s1061123 @bpickard22

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants