Skip to content

Commit 3695546

Browse files
committed
k8s: make download fn; move get_pods_with_label
1 parent 05b864c commit 3695546

File tree

1 file changed

+53
-12
lines changed

1 file changed

+53
-12
lines changed

src/warnet/k8s.py

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
import os
33
import sys
4+
import tarfile
45
import tempfile
56
from pathlib import Path
67
from time import sleep
@@ -60,6 +61,22 @@ def get_pod(name: str, namespace: Optional[str] = None) -> V1Pod:
6061
return sclient.read_namespaced_pod(name=name, namespace=namespace)
6162

6263

64+
def get_pods_with_label(label_selector: str, namespace: Optional[str] = None) -> list[V1Pod]:
65+
"""Get a list of pods by label.
66+
Label example: "mission=lightning"
67+
"""
68+
namespace = get_default_namespace_or(namespace)
69+
v1 = get_static_client()
70+
71+
try:
72+
pods = v1.list_namespaced_pod(namespace=namespace, label_selector=label_selector)
73+
v1_pods = [pod for pod in pods.items]
74+
return v1_pods
75+
except client.exceptions.ApiException as e:
76+
print(f"Error fetching pods: {e}")
77+
return []
78+
79+
6380
def get_mission(mission: str) -> list[V1Pod]:
6481
pods = get_pods()
6582
crew: list[V1Pod] = []
@@ -547,17 +564,41 @@ def write_kubeconfig(kube_config: dict, kubeconfig_path: str) -> None:
547564
raise K8sError(f"Error writing kubeconfig: {kubeconfig_path}") from e
548565

549566

550-
def get_pods_with_label(label_selector: str, namespace: Optional[str] = None) -> list[V1Pod]:
551-
"""Get a list of pods by label.
552-
Label example: "mission=lightning"
553-
"""
554-
namespace = get_default_namespace_or(namespace)
567+
def download(pod_name: str, namespace: str, source_path: Path, destination_path: Path = Path(".")):
568+
"""Download the item from the `source_path` to the `destination_path`"""
569+
555570
v1 = get_static_client()
556571

557-
try:
558-
pods = v1.list_namespaced_pod(namespace=namespace, label_selector=label_selector)
559-
v1_pods = [pod for pod in pods.items]
560-
return v1_pods
561-
except client.exceptions.ApiException as e:
562-
print(f"Error fetching pods: {e}")
563-
return []
572+
os.makedirs(destination_path, exist_ok=True)
573+
target_folder = destination_path / source_path.stem
574+
os.makedirs(target_folder, exist_ok=True)
575+
576+
command = ["tar", "cf", "-", str(source_path)]
577+
578+
resp = stream(
579+
v1.connect_get_namespaced_pod_exec,
580+
name=pod_name,
581+
namespace=namespace,
582+
command=command,
583+
stderr=True,
584+
stdin=False,
585+
stdout=True,
586+
tty=False,
587+
_preload_content=False,
588+
)
589+
590+
tar_file = target_folder.with_suffix(".tar")
591+
with open(tar_file, "wb") as f:
592+
while resp.is_open():
593+
resp.update(timeout=1)
594+
if resp.peek_stdout():
595+
f.write(resp.read_stdout().encode("utf-8"))
596+
if resp.peek_stderr():
597+
print(resp.read_stderr())
598+
599+
resp.close()
600+
601+
with tarfile.open(tar_file, "r") as tar:
602+
tar.extractall(path=target_folder)
603+
604+
os.remove(tar_file)

0 commit comments

Comments
 (0)