Skip to content

Commit f18d236

Browse files
committed
wip commit
1 parent 3d2d712 commit f18d236

File tree

3 files changed

+54
-20
lines changed

3 files changed

+54
-20
lines changed

src/warnet/bitcoin.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from test_framework.messages import ser_uint256
1111
from test_framework.p2p import MESSAGEMAP
1212

13-
from .k8s import get_default_namespace, get_mission
13+
from .k8s import get_mission, get_pod_namespace, V1Pod, get_default_namespace, get_pod
1414
from .process import run_command
1515

1616

@@ -23,26 +23,29 @@ def bitcoin():
2323
@click.argument("tank", type=str)
2424
@click.argument("method", type=str)
2525
@click.argument("params", type=str, nargs=-1) # this will capture all remaining arguments
26-
def rpc(tank: str, method: str, params: str):
26+
@click.option("--namespace", "-n", type=str, help="Namespace (overrides default namespace from kubectl)", default=None)
27+
def rpc(tank: str, method: str, params: str, namespace: str):
2728
"""
2829
Call bitcoin-cli <method> [params] on <tank pod name>
2930
"""
3031
try:
31-
result = _rpc(tank, method, params)
32+
ns = namespace if namespace else get_default_namespace()
33+
print(ns)
34+
pod = get_pod(tank, ns)
35+
result = _rpc(pod, method, params)
3236
except Exception as e:
3337
print(f"{e}")
3438
sys.exit(1)
3539
print(result)
3640

3741

38-
def _rpc(tank: str, method: str, params: str):
42+
def _rpc(tank: V1Pod, method: str, params: str):
3943
# bitcoin-cli should be able to read bitcoin.conf inside the container
4044
# so no extra args like port, chain, username or password are needed
41-
namespace = get_default_namespace()
4245
if params:
43-
cmd = f"kubectl -n {namespace} exec {tank} -- bitcoin-cli {method} {' '.join(map(str, params))}"
46+
cmd = f"kubectl exec {tank.metadata.name} -n {get_pod_namespace(tank)} -- bitcoin-cli {method} {' '.join(map(str, params))}"
4447
else:
45-
cmd = f"kubectl -n {namespace} exec {tank} -- bitcoin-cli {method}"
48+
cmd = f"kubectl exec {tank.metadata.name} -n {get_pod_namespace(tank)} -- bitcoin-cli {method}"
4649
return run_command(cmd)
4750

4851

src/warnet/k8s.py

+42-8
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import sys
44
import tempfile
55
from pathlib import Path
6+
# from typing_extensions import AnyStr
67

78
import yaml
89
from kubernetes import client, config, watch
9-
from kubernetes.client.models import CoreV1Event, V1PodList
10+
from kubernetes.client.models import CoreV1Event, V1PodList, V1NamespaceList, V1Pod
11+
from kubernetes.client.rest import ApiException
1012
from kubernetes.dynamic import DynamicClient
1113
from kubernetes.stream import stream
1214

@@ -16,6 +18,8 @@
1618
INGRESS_NAMESPACE,
1719
KUBECONFIG,
1820
LOGGING_NAMESPACE,
21+
WARNET_ASSETS,
22+
WARNET_NAMESPACE_ANNOTATION,
1923
)
2024
from .process import run_command, stream_command
2125

@@ -31,20 +35,44 @@ def get_dynamic_client() -> DynamicClient:
3135

3236

3337
def get_pods() -> V1PodList:
38+
sclient = get_static_client()
39+
pods = []
40+
namespaces: V1NamespaceList = get_namespaces_by_warnet_type(WARNET_ASSETS)
41+
for ns in namespaces.items:
42+
try:
43+
pods.append(sclient.list_namespaced_pod(ns.metadata.name))
44+
except Exception as e:
45+
raise e
46+
return pods
47+
48+
49+
def get_pod(pod_name: str, namespace: str) -> V1Pod:
3450
sclient = get_static_client()
3551
try:
36-
pod_list: V1PodList = sclient.list_namespaced_pod(get_default_namespace())
37-
except Exception as e:
38-
raise e
39-
return pod_list
52+
pod = sclient.read_namespaced_pod(name=pod_name, namespace=namespace)
53+
return pod
54+
except ApiException as e:
55+
if e.status == 404:
56+
print(f"Pod {pod_name} not found in namespace {namespace}")
57+
else:
58+
print(f"Exception when calling CoreV1Api->read_namespaced_pod: {e}")
59+
return None
60+
61+
62+
def get_pod_namespace(pod: client.V1Pod) -> str:
63+
annotations = pod.metadata.annotations
64+
if annotations:
65+
return annotations.get(WARNET_NAMESPACE_ANNOTATION)
66+
return DEFAULT_NAMESPACE
4067

4168

4269
def get_mission(mission: str) -> list[V1PodList]:
4370
pods = get_pods()
4471
crew = []
45-
for pod in pods.items:
46-
if "mission" in pod.metadata.labels and pod.metadata.labels["mission"] == mission:
47-
crew.append(pod)
72+
for pod_list in pods:
73+
for pod in pod_list.items:
74+
if "mission" in pod.metadata.labels and pod.metadata.labels["mission"] == mission:
75+
crew.append(pod)
4876
return crew
4977

5078

@@ -135,6 +163,12 @@ def get_default_namespace() -> str:
135163
return kubectl_namespace if kubectl_namespace else DEFAULT_NAMESPACE
136164

137165

166+
def get_namespaces_by_warnet_type(warnet_type: str) -> list[V1NamespaceList]:
167+
sclient = get_static_client()
168+
namespaces = sclient.list_namespace(label_selector=f"type={warnet_type}")
169+
return namespaces
170+
171+
138172
def snapshot_bitcoin_datadir(
139173
pod_name: str, chain: str, local_path: str = "./", filters: list[str] = None
140174
) -> None:

src/warnet/network.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,17 @@ def _connected(end="\n"):
6363
for tank in tanks:
6464
# Get actual
6565
try:
66-
peerinfo = json.loads(_rpc(tank.metadata.name, "getpeerinfo", ""))
66+
peerinfo = json.loads(_rpc(tank, "getpeerinfo", ""))
6767
actual = 0
6868
for peer in peerinfo:
6969
if is_connection_manual(peer):
7070
actual += 1
7171
expected = int(tank.metadata.annotations["init_peers"])
72-
print(
73-
f"Tank {tank.metadata.name} peers expected: {expected}, actual: {actual}", end=end
74-
)
72+
print(f"Tank {tank.metadata.name} peers expected: {expected}, actual: {actual}", end=end)
7573
# Even if more edges are specified, bitcoind only allows
7674
# 8 manual outbound connections
7775
if min(8, expected) > actual:
7876
print("\nNetwork not connected")
79-
return False
8077
except Exception:
8178
return False
8279
print("Network connected ")

0 commit comments

Comments
 (0)