Skip to main content
The reconciler can now fetch the WebApp custom resource. Next, we need to build the desired Deployment object in Go. At this stage the helper only constructs the in-memory Deployment object — it does not yet call the Kubernetes API. This section focuses on the shape of the Deployment the controller will want to create.
The image is a slide with the title "Create A Deployment From Spec" on the left and the word "Demo" on the right. It also includes a copyright notice for KodeKloud.

Add imports

Open the controller file and add imports for the built-in Kubernetes types we’ll use. These provide the Deployment, Pod template types, and metadata helpers.

Reconcile skeleton and helper signature

Keep the Reconcile skeleton that reads the WebApp resource. We’ll add a helper named deploymentFor that builds the Deployment object for a given *webappv1.WebApp.

Build labels

Use a Go map to hold labels that will be applied to both the Deployment selector and the pod template. The selector and template labels must match so the Deployment can find and manage its Pods.
Ensure the Deployment selector and the Pod template labels are identical. If they differ, the Deployment will not manage the Pods you expect.

Replicas as a pointer

Kubernetes expects spec.replicas to be an optional pointer (*int32). Create a local variable, then pass its address into the Deployment spec.
replicas must be passed as a pointer (e.g., Replicas: &replicas) because the API type defines it as *int32. Using a plain numeric literal will cause compile-time errors.

Build the Deployment struct

Create the appsv1.Deployment struct literal with metadata, selector, and a Pod template. The Pod template contains a corev1.PodSpec with a single container named web. The container image comes from the WebApp spec and we expose port 80 inside the Pod.
This helper establishes the first clear connection between your custom resource and a built-in Kubernetes workload: the WebApp tells the operator which image and replica count it wants, and the helper translates that into a Deployment object.

Types used

Build and verify

Run go build ./... in your module root. A clean build (no compiler errors) verifies imports, field names, and pointer usage. If the build fails, check for:
  • Missing imports (e.g., metav1, appsv1, corev1)
  • Incorrect field names or types
  • Passing a non-pointer where a pointer is required
At this stage the helper constructs the desired Deployment but does not yet contact the API server. The helper answers: “If this WebApp exists, what Deployment object should represent it?” In a subsequent step you will call deploymentFor from Reconcile and create or update the Deployment via the controller-runtime client.

Watch Video