Code Generation
Overview
FusionInfer's API types live in api/core/v1alpha1/*_types.go. DeepCopy methods, client-go, CRDs, and the controller's RBAC role are generated from these types and the markers in them, so you don't write them by hand.
Generated Go files start with // Code generated by <generator>. DO NOT EDIT., and generated CRDs carry the controller-gen.kubebuilder.io/version annotation. Don't edit these files by hand: the next generation run overwrites your changes, and CI catches generated files that don't match the source.
Generated Files
| Path | Generator | Command |
|---|---|---|
api/core/v1alpha1/zz_generated.deepcopy.go | controller-gen (object) | make generate |
client-go/clientset/ | client-gen | make generate |
client-go/listers/ | lister-gen | make generate |
client-go/informers/ | informer-gen | make generate |
client-go/applyconfiguration/ | applyconfiguration-gen | make generate |
config/crd/bases/*.yaml | controller-gen (crd) | make manifests |
config/rbac/role.yaml | controller-gen (rbac) | make manifests |
make generate first runs controller-gen to generate DeepCopy, then calls hack/update-codegen.sh, which uses the Kubernetes code-generator to generate the code under client-go/. The Makefile reads the k8s.io/api version from go.mod and uses the same code-generator version. CONTROLLER_TOOLS_VERSION in the Makefile pins the controller-gen version. Both tools are downloaded automatically the first time they run.
Changing the API
- Change the fields or markers in
api/core/v1alpha1/*_types.go. - Run
make generate manifeststo regenerate the code and manifests. - Run
make test. It runsmake manifestsandmake generatefirst, so the tests use the latest CRDs and generated code. - Commit the source and the generated files together.
The Unit Tests job in CI runs make generate and make manifests again and checks whether the working tree changed. If the generated files don't match the source, CI fails with:
Generated code or manifests are not up to date. Run 'make generate manifests' and commit the changes.
When you see this error, run make generate manifests locally and commit the result.
Generation Markers
The generators decide what to produce from markers in Go comments. The table lists the markers this repository commonly uses; see the kubebuilder marker reference for the full list.
| Marker | Where | Effect |
|---|---|---|
+kubebuilder:object:generate=true | Above the package clause (groupversion_info.go, doc.go) | Generates DeepCopy and DeepCopyInto for every type in the package |
+groupName=fusioninfer.io | Above the package clause (groupversion_info.go, doc.go) | Sets the API group, which also names the client method FusioninferV1alpha1() |
+kubebuilder:object:root=true | Top-level resource types and their List types | Also generates DeepCopyObject, so the type implements runtime.Object |
+kubebuilder:resource:scope=Namespaced, scope=Cluster | Top-level resource types | Makes the CRD namespaced or cluster-scoped |
+kubebuilder:subresource:status | Top-level resource types | Enables the status subresource on the CRD |
+genclient | Top-level resource types | Generates the typed client, lister, informer, and apply configurations |
+genclient:nonNamespaced | Cluster-scoped resource types | Generates a client without a namespace argument |
+genclient:noStatus | Resource types without status | Skips UpdateStatus and ApplyStatus |
+kubebuilder:validation:*, +required, +optional | Fields or types | Adds OpenAPI validation to the CRD; XValidation adds CEL validation rules |
+kubebuilder:rbac | Controller code | Generates the controller's permissions in config/rbac/role.yaml |