33import sys
44import tempfile
55from pathlib import Path
6+ # from typing_extensions import AnyStr
67
78import yaml
89from kubernetes import client , config , watch
9- from kubernetes .client .models import CoreV1Event , V1PodList
10+ from kubernetes .client .models import CoreV1Event , V1PodList , V1NamespaceList , V1Pod
11+ from kubernetes .client .rest import ApiException
1012from kubernetes .dynamic import DynamicClient
1113from kubernetes .stream import stream
1214
1618 INGRESS_NAMESPACE ,
1719 KUBECONFIG ,
1820 LOGGING_NAMESPACE ,
21+ WARNET_ASSETS ,
22+ WARNET_NAMESPACE_ANNOTATION ,
1923)
2024from .process import run_command , stream_command
2125
@@ -31,20 +35,44 @@ def get_dynamic_client() -> DynamicClient:
3135
3236
3337def get_pods () -> V1PodList :
38+ sclient = get_static_client ()
39+ pods = []
40+ namespaces : V1NamespaceList = get_namespaces_by_warnet_type (WARNET_ASSETS )
41+ for ns in namespaces .items :
42+ try :
43+ pods .append (sclient .list_namespaced_pod (ns .metadata .name ))
44+ except Exception as e :
45+ raise e
46+ return pods
47+
48+
49+ def get_pod (pod_name : str , namespace : str ) -> V1Pod :
3450 sclient = get_static_client ()
3551 try :
36- pod_list : V1PodList = sclient .list_namespaced_pod (get_default_namespace ())
37- except Exception as e :
38- raise e
39- return pod_list
52+ pod = sclient .read_namespaced_pod (name = pod_name , namespace = namespace )
53+ return pod
54+ except ApiException as e :
55+ if e .status == 404 :
56+ print (f"Pod { pod_name } not found in namespace { namespace } " )
57+ else :
58+ print (f"Exception when calling CoreV1Api->read_namespaced_pod: { e } " )
59+ return None
60+
61+
62+ def get_pod_namespace (pod : client .V1Pod ) -> str :
63+ annotations = pod .metadata .annotations
64+ if annotations :
65+ return annotations .get (WARNET_NAMESPACE_ANNOTATION )
66+ return DEFAULT_NAMESPACE
4067
4168
4269def get_mission (mission : str ) -> list [V1PodList ]:
4370 pods = get_pods ()
4471 crew = []
45- for pod in pods .items :
46- if "mission" in pod .metadata .labels and pod .metadata .labels ["mission" ] == mission :
47- crew .append (pod )
72+ for pod_list in pods :
73+ for pod in pod_list .items :
74+ if "mission" in pod .metadata .labels and pod .metadata .labels ["mission" ] == mission :
75+ crew .append (pod )
4876 return crew
4977
5078
@@ -135,6 +163,12 @@ def get_default_namespace() -> str:
135163 return kubectl_namespace if kubectl_namespace else DEFAULT_NAMESPACE
136164
137165
166+ def get_namespaces_by_warnet_type (warnet_type : str ) -> list [V1NamespaceList ]:
167+ sclient = get_static_client ()
168+ namespaces = sclient .list_namespace (label_selector = f"type={ warnet_type } " )
169+ return namespaces
170+
171+
138172def snapshot_bitcoin_datadir (
139173 pod_name : str , chain : str , local_path : str = "./" , filters : list [str ] = None
140174) -> None :
0 commit comments