Skip to content

Commit 2733a95

Browse files
committed
feat: adding function to return namespace main infos
1 parent bf3b9f2 commit 2733a95

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
lines changed

lifeguard_k8s/infrastructure/pods.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,33 @@ def _get_clients():
3333
return client.CoreV1Api()
3434

3535

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+
3663
def get_not_running_pods(namespace):
3764
not_running_pods = []
3865

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
setup(
88
name="lifeguard-k8s",
9-
version="1.2.0",
9+
version="1.2.1",
1010
url="https://github.com/LifeguardSystem/lifeguard-k8s",
1111
author="Diego Rubin",
1212
author_email="[email protected]",

tests/infrastructure/test_pods.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77
get_last_error_event_from_pod,
88
get_logs_from_pod,
99
delete_a_pod,
10+
get_namespace_infos,
1011
)
1112

1213

1314
def build_pod(status, container_status, pod_name, kind="ReplicaSet"):
1415
container_statuses = MagicMock(name="container_statuses")
1516
container_statuses.ready = container_status
17+
container_statuses.restart_count = 0
18+
container_statuses.name = "container_name"
1619

1720
owner_reference = MagicMock(name="owner_reference")
1821
owner_reference.kind = kind
@@ -188,7 +191,7 @@ def test_call_load_kube_config_if_config_is_not_empty(
188191

189192
@patch("lifeguard_k8s.infrastructure.pods.config")
190193
@patch("lifeguard_k8s.infrastructure.pods.client")
191-
def test_get_logs_from_pod(self, mock_client, mock_config):
194+
def test_get_logs_from_pod(self, mock_client, _mock_config):
192195
mock_client.CoreV1Api.return_value.read_namespaced_pod_log.return_value = "log"
193196
self.assertEqual(get_logs_from_pod("namespace", "pod_name"), "log")
194197

@@ -197,18 +200,45 @@ def test_get_logs_from_pod(self, mock_client, mock_config):
197200
@patch(
198201
"lifeguard_k8s.infrastructure.pods.LIFEGUARD_KUBERNETES_READ_LOG_MAX_SIZE", 10
199202
)
200-
def test_get_logs_from_pod_with_limited_size(self, mock_client, mock_config):
203+
def test_get_logs_from_pod_with_limited_size(self, mock_client, _mock_config):
201204
mock_client.CoreV1Api.return_value.read_namespaced_pod_log.return_value = """
202205
a big log that will be limited
203206
send only last 10 characters"""
204207
self.assertEqual(get_logs_from_pod("namespace", "pod_name"), "characters")
205208

206209
@patch("lifeguard_k8s.infrastructure.pods.config")
207210
@patch("lifeguard_k8s.infrastructure.pods.client")
208-
def test_delete_a_pod(self, mock_client, mock_config):
211+
def test_delete_a_pod(self, mock_client, _mock_config):
209212
mock_client.CoreV1Api.return_value.delete_namespaced_pod.return_value = None
210213
delete_a_pod("namespace", "pod_name")
211214

212215
mock_client.CoreV1Api.return_value.delete_namespaced_pod.assert_called_with(
213216
"pod_name", "namespace"
214217
)
218+
219+
@patch("lifeguard_k8s.infrastructure.pods.config")
220+
@patch("lifeguard_k8s.infrastructure.pods.client")
221+
def test_get_namespace_infos(self, mock_client, _mock_config):
222+
pod = build_pod("Running", True, "pod_name")
223+
224+
mock_client.CoreV1Api.return_value.list_namespaced_pod.return_value.items = [
225+
pod
226+
]
227+
self.assertEqual(
228+
get_namespace_infos("namespace"),
229+
{
230+
"pods": [
231+
{
232+
"containers": [
233+
{
234+
"name": "container_name",
235+
"ready": True,
236+
"restart_count": 0,
237+
}
238+
],
239+
"name": "pod_name",
240+
"status": "Running",
241+
}
242+
]
243+
},
244+
)

0 commit comments

Comments
 (0)