Skip to content

Commit edbaa30

Browse files
committed
control: fetch and stream pod logs without kubectl
1 parent 917ee4a commit edbaa30

File tree

2 files changed

+42
-33
lines changed

2 files changed

+42
-33
lines changed

src/warnet/control.py

+27-33
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
get_mission,
2323
get_pods,
2424
snapshot_bitcoin_datadir,
25+
pod_log
2526
)
2627
from .process import run_command, stream_command
2728

@@ -238,46 +239,39 @@ def run(scenario_file: str, additional_args: tuple[str]):
238239
@click.option("--follow", "-f", is_flag=True, default=False, help="Follow logs")
239240
def logs(pod_name: str, follow: bool):
240241
"""Show the logs of a pod"""
241-
follow_flag = "--follow" if follow else ""
242242
namespace = get_default_namespace()
243243

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

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

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

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

282276

283277
@click.command()

src/warnet/k8s.py

+15
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 DEFAULT_NAMESPACE, KUBECONFIG
1415
from .process import run_command, stream_command
@@ -255,3 +256,17 @@ def wait_for_caddy_ready(name, namespace, timeout=300):
255256
return True
256257
print(f"Timeout waiting for Caddy pod {name} to be ready.")
257258
return False
259+
260+
261+
def pod_log(pod_name, container_name=None, follow=False):
262+
sclient = get_static_client()
263+
try:
264+
return sclient.read_namespaced_pod_log(
265+
name=pod_name,
266+
namespace=get_default_namespace(),
267+
container=container_name,
268+
follow=follow,
269+
_preload_content=False
270+
)
271+
except ApiException as e:
272+
raise Exception(json.loads(e.body.decode('utf-8'))["message"])

0 commit comments

Comments
 (0)