Skip to content

Commit 1cf5842

Browse files
committed
namespaces: remove kubectl
This seems like it needs further testing.
1 parent 89ea5da commit 1cf5842

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

src/warnet/namespaces.py

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
NAMESPACES_DIR,
1010
NAMESPACES_FILE,
1111
)
12-
from .process import run_command, stream_command
12+
from .k8s import CoreV1Api, V1Status, get_static_client
1313

1414

1515
def copy_namespaces_defaults(directory: Path):
@@ -32,17 +32,14 @@ def namespaces():
3232
"""Namespaces commands"""
3333

3434

35-
@click.argument(
36-
"namespaces_dir", type=click.Path(exists=True, file_okay=False, dir_okay=True, path_type=Path)
37-
)
3835
@namespaces.command()
3936
def list():
4037
"""List all namespaces with 'warnet-' prefix"""
41-
cmd = "kubectl get namespaces -o jsonpath='{.items[*].metadata.name}'"
42-
res = run_command(cmd)
43-
all_namespaces = res.split()
44-
warnet_namespaces = [ns for ns in all_namespaces if ns.startswith("warnet-")]
45-
38+
sclient: CoreV1Api = get_static_client()
39+
all_namespaces = sclient.list_namespace()
40+
warnet_namespaces = [
41+
ns.metadata.name for ns in all_namespaces.items if ns.metadata.name.startswith("warnet-")
42+
]
4643
if warnet_namespaces:
4744
print("Warnet namespaces:")
4845
for ns in warnet_namespaces:
@@ -56,33 +53,31 @@ def list():
5653
@click.argument("namespace", required=False)
5754
def destroy(destroy_all: bool, namespace: str):
5855
"""Destroy a specific namespace or all warnet- prefixed namespaces"""
56+
sclient: CoreV1Api = get_static_client()
5957
if destroy_all:
60-
cmd = "kubectl get namespaces -o jsonpath='{.items[*].metadata.name}'"
61-
res = run_command(cmd)
62-
63-
# Get the list of namespaces
64-
all_namespaces = res.split()
65-
warnet_namespaces = [ns for ns in all_namespaces if ns.startswith("warnet-")]
58+
all_namespaces = sclient.list_namespace()
59+
warnet_namespaces = [ns.metadata.name for ns in all_namespaces.items
60+
if ns.metadata.name.startswith("warnet-")]
6661

6762
if not warnet_namespaces:
6863
print("No warnet namespaces found to destroy.")
6964
return
7065

7166
for ns in warnet_namespaces:
72-
destroy_cmd = f"kubectl delete namespace {ns}"
73-
if not stream_command(destroy_cmd):
74-
print(f"Failed to destroy namespace: {ns}")
67+
resp: V1Status = sclient.delete_namespace(ns)
68+
if resp.status:
69+
print(f"Destroyed namespace: {ns} with {resp.status}")
7570
else:
76-
print(f"Destroyed namespace: {ns}")
71+
print(f"Failed to destroy namespace: {ns}")
7772
elif namespace:
7873
if not namespace.startswith("warnet-"):
7974
print("Error: Can only destroy namespaces with 'warnet-' prefix")
8075
return
8176

82-
destroy_cmd = f"kubectl delete namespace {namespace}"
83-
if not stream_command(destroy_cmd):
84-
print(f"Failed to destroy namespace: {namespace}")
77+
resp: V1Status = sclient.delete_namespace(namespace)
78+
if resp.status:
79+
print(f"Destroying namespace: {namespace} with {resp.status}")
8580
else:
86-
print(f"Destroyed namespace: {namespace}")
81+
print(f"Failed to destroy namespace: {namespace}")
8782
else:
8883
print("Error: Please specify a namespace or use --all flag.")

0 commit comments

Comments
 (0)