1- from kubernetes import client , config
1+ from lifeguard_k8s . infrastructure import get_client
22from lifeguard_k8s .settings import (
3- LIFEGUARD_KUBERNETES_CONFIG ,
43 LIFEGUARD_KUBERNETES_READ_LOG_MAX_SIZE ,
54)
65
@@ -24,46 +23,10 @@ def _exists_success_pod_after_job(job_pod, pods):
2423 return False
2524
2625
27- def _get_clients ():
28- if LIFEGUARD_KUBERNETES_CONFIG :
29- config .load_kube_config (LIFEGUARD_KUBERNETES_CONFIG )
30- else :
31- config .load_incluster_config ()
32-
33- return client .CoreV1Api ()
34-
35-
36- def get_namespace_infos (namespace ):
37- """
38- Return current main infos of a namespace
39- """
40- infos = {"pods" : []}
41-
42- v1 = _get_clients ()
43- pods = v1 .list_namespaced_pod (namespace )
44-
45- for pod in pods .items :
46- infos ["pods" ].append (
47- {
48- "name" : pod .metadata .name ,
49- "status" : pod .status .phase ,
50- "containers" : [
51- {
52- "name" : container .name ,
53- "ready" : container .ready ,
54- "restart_count" : container .restart_count ,
55- }
56- for container in pod .status .container_statuses
57- ],
58- }
59- )
60- return infos
61-
62-
6326def get_not_running_pods (namespace ):
6427 not_running_pods = []
6528
66- v1 = _get_clients ()
29+ v1 = get_client ()
6730 pods = v1 .list_namespaced_pod (namespace )
6831
6932 for pod in pods .items :
@@ -80,12 +43,12 @@ def get_not_running_pods(namespace):
8043
8144
8245def delete_a_pod (namespace , pod_name ):
83- v1 = _get_clients ()
46+ v1 = get_client ()
8447 v1 .delete_namespaced_pod (pod_name , namespace )
8548
8649
8750def get_events_from_pod (namespace , pod_name ):
88- v1 = _get_clients ()
51+ v1 = get_client ()
8952 events = v1 .list_namespaced_event (
9053 namespace , field_selector = f"involvedObject.name={ pod_name } "
9154 )
@@ -109,6 +72,6 @@ def get_last_error_event_from_pod(namespace, pod_name):
10972
11073
11174def get_logs_from_pod (namespace , pod_name ):
112- v1 = _get_clients ()
75+ v1 = get_client ()
11376 log = v1 .read_namespaced_pod_log (pod_name , namespace )
11477 return log [- LIFEGUARD_KUBERNETES_READ_LOG_MAX_SIZE :]
0 commit comments