Skip to content

Commit c4b2239

Browse files
authored
Merge pull request #497 from mplsgrant/2024-08-pod-logs
2 parents b0a78ef + 576f67f commit c4b2239

File tree

3 files changed

+145
-86
lines changed

3 files changed

+145
-86
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ dependencies = [
1414
"click==8.1.7",
1515
"docker==7.1.0",
1616
"flask==3.0.3",
17+
"inquirer==3.4.0",
1718
"kubernetes==30.1.0",
1819
"rich==13.7.1",
1920
"tabulate==0.9.0",

src/warnet/main.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import os
23
import random
34
import subprocess
@@ -6,7 +7,12 @@
67
from pathlib import Path
78

89
import click
10+
import inquirer
911
import yaml
12+
from inquirer.themes import GreenPassion
13+
14+
from warnet.k8s import get_default_namespace
15+
from warnet.process import run_command, stream_command
1016

1117
from .admin import admin
1218
from .bitcoin import bitcoin
@@ -225,6 +231,53 @@ def auth(kube_config: str) -> None:
225231
)
226232

227233

234+
@cli.command()
235+
@click.argument("pod_name", type=str, default="")
236+
@click.option("--follow", "-f", is_flag=True, default=False, help="Follow logs")
237+
def logs(pod_name: str, follow: bool):
238+
"""Show the logs of a pod"""
239+
follow_flag = "--follow" if follow else ""
240+
namespace = get_default_namespace()
241+
242+
if pod_name:
243+
try:
244+
command = f"kubectl logs pod/{pod_name} -n {namespace} {follow_flag}"
245+
stream_command(command)
246+
return
247+
except Exception as e:
248+
print(f"Could not find the pod {pod_name}: {e}")
249+
250+
try:
251+
pods = run_command(f"kubectl get pods -n {namespace} -o json")
252+
pods = json.loads(pods)
253+
pod_list = [item["metadata"]["name"] for item in pods["items"]]
254+
except Exception as e:
255+
print(f"Could not fetch any pods in namespace {namespace}: {e}")
256+
return
257+
258+
if not pod_list:
259+
print(f"Could not fetch any pods in namespace {namespace}")
260+
return
261+
262+
q = [
263+
inquirer.List(
264+
name="pod",
265+
message="Please choose a pod",
266+
choices=pod_list,
267+
)
268+
]
269+
selected = inquirer.prompt(q, theme=GreenPassion())
270+
if selected:
271+
pod_name = selected["pod"]
272+
try:
273+
command = f"kubectl logs pod/{pod_name} -n {namespace} {follow_flag}"
274+
stream_command(command)
275+
except Exception as e:
276+
print(f"Please consider waiting for the pod to become available. Encountered: {e}")
277+
else:
278+
pass # cancelled by user
279+
280+
228281
if __name__ == "__main__":
229282
cli()
230283

0 commit comments

Comments
 (0)