Skip to content

Commit 59913cd

Browse files
committed
bitcoin: add ns to debug_log and grep_logs
1 parent 5100b0a commit 59913cd

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/warnet/bitcoin.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,14 @@ def _rpc(tank: str, method: str, params: str, namespace: Optional[str] = None):
5252

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

8184
matching_logs = []
85+
longest_namespace_len = 0
8286

8387
for tank in tanks:
88+
if len(tank.metadata.namespace) > longest_namespace_len:
89+
longest_namespace_len = len(tank.metadata.namespace)
90+
8491
pod_name = tank.metadata.name
8592
logs = pod_log(pod_name, BITCOINCORE_CONTAINER)
8693

@@ -89,7 +96,7 @@ def grep_logs(pattern: str, show_k8s_timestamps: bool, no_sort: bool):
8996
for line in logs:
9097
log_entry = line.decode("utf-8").rstrip()
9198
if re.search(pattern, log_entry):
92-
matching_logs.append((log_entry, pod_name))
99+
matching_logs.append((log_entry, tank.metadata.namespace, pod_name))
93100
except Exception as e:
94101
print(e)
95102
except KeyboardInterrupt:
@@ -100,17 +107,21 @@ def grep_logs(pattern: str, show_k8s_timestamps: bool, no_sort: bool):
100107
matching_logs.sort(key=lambda x: x[0])
101108

102109
# Print matching logs
103-
for log_entry, pod_name in matching_logs:
110+
for log_entry, namespace, pod_name in matching_logs:
104111
try:
105112
# Split the log entry into Kubernetes timestamp, Bitcoin timestamp, and the rest of the log
106113
k8s_timestamp, rest = log_entry.split(" ", 1)
107114
bitcoin_timestamp, log_message = rest.split(" ", 1)
108115

109116
# Format the output based on the show_k8s_timestamps option
110117
if show_k8s_timestamps:
111-
print(f"{pod_name}: {k8s_timestamp} {bitcoin_timestamp} {log_message}")
118+
print(
119+
f"{pod_name} {namespace:<{longest_namespace_len}} {k8s_timestamp} {bitcoin_timestamp} {log_message}"
120+
)
112121
else:
113-
print(f"{pod_name}: {bitcoin_timestamp} {log_message}")
122+
print(
123+
f"{pod_name} {namespace:<{longest_namespace_len}} {bitcoin_timestamp} {log_message}"
124+
)
114125
except ValueError:
115126
# If we can't parse the timestamps, just print the original log entry
116127
print(f"{pod_name}: {log_entry}")

0 commit comments

Comments
 (0)