Skip to content

Commit 5df196d

Browse files
committed
move fork-observer and caddy into warnet-logging namespace
1 parent a28d262 commit 5df196d

File tree

5 files changed

+23
-75
lines changed

5 files changed

+23
-75
lines changed

src/warnet/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
]
1313

1414
DEFAULT_NAMESPACE = "warnet"
15+
LOGGING_NAMESPACE = "warnet-logging"
1516
HELM_COMMAND = "helm upgrade --install --create-namespace"
1617

1718
# Directories and files for non-python assets, e.g., helm charts, example scenarios, default configs

src/warnet/control.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from rich.prompt import Confirm, Prompt
1515
from rich.table import Table
1616

17-
from .constants import COMMANDER_CHART
17+
from .constants import COMMANDER_CHART, LOGGING_NAMESPACE
1818
from .deploy import _port_stop_internal
1919
from .k8s import (
2020
get_default_namespace,
@@ -130,7 +130,7 @@ def down():
130130
"""Bring down a running warnet quickly"""
131131
console.print("[bold yellow]Bringing down the warnet...[/bold yellow]")
132132

133-
namespaces = [get_default_namespace(), "warnet-logging"]
133+
namespaces = [get_default_namespace(), LOGGING_NAMESPACE]
134134

135135
def uninstall_release(namespace, release_name):
136136
cmd = f"helm uninstall {release_name} --namespace {namespace} --wait=false"
@@ -164,7 +164,7 @@ def delete_pod(pod_name, namespace):
164164
console.print(f"[yellow]{future.result()}[/yellow]")
165165

166166
# Shutdown any port forwarding
167-
_port_stop_internal()
167+
_port_stop_internal("caddy", namespaces[1])
168168
console.print("[bold yellow]Teardown process initiated for all components.[/bold yellow]")
169169
console.print("[bold yellow]Note: Some processes may continue in the background.[/bold yellow]")
170170
console.print("[bold green]Warnet teardown process completed.[/bold green]")

src/warnet/deploy.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
FORK_OBSERVER_CHART,
1616
HELM_COMMAND,
1717
LOGGING_HELM_COMMANDS,
18+
LOGGING_NAMESPACE,
1819
NAMESPACES_CHART_LOCATION,
1920
NAMESPACES_FILE,
2021
NETWORK_FILE,
@@ -100,12 +101,14 @@ def deploy_caddy(directory: Path, debug: bool):
100101
with network_file_path.open() as f:
101102
network_file = yaml.safe_load(f)
102103

104+
namespace = LOGGING_NAMESPACE
105+
# TODO: get this from the helm chart
106+
name = "caddy"
107+
103108
# Only start if configured in the network file
104-
if not network_file.get("fork_observer", {}).get("enabled", False):
109+
if not network_file.get(name, {}).get("enabled", False):
105110
return
106111

107-
namespace = get_default_namespace()
108-
name = "caddy"
109112
cmd = f"{HELM_COMMAND} {name} {CADDY_CHART} --namespace {namespace}"
110113
if debug:
111114
cmd += " --debug"
@@ -115,7 +118,7 @@ def deploy_caddy(directory: Path, debug: bool):
115118
return
116119

117120
wait_for_caddy_ready(name, namespace)
118-
_port_start_internal()
121+
_port_start_internal(name, namespace)
119122

120123

121124
def deploy_fork_observer(directory: Path, debug: bool):
@@ -127,7 +130,8 @@ def deploy_fork_observer(directory: Path, debug: bool):
127130
if not network_file.get("fork_observer", {}).get("enabled", False):
128131
return
129132

130-
namespace = get_default_namespace()
133+
default_namespace = get_default_namespace()
134+
namespace = LOGGING_NAMESPACE
131135
cmd = f"{HELM_COMMAND} 'fork-observer' {FORK_OBSERVER_CHART} --namespace {namespace}"
132136
if debug:
133137
cmd += " --debug"
@@ -143,7 +147,7 @@ def deploy_fork_observer(directory: Path, debug: bool):
143147
id = {i}
144148
name = "{node_name}"
145149
description = "A node. Just A node."
146-
rpc_host = "{node_name}"
150+
rpc_host = "{node_name}.{default_namespace}.svc"
147151
rpc_port = 18443
148152
rpc_user = "forkobserver"
149153
rpc_password = "tabconf2024"
@@ -275,17 +279,17 @@ def run_detached_process(command):
275279
print(f"Started detached process: {command}")
276280

277281

278-
def _port_start_internal():
279-
command = "kubectl port-forward service/caddy 2019:80"
282+
def _port_start_internal(name, namespace):
283+
command = f"kubectl port-forward -n {namespace} service/{name} 2019:80"
280284
run_detached_process(command)
281285
click.echo(
282286
"Port forwarding on port 2019 started in the background. To access landing page visit localhost:2019."
283287
)
284288

285289

286-
def _port_stop_internal():
290+
def _port_stop_internal(name, namespace):
287291
if is_windows():
288292
os.system("taskkill /F /IM kubectl.exe")
289293
else:
290-
os.system("pkill -f 'kubectl port-forward service/caddy-service 2019:80'")
294+
os.system(f"pkill -f 'kubectl port-forward -n {namespace} service/{name} 2019:80'")
291295
click.echo("Port forwarding stopped.")

src/warnet/graph.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def custom_graph(
3333
datadir: Path,
3434
fork_observer: bool,
3535
fork_obs_query_interval: int,
36+
caddy: bool,
3637
):
3738
datadir.mkdir(parents=False, exist_ok=False)
3839
# Generate network.yaml
@@ -68,6 +69,9 @@ def custom_graph(
6869
"enabled": fork_observer,
6970
"configQueryInterval": fork_obs_query_interval,
7071
}
72+
network_yaml_data["caddy"] = {
73+
"enabled": caddy,
74+
}
7175

7276
with open(os.path.join(datadir, "network.yaml"), "w") as f:
7377
yaml.dump(network_yaml_data, f, default_flow_style=False)
@@ -180,6 +184,7 @@ def inquirer_create_network(project_path: Path):
180184
custom_network_path,
181185
fork_observer,
182186
fork_observer_query_interval,
187+
fork_observer, # This enables caddy whenever fork-observer is enabled
183188
)
184189
return custom_network_path
185190

src/warnet/project.py

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import os
22
import platform
3-
import random
43
import subprocess
54
import sys
65
from dataclasses import dataclass
76
from enum import Enum, auto
8-
from importlib.resources import files
97
from pathlib import Path
108
from typing import Callable
119

1210
import click
1311
import inquirer
14-
import yaml
1512

1613
from .graph import inquirer_create_network
1714
from .network import copy_network_defaults, copy_scenario_defaults
@@ -319,62 +316,3 @@ def init():
319316
"""Initialize a warnet project in the current directory"""
320317
current_dir = Path.cwd()
321318
new_internal(directory=current_dir, from_init=True)
322-
323-
324-
def custom_graph(
325-
num_nodes: int,
326-
num_connections: int,
327-
version: str,
328-
datadir: Path,
329-
fork_observer: bool,
330-
fork_obs_query_interval: int,
331-
):
332-
datadir.mkdir(parents=False, exist_ok=False)
333-
# Generate network.yaml
334-
nodes = []
335-
connections = set()
336-
337-
for i in range(num_nodes):
338-
node = {"name": f"tank-{i:04d}", "connect": [], "image": {"tag": version}}
339-
340-
# Add round-robin connection
341-
next_node = (i + 1) % num_nodes
342-
node["connect"].append(f"tank-{next_node:04d}")
343-
connections.add((i, next_node))
344-
345-
# Add random connections
346-
available_nodes = list(range(num_nodes))
347-
available_nodes.remove(i)
348-
if next_node in available_nodes:
349-
available_nodes.remove(next_node)
350-
351-
for _ in range(min(num_connections - 1, len(available_nodes))):
352-
random_node = random.choice(available_nodes)
353-
# Avoid circular loops of A -> B -> A
354-
if (random_node, i) not in connections:
355-
node["connect"].append(f"tank-{random_node:04d}")
356-
connections.add((i, random_node))
357-
available_nodes.remove(random_node)
358-
359-
nodes.append(node)
360-
361-
network_yaml_data = {"nodes": nodes}
362-
network_yaml_data["fork_observer"] = {
363-
"enabled": fork_observer,
364-
"configQueryInterval": fork_obs_query_interval,
365-
}
366-
367-
with open(os.path.join(datadir, "network.yaml"), "w") as f:
368-
yaml.dump(network_yaml_data, f, default_flow_style=False)
369-
370-
# Generate node-defaults.yaml
371-
default_yaml_path = files("resources.networks").joinpath("node-defaults.yaml")
372-
with open(str(default_yaml_path)) as f:
373-
defaults_yaml_content = f.read()
374-
375-
with open(os.path.join(datadir, "node-defaults.yaml"), "w") as f:
376-
f.write(defaults_yaml_content)
377-
378-
click.echo(
379-
f"Project '{datadir}' has been created with 'network.yaml' and 'node-defaults.yaml'."
380-
)

0 commit comments

Comments
 (0)