3
3
import sys
4
4
import tempfile
5
5
from pathlib import Path
6
+ # from typing_extensions import AnyStr
6
7
7
8
import yaml
8
9
from 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
10
12
from kubernetes .dynamic import DynamicClient
11
13
from kubernetes .stream import stream
12
14
16
18
INGRESS_NAMESPACE ,
17
19
KUBECONFIG ,
18
20
LOGGING_NAMESPACE ,
21
+ WARNET_ASSETS ,
22
+ WARNET_NAMESPACE_ANNOTATION ,
19
23
)
20
24
from .process import run_command , stream_command
21
25
@@ -31,20 +35,44 @@ def get_dynamic_client() -> DynamicClient:
31
35
32
36
33
37
def 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 :
34
50
sclient = get_static_client ()
35
51
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
40
67
41
68
42
69
def get_mission (mission : str ) -> list [V1PodList ]:
43
70
pods = get_pods ()
44
71
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 )
48
76
return crew
49
77
50
78
@@ -135,6 +163,12 @@ def get_default_namespace() -> str:
135
163
return kubectl_namespace if kubectl_namespace else DEFAULT_NAMESPACE
136
164
137
165
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
+
138
172
def snapshot_bitcoin_datadir (
139
173
pod_name : str , chain : str , local_path : str = "./" , filters : list [str ] = None
140
174
) -> None :
0 commit comments