44
55import typer
66
7+ from kube_galaxy .pkg .utils .client import get_context , get_nodes , wait_for_nodes , wait_for_pods
8+ from kube_galaxy .pkg .utils .errors import ClusterError
79from kube_galaxy .pkg .utils .logging import error , info , print_dict , section , success , warning
8- from kube_galaxy .pkg .utils .shell import ShellError , run
10+ from kube_galaxy .pkg .utils .shell import run
911
1012
1113def status (wait : bool = False , timeout : int = 300 ) -> None :
@@ -39,21 +41,20 @@ def _print_cluster_context() -> None:
3941
4042 info ("" )
4143 try :
42- result = run (["kubectl" , "config" , "current-context" ], capture_output = True , check = False )
43- context = result .stdout .strip () if result .returncode == 0 else "none"
44+ context = get_context ()
4445 info (f"Active Cluster: { context } " )
45- except Exception :
46+ except ClusterError :
4647 info ("Active Cluster: error checking" )
4748
4849 try :
49- result = run ([ "kubectl" , "get" , "nodes" ], capture_output = True , check = False )
50- if result . returncode == 0 and result . stdout :
51- lines = result . stdout .strip ().split ("\n " )
50+ nodes_output = get_nodes ( )
51+ if nodes_output :
52+ lines = nodes_output .strip ().split ("\n " )
5253 info (f"Cluster Nodes: { len (lines ) - 1 } " )
5354 for line in lines [1 :]:
5455 if line :
5556 info (f" { line } " )
56- except Exception :
57+ except ClusterError :
5758 pass
5859
5960
@@ -63,54 +64,17 @@ def _verify_cluster_health(timeout: int) -> None:
6364 error ("kubectl is required for --wait health checks" , show_traceback = False )
6465 raise typer .Exit (code = 1 )
6566
66- timeout_arg = f"--timeout={ timeout } s"
6767 section ("Cluster Health Verification" )
6868 info ("Waiting for nodes to be Ready..." )
6969
7070 try :
71- run (
72- ["kubectl" , "wait" , "--for=condition=Ready" , "node" , "--all" , timeout_arg ],
73- capture_output = True ,
74- )
75- run (
76- [
77- "kubectl" ,
78- "wait" ,
79- "--for=condition=Ready" ,
80- "pod" ,
81- "--all" ,
82- "-n" ,
83- "kube-system" ,
84- timeout_arg ,
85- ],
86- capture_output = True ,
87- )
88- except ShellError as exc :
89- if exc .stderr .strip ():
90- error (exc .stderr .strip (), show_traceback = False )
71+ wait_for_nodes (timeout = timeout )
72+ wait_for_pods (namespace = "kube-system" , timeout = timeout )
73+ except ClusterError as exc :
74+ error (str (exc ), show_traceback = False )
9175 error ("Cluster readiness checks failed" , show_traceback = False )
9276 raise typer .Exit (code = 1 ) from exc
9377
94- _print_command_output (["kubectl" , "cluster-info" ], "Cluster Info" )
95- _print_command_output (["kubectl" , "get" , "nodes" , "-o" , "wide" ], "Nodes" )
96- _print_command_output (["kubectl" , "get" , "pods" , "-A" , "-o" , "wide" ], "Pods" )
97-
98-
99- def _print_command_output (command : list [str ], title : str ) -> None :
100- """Run command and print its output with a section label."""
101- info ("" )
102- info (f"{ title } :" )
103- try :
104- result = run (command , capture_output = True )
105- output = result .stdout .strip ()
106- if output :
107- info (output )
108- except ShellError as exc :
109- if exc .stderr .strip ():
110- error (exc .stderr .strip (), show_traceback = False )
111- error (f"Failed to run: { ' ' .join (command )} " , show_traceback = False )
112- raise typer .Exit (code = 1 ) from exc
113-
11478
11579def _check_command (cmd : str ) -> str :
11680 """Check if a command is installed and return status."""
0 commit comments