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,34 @@ 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 = [
60
+ ns . metadata . name
61
+ for ns in all_namespaces . items
62
+ if ns . metadata . name . startswith ( "warnet-" )
63
+ ]
66
64
67
65
if not warnet_namespaces :
68
66
print ("No warnet namespaces found to destroy." )
69
67
return
70
68
71
69
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 } " )
70
+ resp : V1Status = sclient . delete_namespace ( ns )
71
+ if resp . status :
72
+ print (f"Destroyed namespace: { ns } with { resp . status } " )
75
73
else :
76
- print (f"Destroyed namespace: { ns } " )
74
+ print (f"Failed to destroy namespace: { ns } " )
77
75
elif namespace :
78
76
if not namespace .startswith ("warnet-" ):
79
77
print ("Error: Can only destroy namespaces with 'warnet-' prefix" )
80
78
return
81
79
82
- destroy_cmd = f"kubectl delete namespace { namespace } "
83
- if not stream_command ( destroy_cmd ) :
84
- print (f"Failed to destroy namespace: { namespace } " )
80
+ resp : V1Status = sclient . delete_namespace ( namespace )
81
+ if resp . status :
82
+ print (f"Destroying namespace: { namespace } with { resp . status } " )
85
83
else :
86
- print (f"Destroyed namespace: { namespace } " )
84
+ print (f"Failed to destroy namespace: { namespace } " )
87
85
else :
88
86
print ("Error: Please specify a namespace or use --all flag." )
0 commit comments