Skip to content

Commit c69f222

Browse files
committed
control: fetch and stream pod logs without kubectl
1 parent c23b5ac commit c69f222

File tree

2 files changed

+42
-33
lines changed

2 files changed

+42
-33
lines changed

src/warnet/control.py

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
get_mission,
2222
get_pods,
2323
snapshot_bitcoin_datadir,
24+
pod_log
2425
)
2526
from .process import run_command, stream_command
2627

@@ -235,46 +236,39 @@ def run(scenario_file: str, additional_args: tuple[str]):
235236
@click.option("--follow", "-f", is_flag=True, default=False, help="Follow logs")
236237
def logs(pod_name: str, follow: bool):
237238
"""Show the logs of a pod"""
238-
follow_flag = "--follow" if follow else ""
239239
namespace = get_default_namespace()
240240

241-
if pod_name:
241+
if pod_name == "":
242242
try:
243-
command = f"kubectl logs pod/{pod_name} -n {namespace} {follow_flag}"
244-
stream_command(command)
245-
return
243+
pods = get_pods()
244+
pod_list = [item.metadata.name for item in pods.items]
246245
except Exception as e:
247-
print(f"Could not find the pod {pod_name}: {e}")
246+
print(f"Could not fetch any pods in namespace {namespace}: {e}")
247+
return
248248

249-
try:
250-
pods = run_command(f"kubectl get pods -n {namespace} -o json")
251-
pods = json.loads(pods)
252-
pod_list = [item["metadata"]["name"] for item in pods["items"]]
253-
except Exception as e:
254-
print(f"Could not fetch any pods in namespace {namespace}: {e}")
255-
return
249+
if not pod_list:
250+
print(f"Could not fetch any pods in namespace {namespace}")
251+
return
256252

257-
if not pod_list:
258-
print(f"Could not fetch any pods in namespace {namespace}")
259-
return
253+
q = [
254+
inquirer.List(
255+
name="pod",
256+
message="Please choose a pod",
257+
choices=pod_list,
258+
)
259+
]
260+
selected = inquirer.prompt(q, theme=GreenPassion())
261+
if selected:
262+
pod_name = selected["pod"]
263+
else:
264+
return # cancelled by user
260265

261-
q = [
262-
inquirer.List(
263-
name="pod",
264-
message="Please choose a pod",
265-
choices=pod_list,
266-
)
267-
]
268-
selected = inquirer.prompt(q, theme=GreenPassion())
269-
if selected:
270-
pod_name = selected["pod"]
271-
try:
272-
command = f"kubectl logs pod/{pod_name} -n {namespace} {follow_flag}"
273-
stream_command(command)
274-
except Exception as e:
275-
print(f"Please consider waiting for the pod to become available. Encountered: {e}")
276-
else:
277-
pass # cancelled by user
266+
try:
267+
stream = pod_log(pod_name, container_name=None, follow=follow)
268+
for line in stream.stream():
269+
print(line.decode('utf-8'), end=None)
270+
except Exception as e:
271+
print(e)
278272

279273

280274
@click.command()

src/warnet/k8s.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from kubernetes.client.models import CoreV1Event, V1PodList
1010
from kubernetes.dynamic import DynamicClient
1111
from kubernetes.stream import stream
12+
from kubernetes.client.rest import ApiException
1213

1314
from .constants import (
1415
CADDY_INGRESS_NAME,
@@ -282,3 +283,17 @@ def get_ingress_ip_or_host():
282283
except Exception as e:
283284
print(f"Error getting ingress IP: {e}")
284285
return None
286+
287+
288+
def pod_log(pod_name, container_name=None, follow=False):
289+
sclient = get_static_client()
290+
try:
291+
return sclient.read_namespaced_pod_log(
292+
name=pod_name,
293+
namespace=get_default_namespace(),
294+
container=container_name,
295+
follow=follow,
296+
_preload_content=False
297+
)
298+
except ApiException as e:
299+
raise Exception(json.loads(e.body.decode('utf-8'))["message"])

0 commit comments

Comments
 (0)