Resource Example
Create the scaffolding for a new resource using the kubebuilder cli:
This creates several files, including the Resource schema definition in:
pkg/apis/workloads/v1beta1/containerset_types.go
- Spec contains the desired cluster state specified by the object. While much of the Spec isdefined by users, unspecified parts may be filled in with defaults or by Controllers such as autoscalers.
- TypeMeta contains metadata about the API itself - such as Group, Version, Kind.
- ObjectMeta contains metadata about the specific object instance - such as the name, namespace,labels and annotations. ObjectMeta contains data common to most objects.
Comment annotation directives
The definition contains several comment annotations of the form . These areused to configure code generators to run against this code. The code generators will generate boilerplate functions and types to complete the Resource definition.
To learn more about how to use annotations in kubebuilder, refer to Generating CRD.
The ContainerSetSpec contains the container image and replica count, which should be read bythe controller and used to create and manage a new Deployment. The Spec field contains desiredstate defined by the user or, if unspecified, field defaults or Controllers set values.An example of an unspecified field that could be owned by a Controller would be the field, which may be set by autoscalers.
This field is propagated from the DeploymentStatus, and so the controller must watch for Deploymentevents to update the field.
Running Code Generators
While users don't directly modify generated code, the code must be regenerated after resources aremodified by adding or removing fields. This is automatically done when running .
Kubebuilder scaffolds boilerplate code to register resources with the runtime.Scheme used tomap go structs to GroupVersionKinds.
- SchemeBuilder should have every API in the package type added to it