Skip to main content
The WebApp types file is where your Custom Resource becomes a Go type. Kubebuilder scaffolds this file with placeholder fields; in this lesson we replace the placeholder with a minimal, useful API consisting of 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 Image field of type string (exposed as spec.image in YAML/JSON), and
  • a Replicas field of type int32 with a default value.
The updated spec and an empty status look like this:
Why these fields and tags matter
  • Image string becomes spec.image in 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:Required and // +kubebuilder:default=1 are read by controller-tools to populate the CRD validation schema and defaults.
Summary of markers and JSON tag behavior:
Run the code generators after changing types so controller-tools can update the generated helpers and the CRD schema.
Regenerate generated code and manifests After updating your types run the code generators so the deepcopy helpers, CRDs, RBAC, and other artifacts reflect your changes.
  1. Generate deepcopy helpers and other generated code:
Typical abbreviated output:
  1. Regenerate manifests (CRDs, roles, webhooks, etc.):
Typical abbreviated output:
What controller-gen produces 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:
This confirms your Go fields are published in the Kubernetes API schema: the WebApp contract now consists of 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

Watch Video