Skip to content

Commit

Permalink
feat: Create persistent volume if it does not yet exist
Browse files Browse the repository at this point in the history
  • Loading branch information
amolenaar committed Sep 28, 2023
1 parent d311842 commit 0b95207
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
50 changes: 49 additions & 1 deletion backend/capellacollab/cli/ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,20 @@ def backup(volume_name: str, namespace: str = None, out: Path = None):


@app.command()
def restore(volume_name: str, tarfile: Path, namespace: str = None):
def restore(
volume_name: str,
tarfile: Path,
namespace: str = None,
access_modes: str = "ReadWriteMany",
storage_class_name: str = "persistent-sessions-csi",
):
config.load_kube_config()
v1 = client.CoreV1Api()

create_persistent_volume(
volume_name, namespace, access_modes, storage_class_name, v1
)

with pod_for_volume(
volume_name, namespace, MOUNT_PATH, v1, read_only=False
) as pod_name:
Expand Down Expand Up @@ -128,6 +138,44 @@ def pod_for_volume(
v1.delete_namespaced_pod(name, namespace)


def create_persistent_volume(
name: str, namespace: str, access_modes: str, storage_class_name: str, v1
):
"""Rebuild a PVC, according to the config defined in
`capellacollab/sessions/hooks/persistent_workspace.py`.
"""

prefix = "persistent-session-"
username = name[len(prefix) :] if name.startswith(prefix) else name

pvc = client.V1PersistentVolumeClaim(
kind="PersistentVolumeClaim",
api_version="v1",
metadata=client.V1ObjectMeta(
name=name,
labels={
"capellacollab/username": username,
},
),
spec=client.V1PersistentVolumeClaimSpec(
access_modes=[access_modes],
storage_class_name=storage_class_name,
resources=client.V1ResourceRequirements(
requests={"storage": "20Gi"}
),
),
)

try:
v1.create_namespaced_persistent_volume_claim(namespace, pvc)
except client.exceptions.ApiException as e:

Check warning on line 171 in backend/capellacollab/cli/ws.py

View check run for this annotation

Codecov / codecov/patch

backend/capellacollab/cli/ws.py#L171

Added line #L171 was not covered by tests
# Persistent volume already exists
if e.status == 409:
print(f"Using existing volume {name}")
return
raise

Check warning on line 176 in backend/capellacollab/cli/ws.py

View check run for this annotation

Codecov / codecov/patch

backend/capellacollab/cli/ws.py#L174-L176

Added lines #L174 - L176 were not covered by tests


def get_current_namespace():
try:
_, active_context = config.list_kube_config_contexts()
Expand Down
4 changes: 4 additions & 0 deletions backend/tests/cli/test_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def mock_core_v1_api(monkeypatch):
status=kubernetes.client.V1PodStatus(phase="Running")
),
)
monkeypatch.setattr(
"kubernetes.client.CoreV1Api.create_namespaced_persistent_volume_claim",
lambda self, ns, vpc: None,
)


def test_workspace_volumes(monkeypatch, capsys):
Expand Down

0 comments on commit 0b95207

Please sign in to comment.