Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,17 @@ def build_velero_restore(
source_namespace: str,
target_namespace: str,
instance_id: str,
restore_id: str,
driver_name: str,
existing_resource_policy: str | None = None,
hooks: list[VeleroRestoreHook] | None = None,
target_instance_id: str | None = None,
) -> VeleroRestore:
"""Build a VeleroRestore resource for an Styrmin instance.

``restore_id`` is the id of the Restore record. The object carries it as the
label ``styrmin.io/restore-id`` (ADR-073).

``instance_id`` identifies the instance the backup was taken from. For a
**cross-target** restore (environment clone) that is the source instance,
which is not what the Restore CR should be attributed to — pass
Expand All @@ -107,6 +111,7 @@ def build_velero_restore(

labels = {
"styrmin.io/instance": target_instance_id or instance_id,
"styrmin.io/restore-id": restore_id,
"styrmin.io/driver": driver_name,
}
if target_instance_id is not None and target_instance_id != instance_id:
Expand Down
1 change: 1 addition & 0 deletions backend/styrmin_backend/workflows/templates/backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,7 @@ async def execute_restore_workflow(
source_namespace=source_namespace,
target_namespace=target_namespace,
instance_id=str(backup.instance_id),
restore_id=str(restore_uuid),
driver_name="styrmin",
existing_resource_policy="update",
hooks=restore_hooks,
Expand Down
26 changes: 22 additions & 4 deletions backend/tests/e2e/test_backup_restore.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,8 @@ def _helmrelease_settled(release: dict[str, Any]) -> bool:
)


async def read_velero_backup(kubeconfig_path: str, backup_name: str) -> dict:
"""Return the Velero Backup CRD named *backup_name* from the cluster."""
async def read_velero_object(kubeconfig_path: str, plural: str, name: str) -> dict:
"""Return the Velero object of kind *plural* named *name* from the cluster."""
from kubernetes_asyncio import client as kubeclient
from kubernetes_asyncio import config as k8s_config
from kubernetes_asyncio.client import CustomObjectsApi
Expand All @@ -424,13 +424,18 @@ async def read_velero_backup(kubeconfig_path: str, backup_name: str) -> dict:
group="velero.io",
version="v1",
namespace="styrmin",
plural="backups",
name=backup_name,
plural=plural,
name=name,
)
finally:
await api_client.close()


async def read_velero_backup(kubeconfig_path: str, backup_name: str) -> dict:
"""Return the Velero Backup CRD named *backup_name* from the cluster."""
return await read_velero_object(kubeconfig_path, "backups", backup_name)


async def assert_backup_size_matches_pod_volume_backups(
kubeconfig_path: str,
backup_name: str,
Expand Down Expand Up @@ -1148,6 +1153,19 @@ async def test_backup_and_restore_lifecycle(
# 4a. The three restore steps in order, each with a duration (spec 019 US6).
assert_completed_restore_steps(restore)

# 4b. The Velero Restore is named after the restore's id and carries
# that id as a label (ADR-073).
velero_restore = await read_velero_object(
kubeconfig_path, "restores", f"styrmin-{created_restore.id}"
)
velero_restore_labels = velero_restore["metadata"].get("labels") or {}
assert velero_restore_labels.get("styrmin.io/restore-id") == (
created_restore.id
), (
f"Velero Restore styrmin-{created_restore.id} should carry the label "
f"styrmin.io/restore-id={created_restore.id}, got {velero_restore_labels!r}"
)

# 5. Wait for pods to recover, verify data restored
await wait_for_pods_ready(kubeconfig_path, namespace)

Expand Down
22 changes: 22 additions & 0 deletions backend/tests/unit/agent/test_backup_builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def test_basic_restore_same_namespace(self) -> None:
source_namespace="app-ns",
target_namespace="app-ns",
instance_id="inst-123",
restore_id="restore-789",
driver_name="mydriver",
)
rendered = restore.render()
Expand All @@ -105,6 +106,7 @@ def test_restore_different_namespace(self) -> None:
source_namespace="ns-a",
target_namespace="ns-b",
instance_id="inst-123",
restore_id="restore-789",
driver_name="mydriver",
)
rendered = restore.render()
Expand All @@ -118,6 +120,7 @@ def test_restore_with_existing_resource_policy(self) -> None:
source_namespace="app-ns",
target_namespace="app-ns",
instance_id="inst-123",
restore_id="restore-789",
driver_name="mydriver",
existing_resource_policy="update",
)
Expand All @@ -132,11 +135,14 @@ def test_restore_labels(self) -> None:
source_namespace="app-ns",
target_namespace="app-ns",
instance_id="inst-123",
restore_id="restore-789",
driver_name="mydriver",
)
rendered = restore.render()
assert rendered["metadata"]["labels"]["styrmin.io/instance"] == "inst-123"
assert rendered["metadata"]["labels"]["styrmin.io/driver"] == "mydriver"
# ADR-073: the object carries the id of its Restore record.
assert rendered["metadata"]["labels"]["styrmin.io/restore-id"] == "restore-789"

def test_restore_with_hooks(self) -> None:
from styrmin_backend.agent.service.kubernetes.velero.resources import (
Expand All @@ -162,6 +168,7 @@ def test_restore_with_hooks(self) -> None:
source_namespace="app-ns",
target_namespace="app-ns",
instance_id="inst-123",
restore_id="restore-789",
driver_name="mydriver",
hooks=[hook],
)
Expand All @@ -178,6 +185,7 @@ def test_restore_without_hooks(self) -> None:
source_namespace="app-ns",
target_namespace="app-ns",
instance_id="inst-123",
restore_id="restore-789",
driver_name="mydriver",
)
rendered = restore.render()
Expand All @@ -197,6 +205,7 @@ def test_cross_target_restore_labels_the_counterpart_instance(self) -> None:
source_namespace="styrmin-source",
target_namespace="styrmin-counterpart",
instance_id="source-inst",
restore_id="restore-789",
driver_name="mydriver",
target_instance_id="counterpart-inst",
)
Expand All @@ -206,6 +215,7 @@ def test_cross_target_restore_labels_the_counterpart_instance(self) -> None:
# Provenance is preserved rather than dropped.
assert labels["styrmin.io/source-instance"] == "source-inst"
assert labels["styrmin.io/driver"] == "mydriver"
assert labels["styrmin.io/restore-id"] == "restore-789"
assert rendered["spec"]["namespaceMapping"] == {
"styrmin-source": "styrmin-counterpart"
}
Expand All @@ -221,11 +231,13 @@ def test_in_place_restore_labels_are_unchanged_without_target_instance(
source_namespace="app-ns",
target_namespace="app-ns",
instance_id="inst-123",
restore_id="restore-789",
driver_name="mydriver",
)
rendered = restore.render()
assert rendered["metadata"]["labels"] == {
"styrmin.io/instance": "inst-123",
"styrmin.io/restore-id": "restore-789",
"styrmin.io/driver": "mydriver",
}

Expand All @@ -238,12 +250,14 @@ def test_target_instance_equal_to_source_adds_no_provenance_label(self) -> None:
source_namespace="app-ns",
target_namespace="app-ns",
instance_id="inst-123",
restore_id="restore-789",
driver_name="mydriver",
target_instance_id="inst-123",
)
rendered = restore.render()
assert rendered["metadata"]["labels"] == {
"styrmin.io/instance": "inst-123",
"styrmin.io/restore-id": "restore-789",
"styrmin.io/driver": "mydriver",
}

Expand All @@ -256,6 +270,7 @@ def test_cross_target_restore_leaves_out_the_helmrelease(self) -> None:
source_namespace="styrmin-source",
target_namespace="styrmin-counterpart",
instance_id="source-inst",
restore_id="restore-789",
driver_name="mydriver",
)
assert restore.render()["spec"]["excludedResources"] == [
Expand All @@ -272,6 +287,7 @@ def test_in_place_restore_leaves_out_only_cilium_endpoints(self) -> None:
source_namespace="app-ns",
target_namespace="app-ns",
instance_id="inst-123",
restore_id="restore-789",
driver_name="mydriver",
)
assert restore.render()["spec"]["excludedResources"] == [
Expand All @@ -287,6 +303,7 @@ def test_restores_do_not_share_the_excluded_list(self) -> None:
source_namespace="styrmin-source",
target_namespace="styrmin-counterpart",
instance_id="source-inst",
restore_id="restore-789",
driver_name="mydriver",
)
restore = build_velero_restore(
Expand All @@ -296,6 +313,7 @@ def test_restores_do_not_share_the_excluded_list(self) -> None:
source_namespace="app-ns",
target_namespace="app-ns",
instance_id="inst-123",
restore_id="restore-789",
driver_name="mydriver",
)
assert restore.render()["spec"]["excludedResources"] == [
Expand All @@ -311,6 +329,7 @@ def test_cross_target_restore_leaves_out_the_service_aliases(self) -> None:
source_namespace="styrmin-source",
target_namespace="styrmin-counterpart",
instance_id="source-inst",
restore_id="restore-789",
driver_name="mydriver",
)
assert restore.render()["spec"]["labelSelector"] == {
Expand All @@ -332,6 +351,7 @@ def test_in_place_restore_has_no_label_selector(self) -> None:
source_namespace="app-ns",
target_namespace="app-ns",
instance_id="inst-123",
restore_id="restore-789",
driver_name="mydriver",
)
assert "labelSelector" not in restore.render()["spec"]
Expand All @@ -345,6 +365,7 @@ def test_restores_do_not_share_the_label_selector(self) -> None:
source_namespace="styrmin-source",
target_namespace="styrmin-counterpart",
instance_id="source-inst",
restore_id="restore-789",
driver_name="mydriver",
)
assert first.label_selector is not None
Expand All @@ -356,6 +377,7 @@ def test_restores_do_not_share_the_label_selector(self) -> None:
source_namespace="styrmin-source",
target_namespace="styrmin-counterpart",
instance_id="source-inst",
restore_id="restore-789",
driver_name="mydriver",
)
assert second.render()["spec"]["labelSelector"]["matchExpressions"][0][
Expand Down
12 changes: 12 additions & 0 deletions backend/tests/unit/workflows/test_restore_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,18 @@ async def test_restore_workflow_restores_the_velero_backup_named_after_the_id(
)


async def test_restore_workflow_labels_the_velero_restore_with_the_restore_id(
events: list[Event],
) -> None:
"""The Velero ``Restore`` carries the id of its Restore record (ADR-073)."""
restore_id = uuid4()

async with _internal_restore_flow(events) as flow:
await _run_restore_workflow(restore_id)

assert flow.build_restore.call_args.kwargs["restore_id"] == str(restore_id)


async def test_restore_workflow_restores_a_legacy_backup_by_its_stored_name(
events: list[Event],
) -> None:
Expand Down
1 change: 1 addition & 0 deletions changelog/+velero-restore-id-label.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added the label `styrmin.io/restore-id` to the Velero `Restore` object of each new restore. The label holds the id of the Styrmin restore, so the object can be selected by that id.
2 changes: 1 addition & 1 deletion dev/knowledge/backend/backup-restore.md
Original file line number Diff line number Diff line change
Expand Up @@ -1724,7 +1724,7 @@ Backups and restores track status at the component level. The overall status is
### Builders (`agent/service/kubernetes/resources/builders/backup.py`)

- `build_velero_backup()` — Creates Velero Backup CRD spec with labels, hooks, and TTL. `ttl` is a **required** parameter (as it is on the `VeleroBackup` dataclass in `agent/service/kubernetes/velero/resources.py`): neither layer carries a fallback literal, so `services/backup/ttl.py` stays the single home of the default
- `build_velero_restore()` — Creates Velero Restore CRD spec with namespace mapping and hooks. When the source and target namespaces differ (a cross-target restore), it:
- `build_velero_restore()` — Creates Velero Restore CRD spec with namespace mapping and hooks. Every Restore carries the label `styrmin.io/restore-id` with the id of its Restore record, which `execute_restore_workflow` passes as `restore_id` ([ADR-073](../../adr/0073-kubernetes-objects-named-and-found-by-record-id.md)). When the source and target namespaces differ (a cross-target restore), it:
- sets `namespaceMapping` from the source namespace to the target namespace;
- adds `CROSS_TARGET_EXCLUDED_RESOURCES` (the Flux HelmRelease) to the excluded resources;
- sets `labelSelector` to `CROSS_TARGET_LABEL_SELECTOR`, which leaves out the service aliases (`styrmin.io/service-alias` `NotIn` `["true"]`), because they point to the source's namespaces ([#530](https://github.com/opsmill/styrmin/issues/530));
Expand Down
4 changes: 4 additions & 0 deletions docs/docs/concepts-and-architecture/backups-and-restores.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,10 @@ A restore runs the same steps in reverse order:
2. Styrmin adds the restore steps of each component's strategy. For a
PostgreSQL component, these steps load the dump into the database.
3. The agent creates a Velero `Restore` object with these restore hooks.
The object is named `styrmin-<restore id>` and has the label
`styrmin.io/restore-id` with the restore's id, so you can find it
with `kubectl get restores.velero.io -n styrmin -l
styrmin.io/restore-id=<restore id>`.
4. Velero reads the data from the BSL and writes it to the restored
volumes.
5. The restore hooks run, and the deployment serves the restored data.
Expand Down
Loading