# yaml-language-server: $schema=../references/diagram.schema.json
title: PostgreSQL StatefulSet with CSI storage and backups
subtitle: A headless Service gives each replica a stable name, each pod claims its own volume from the Longhorn CSI driver, and Velero backs it all up
# One column per ordinal, top to bottom: pod, claim, volume. The edge labels then add height,
# not width, so the figure stays legible in an 880px README column (orrery review).
direction: down
layout: { spacing: compact }

legend:
  solid: Traffic and references
  dashed: Creates
  dotted: Backups

groups:
  cluster: { type: k8s:cluster, label: Kubernetes cluster }
  ns: { type: k8s:namespace, label: Namespace orders, in: cluster }
  # The StatefulSet owns its pods; the claims it stamps out from volumeClaimTemplates outlive them, so they sit outside.
  sts: { type: box, label: StatefulSet postgres, icon: k8s:sts, in: ns }
  velero-ns: { type: k8s:namespace, label: Namespace velero, in: cluster }
  # PersistentVolumes, the StorageClass and the CSI driver are cluster wide, so they sit outside the
  # namespace. A short header keeps it clear of the line into the first volume.
  pvs: { type: box, label: PVs, icon: k8s:pv, in: cluster }

nodes:
  # Laid out above the cluster (see its edge), so the backup leg is one short line.
  bucket: { icon: generic:bucket, label: Object storage, note: S3-compatible bucket }
  # Beside the Service (align), so the client's call is one short line across the top.
  api: { icon: k8s:pod, label: orders-api, note: Client pods, in: ns, align: svc }
  svc: { icon: k8s:svc, label: Headless Service, note: "postgres, clusterIP None", in: ns }
  pod0: { icon: k8s:pod, label: postgres-0, note: Primary, in: sts }
  pod1: { icon: k8s:pod, label: postgres-1, note: Replica, in: sts }
  pod2: { icon: k8s:pod, label: postgres-2, note: Replica, in: sts }
  pvc0: { icon: k8s:pvc, label: data-postgres-0, in: ns }
  pvc1: { icon: k8s:pvc, label: data-postgres-1, in: ns }
  pvc2: { icon: k8s:pvc, label: data-postgres-2, in: ns }
  # No Velero mark in the packs: a generic backup glyph.
  velero: { icon: tabler:history, label: Velero, note: CSI snapshots, in: velero-ns }
  pv0: { icon: k8s:pv, label: PersistentVolume, note: 50Gi RWO, in: pvs }
  pv1: { icon: k8s:pv, label: PersistentVolume, note: 50Gi RWO, in: pvs }
  pv2: { icon: k8s:pv, label: PersistentVolume, note: 50Gi RWO, in: pvs }
  # The driver and the class share the volumes' row (rank, align), right of them.
  csi: { icon: si:longhorn, label: Longhorn CSI driver, note: driver.longhorn.io, in: cluster, align: sc }
  sc: { icon: k8s:sc, label: StorageClass, note: longhorn, in: cluster, rank: last }

edges:
  - api -> svc: postgres-0.postgres
  # The headless Service is the StatefulSet's serviceName: one DNS record per pod.
  - svc -> sts: per-pod DNS
  # One column per ordinal: each pod mounts its own claim, bound to its own volume.
  - pod0 -> pvc0: mounts
  - pod1 -> pvc1: mounts
  - pod2 -> pvc2: mounts
  - pvc0 -> pv0: bound
  - pvc1 -> pv1: bound
  - pvc2 -> pv2: bound
  # The claim template names the class and the class names the driver, which creates the volumes.
  # Only class -> driver is laid out, so the two stay on the volumes' row.
  - sts -> sc: { label: storageClassName, constraint: false }
  - sc -> csi: provisioner
  - csi --> pvs: { constraint: false }
  # Drawn after layout, so the Velero namespace sits beside the orders namespace.
  - { from: velero, to: ns, line: dotted, label: nightly backup, constraint: false }
  # Laid out bucket -> Velero so the bucket sits above the cluster; the arrowhead stays on the bucket.
  - { from: bucket, to: velero, arrow: start, line: dotted, label: S3 API }
