Skip to content

Commit 9a2403c

Browse files
committed
bitcoin: add ns to debug_log and grep_logs
1 parent d14bb22 commit 9a2403c

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/warnet/bitcoin.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,14 @@ def _rpc(tank: str, method: str, params: str, namespace: Optional[str] = None):
5151

5252
@bitcoin.command()
5353
@click.argument("tank", type=str, required=True)
54-
def debug_log(tank: str):
54+
@click.option("--namespace", default=None, show_default=True)
55+
def debug_log(tank: str, namespace: Optional[str]):
5556
"""
5657
Fetch the Bitcoin Core debug log from <tank pod name>
5758
"""
58-
cmd = f"kubectl logs {tank}"
59+
if not namespace:
60+
namespace = get_default_namespace()
61+
cmd = f"kubectl logs {tank} --namespace {namespace}"
5962
try:
6063
print(run_command(cmd))
6164
except Exception as e:
@@ -78,8 +81,12 @@ def grep_logs(pattern: str, show_k8s_timestamps: bool, no_sort: bool):
7881
sys.exit(1)
7982

8083
matching_logs = []
84+
longest_namespace_len = 0
8185

8286
for tank in tanks:
87+
if len(tank.metadata.namespace) > longest_namespace_len:
88+
longest_namespace_len = len(tank.metadata.namespace)
89+
8390
pod_name = tank.metadata.name
8491
# Get container names for this pod
8592
containers = tank.spec.containers
@@ -92,31 +99,35 @@ def grep_logs(pattern: str, show_k8s_timestamps: bool, no_sort: bool):
9299
continue
93100

94101
# Get logs from the specific container
95-
command = f"kubectl logs {pod_name} -c {container_name} --timestamps"
102+
command = f"kubectl logs {pod_name} -c {container_name} --timestamps --namespace {tank.metadata.namespace}"
96103
logs = run_command(command)
97104

98105
if logs is not False:
99106
# Process logs
100107
for log_entry in logs.splitlines():
101108
if re.search(pattern, log_entry):
102-
matching_logs.append((log_entry, pod_name))
109+
matching_logs.append((log_entry, tank.metadata.namespace, pod_name))
103110

104111
# Sort logs if needed
105112
if not no_sort:
106113
matching_logs.sort(key=lambda x: x[0])
107114

108115
# Print matching logs
109-
for log_entry, pod_name in matching_logs:
116+
for log_entry, namespace, pod_name in matching_logs:
110117
try:
111118
# Split the log entry into Kubernetes timestamp, Bitcoin timestamp, and the rest of the log
112119
k8s_timestamp, rest = log_entry.split(" ", 1)
113120
bitcoin_timestamp, log_message = rest.split(" ", 1)
114121

115122
# Format the output based on the show_k8s_timestamps option
116123
if show_k8s_timestamps:
117-
print(f"{pod_name}: {k8s_timestamp} {bitcoin_timestamp} {log_message}")
124+
print(
125+
f"{pod_name} {namespace:<{longest_namespace_len}} {k8s_timestamp} {bitcoin_timestamp} {log_message}"
126+
)
118127
else:
119-
print(f"{pod_name}: {bitcoin_timestamp} {log_message}")
128+
print(
129+
f"{pod_name} {namespace:<{longest_namespace_len}} {bitcoin_timestamp} {log_message}"
130+
)
120131
except ValueError:
121132
# If we can't parse the timestamps, just print the original log entry
122133
print(f"{pod_name}: {log_entry}")

0 commit comments

Comments
 (0)