Skip to content

Commit 2022c97

Browse files
committed
add caddy readiness check and auto-port-forward
1 parent f2ca32b commit 2022c97

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed

resources/manifests/caddy/caddy-configmap.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ data:
88
debug
99
}
1010
:80 {
11+
respond /live 200
12+
respond /ready 200
13+
1114
root * /usr/share/caddy
1215
file_server
1316

resources/manifests/caddy/caddy-deployment.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ spec:
1717
image: caddy:2.8.4
1818
ports:
1919
- containerPort: 80
20+
readinessProbe:
21+
httpGet:
22+
path: /ready
23+
port: 80
24+
initialDelaySeconds: 10
25+
periodSeconds: 5
26+
livenessProbe:
27+
httpGet:
28+
path: /live
29+
port: 80
30+
initialDelaySeconds: 15
31+
periodSeconds: 10
2032
volumeMounts:
2133
- name: caddy-config
2234
mountPath: /etc/caddy/Caddyfile

src/warnet/control.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from rich.prompt import Confirm, Prompt
1111
from rich.table import Table
1212

13+
from .deploy import _port_stop_internal
1314
from .k8s import (
1415
apply_kubernetes_yaml,
1516
delete_all_resources,
@@ -118,6 +119,7 @@ def down():
118119
console.print("[green]Warnet logging cleaned up[/green]")
119120
delete_all_resources()
120121
console.print("[bold green]Warnet has been brought down.[/bold green]")
122+
_port_stop_internal()
121123

122124

123125
def get_active_network(namespace):

src/warnet/deploy.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import os
2+
import subprocess
3+
import sys
14
import tempfile
25
from pathlib import Path
36

@@ -63,6 +66,7 @@ def deploy(directory, debug):
6366
click.echo(
6467
"Error: Neither network.yaml nor namespaces.yaml found in the specified directory."
6568
)
69+
_port_start_internal()
6670

6771

6872
def deploy_fork_observer(directory: Path, debug: bool):
@@ -198,3 +202,43 @@ def deploy_namespaces(directory: Path):
198202
finally:
199203
if temp_override_file_path.exists():
200204
temp_override_file_path.unlink()
205+
206+
207+
def is_windows():
208+
return sys.platform.startswith("win")
209+
210+
211+
def run_detached_process(command):
212+
if is_windows():
213+
# For Windows, use CREATE_NEW_PROCESS_GROUP and DETACHED_PROCESS
214+
subprocess.Popen(
215+
command,
216+
shell=True,
217+
stdin=None,
218+
stdout=None,
219+
stderr=None,
220+
close_fds=True,
221+
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP | subprocess.DETACHED_PROCESS,
222+
)
223+
else:
224+
# For Unix-like systems, use nohup and redirect output
225+
command = f"nohup {command} > /dev/null 2>&1 &"
226+
subprocess.Popen(command, shell=True, stdin=None, stdout=None, stderr=None, close_fds=True)
227+
228+
print(f"Started detached process: {command}")
229+
230+
231+
def _port_start_internal():
232+
command = "kubectl port-forward service/caddy-service 2019:80"
233+
run_detached_process(command)
234+
print(
235+
"Port forwarding on port 2019 started in the background. Use 'warcli' (or 'kubectl') to manage the warnet."
236+
)
237+
238+
239+
def _port_stop_internal():
240+
if is_windows():
241+
os.system("taskkill /F /IM kubectl.exe")
242+
else:
243+
os.system("pkill -f 'kubectl port-forward service/caddy-service 2019:80'")
244+
print("Port forwarding stopped.")

src/warnet/k8s.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import tempfile
3+
import time
34
from pathlib import Path
45

56
import yaml
@@ -233,6 +234,7 @@ def deploy_caddy(directory: Path):
233234
elif kind == "Deployment":
234235
apps_v1.create_namespaced_deployment(namespace, doc)
235236
print(f"Created Deployment: {name}")
237+
wait_for_caddy_ready(name, namespace)
236238
elif kind == "Service":
237239
core_v1.create_namespaced_service(namespace, doc)
238240
print(f"Created Service: {name}")
@@ -250,6 +252,7 @@ def deploy_caddy(directory: Path):
250252
core_v1.replace_namespaced_config_map(name, namespace, doc)
251253
elif kind == "Deployment":
252254
apps_v1.replace_namespaced_deployment(name, namespace, doc)
255+
wait_for_caddy_ready(name, namespace)
253256
elif kind == "Service":
254257
core_v1.replace_namespaced_service(name, namespace, doc)
255258
else:
@@ -262,3 +265,23 @@ def deploy_caddy(directory: Path):
262265
print(f"Error creating/updating {kind} {name}: {e}")
263266

264267
print("Caddy deployment completed.")
268+
269+
270+
def wait_for_caddy_ready(name: str, namespace: str, timeout: int = 300):
271+
apps_v1 = client.AppsV1Api()
272+
start_time = time.time()
273+
274+
while True:
275+
try:
276+
deployment = apps_v1.read_namespaced_deployment(name, namespace)
277+
if deployment.status.ready_replicas == deployment.spec.replicas:
278+
print(f"Caddy deployment {name} is ready")
279+
return True
280+
except ApiException as e:
281+
print(f"Error checking Caddy deployment {name}: {e}")
282+
283+
if time.time() - start_time > timeout:
284+
print(f"Timeout waiting for Caddy deployment {name} to be ready")
285+
return False
286+
287+
time.sleep(0.1)

0 commit comments

Comments
 (0)