Skip to main content
The WebApp controller in this tutorial already creates a Deployment. We will extend it to create a second child object: a ConfigMap. A ConfigMap is a Kubernetes object for storing small pieces of configuration text, such as settings, file contents, or simple HTML that an application can read later. This demonstrates the common controller-runtime reconcile pattern: build the desired child object, set ownership, check if it exists, create it if missing, and return real errors so the controller requeues appropriately. Quick links and references Imports Add the following imports to the top of your controller file if they are not already present:
Helpers Keep object construction logic in helper functions so Reconcile stays compact and easy to follow.
  • deploymentFor constructs the desired Deployment for a given WebApp.
  • configMapFor constructs the desired ConfigMap for a given WebApp.
Example helper implementations:
Why these fields? Setup the controller Ensure SetupWithManager still registers the WebApp type:
Call the helper from Reconcile Update Reconcile to build the desired ConfigMap, set the owner reference, and create it if it’s missing. The pattern:
  1. Fetch the WebApp instance.
  2. Build the desired child: desiredCM := configMapFor(&webapp).
  3. Set the controller reference so garbage collection and ownership work.
  4. Attempt to Get the existing ConfigMap.
  5. If IsNotFound, Create the ConfigMap.
  6. Return real errors for other failures (so controller-runtime retries).
Example Reconcile (only the relevant parts for ConfigMap handling are shown):
Always set the controller reference (owner reference) on child objects. Without it, the garbage collector won’t remove children when the parent is deleted, and the relationship won’t be visible to controller-runtime’s owner-based watches.
Error handling notes
  • If controllerutil.SetControllerReference fails, return the error to requeue reconciliation.
  • If Get returns IsNotFound, create the resource; if the Create call fails, return the error so the reconcile is retried.
  • For any other Get error (API server unavailable, permission issues, etc.), return that error so controller-runtime retries.
  • This initial implementation implements a create-if-missing pattern. You can extend it later to add update-and-repair behavior for existing children (compare desired vs found and patch/update as needed).
Build and test Compile and run your controller to catch Go mistakes and test behavior:
Example output:
Summary This pattern is reusable for other child types (Secrets, Services, etc.). The key steps are: construct the desired child object, set the owner reference so the parent controls lifecycle and ownership, Get the existing child, Create if missing, and return errors to allow controller-runtime to requeue and retry.
ConfigMaps are namespaced. Always set the child’s Namespace to the parent’s namespace and set the controller reference so garbage collection and ownership are handled correctly.

Watch Video