diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 3890169..8f3649c 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -250,6 +250,22 @@ jobs: - name: 🔒 Validate the app container's read-only root filesystem run: sh scripts/readonly-rootfs.test.sh + # The platform reconciles deploy/ for production and deploy/dev for the dev environment, both from + # the same published artifact. This renders all three entry points and keeps the production + # identity (route, PDB, backups, OpenBao admin code, production namespace) out of dev. `sh` + + # `kubectl` + `yq` only, all present on the runner. + deploy-overlays: + name: Deploy Overlays + runs-on: ubuntu-latest + permissions: + contents: read + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - name: 🧱 Render production and dev manifests + run: sh scripts/deploy-overlays.test.sh + # The platform deploys a release only when its publish-app.yaml revision is approved for this tenant. # A PR that moves the pin to an unapproved revision would ship a release the platform refuses, so the # template's check compares the base and head pins and fails the move here instead. An unchanged pin @@ -323,7 +339,7 @@ jobs: ci-required-checks: name: CI - Required Checks runs-on: ubuntu-latest - needs: [lint, check, test, e2e, build, boot-smoke, workflow-caller-pins, readonly-rootfs, publish-pin-approved, validate-retired-links, node-toolchain, release-classification] + needs: [lint, check, test, e2e, build, boot-smoke, workflow-caller-pins, readonly-rootfs, deploy-overlays, publish-pin-approved, validate-retired-links, node-toolchain, release-classification] permissions: actions: read if: ${{ always() }} @@ -341,6 +357,7 @@ jobs: ${{ needs.boot-smoke.result }} ${{ needs.workflow-caller-pins.result }} ${{ needs.readonly-rootfs.result }} + ${{ needs.deploy-overlays.result }} ${{ needs.publish-pin-approved.result }} ${{ needs.validate-retired-links.result }} ${{ needs.node-toolchain.result }} diff --git a/AGENTS.md b/AGENTS.md index 507099b..249da58 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -22,7 +22,7 @@ - `src/hooks.server.ts` — request hooks (auth/session). - `drizzle/` — generated SQL migrations and metadata; `drizzle.config.ts` at root. - `tests/unit/` (Vitest) and `tests/e2e/` (Playwright). -- `deploy/` — Kustomize manifests for the platform cluster (Deployment, Service, HTTPRoute, CNPG Cluster, ExternalSecret). App secrets come from OpenBao via External Secrets through the platform-provisioned namespaced `SecretStore` (`openbao`) — no SOPS. +- `deploy/` — Kustomize manifests for the platform cluster, published as one signed artifact for both environments. `deploy/kustomization.yaml` is the production entry the platform reconciles at `path: .` and includes only `prod`. `deploy/base/` is what production and dev share: the app Deployment (whose container receives the published image digest), Service, network policy and the `wedding-db` CloudNativePG Cluster without backups. `deploy/prod/` adds everything that carries the production identity: the public HTTPRoute, the PodDisruptionBudget, the admin code (generated into OpenBao and read back by an ExternalSecret through the namespaced `SecretStore` `openbao` — no SOPS), and the backups (`cluster-backup.yaml` patches the Barman Cloud plugin onto the base Cluster, `db-scheduled-backup.yaml`, the R2 egress patch, and `RESTORE-RUNBOOK.md`). `deploy/dev/` is the dev overlay: the base in the `wedding-app-dev` namespace with one app replica and one database instance, changed only by kustomize settings and patches, never a copied manifest and never a route, backup, production secret path or namespace-keyed network rule (the platform reconciles it at `path: ./dev` and owns its ingress behind sign-on). `scripts/deploy-overlays.test.sh` (CI job `Deploy Overlays`) renders all three and enforces that split. Dev's admin-code source, seeded data, mail sink and bucket are tracked in #467. - CI/CD: `.github/workflows/` — `ci.yaml` (PR gate, also run on every push to `main`), `release.yaml` (semantic-release on `main`), `cd.yaml` (publish OCI artifact on tags). A release, and so a deploy, is cut for `feat`, `fix`, `perf`, breaking and `build(deps)` commits, and for `build(deps-dev)` commits whose title names the `site-bundle` group (`.releaserc`). `build(deps)` is Dependabot's prefix for runtime packages and the Docker base image. The `site-bundle` group in `.github/dependabot.yml` lists the devDependencies adapter-node bundles into the server build (Svelte, SvelteKit, Vite, Tailwind, fonts, icons). Workflow updates use `ci(deps)` and other development-tool updates plain `build(deps-dev)`, and neither releases on its own. `scripts/release-classification.test.sh` fails until a newly added devDependency is classified as bundled or tooling. ## Validation @@ -37,6 +37,7 @@ npm test # Vitest unit tests npm run test:e2e # Playwright E2E (set DEV_SKIP_AUTH=true) npm run build # Vite production build npm run bundlesize # Client bundle-size budget gate (runs after build; see bundle-budget.json) +sh scripts/deploy-overlays.test.sh # Render deploy/ (production) and deploy/dev; needs kubectl + yq ``` The **bundle-size budget** (`bundle-budget.json` + `scripts/check-bundle-budget.mjs`) is a deterministic, first-approximation performance gate: it brotli-compresses the built client JS+CSS in-process and fails CI if the total or the largest single chunk exceeds budget. It runs in the Build job (so it gates `CI - Required Checks`). Tighten the budget as the bundle shrinks; loosen only with a justification in the PR. A full Lighthouse CI run (Core Web Vitals) now runs as a **non-required, warn-only** lane (`npm run lhci`, the `lighthouse` job in `ci.yaml`; see `lighthouserc.json` + `scripts/lighthouse.mjs`): it measures LCP/CLS/TBT/FCP/Performance on the login and program pages against the `@sveltejs/adapter-node` server, reports budget misses as warnings, and is deliberately kept **out of `CI - Required Checks`** until its red/green over ~10 runs shows the Node-adapter server (port binding + cold-start jitter, the reason it was originally deferred) is stable enough to promote. diff --git a/README.md b/README.md index 34bc70d..5bf0384 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,7 @@ Keep connection strings and codes out of commits, logs and public discussion. Live publication is owned by the repository's release/CD workflows and platform reconciliation. Do not manually apply deployment manifests to try the README. Authorized recovery operators -follow [the restore runbook](deploy/RESTORE-RUNBOOK.md); it is not contributor onboarding. +follow [the restore runbook](deploy/prod/RESTORE-RUNBOOK.md); it is not contributor onboarding. ## Security and reuse diff --git a/deploy/cluster.yaml b/deploy/base/cluster.yaml similarity index 84% rename from deploy/cluster.yaml rename to deploy/base/cluster.yaml index 2602383..9f86c02 100644 --- a/deploy/cluster.yaml +++ b/deploy/base/cluster.yaml @@ -57,12 +57,6 @@ spec: # AND runs CREATE EXTENSION IF NOT EXISTS in every database automatically. pg_stat_statements.max: "10000" pg_stat_statements.track: all - # Continuous backup through the tenant's dedicated ObjectStore, supplied by - # the platform together with its dedicated credential. The platform pins the - # production archive identity separately. No shared backup credential belongs - # in this tenant. Daily base backups are configured in db-scheduled-backup.yaml. - plugins: - - name: barman-cloud.cloudnative-pg.io - isWALArchiver: true - parameters: - barmanObjectName: wedding-db-dedicated + # Backups are production-only: deploy/prod/cluster-backup.yaml adds the + # Barman Cloud plugin, and deploy/prod/db-scheduled-backup.yaml the daily + # base backup. diff --git a/deploy/deployment.yaml b/deploy/base/deployment.yaml similarity index 100% rename from deploy/deployment.yaml rename to deploy/base/deployment.yaml diff --git a/deploy/base/kustomization.yaml b/deploy/base/kustomization.yaml new file mode 100644 index 0000000..f09c6e7 --- /dev/null +++ b/deploy/base/kustomization.yaml @@ -0,0 +1,14 @@ +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +# The app and its database that production (../prod) and dev (../dev) both +# run. Put a manifest here only when both environments run it; production-only +# objects and settings go in ../prod, and dev changes these with patches in +# ../dev. +resources: + - cluster.yaml + - deployment.yaml + - service.yaml + # Tenant-owned CiliumNetworkPolicy allow-list (layered on the platform's + # default-deny floor): Gateway ingress (3000), app⇄DB, CNPG + DNS egress. + # Production adds the R2 backup egress. + - networkpolicy.yaml diff --git a/deploy/networkpolicy.yaml b/deploy/base/networkpolicy.yaml similarity index 80% rename from deploy/networkpolicy.yaml rename to deploy/base/networkpolicy.yaml index d574ad3..1845053 100644 --- a/deploy/networkpolicy.yaml +++ b/deploy/base/networkpolicy.yaml @@ -54,23 +54,13 @@ spec: - toEndpoints: - matchLabels: k8s:io.kubernetes.pod.namespace: wedding-app - # CNPG → Cloudflare R2 (S3) for barman-cloud WAL archiving + base backups - # (the wedding-db CloudNativePG cluster). Pinned by FQDN instead of - # world:443 so a compromised pod cannot exfiltrate to arbitrary external - # services; the R2 pattern matches any account host without committing the - # account id. - - toFQDNs: - - matchPattern: "*.r2.cloudflarestorage.com" - toPorts: - - ports: - - port: "443" - protocol: TCP # Kube API (for CNPG operator managing the cluster) - toEntities: - kube-apiserver # DNS resolution. rules.dns engages Cilium's L7 DNS proxy so the toFQDNs - # allow-list above can be enforced; matchPattern "*" leaves resolution - # itself unrestricted (the FQDN list constrains where traffic may go). + # allow-list production adds for R2 (deploy/prod) can be enforced; + # matchPattern "*" leaves resolution itself unrestricted (the FQDN list + # constrains where traffic may go). - toEndpoints: - matchLabels: k8s:io.kubernetes.pod.namespace: kube-system diff --git a/deploy/service.yaml b/deploy/base/service.yaml similarity index 100% rename from deploy/service.yaml rename to deploy/base/service.yaml diff --git a/deploy/dev/kustomization.yaml b/deploy/dev/kustomization.yaml new file mode 100644 index 0000000..208435a --- /dev/null +++ b/deploy/dev/kustomization.yaml @@ -0,0 +1,68 @@ +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +# Dev: the shared app and database (../base), changed only by the settings and +# patches below. The platform reconciles it at `path: ./dev` into its own +# namespace and owns its ingress behind sign-on, so dev carries none of +# production's identity: no route, disruption budget, backup, or OpenBao path. +# Its database is a separate, empty CloudNativePG cluster in the dev namespace. +# Change base objects with patches here; never copy a manifest. +# +# Left to the dev deployment work (#467): the dev source of the +# `wedding-app-admin-code` Secret the Deployment reads (dev secrets stay +# separate from production's), seeded data, the mail sink and the dev bucket. +# +# The base hard-codes the production namespace on some objects; pin dev's own +# (the platform's Flux targetNamespace sets the same) so dev never renders +# into production's namespace. +namespace: wedding-app-dev +resources: + - ../base +# One app replica; production runs two. +replicas: + - name: wedding-app + count: 1 +patches: + # One database instance. A single instance cannot keep CNPG's own + # PodDisruptionBudget without deadlocking node drains, so turn it off. + - target: + group: postgresql.cnpg.io + kind: Cluster + name: wedding-db + patch: |- + - op: replace + path: /spec/instances + value: 1 + - op: add + path: /spec/enablePDB + value: false + # The base policy selects its own pods through the production namespace name, + # and names the cnpg-system, observability and kube-dns namespaces. A new + # tenant policy may name no namespace (the platform's + # restrict-tenant-network-policies), so dev selects its app and database + # pods by label instead and relies on the allow-cnpg-operator and allow-dns + # policies the platform generates in every tenant namespace. Dev's database + # is not scraped by Coroot. + - target: + kind: CiliumNetworkPolicy + name: app + patch: |- + - op: remove + path: /spec/ingress/3 + - op: remove + path: /spec/ingress/2 + - op: replace + path: /spec/ingress/1/fromEndpoints + value: + - matchLabels: + k8s:app.kubernetes.io/name: wedding-app + - matchLabels: + k8s:cnpg.io/cluster: wedding-db + - op: remove + path: /spec/egress/2 + - op: replace + path: /spec/egress/0/toEndpoints + value: + - matchLabels: + k8s:app.kubernetes.io/name: wedding-app + - matchLabels: + k8s:cnpg.io/cluster: wedding-db diff --git a/deploy/kustomization.yaml b/deploy/kustomization.yaml index df0e775..23d82ac 100644 --- a/deploy/kustomization.yaml +++ b/deploy/kustomization.yaml @@ -1,16 +1,9 @@ apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization +# Production entry point. The platform reconciles the published manifests +# artifact at `path: .`, which is this file, so it must only include ./prod. +# Shared manifests live in ./base, production-only ones in ./prod, and the dev +# overlay in ./dev (reconciled at `path: ./dev` into the `wedding-app-dev` +# namespace). resources: - # Namespaced SecretStore backing the admin-code secret below. - - secretstore.yaml - - admin-code-externalsecret.yaml - - admin-code-generator.yaml - - cluster.yaml - - db-scheduled-backup.yaml - - deployment.yaml - - httproute.yaml - - pod-disruption-budget.yaml - - service.yaml - # Tenant-owned CiliumNetworkPolicy allow-list (layered on the platform's - # default-deny floor): Gateway ingress (3000), app⇄DB, CNPG, R2 + DNS egress. - - networkpolicy.yaml + - prod diff --git a/deploy/RESTORE-RUNBOOK.md b/deploy/prod/RESTORE-RUNBOOK.md similarity index 82% rename from deploy/RESTORE-RUNBOOK.md rename to deploy/prod/RESTORE-RUNBOOK.md index c89c5d0..38e7a0b 100644 --- a/deploy/RESTORE-RUNBOOK.md +++ b/deploy/prod/RESTORE-RUNBOOK.md @@ -1,9 +1,12 @@ # Wedding database backup and restore The database archives WAL and daily base backups through the Barman Cloud -Plugin to the dedicated backup destination. The tenant Cluster references -`wedding-db-dedicated`; the platform supplies that ObjectStore and its dedicated -credential. The platform also preserves the production archive identity. +Plugin to the dedicated backup destination. The production Cluster references +`wedding-db-dedicated` through [`cluster-backup.yaml`](cluster-backup.yaml), a +patch on the shared [`../base/cluster.yaml`](../base/cluster.yaml) that only +production applies; the dev database has no backup. The platform supplies that +ObjectStore and its dedicated credential, and preserves the production archive +identity. The shared platform backup credential must not be projected into this tenant. ## Verify a restore diff --git a/deploy/admin-code-externalsecret.yaml b/deploy/prod/admin-code-externalsecret.yaml similarity index 100% rename from deploy/admin-code-externalsecret.yaml rename to deploy/prod/admin-code-externalsecret.yaml diff --git a/deploy/admin-code-generator.yaml b/deploy/prod/admin-code-generator.yaml similarity index 100% rename from deploy/admin-code-generator.yaml rename to deploy/prod/admin-code-generator.yaml diff --git a/deploy/prod/cluster-backup.yaml b/deploy/prod/cluster-backup.yaml new file mode 100644 index 0000000..1f3bc31 --- /dev/null +++ b/deploy/prod/cluster-backup.yaml @@ -0,0 +1,18 @@ +# Production-only: adds continuous backup to the shared wedding-db Cluster +# (../base/cluster.yaml). Dev runs its own database and never writes to the +# production archive. +# +# Continuous backup through the tenant's dedicated ObjectStore, supplied by +# the platform together with its dedicated credential. The platform pins the +# production archive identity separately. No shared backup credential belongs +# in this tenant. Daily base backups are configured in db-scheduled-backup.yaml. +apiVersion: postgresql.cnpg.io/v1 +kind: Cluster +metadata: + name: wedding-db +spec: + plugins: + - name: barman-cloud.cloudnative-pg.io + isWALArchiver: true + parameters: + barmanObjectName: wedding-db-dedicated diff --git a/deploy/db-scheduled-backup.yaml b/deploy/prod/db-scheduled-backup.yaml similarity index 100% rename from deploy/db-scheduled-backup.yaml rename to deploy/prod/db-scheduled-backup.yaml diff --git a/deploy/httproute.yaml b/deploy/prod/httproute.yaml similarity index 100% rename from deploy/httproute.yaml rename to deploy/prod/httproute.yaml diff --git a/deploy/prod/kustomization.yaml b/deploy/prod/kustomization.yaml new file mode 100644 index 0000000..3962f96 --- /dev/null +++ b/deploy/prod/kustomization.yaml @@ -0,0 +1,36 @@ +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +# Production: the shared app and database (../base) plus everything that +# carries the production identity — the public route, the disruption budget, +# the admin code in OpenBao, and the database backups. The platform reconciles +# this through ../kustomization.yaml. RESTORE-RUNBOOK.md documents the backups. +resources: + - ../base + # Namespaced SecretStore backing the admin-code secret below. + - secretstore.yaml + - admin-code-externalsecret.yaml + - admin-code-generator.yaml + - db-scheduled-backup.yaml + - httproute.yaml + - pod-disruption-budget.yaml +patches: + # Continuous WAL archiving + base backups to the dedicated ObjectStore. + - path: cluster-backup.yaml + # CNPG → Cloudflare R2 (S3) for barman-cloud WAL archiving + base backups + # (the wedding-db CloudNativePG cluster). Pinned by FQDN instead of + # world:443 so a compromised pod cannot exfiltrate to arbitrary external + # services; the R2 pattern matches any account host without committing the + # account id. + - target: + kind: CiliumNetworkPolicy + name: app + patch: |- + - op: add + path: /spec/egress/1 + value: + toFQDNs: + - matchPattern: "*.r2.cloudflarestorage.com" + toPorts: + - ports: + - port: "443" + protocol: TCP diff --git a/deploy/pod-disruption-budget.yaml b/deploy/prod/pod-disruption-budget.yaml similarity index 100% rename from deploy/pod-disruption-budget.yaml rename to deploy/prod/pod-disruption-budget.yaml diff --git a/deploy/secretstore.yaml b/deploy/prod/secretstore.yaml similarity index 100% rename from deploy/secretstore.yaml rename to deploy/prod/secretstore.yaml diff --git a/scripts/deploy-overlays.test.sh b/scripts/deploy-overlays.test.sh new file mode 100755 index 0000000..eef4c39 --- /dev/null +++ b/scripts/deploy-overlays.test.sh @@ -0,0 +1,61 @@ +#!/bin/sh +# Render deploy/ (the production entry), deploy/prod and deploy/dev, and keep +# production's identity out of dev. Needs kubectl (with its built-in Kustomize) +# and yq, both present on the runner. +set -eu + +repo_root=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd) +work=$(mktemp -d) +trap 'rm -rf "$work"' EXIT + +fail() { + printf 'FAIL: %s\n' "$*" >&2 + exit 1 +} + +kubectl kustomize "$repo_root/deploy" > "$work/root.yaml" || fail "deploy/ does not render" +kubectl kustomize "$repo_root/deploy/prod" > "$work/prod.yaml" || fail "deploy/prod does not render" +kubectl kustomize "$repo_root/deploy/dev" > "$work/dev.yaml" || fail "deploy/dev does not render" + +# The platform reconciles deploy/ for production, so it must be exactly deploy/prod. +cmp -s "$work/root.yaml" "$work/prod.yaml" || + fail "deploy/kustomization.yaml must include only ./prod" + +count_kind() { + KIND=$1 yq eval-all '[.] | map(select(.kind == strenv(KIND))) | length' "$work/dev.yaml" +} +dev_field() { + KIND=$1 yq eval-all "select(.kind == strenv(KIND)) | $2" "$work/dev.yaml" +} + +# Dev runs one app replica on one database instance of its own, without backup. +[ "$(count_kind Deployment)" -eq 1 ] || fail "deploy/dev must render only the app Deployment" +[ "$(dev_field Deployment .metadata.name)" = wedding-app ] || fail "deploy/dev must render the wedding-app Deployment" +[ "$(dev_field Deployment .spec.replicas)" = 1 ] || fail "deploy/dev must run one app replica" +[ "$(count_kind Cluster)" -eq 1 ] || fail "deploy/dev must render one database Cluster" +[ "$(dev_field Cluster .spec.instances)" = 1 ] || fail "deploy/dev must run one database instance" +[ "$(dev_field Cluster .spec.enablePDB)" = false ] || fail "deploy/dev's single database instance must not get a CNPG PodDisruptionBudget" +[ "$(dev_field Cluster '.spec | has("plugins")')" = false ] || fail "deploy/dev's database must not archive to the production backup store" + +# Everything dev renders lives in the wedding-app-dev namespace, and nowhere else. +[ "$(yq eval-all '[.] | map(select(.metadata.namespace != "wedding-app-dev")) | length' "$work/dev.yaml")" -eq 0 ] || + fail "every deploy/dev resource must be in the wedding-app-dev namespace" + +# Production identity stays in deploy/prod: the route, the disruption budget, +# the backups and the admin code in OpenBao. +for kind in HTTPRoute Gateway Certificate Issuer PodDisruptionBudget ScheduledBackup Backup ObjectStore \ + SecretStore ExternalSecret PushSecret Password; do + [ "$(count_kind "$kind")" -eq 0 ] || fail "deploy/dev renders a $kind; it belongs in deploy/prod" +done +for needle in wedding.platform.devantler.tech apps/wedding-app/ wedding-db-dedicated r2.cloudflarestorage.com 'namespace: wedding-app$'; do + if grep -Eq -- "$needle" "$work/dev.yaml"; then + fail "deploy/dev references production ($needle)" + fi +done +# Dev's network policy is created fresh in its own namespace, where the +# platform refuses a tenant policy that names any namespace. +if grep -Fq 'io.kubernetes.pod.namespace' "$work/dev.yaml"; then + fail "deploy/dev network policy names a namespace; select pods by label instead" +fi + +printf '%s\n' 'PASS: deploy/ renders deploy/prod; deploy/dev runs one replica on its own database without production identity' diff --git a/scripts/homepage-group.test.sh b/scripts/homepage-group.test.sh index 0be8c67..677f699 100644 --- a/scripts/homepage-group.test.sh +++ b/scripts/homepage-group.test.sh @@ -3,7 +3,7 @@ set -eu repo_root=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd) -route="${repo_root}/deploy/httproute.yaml" +route="${repo_root}/deploy/prod/httproute.yaml" enabled=$(yq -r '.metadata.annotations."gethomepage.dev/enabled"' "${route}") group=$(yq -r '.metadata.annotations."gethomepage.dev/group"' "${route}") if [ "${enabled}" != true ] || [ "${group}" != Personal ]; then diff --git a/scripts/readonly-rootfs.test.sh b/scripts/readonly-rootfs.test.sh index d0e13ef..c6b1ce9 100755 --- a/scripts/readonly-rootfs.test.sh +++ b/scripts/readonly-rootfs.test.sh @@ -7,12 +7,12 @@ # path (/tmp) mounted, so a future dependency that needs scratch space fails here rather than by # crash-looping in production. # -# Usage: readonly-rootfs.test.sh [deployment.yaml] (default: deploy/deployment.yaml) +# Usage: readonly-rootfs.test.sh [deployment.yaml] (default: deploy/base/deployment.yaml) set -eu script_dir=$(CDPATH='' cd -P -- "$(dirname -- "$0")" && pwd) repo_root=$(dirname -- "$script_dir") -deployment=${1:-$repo_root/deploy/deployment.yaml} +deployment=${1:-$repo_root/deploy/base/deployment.yaml} fail() { echo "FAIL: $*" >&2