Workshop 2: Turn it into an API with Crossplane¶
This workshop shows how to create a new kind of resource, Cache, that a
tenant can request in their own chart. Crossplane turns each Cache into a
Valkey StatefulSet and a Service.
Before you begin¶
- A running platform.
make statusshows all pods healthy. - Workshop 1 is not required.
- Read
platform-apps/orchestration/crossplane-compositions. ThePostgresDatabasekind there follows the same pattern.
This workshop takes one to two hours.
Objectives¶
- Define a
Cachekind with a CompositeResourceDefinition. - Implement it with a Composition rendered by
function-go-templating. - Grant Crossplane permission to create the composed resources.
- Request a
Cacheand verify it becomes Ready.
How compositions work in LDP¶
Crossplane creates ordinary Kubernetes resources. A CompositeResourceDefinition
(XRD) declares a new kind and its schema. A Composition says what to create
for each instance; in LDP it delegates rendering to function-go-templating,
so the resources are a Go template in files/. Crossplane can only create
kinds it has RBAC for, which rbac.yaml grants. Argo CD treats the Ready
condition of every *.ldp kind as its health, so a Cache shows as Healthy or
Progressing like any other resource.
Define the kind¶
Create the XRD:
# Namespaced so a cache lives beside the app that uses it.
apiVersion: apiextensions.crossplane.io/v2
kind: CompositeResourceDefinition
metadata:
name: caches.storage.ldp
annotations:
argocd.argoproj.io/sync-options: SkipDryRunOnMissingResource=true
argocd.argoproj.io/sync-wave: '-1'
spec:
scope: Namespaced
group: storage.ldp
names:
kind: Cache
plural: caches
versions:
- name: v1alpha1
served: true
referenceable: true
additionalPrinterColumns:
- name: Memory
type: string
jsonPath: .spec.memory
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
memory:
type: string
description: Memory limit for the cache, also its maxmemory
default: 64Mi
labels:
type: object
description: Labels copied onto the pod and service
additionalProperties:
type: string
The schema is the API contract. Keep it small: every field you expose is one
you support from now on. The defaults let a tenant write kind: Cache with an
empty spec.
Write the template¶
Create the Go template that renders each Cache:
{{- /* Rendered by function-go-templating, not Helm. */ -}}
{{- $xr := getCompositeResource . }}
{{- $name := $xr.metadata.name }}
{{- $ns := $xr.metadata.namespace }}
{{- $spec := $xr.spec }}
{{- $server := getComposedResource . "server" }}
{{- $ready := and $server (eq (int (dig "status" "readyReplicas" 0 $server)) 1) }}
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: {{ $name }}
namespace: {{ $ns }}
{{- with $spec.labels }}
labels:
{{- toYaml . | nindent 4 }}
{{- end }}
annotations:
gotemplating.fn.crossplane.io/composition-resource-name: server
gotemplating.fn.crossplane.io/ready: {{ if $ready }}"True"{{ else }}"False"{{ end }}
spec:
serviceName: {{ $name }}
replicas: 1
selector:
matchLabels:
storage.ldp/cache: {{ $name }}
template:
metadata:
labels:
storage.ldp/cache: {{ $name }}
{{- with $spec.labels }}
{{- toYaml . | nindent 8 }}
{{- end }}
spec:
containers:
- name: valkey
image: docker.io/valkey/valkey:8-alpine
args: ["--maxmemory", "{{ $spec.memory | lower | replace "i" "" }}b", "--maxmemory-policy", "allkeys-lru"]
ports:
- name: valkey
containerPort: 6379
readinessProbe:
exec:
command: ["valkey-cli", "PING"]
resources:
requests:
cpu: 10m
memory: {{ $spec.memory }}
limits:
memory: {{ $spec.memory }}
---
apiVersion: v1
kind: Service
metadata:
name: {{ $name }}
namespace: {{ $ns }}
annotations:
gotemplating.fn.crossplane.io/composition-resource-name: service
# A Service has no Ready condition, so say so, or the Cache never goes Ready.
gotemplating.fn.crossplane.io/ready: "True"
spec:
selector:
storage.ldp/cache: {{ $name }}
ports:
- name: valkey
port: 6379
Notice three things:
composition-resource-namenames each composed resource so Crossplane can track it between reconciles, andgetComposedResourcereads back what was created last time.- The
readyannotation tells Crossplane when each resource is ready. Every composed resource needs one. The StatefulSet is ready when it has one ready replica. A Service has no Ready condition, so its annotation is simply"True". Without it, theCachereportsUnready resources: serviceforever. - Everything is created in the
Cache's own namespace, because the XRD is namespaced.
Configure the composition¶
Create the Composition. It inlines the template file:
apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
name: cache.storage.ldp
annotations:
argocd.argoproj.io/sync-options: SkipDryRunOnMissingResource=true
argocd.argoproj.io/sync-wave: '-1'
spec:
compositeTypeRef:
apiVersion: storage.ldp/v1alpha1
kind: Cache
mode: Pipeline
pipeline:
- step: render
functionRef:
name: function-go-templating
input:
apiVersion: gotemplating.fn.crossplane.io/v1beta1
kind: GoTemplate
source: Inline
inline:
template: |
{{ .Files.Get "files/cache.yaml.gotmpl" | indent 12 }}
Helm inlines the file; Crossplane's function renders it. The .gotmpl file
starts with a comment saying which of the two template languages it is for.
Grant permission¶
Add this ClusterRole to
platform-apps/orchestration/crossplane-compositions/templates/rbac.yaml:
---
# Grants the kinds the Cache composition creates.
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: crossplane:compose-cache
labels:
rbac.crossplane.io/aggregate-to-crossplane: "true"
annotations:
argocd.argoproj.io/sync-wave: '-2'
rules:
- apiGroups: ["apps"]
resources: ["statefulsets"]
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
- apiGroups: [""]
resources: ["services"]
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
Without it the composition renders but nothing is created, and the Cache
shows a forbidden error in its Synced condition.
Deploy the definition¶
-
Lint and commit:
helm lint platform-apps/orchestration/crossplane-compositions git add platform-apps/orchestration/crossplane-compositions git commit -m "feat(crossplane): Cache kind for tenant caches"The
crossplane-compositionsApplication is in wave 2, so Argo CD syncs it as soon as it sees the commit. -
Check that the definition is established:
The output is similar to:
Request a Cache¶
-
Create a namespace and a
Cache: -
Watch it become Ready:
The output is similar to:
-
Look at what was created:
The output is similar to:
-
Connect to it:
kubectl -n demo run cli --rm -it --restart=Never \ --image=docker.io/valkey/valkey:8-alpine -- valkey-cli -h demo PINGThe output is
PONG.
In a tenant chart, the same request looks like this, and the app reaches the
cache at <release>-cache:6379:
apiVersion: storage.ldp/v1alpha1
kind: Cache
metadata:
name: {{ .Release.Name }}-cache
annotations:
argocd.argoproj.io/sync-options: SkipDryRunOnMissingResource=true
spec:
memory: 32Mi
Clean up¶
Delete the Cache and the namespace. Crossplane removes the StatefulSet and
Service:
Keep the XRD and Composition if you are continuing to Workshop 3.
What's next¶
- Workshop 3: Ship a golden path puts a
Cachein a Backstage template so tenants get one without writing YAML. - To add a generated password, see how
client.yaml.gotmpluses an External SecretsPasswordgenerator, and the rules for generated secrets.