Skip to content

Commit d2e277d

Browse files
authored
Merge pull request #626 from mplsgrant/2024-10-fix-logs-container
Add container selection logic
2 parents 6c23e2a + 9a7a973 commit d2e277d

File tree

4 files changed

+46
-9
lines changed

4 files changed

+46
-9
lines changed

src/warnet/bitcoin.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
from io import BytesIO
66

77
import click
8-
from urllib3.exceptions import MaxRetryError
9-
108
from test_framework.messages import ser_uint256
119
from test_framework.p2p import MESSAGEMAP
10+
from urllib3.exceptions import MaxRetryError
1211

12+
from .constants import BITCOINCORE_CONTAINER
1313
from .k8s import get_default_namespace, get_mission
1414
from .process import run_command
1515

@@ -39,10 +39,11 @@ def _rpc(tank: str, method: str, params: str):
3939
# bitcoin-cli should be able to read bitcoin.conf inside the container
4040
# so no extra args like port, chain, username or password are needed
4141
namespace = get_default_namespace()
42+
4243
if params:
43-
cmd = f"kubectl -n {namespace} exec {tank} -- bitcoin-cli {method} {' '.join(map(str, params))}"
44+
cmd = f"kubectl -n {namespace} exec {tank} --container {BITCOINCORE_CONTAINER} -- bitcoin-cli {method} {' '.join(map(str, params))}"
4445
else:
45-
cmd = f"kubectl -n {namespace} exec {tank} -- bitcoin-cli {method}"
46+
cmd = f"kubectl -n {namespace} exec {tank} --container {BITCOINCORE_CONTAINER} -- bitcoin-cli {method}"
4647
return run_command(cmd)
4748

4849

src/warnet/constants.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
INGRESS_NAMESPACE = "ingress"
1717
HELM_COMMAND = "helm upgrade --install --create-namespace"
1818

19+
BITCOINCORE_CONTAINER = "bitcoincore"
20+
COMMANDER_CONTAINER = "commander"
21+
1922
# Directories and files for non-python assets, e.g., helm charts, example scenarios, default configs
2023
SRC_DIR = files("warnet")
2124
RESOURCES_DIR = files("resources")

src/warnet/control.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@
1616
from rich.prompt import Confirm, Prompt
1717
from rich.table import Table
1818

19-
from .constants import COMMANDER_CHART, LOGGING_NAMESPACE
19+
from .constants import (
20+
BITCOINCORE_CONTAINER,
21+
COMMANDER_CHART,
22+
COMMANDER_CONTAINER,
23+
LOGGING_NAMESPACE,
24+
)
2025
from .k8s import (
2126
delete_pod,
2227
get_default_namespace,
2328
get_mission,
29+
get_pod,
2430
get_pods,
2531
pod_log,
2632
snapshot_bitcoin_datadir,
@@ -329,9 +335,28 @@ def _logs(pod_name: str, follow: bool):
329335
return # cancelled by user
330336

331337
try:
332-
stream = pod_log(pod_name, container_name=None, follow=follow)
333-
for line in stream.stream():
334-
print(line.decode("utf-8"), end=None)
338+
pod = get_pod(pod_name)
339+
eligible_container_names = [BITCOINCORE_CONTAINER, COMMANDER_CONTAINER]
340+
available_container_names = [container.name for container in pod.spec.containers]
341+
container_name = next(
342+
(
343+
container_name
344+
for container_name in available_container_names
345+
if container_name in eligible_container_names
346+
),
347+
None,
348+
)
349+
if not container_name:
350+
print("Could not determine primary container.")
351+
return
352+
except Exception as e:
353+
print(f"Error getting pods. Could not determine primary container: {e}")
354+
return
355+
356+
try:
357+
stream = pod_log(pod_name, container_name=container_name, follow=follow)
358+
for line in stream:
359+
click.echo(line.decode("utf-8").rstrip())
335360
except Exception as e:
336361
print(e)
337362
except KeyboardInterrupt:

src/warnet/k8s.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
import tempfile
55
from pathlib import Path
66
from time import sleep
7+
from typing import Optional
78

89
import yaml
910
from kubernetes import client, config, watch
10-
from kubernetes.client.models import CoreV1Event, V1PodList
11+
from kubernetes.client.models import CoreV1Event, V1Pod, V1PodList
1112
from kubernetes.client.rest import ApiException
1213
from kubernetes.dynamic import DynamicClient
1314
from kubernetes.stream import stream
@@ -41,6 +42,13 @@ def get_pods() -> V1PodList:
4142
return pod_list
4243

4344

45+
def get_pod(name: str, namespace: Optional[str] = None) -> V1Pod:
46+
sclient = get_static_client()
47+
if not namespace:
48+
namespace = get_default_namespace()
49+
return sclient.read_namespaced_pod(name=name, namespace=namespace)
50+
51+
4452
def get_mission(mission: str) -> list[V1PodList]:
4553
pods = get_pods()
4654
crew = []

0 commit comments

Comments
 (0)