Skip to main content

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​

PathGeneratorCommand
api/core/v1alpha1/zz_generated.deepcopy.gocontroller-gen (object)make generate
client-go/clientset/client-genmake generate
client-go/listers/lister-genmake generate
client-go/informers/informer-genmake generate
client-go/applyconfiguration/applyconfiguration-genmake generate
config/crd/bases/*.yamlcontroller-gen (crd)make manifests
config/rbac/role.yamlcontroller-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​

  1. Change the fields or markers in api/core/v1alpha1/*_types.go.
  2. Run make generate manifests to regenerate the code and manifests.
  3. Run make test. It runs make manifests and make generate first, so the tests use the latest CRDs and generated code.
  4. 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.

MarkerWhereEffect
+kubebuilder:object:generate=trueAbove the package clause (groupversion_info.go, doc.go)Generates DeepCopy and DeepCopyInto for every type in the package
+groupName=fusioninfer.ioAbove the package clause (groupversion_info.go, doc.go)Sets the API group, which also names the client method FusioninferV1alpha1()
+kubebuilder:object:root=trueTop-level resource types and their List typesAlso generates DeepCopyObject, so the type implements runtime.Object
+kubebuilder:resource:scope=Namespaced, scope=ClusterTop-level resource typesMakes the CRD namespaced or cluster-scoped
+kubebuilder:subresource:statusTop-level resource typesEnables the status subresource on the CRD
+genclientTop-level resource typesGenerates the typed client, lister, informer, and apply configurations
+genclient:nonNamespacedCluster-scoped resource typesGenerates a client without a namespace argument
+genclient:noStatusResource types without statusSkips UpdateStatus and ApplyStatus
+kubebuilder:validation:*, +required, +optionalFields or typesAdds OpenAPI validation to the CRD; XValidation adds CEL validation rules
+kubebuilder:rbacController codeGenerates the controller's permissions in config/rbac/role.yaml