Skip to content

Commit

Permalink
wip commit
Browse files Browse the repository at this point in the history
  • Loading branch information
josibake committed Sep 26, 2024
1 parent 3d2d712 commit f18d236
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 20 deletions.
17 changes: 10 additions & 7 deletions src/warnet/bitcoin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from test_framework.messages import ser_uint256
from test_framework.p2p import MESSAGEMAP

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


Expand All @@ -23,26 +23,29 @@ def bitcoin():
@click.argument("tank", type=str)
@click.argument("method", type=str)
@click.argument("params", type=str, nargs=-1) # this will capture all remaining arguments
def rpc(tank: str, method: str, params: str):
@click.option("--namespace", "-n", type=str, help="Namespace (overrides default namespace from kubectl)", default=None)
def rpc(tank: str, method: str, params: str, namespace: str):
"""
Call bitcoin-cli <method> [params] on <tank pod name>
"""
try:
result = _rpc(tank, method, params)
ns = namespace if namespace else get_default_namespace()
print(ns)
pod = get_pod(tank, ns)
result = _rpc(pod, method, params)
except Exception as e:
print(f"{e}")
sys.exit(1)
print(result)


def _rpc(tank: str, method: str, params: str):
def _rpc(tank: V1Pod, 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()
if params:
cmd = f"kubectl -n {namespace} exec {tank} -- bitcoin-cli {method} {' '.join(map(str, params))}"
cmd = f"kubectl exec {tank.metadata.name} -n {get_pod_namespace(tank)} -- bitcoin-cli {method} {' '.join(map(str, params))}"
else:
cmd = f"kubectl -n {namespace} exec {tank} -- bitcoin-cli {method}"
cmd = f"kubectl exec {tank.metadata.name} -n {get_pod_namespace(tank)} -- bitcoin-cli {method}"
return run_command(cmd)


Expand Down
50 changes: 42 additions & 8 deletions src/warnet/k8s.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import sys
import tempfile
from pathlib import Path
# from typing_extensions import AnyStr

import yaml
from kubernetes import client, config, watch
from kubernetes.client.models import CoreV1Event, V1PodList
from kubernetes.client.models import CoreV1Event, V1PodList, V1NamespaceList, V1Pod
from kubernetes.client.rest import ApiException
from kubernetes.dynamic import DynamicClient
from kubernetes.stream import stream

Expand All @@ -16,6 +18,8 @@
INGRESS_NAMESPACE,
KUBECONFIG,
LOGGING_NAMESPACE,
WARNET_ASSETS,
WARNET_NAMESPACE_ANNOTATION,
)
from .process import run_command, stream_command

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


def get_pods() -> V1PodList:
sclient = get_static_client()
pods = []
namespaces: V1NamespaceList = get_namespaces_by_warnet_type(WARNET_ASSETS)
for ns in namespaces.items:
try:
pods.append(sclient.list_namespaced_pod(ns.metadata.name))
except Exception as e:
raise e
return pods


def get_pod(pod_name: str, namespace: str) -> V1Pod:
sclient = get_static_client()
try:
pod_list: V1PodList = sclient.list_namespaced_pod(get_default_namespace())
except Exception as e:
raise e
return pod_list
pod = sclient.read_namespaced_pod(name=pod_name, namespace=namespace)
return pod
except ApiException as e:
if e.status == 404:
print(f"Pod {pod_name} not found in namespace {namespace}")
else:
print(f"Exception when calling CoreV1Api->read_namespaced_pod: {e}")
return None


def get_pod_namespace(pod: client.V1Pod) -> str:
annotations = pod.metadata.annotations
if annotations:
return annotations.get(WARNET_NAMESPACE_ANNOTATION)
return DEFAULT_NAMESPACE


def get_mission(mission: str) -> list[V1PodList]:
pods = get_pods()
crew = []
for pod in pods.items:
if "mission" in pod.metadata.labels and pod.metadata.labels["mission"] == mission:
crew.append(pod)
for pod_list in pods:
for pod in pod_list.items:
if "mission" in pod.metadata.labels and pod.metadata.labels["mission"] == mission:
crew.append(pod)
return crew


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


def get_namespaces_by_warnet_type(warnet_type: str) -> list[V1NamespaceList]:
sclient = get_static_client()
namespaces = sclient.list_namespace(label_selector=f"type={warnet_type}")
return namespaces


def snapshot_bitcoin_datadir(
pod_name: str, chain: str, local_path: str = "./", filters: list[str] = None
) -> None:
Expand Down
7 changes: 2 additions & 5 deletions src/warnet/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,17 @@ def _connected(end="\n"):
for tank in tanks:
# Get actual
try:
peerinfo = json.loads(_rpc(tank.metadata.name, "getpeerinfo", ""))
peerinfo = json.loads(_rpc(tank, "getpeerinfo", ""))
actual = 0
for peer in peerinfo:
if is_connection_manual(peer):
actual += 1
expected = int(tank.metadata.annotations["init_peers"])
print(
f"Tank {tank.metadata.name} peers expected: {expected}, actual: {actual}", end=end
)
print(f"Tank {tank.metadata.name} peers expected: {expected}, actual: {actual}", end=end)
# Even if more edges are specified, bitcoind only allows
# 8 manual outbound connections
if min(8, expected) > actual:
print("\nNetwork not connected")
return False
except Exception:
return False
print("Network connected ")
Expand Down

0 comments on commit f18d236

Please sign in to comment.