diff options
author | 2024-03-31 19:34:20 +0100 | |
---|---|---|
committer | 2024-03-31 19:34:20 +0100 | |
commit | faa275d9519d089d35e7222058e40f36e10de643 (patch) | |
tree | 0757034d1f4187f3f22a2a8c735bfc3c44ce68b6 | |
parent | Add new columns to pods command (diff) |
add new API functions for pod log fetching
-rw-r--r-- | arthur/apis/kubernetes/pods.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/arthur/apis/kubernetes/pods.py b/arthur/apis/kubernetes/pods.py index dfdc80c..926859c 100644 --- a/arthur/apis/kubernetes/pods.py +++ b/arthur/apis/kubernetes/pods.py @@ -10,3 +10,37 @@ async def list_pods(namespace: str) -> V1PodList: async with ApiClient() as api_client: api = client.CoreV1Api(api_client) return await api.list_namespaced_pod(namespace=namespace) + + +async def tail_pod(namespace: str, pod_name: str, lines: int = 10) -> str: + """Tail the logs of a pod in the provided namespace.""" + async with ApiClient() as api_client: + api = client.CoreV1Api(api_client) + return await api.read_namespaced_pod_log( + namespace=namespace, name=pod_name, tail_lines=lines + ) + + +async def get_pod_names_from_deployment(namespace: str, deployment_name: str) -> list[str]: + """Get the pods associated with the provided deployment name.""" + async with ApiClient() as api_client: + apps_api = client.AppsV1Api(api_client) + core_api = client.CoreV1Api(api_client) + deployment = await apps_api.read_namespaced_deployment( + namespace=namespace, name=deployment_name + ) + + if deployment.spec.selector is None: + return None + + pod = await core_api.list_namespaced_pod( + namespace=namespace, + label_selector=",".join([ + f"{k}={v}" for k, v in deployment.spec.selector.match_labels.items() + ]), + ) + + if not pod.items: + return None + + return [p.metadata.name for p in pod.items] |