9
9
NAMESPACES_DIR ,
10
10
NAMESPACES_FILE ,
11
11
)
12
- from .process import run_command , stream_command
12
+ from .k8s import CoreV1Api , V1Status , get_static_client
13
13
14
14
15
15
def copy_namespaces_defaults (directory : Path ):
@@ -32,17 +32,14 @@ def namespaces():
32
32
"""Namespaces commands"""
33
33
34
34
35
- @click .argument (
36
- "namespaces_dir" , type = click .Path (exists = True , file_okay = False , dir_okay = True , path_type = Path )
37
- )
38
35
@namespaces .command ()
39
36
def list ():
40
37
"""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
+ ]
46
43
if warnet_namespaces :
47
44
print ("Warnet namespaces:" )
48
45
for ns in warnet_namespaces :
@@ -56,33 +53,31 @@ def list():
56
53
@click .argument ("namespace" , required = False )
57
54
def destroy (destroy_all : bool , namespace : str ):
58
55
"""Destroy a specific namespace or all warnet- prefixed namespaces"""
56
+ sclient : CoreV1Api = get_static_client ()
59
57
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-" )]
66
61
67
62
if not warnet_namespaces :
68
63
print ("No warnet namespaces found to destroy." )
69
64
return
70
65
71
66
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 } " )
75
70
else :
76
- print (f"Destroyed namespace: { ns } " )
71
+ print (f"Failed to destroy namespace: { ns } " )
77
72
elif namespace :
78
73
if not namespace .startswith ("warnet-" ):
79
74
print ("Error: Can only destroy namespaces with 'warnet-' prefix" )
80
75
return
81
76
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 } " )
85
80
else :
86
- print (f"Destroyed namespace: { namespace } " )
81
+ print (f"Failed to destroy namespace: { namespace } " )
87
82
else :
88
83
print ("Error: Please specify a namespace or use --all flag." )
0 commit comments