image and replicas.
Open the WebApp types file. It already contains Go structs for WebApp, WebAppSpec, WebAppStatus, and WebAppList. In Go, a struct groups named fields together (similar to a small data-holding class in other languages) and each field must have an explicit type.
Replace the placeholder foo field with:
- an
Imagefield of typestring(exposed asspec.imagein YAML/JSON), and - a
Replicasfield of typeint32with a default value.
Image stringbecomesspec.imagein the Kubernetes API, and the JSON struct tag controls the YAML/JSON field name.- Exported Go field names (capitalized) are required so controller tooling and code generation can see and process them.
- Marker comments like
// +kubebuilder:validation:Requiredand// +kubebuilder:default=1are read by controller-tools to populate the CRD validation schema and defaults.
Run the code generators after changing types so controller-tools can update the generated helpers and the CRD schema.
- Generate deepcopy helpers and other generated code:
- Regenerate manifests (CRDs, roles, webhooks, etc.):
controller-gen reads your Go structs and Kubebuilder marker comments to generate the CustomResourceDefinition YAML. The CRD’s OpenAPIv3 schema will include the new image and replicas fields and will reflect the replicas default. Conceptually, the generated CRD schema for spec will look like:
image and replicas.
Next steps
Now that the types and CRD are in place, the next task is to use spec.image and spec.replicas inside your controller’s reconcile loop so your controller can create and manage underlying resources (Deployments, Services, etc.) to match the desired WebApp state.
Links and references
- Kubebuilder Book
- controller-tools (controller-gen)
- Kubernetes CustomResourceDefinition docs
- OpenAPI v3 validation for CRDs