|
1 | 1 | import json
|
2 | 2 | import os
|
3 | 3 | import sys
|
| 4 | +import tarfile |
4 | 5 | import tempfile
|
5 | 6 | from pathlib import Path
|
6 | 7 | from time import sleep
|
@@ -60,6 +61,22 @@ def get_pod(name: str, namespace: Optional[str] = None) -> V1Pod:
|
60 | 61 | return sclient.read_namespaced_pod(name=name, namespace=namespace)
|
61 | 62 |
|
62 | 63 |
|
| 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 | + |
63 | 80 | def get_mission(mission: str) -> list[V1Pod]:
|
64 | 81 | pods = get_pods()
|
65 | 82 | crew: list[V1Pod] = []
|
@@ -547,17 +564,41 @@ def write_kubeconfig(kube_config: dict, kubeconfig_path: str) -> None:
|
547 | 564 | raise K8sError(f"Error writing kubeconfig: {kubeconfig_path}") from e
|
548 | 565 |
|
549 | 566 |
|
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 | + |
555 | 570 | v1 = get_static_client()
|
556 | 571 |
|
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