fix: preserve Longhorn source during VPS restore
Build and publish Docker image / build-and-publish (push) Successful in 10s
Build and publish Docker image / build-and-publish (push) Successful in 10s
This commit is contained in:
1 parent
f1c4c5f29a
commit
161a895802
2 files changed
+190
-5
No files matched your search
+83
-5
@@ -2784,17 +2784,29 @@ async def _wait_for_data_volume_ready(data_volume_name, namespace, timeout_secon
|
||||
raise TimeoutError(f"DataVolume {data_volume_name} did not finish restoring in time")
|
||||
|
||||
|
||||
def _build_vps_restore_data_volume(server_id, namespace, backup_id, pvc_spec):
|
||||
def _build_vps_restore_data_volume(
|
||||
server_id, namespace, backup_id, pvc_spec, data_volume_name=None, source_pvc_name=None
|
||||
):
|
||||
source = (
|
||||
{'pvc': {'name': source_pvc_name, 'namespace': namespace}}
|
||||
if source_pvc_name
|
||||
else {'snapshot': {'name': backup_id, 'namespace': namespace}}
|
||||
)
|
||||
return {
|
||||
'apiVersion': 'cdi.kubevirt.io/v1beta1',
|
||||
'kind': 'DataVolume',
|
||||
'metadata': {
|
||||
'name': f'{server_id}-rootdisk',
|
||||
'name': data_volume_name or f'{server_id}-rootdisk',
|
||||
'namespace': namespace,
|
||||
'labels': {'app': server_id, 'managed-by': 'anchorhost', 'server-type': 'vps'},
|
||||
'labels': {
|
||||
'app': server_id,
|
||||
'managed-by': 'anchorhost',
|
||||
'server-type': 'vps',
|
||||
'backup-id': backup_id,
|
||||
},
|
||||
},
|
||||
'spec': {
|
||||
'source': {'snapshot': {'name': backup_id, 'namespace': namespace}},
|
||||
'source': source,
|
||||
'pvc': pvc_spec,
|
||||
},
|
||||
}
|
||||
@@ -3093,6 +3105,54 @@ async def handle_restore_backup(client_id, data):
|
||||
|
||||
await notify_progress('Recreating volume from backup...')
|
||||
if old_data_volume:
|
||||
# Longhorn stores CSI snapshots on their source volume. CDI's clone
|
||||
# from a snapshot still needs that source volume while it materializes
|
||||
# the restored disk, so never delete the current disk first.
|
||||
staging_name = f'{server_id}-restore-{backup_id[:8]}'
|
||||
try:
|
||||
staging_volume = custom_objects_api.get_namespaced_custom_object(
|
||||
group='cdi.kubevirt.io', version='v1beta1', namespace=namespace,
|
||||
plural='datavolumes', name=staging_name,
|
||||
)
|
||||
labels = staging_volume.get('metadata', {}).get('labels', {})
|
||||
if labels.get('app') != server_id or labels.get('backup-id') != backup_id:
|
||||
raise Exception(f"A conflicting restore staging volume {staging_name} already exists")
|
||||
if staging_volume.get('status', {}).get('phase') == 'Failed':
|
||||
custom_objects_api.delete_namespaced_custom_object(
|
||||
group='cdi.kubevirt.io', version='v1beta1', namespace=namespace,
|
||||
plural='datavolumes', name=staging_name,
|
||||
)
|
||||
await _wait_for_data_volume_absent(staging_name, namespace)
|
||||
try:
|
||||
v1.delete_namespaced_persistent_volume_claim(staging_name, namespace)
|
||||
except ApiException as error:
|
||||
if error.status != 404:
|
||||
raise
|
||||
await _wait_for_pvc_absent(staging_name, namespace)
|
||||
staging_volume = None
|
||||
except ApiException as error:
|
||||
if error.status != 404:
|
||||
raise
|
||||
staging_volume = None
|
||||
|
||||
if not staging_volume:
|
||||
custom_objects_api.create_namespaced_custom_object(
|
||||
group='cdi.kubevirt.io', version='v1beta1', namespace=namespace,
|
||||
plural='datavolumes',
|
||||
body=_build_vps_restore_data_volume(
|
||||
server_id, namespace, backup_id, data_volume_pvc_spec,
|
||||
data_volume_name=staging_name,
|
||||
),
|
||||
)
|
||||
await _wait_for_data_volume_ready(staging_name, namespace)
|
||||
staging_pvc = await _wait_for_pvc_bound(staging_name, namespace)
|
||||
if not staging_pvc.spec.volume_name:
|
||||
raise Exception("Staged restore claim is bound without a Longhorn volume")
|
||||
await _wait_for_longhorn_restore(staging_pvc.spec.volume_name)
|
||||
|
||||
# The staging clone is independent now. Keep it as the CDI source
|
||||
# while replacing the canonical root disk so the backup snapshot's
|
||||
# source volume can be safely reclaimed.
|
||||
custom_objects_api.delete_namespaced_custom_object(
|
||||
group='cdi.kubevirt.io', version='v1beta1', namespace=namespace,
|
||||
plural='datavolumes', name=pvc_name,
|
||||
@@ -3108,7 +3168,10 @@ async def handle_restore_backup(client_id, data):
|
||||
custom_objects_api.create_namespaced_custom_object(
|
||||
group='cdi.kubevirt.io', version='v1beta1', namespace=namespace,
|
||||
plural='datavolumes',
|
||||
body=_build_vps_restore_data_volume(server_id, namespace, backup_id, data_volume_pvc_spec),
|
||||
body=_build_vps_restore_data_volume(
|
||||
server_id, namespace, backup_id, data_volume_pvc_spec,
|
||||
source_pvc_name=staging_name,
|
||||
),
|
||||
)
|
||||
await _wait_for_data_volume_ready(pvc_name, namespace)
|
||||
else:
|
||||
@@ -3134,6 +3197,21 @@ async def handle_restore_backup(client_id, data):
|
||||
await notify_progress('Restore finished. Starting server...')
|
||||
if not start_server(server_id, user_id):
|
||||
raise Exception("Files were restored, but the server could not be started. Start it from Overview.")
|
||||
if old_data_volume:
|
||||
try:
|
||||
custom_objects_api.delete_namespaced_custom_object(
|
||||
group='cdi.kubevirt.io', version='v1beta1', namespace=namespace,
|
||||
plural='datavolumes', name=staging_name,
|
||||
)
|
||||
await _wait_for_data_volume_absent(staging_name, namespace)
|
||||
try:
|
||||
v1.delete_namespaced_persistent_volume_claim(staging_name, namespace)
|
||||
except ApiException as error:
|
||||
if error.status != 404:
|
||||
raise
|
||||
await _wait_for_pvc_absent(staging_name, namespace)
|
||||
except Exception:
|
||||
logger.warning("Could not clean up restore staging claim %s", staging_name, exc_info=True)
|
||||
await notify_progress('Restoration completed successfully. Server is starting.')
|
||||
logger.info("Successfully restored backup %s for server %s", backup_id, server_id)
|
||||
except Exception as error:
|
||||
|
||||
Reference in new issue
Block a user