Skip to content

Commit 1feed7a

Browse files
committed
DRY out the namespace check
Replace the namespace check with this function: `get_default_namespace_or(namespace)`
1 parent 809eb5c commit 1feed7a

File tree

4 files changed

+26
-36
lines changed

4 files changed

+26
-36
lines changed

src/warnet/bitcoin.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from urllib3.exceptions import MaxRetryError
1313

1414
from .constants import BITCOINCORE_CONTAINER
15-
from .k8s import get_default_namespace, get_mission, pod_log
15+
from .k8s import get_default_namespace, get_mission, pod_log, get_default_namespace_or
1616
from .process import run_command
1717

1818

@@ -41,8 +41,7 @@ def rpc(tank: str, method: str, params: str, namespace: Optional[str]):
4141
def _rpc(tank: str, method: str, params: str, namespace: Optional[str] = None):
4242
# bitcoin-cli should be able to read bitcoin.conf inside the container
4343
# so no extra args like port, chain, username or password are needed
44-
if not namespace:
45-
namespace = get_default_namespace()
44+
namespace = get_default_namespace_or(namespace)
4645
if params:
4746
cmd = f"kubectl -n {namespace} exec {tank} --container {BITCOINCORE_CONTAINER} -- bitcoin-cli {method} {' '.join(map(str, params))}"
4847
else:
@@ -57,8 +56,7 @@ def debug_log(tank: str, namespace: Optional[str]):
5756
"""
5857
Fetch the Bitcoin Core debug log from <tank pod name>
5958
"""
60-
if not namespace:
61-
namespace = get_default_namespace()
59+
namespace = get_default_namespace_or(namespace)
6260
cmd = f"kubectl logs {tank} --namespace {namespace}"
6361
try:
6462
print(run_command(cmd))
@@ -142,10 +140,8 @@ def messages(
142140
Fetch messages sent between <tank_a pod name> and <tank_b pod name> in [chain]
143141
"""
144142
try:
145-
if not namespace_a:
146-
namespace_a = get_default_namespace()
147-
if not namespace_b:
148-
namespace_b = get_default_namespace()
143+
namespace_a = get_default_namespace_or(namespace_a)
144+
namespace_b = get_default_namespace_or(namespace_b)
149145

150146
# Get the messages
151147
messages = get_messages(

src/warnet/control.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
snapshot_bitcoin_datadir,
3636
wait_for_init,
3737
wait_for_pod,
38-
write_file_to_container,
38+
write_file_to_container, get_default_namespace_or,
3939
)
4040
from .process import run_command, stream_command
4141

@@ -236,8 +236,7 @@ def run(
236236
Run a scenario from a file.
237237
Pass `-- --help` to get individual scenario help
238238
"""
239-
if not namespace:
240-
namespace = get_default_namespace()
239+
namespace = get_default_namespace_or(namespace)
241240

242241
scenario_path = Path(scenario_file).resolve()
243242
scenario_dir = scenario_path.parent if not source_dir else Path(source_dir).resolve()
@@ -361,8 +360,7 @@ def logs(pod_name: str, follow: bool, namespace: str):
361360

362361

363362
def _logs(pod_name: str, follow: bool, namespace: Optional[str] = None):
364-
if not namespace:
365-
namespace = get_default_namespace()
363+
namespace = get_default_namespace_or(namespace)
366364

367365
if pod_name == "":
368366
try:

src/warnet/deploy.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
get_default_namespace,
2727
get_namespaces_by_prefix,
2828
wait_for_ingress_controller,
29-
wait_for_pod_ready,
29+
wait_for_pod_ready, get_default_namespace_or,
3030
)
3131
from .process import stream_command
3232

@@ -218,12 +218,11 @@ def deploy_network(directory: Path, debug: bool = False, namespace: Optional[str
218218
network_file_path = directory / NETWORK_FILE
219219
defaults_file_path = directory / DEFAULTS_FILE
220220

221+
namespace = get_default_namespace_or(namespace)
222+
221223
with network_file_path.open() as f:
222224
network_file = yaml.safe_load(f)
223225

224-
if not namespace:
225-
namespace = get_default_namespace()
226-
227226
for node in network_file["nodes"]:
228227
click.echo(f"Deploying node: {node.get('name')}")
229228
try:

src/warnet/k8s.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ def get_pods() -> list[V1Pod]:
5151

5252

5353
def get_pod(name: str, namespace: Optional[str] = None) -> V1Pod:
54+
namespace = get_default_namespace_or(namespace)
5455
sclient = get_static_client()
55-
if not namespace:
56-
namespace = get_default_namespace()
5756
return sclient.read_namespaced_pod(name=name, namespace=namespace)
5857

5958

@@ -67,8 +66,7 @@ def get_mission(mission: str) -> list[V1Pod]:
6766

6867

6968
def get_pod_exit_status(pod_name, namespace: Optional[str] = None):
70-
if not namespace:
71-
namespace = get_default_namespace()
69+
namespace = get_default_namespace_or(namespace)
7270
try:
7371
sclient = get_static_client()
7472
pod = sclient.read_namespaced_pod(name=pod_name, namespace=namespace)
@@ -82,8 +80,7 @@ def get_pod_exit_status(pod_name, namespace: Optional[str] = None):
8280

8381

8482
def get_edges(namespace: Optional[str] = None) -> any:
85-
if not namespace:
86-
namespace = get_default_namespace()
83+
namespace = get_default_namespace_or(namespace)
8784
sclient = get_static_client()
8885
configmap = sclient.read_namespaced_config_map(name="edges", namespace=namespace)
8986
return json.loads(configmap.data["data"])
@@ -138,8 +135,7 @@ def delete_namespace(namespace: str) -> bool:
138135

139136

140137
def delete_pod(pod_name: str, namespace: Optional[str] = None) -> bool:
141-
if not namespace:
142-
namespace = get_default_namespace()
138+
namespace = get_default_namespace_or(namespace)
143139
command = f"kubectl -n {namespace} delete pod {pod_name}"
144140
return stream_command(command)
145141

@@ -159,15 +155,18 @@ def get_default_namespace() -> str:
159155
return kubectl_namespace if kubectl_namespace else DEFAULT_NAMESPACE
160156

161157

158+
def get_default_namespace_or(namespace: Optional[str]) -> str:
159+
return namespace if namespace else get_default_namespace()
160+
161+
162162
def snapshot_bitcoin_datadir(
163163
pod_name: str,
164164
chain: str,
165165
local_path: str = "./",
166166
filters: list[str] = None,
167167
namespace: Optional[str] = None,
168168
) -> None:
169-
if not namespace:
170-
namespace = get_default_namespace()
169+
namespace = get_default_namespace_or(namespace)
171170
sclient = get_static_client()
172171

173172
try:
@@ -292,8 +291,7 @@ def wait_for_pod_ready(name, namespace, timeout=300):
292291

293292

294293
def wait_for_init(pod_name, timeout=300, namespace: Optional[str] = None):
295-
if not namespace:
296-
namespace = get_default_namespace()
294+
namespace = get_default_namespace_or(namespace)
297295
sclient = get_static_client()
298296
w = watch.Watch()
299297
for event in w.stream(
@@ -335,9 +333,9 @@ def get_ingress_ip_or_host():
335333

336334

337335
def pod_log(pod_name, container_name=None, follow=False, namespace: Optional[str] = None):
336+
namespace = get_default_namespace_or(namespace)
338337
sclient = get_static_client()
339-
if not namespace:
340-
namespace = get_default_namespace()
338+
341339
try:
342340
return sclient.read_namespaced_pod_log(
343341
name=pod_name,
@@ -351,8 +349,7 @@ def pod_log(pod_name, container_name=None, follow=False, namespace: Optional[str
351349

352350

353351
def wait_for_pod(pod_name, timeout_seconds=10, namespace: Optional[str] = None):
354-
if not namespace:
355-
namespace = get_default_namespace()
352+
namespace = get_default_namespace_or(namespace)
356353
sclient = get_static_client()
357354
while timeout_seconds > 0:
358355
pod = sclient.read_namespaced_pod_status(name=pod_name, namespace=namespace)
@@ -365,8 +362,7 @@ def wait_for_pod(pod_name, timeout_seconds=10, namespace: Optional[str] = None):
365362
def write_file_to_container(
366363
pod_name, container_name, dst_path, data, namespace: Optional[str] = None
367364
):
368-
if not namespace:
369-
namespace = get_default_namespace()
365+
namespace = get_default_namespace_or(namespace)
370366
sclient = get_static_client()
371367
exec_command = ["sh", "-c", f"cat > {dst_path}"]
372368
try:
@@ -428,3 +424,4 @@ def get_service_accounts_in_namespace(namespace):
428424
# skip the default service account created by k8s
429425
service_accounts = run_command(command).split()
430426
return [sa for sa in service_accounts if sa != "default"]
427+

0 commit comments

Comments
 (0)