Skip to main content
When a controller receives a Reconcile request it is given only the resource key (namespace + name), not the full custom resource object. The first step for a robust Kubernetes controller is to use that key to fetch the current WebApp object from the API server before making changes to any child resources. Below is an example controller file showing the file header Kubebuilder generates and the imports used by the reconciler implementation.
Why fetch the object?
  • The Reconcile request provides req.NamespacedName (namespace + name). You must call the API to obtain the live object so your controller works against the current desired state.
  • Fetching the object lets you inspect webapp.Spec, detect deletions, and avoid reconciling against stale or deleted resources.
Reconcile implementation (fetch and handle not-found)
  • Obtain a logger from the context and store it in a real variable.
  • Create an empty webapp variable of type webappv1.WebApp which the client will populate.
  • Call the reconciler’s Get with req.NamespacedName and a pointer to that variable.
  • Treat “not found” as a normal condition (object deleted after the request was queued) using client.IgnoreNotFound(err). Return real errors for other failure cases.
Quick summary of the core steps Register the controller with the manager
  • The generated SetupWithManager wires the reconciler to watch webappv1.WebApp objects. This ensures your reconciler gets called whenever the API server reports changes to those CRs.
Use client.IgnoreNotFound(err) to treat deleted objects as a normal condition. That prevents your controller from requeuing errors for resources that no longer exist.
Build and test locally
  • Run the full project build to catch missing imports, typos, and invalid syntax before running the controller:
  • Start the manager and apply a sample WebApp manifest. Verify the controller logs show the expected fields (for example, image and replicas) — that indicates the initial reconcile step is functioning.
Useful links and references

Watch Video

Practice Lab