Skip to main content
In this lesson we demonstrate Crossplane Composition functions that Patch and Transform fields while composing resources. Using a WebApp composite resource (XR), we show four transform types supported by the Patch-and-Transform function:
  • map — value mapping (e.g., tier -> cpuLimit)
  • combine — join multiple XR fields into a single string (e.g., appId)
  • toCompositeFieldPath — write data from composed resources back into the XR status
  • convert — type conversion (e.g., integer -> string)
The WebApp composite allows platform users to request an application environment by specifying tier, environment, and replicas. The platform (the Composition) decides how those values map to concrete resource configuration (here, a ConfigMap in the function-lab namespace). Prerequisites
  • A Crossplane control plane with the function function-patch-and-transform installed.
  • A Kubernetes cluster where composed resources are created in the function-lab namespace.
  • Familiarity with Crossplane CompositeResourceDefinitions (XRD) and Compositions.
Overview of the XRD First, verify the CompositeResourceDefinition (XRD) for WebApp exists and is established:
A minimal XRD used in this lesson (before adding a status field) looks like:
Composition: pipeline with Patch-and-Transform function This Composition uses mode: Pipeline and a single pipeline step that references the function-patch-and-transform function. The pipeline composes one resource: a Kubernetes Object that becomes a ConfigMap (in function-lab) whose data is populated via patches and transforms. Important: when editing an existing Composition to add patches, always append to the patches array — do not replace it. This preserves previously defined mappings.
When adding new patches, append them to the existing patches array. Replacing the array can remove previously defined mappings.
Full Composition (final version used in this lesson)
Notes on the Composition
  • ConfigMap data is string-keyed and string-valued; conversions are often necessary (e.g., integer -> string).
  • ToCompositeFieldPath writes back into the XR status; the XRD must declare the status field being written or Crossplane will reject the status update.
  1. Map transform (value mapping)
Use-case: translate a user-friendly tier into a concrete CPU limit string that the composed ConfigMap expects. Patch snippet (included above):
Apply the updated Composition:
Create a WebApp XR to test mapping:
Inspect the generated ConfigMap (namespace function-lab):
Example resulting ConfigMap data:
  1. Combine transform (CombineFromComposite)
Use-case: generate a unique app ID by concatenating the XR resource name and the requested environment. Patch snippet (included in the full composition above):
After applying the Composition and the XR (example above), the ConfigMap will include:
  1. ToCompositeFieldPath (write back to XR status)
Use-case: surface composed resource metadata back to the XR status so users can easily find composed resource identifiers (e.g., the generated ConfigMap name). Patch snippet (included in the full composition):
Crossplane requires that any status field you write with ToCompositeFieldPath be declared in the XRD schema. If status.configName is not declared, Crossplane will reject the status patch with an error like:
Add the configName field to your XRD status properties before applying this patch.
Example XRD status addition:
After updating the XRD and reapplying the Composition and XR, the WebApp’s status will include the composed ConfigMap name:
Excerpt of status:
  1. Convert transform (type conversion)
Use-case: ConfigMap data values must be strings. Convert integer spec.replicas to a string and place it in the ConfigMap data. Patch snippet (included earlier):
After applying the Composition and XR, the ConfigMap data will include replicas as a string:
Transform summary table Putting it all together — expected results
  • Map: maps spec.tier -> data.cpuLimit according to the map table.
  • Combine: concatenates metadata.name and spec.environment -> data.appId.
  • ToCompositeFieldPath: writes the composed ConfigMap name back to status.configName on the XR (requires XRD declaration).
  • Convert: converts spec.replicas (integer) -> data.replicas (string).
Best practices and tips
  • Always append new patches to the end of the patches array instead of replacing it to avoid accidental removal of existing mappings.
  • When writing back to XR status, update the XRD first to include any new status properties you plan to set.
  • Verify composed resource types (e.g., ConfigMap expects string data) and use convert transforms when necessary.
References and next steps
  • Crossplane Composition docs: https://crossplane.io/docs/
  • Crossplane Functions (patch-and-transform): check your installed function manifest and documentation for additional transform options.
Try applying these patterns in your own Compositions to standardize how platform users express intent (XR fields) and how the platform maps that intent to concrete Kubernetes resources.

Watch Video

Practice Lab