Skip to content

Commit 41cbc08

Browse files
committed
add ns to debug_log and grep_logs
1 parent 74611ef commit 41cbc08

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
@@ -50,11 +50,14 @@ def _rpc(tank: str, method: str, params: str, namespace: Optional[str] = None):
5050

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

7982
matching_logs = []
83+
longest_namespace_len = 0
8084

8185
for tank in tanks:
86+
if len(tank.metadata.namespace) > longest_namespace_len:
87+
longest_namespace_len = len(tank.metadata.namespace)
88+
8289
pod_name = tank.metadata.name
8390
# Get container names for this pod
8491
containers = tank.spec.containers
@@ -91,31 +98,35 @@ def grep_logs(pattern: str, show_k8s_timestamps: bool, no_sort: bool):
9198
continue
9299

93100
# Get logs from the specific container
94-
command = f"kubectl logs {pod_name} -c {container_name} --timestamps"
101+
command = f"kubectl logs {pod_name} -c {container_name} --timestamps --namespace {tank.metadata.namespace}"
95102
logs = run_command(command)
96103

97104
if logs is not False:
98105
# Process logs
99106
for log_entry in logs.splitlines():
100107
if re.search(pattern, log_entry):
101-
matching_logs.append((log_entry, pod_name))
108+
matching_logs.append((log_entry, tank.metadata.namespace, pod_name))
102109

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

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

114121
# Format the output based on the show_k8s_timestamps option
115122
if show_k8s_timestamps:
116-
print(f"{pod_name}: {k8s_timestamp} {bitcoin_timestamp} {log_message}")
123+
print(
124+
f"{pod_name} {namespace:<{longest_namespace_len}} {k8s_timestamp} {bitcoin_timestamp} {log_message}"
125+
)
117126
else:
118-
print(f"{pod_name}: {bitcoin_timestamp} {log_message}")
127+
print(
128+
f"{pod_name} {namespace:<{longest_namespace_len}} {bitcoin_timestamp} {log_message}"
129+
)
119130
except ValueError:
120131
# If we can't parse the timestamps, just print the original log entry
121132
print(f"{pod_name}: {log_entry}")

0 commit comments

Comments
 (0)