Build an Internal Developer Platform — Backstage, Golden Paths & Crossplane›08 · Platform APIs with Crossplane

Lesson 08 of 11 · Self-service with guard-rails

Platform APIs with Crossplane

Offer infrastructure as a simple, cloud-neutral API: define what developers may ask for with an XRD, implement it per environment with Compositions built from functions, let Crossplane reconcile and correct drift continuously, and handle the edge cases (immutable fields, adopting existing resources, emergency changes).

Advanced → Architect
Key wordsCrossplaneplatform APICompositeResourceDefinitionXRDCompositioncomposition functionsfunction-go-templatingprovidersmanaged resourcescomposite resourceclaimscompositionSelectordriftmanagementPoliciescrossplane.io/pausedexternal-name

Why platform APIs

Tickets for databases, buckets and queues are slow; giving every team full cloud access is unsafe and inconsistent. A platform API sits in between: teams ask for what they need in a small, safe vocabulary; the platform decides how.

apiVersion: platform.example.io/v1alpha1
kind: Database
metadata:
  name: orders
  namespace: team-shop
spec:
  size: small        # small | medium: no instance classes, no subnets, no cloud names

The same request can become a managed cloud database in one environment and an in-cluster PostgreSQL in another.

You order "a medium pizza, vegetarian" from the menu. You don't tell the kitchen which oven, which supplier or how long to bake it. Different branches may cook it differently, but you always get a medium vegetarian pizza.

The pieces

XRD (the API)  ──►  Composite resource (a team's request: kind Database)
                       │ selected Composition (per backend)
                       ▼
                    Composition pipeline of functions  ──►  composed resources
                       e.g. RDS instance + subnet group   (cloud: via providers)
                       or Secret + Service + StatefulSet  (local)
Providers (AWS, Azure, GCP, Kubernetes, Helm…) talk to the real APIs, with their own credentials.

The API: an XRD

apiVersion: apiextensions.crossplane.io/v2
kind: CompositeResourceDefinition
metadata:
  name: databases.platform.example.io
spec:
  group: platform.example.io
  names: { kind: Database, plural: databases }
  scope: Namespaced
  versions:
  - name: v1alpha1
    served: true
    referenceable: true
    schema:
      openAPIV3Schema:
        type: object
        properties:
          spec:
            type: object
            required: [ size ]
            properties:
              engine: { type: string, enum: [ postgres ], default: postgres }
              size: { type: string, enum: [ small, medium ] }
              tenant: { type: string, description: "Used for cost attribution labels" }
          status:
            type: object
            properties:
              endpoint: { type: string }
              connectionSecret: { type: string }

With Crossplane v2, composite resources can be namespaced, so teams create them directly in their namespace (v1 used a separate claim object for this). Validation (enums, required fields) happens at the API server: a bad request fails immediately.

The implementation: Compositions made of functions

A Composition is a pipeline of functions. The tested local implementation used function-go-templating to render a Secret, a Service and a StatefulSet, then function-auto-ready to mark readiness:

apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
  name: database-local
  labels: { provider: local }
spec:
  compositeTypeRef: { apiVersion: platform.example.io/v1alpha1, kind: Database }
  mode: Pipeline
  pipeline:
  - step: render
    functionRef: { name: function-go-templating }
    input:
      apiVersion: gotemplating.fn.crossplane.io/v1beta1
      kind: GoTemplate
      source: Inline
      inline:
        template: |
          {{- $xr := .observed.composite.resource }}
          # … Secret, Service and StatefulSet named after $xr.metadata.name,
          # with CPU/memory chosen from $xr.spec.size …
  - step: ready
    functionRef: { name: function-auto-ready }

Write one Composition per backend (provider: aws rendering an RDS instance through the AWS provider, provider: azure rendering an Azure Database flexible server, provider: local) and select by label: a default per cluster, or spec.crossplane.compositionSelector on the request.

Found while testing

  • Immutable fields: tying size to the StatefulSet's volumeClaimTemplates made every size change fail, because those templates are immutable. Map size to CPU and memory only, and grow storage through volume expansion. On clouds, check which instance fields need replacement.
  • Stable generated values: generating a password with a random function changes it on every reconcile. Derive it from something stable (for example a hash of the resource UID) or let a secret store generate it once.

Continuous reconciliation and drift

Crossplane re-observes every managed resource on an interval and changes it back if it differs from the spec. Plan for the edge cases:

  • Prevent manual edits in the cloud: deny changes to resources tagged as Crossplane-managed except by Crossplane's identity (SCPs, Azure Policy).
  • Break-glass: to change something by hand during an incident, annotate the resource crossplane.io/paused: "true", make the change, update Git to match, then unpause.
  • Adopt existing resources with the crossplane.io/external-name annotation, and use managementPolicies (for example observe-only) while migrating from Terraform. One resource must have one owner, or two tools will fight.
  • Deletion safety: deletionPolicy: Orphan for anything holding data you can't afford to lose by mistake.

Try it

Install Crossplane in the hub, the two functions, the XRD and the local Composition. Create the orders Database in team-shop, then trace it (crossplane beta trace, or crossplane resource trace in newer CLI versions) to see what was composed. Change size to medium and watch requests and limits change. Delete the generated Service and watch Crossplane recreate it.

Recap

  • An XRD defines a small, validated, cloud-neutral API; Compositions implement it per backend with functions.
  • Crossplane reconciles continuously: drift is corrected. Plan pause, adoption and deletion policies.
  • Design the API mapping around immutable fields, and keep generated values stable.

This site is a public version of my personal engineering knowledge hub. It intentionally excludes confidential company information and internal operational details.