Skip to content

Commit 18c88f2

Browse files
pinheadmzwillcl-ark
authored andcommitted
k8s utils
1 parent 8998a86 commit 18c88f2

File tree

2 files changed

+106
-38
lines changed

2 files changed

+106
-38
lines changed

src/warnet/cli/k8s.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import os
2+
import subprocess
3+
4+
from importlib.resources import files
5+
6+
from kubernetes import client, config
7+
from kubernetes.dynamic import DynamicClient
8+
9+
10+
WAR_MANIFESTS = files("manifests")
11+
12+
def get_static_client():
13+
config.load_kube_config()
14+
return client.CoreV1Api()
15+
16+
def get_dynamic_client():
17+
config.load_kube_config()
18+
return DynamicClient(client.ApiClient())
19+
20+
def get_pods():
21+
sclient = get_static_client()
22+
return sclient.list_namespaced_pod("warnet")
23+
24+
def run_command(command, stream_output=False, env=None):
25+
# Merge the current environment with the provided env
26+
full_env = os.environ.copy()
27+
if env:
28+
# Convert all env values to strings (only a safeguard)
29+
env = {k: str(v) for k, v in env.items()}
30+
full_env.update(env)
31+
32+
if stream_output:
33+
process = subprocess.Popen(
34+
["/bin/bash", "-c", command],
35+
stdout=subprocess.PIPE,
36+
stderr=subprocess.STDOUT,
37+
text=True,
38+
bufsize=1,
39+
universal_newlines=True,
40+
env=full_env,
41+
)
42+
43+
for line in iter(process.stdout.readline, ""):
44+
print(line, end="")
45+
46+
process.stdout.close()
47+
return_code = process.wait()
48+
49+
if return_code != 0:
50+
print(f"Command failed with return code {return_code}")
51+
return False
52+
return True
53+
else:
54+
result = subprocess.run(
55+
command, shell=True, capture_output=True, text=True, executable="/bin/bash"
56+
)
57+
if result.returncode != 0:
58+
print(f"Error: {result.stderr}")
59+
return False
60+
print(result.stdout)
61+
return True
62+
63+
64+
def set_kubectl_context(namespace: str):
65+
"""
66+
Set the default kubectl context to the specified namespace.
67+
"""
68+
command = f"kubectl config set-context --current --namespace={namespace}"
69+
result = run_command(command, stream_output=True)
70+
if result:
71+
print(f"Kubectl context set to namespace: {namespace}")
72+
else:
73+
print(f"Failed to set kubectl context to namespace: {namespace}")
74+
return result
75+
76+
77+
def deploy_base_configurations():
78+
base_configs = [
79+
"namespace.yaml",
80+
"rbac-config.yaml",
81+
]
82+
83+
for config in base_configs:
84+
command = f"kubectl apply -f {WAR_MANIFESTS}/{config}"
85+
if not run_command(command, stream_output=True):
86+
print(f"Failed to apply {config}")
87+
return False
88+
return True
89+
90+
91+
def apply_kubernetes_yaml(yaml_file: str):
92+
command = f"kubectl apply -f {yaml_file}"
93+
return run_command(command, stream_output=True)
94+
95+
96+
def delete_namespace(namespace: str):
97+
command = f"kubectl delete namespace {namespace}"
98+
return run_command(command, stream_output=True)
99+

src/warnet/cli/network.py

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@
88
import yaml
99
from rich import print
1010

11-
from .util import run_command
11+
from .k8s import (
12+
run_command,
13+
set_kubectl_context,
14+
deploy_base_configurations,
15+
apply_kubernetes_yaml,
16+
delete_namespace
17+
)
1218

1319
DEFAULT_GRAPH_FILE = files("graphs").joinpath("default.graphml")
1420
WAR_MANIFESTS = files("manifests")
@@ -229,43 +235,6 @@ def generate_node_config(node: int, data: dict) -> str:
229235
return f"{base_config}\n{node_specific_config}"
230236

231237

232-
def set_kubectl_context(namespace: str):
233-
"""
234-
Set the default kubectl context to the specified namespace.
235-
"""
236-
command = f"kubectl config set-context --current --namespace={namespace}"
237-
result = run_command(command, stream_output=True)
238-
if result:
239-
print(f"Kubectl context set to namespace: {namespace}")
240-
else:
241-
print(f"Failed to set kubectl context to namespace: {namespace}")
242-
return result
243-
244-
245-
def deploy_base_configurations():
246-
base_configs = [
247-
"namespace.yaml",
248-
"rbac-config.yaml",
249-
]
250-
251-
for config in base_configs:
252-
command = f"kubectl apply -f {WAR_MANIFESTS}/{config}"
253-
if not run_command(command, stream_output=True):
254-
print(f"Failed to apply {config}")
255-
return False
256-
return True
257-
258-
259-
def apply_kubernetes_yaml(yaml_file: str):
260-
command = f"kubectl apply -f {yaml_file}"
261-
return run_command(command, stream_output=True)
262-
263-
264-
def delete_namespace(namespace: str):
265-
command = f"kubectl delete namespace {namespace}"
266-
return run_command(command, stream_output=True)
267-
268-
269238
def setup_logging_helm():
270239
"""
271240
Run the required Helm commands for setting up Grafana, Prometheus, and Loki.

0 commit comments

Comments
 (0)