Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/warnet/bitcoin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
from io import BytesIO

import click
from urllib3.exceptions import MaxRetryError

from test_framework.messages import ser_uint256
from test_framework.p2p import MESSAGEMAP
from urllib3.exceptions import MaxRetryError

from .k8s import get_default_namespace, get_mission
from .k8s import get_default_namespace, get_mission, get_pod
from .process import run_command


Expand Down Expand Up @@ -39,10 +38,19 @@ def _rpc(tank: str, method: str, params: str):
# bitcoin-cli should be able to read bitcoin.conf inside the container
# so no extra args like port, chain, username or password are needed
namespace = get_default_namespace()

try:
pod = get_pod(tank)
container_names = [container.name for container in pod.spec.containers]
container_name = container_names[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What determines the order of containers in a pod? Can we guarantee the one we want is always [0] because of the yaml file? Or should we hard code the name from the chart bitcoincore ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first container has been the bitcoincore container and commander container from what I have seen, but I agree that we have no way of guaranteeing that.

I changed the logic so that we use hard coded values from the constants file which match the helm charts.

except Exception as e:
print(f"Could not determine primary container: {e}")
return

if params:
cmd = f"kubectl -n {namespace} exec {tank} -- bitcoin-cli {method} {' '.join(map(str, params))}"
cmd = f"kubectl -n {namespace} exec {tank} --container {container_name} -- bitcoin-cli {method} {' '.join(map(str, params))}"
else:
cmd = f"kubectl -n {namespace} exec {tank} -- bitcoin-cli {method}"
cmd = f"kubectl -n {namespace} exec {tank} --container {container_name} -- bitcoin-cli {method}"
return run_command(cmd)


Expand Down
15 changes: 12 additions & 3 deletions src/warnet/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
delete_pod,
get_default_namespace,
get_mission,
get_pod,
get_pods,
pod_log,
snapshot_bitcoin_datadir,
Expand Down Expand Up @@ -329,9 +330,17 @@ def _logs(pod_name: str, follow: bool):
return # cancelled by user

try:
stream = pod_log(pod_name, container_name=None, follow=follow)
for line in stream.stream():
print(line.decode("utf-8"), end=None)
pod = get_pod(pod_name)
container_names = [container.name for container in pod.spec.containers]
container_name = container_names[0]
except Exception as e:
print(f"Could not determine primary container: {e}")
return

try:
stream = pod_log(pod_name, container_name=container_name, follow=follow)
for line in stream:
click.echo(line.decode("utf-8").rstrip())
except Exception as e:
print(e)
except KeyboardInterrupt:
Expand Down
10 changes: 9 additions & 1 deletion src/warnet/k8s.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import tempfile
from pathlib import Path
from time import sleep
from typing import Optional

import yaml
from kubernetes import client, config, watch
from kubernetes.client.models import CoreV1Event, V1PodList
from kubernetes.client.models import CoreV1Event, V1Pod, V1PodList
from kubernetes.client.rest import ApiException
from kubernetes.dynamic import DynamicClient
from kubernetes.stream import stream
Expand Down Expand Up @@ -41,6 +42,13 @@ def get_pods() -> V1PodList:
return pod_list


def get_pod(name: str, namespace: Optional[str] = None) -> V1Pod:
sclient = get_static_client()
if not namespace:
namespace = get_default_namespace()
return sclient.read_namespaced_pod(name=name, namespace=namespace)


def get_mission(mission: str) -> list[V1PodList]:
pods = get_pods()
crew = []
Expand Down