11"""Status command handler."""
22
33import shutil
4+ from collections .abc import Callable
45
56import typer
67
8+ from kube_galaxy .pkg .utils .client import (
9+ get_cluster_info ,
10+ get_context ,
11+ get_nodes ,
12+ get_pods ,
13+ wait_for_nodes ,
14+ wait_for_pods ,
15+ )
16+ from kube_galaxy .pkg .utils .errors import ClusterError
717from kube_galaxy .pkg .utils .logging import error , info , print_dict , section , success , warning
8- from kube_galaxy .pkg .utils .shell import ShellError , run
18+ from kube_galaxy .pkg .utils .shell import run
919
1020
1121def status (wait : bool = False , timeout : int = 300 ) -> None :
@@ -25,7 +35,6 @@ def _print_dependency_status() -> None:
2535 info ("Dependencies:" )
2636 deps = {
2737 "kubectl" : _check_command ("kubectl" ),
28- "kubeadm" : _check_command ("kubeadm" ),
2938 "spread" : _check_command ("spread" ),
3039 }
3140 print_dict (deps )
@@ -39,22 +48,17 @@ def _print_cluster_context() -> None:
3948
4049 info ("" )
4150 try :
42- result = run (["kubectl" , "config" , "current-context" ], capture_output = True , check = False )
43- context = result .stdout .strip () if result .returncode == 0 else "none"
51+ context = get_context ()
4452 info (f"Active Cluster: { context } " )
45- except Exception :
46- info ("Active Cluster: error checking" )
47-
48- 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 " )
53+ nodes_output = get_nodes ()
54+ if nodes_output :
55+ lines = nodes_output .strip ().split ("\n " )
5256 info (f"Cluster Nodes: { len (lines ) - 1 } " )
5357 for line in lines [1 :]:
5458 if line :
5559 info (f" { line } " )
56- except Exception :
57- pass
60+ except ClusterError :
61+ info ( "Active Cluster: error checking" )
5862
5963
6064def _verify_cluster_health (timeout : int ) -> None :
@@ -63,52 +67,31 @@ def _verify_cluster_health(timeout: int) -> None:
6367 error ("kubectl is required for --wait health checks" , show_traceback = False )
6468 raise typer .Exit (code = 1 )
6569
66- timeout_arg = f"--timeout={ timeout } s"
6770 section ("Cluster Health Verification" )
6871 info ("Waiting for nodes to be Ready..." )
6972
7073 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 )
74+ wait_for_nodes (timeout = timeout )
75+ wait_for_pods (namespace = "kube-system" , timeout = timeout )
76+ except ClusterError as exc :
77+ error (str (exc ), show_traceback = False )
9178 error ("Cluster readiness checks failed" , show_traceback = False )
9279 raise typer .Exit (code = 1 ) from exc
9380
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" )
81+ _print_command_output (get_cluster_info , "Cluster Info" )
82+ _print_command_output (get_nodes , "Nodes" )
83+ _print_command_output (get_pods , "Pods" )
9784
9885
99- def _print_command_output (command : list [ str ], title : str ) -> None :
86+ def _print_command_output (command : Callable [[], str ], title : str ) -> None :
10087 """Run command and print its output with a section label."""
10188 info ("" )
10289 info (f"{ title } :" )
10390 try :
104- result = run (command , capture_output = True )
105- output = result .stdout .strip ()
106- if output :
91+ if output := command ().strip ():
10792 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 )
93+ except ClusterError as exc :
94+ error (f"Failed to run: { title } " , show_traceback = False )
11295 raise typer .Exit (code = 1 ) from exc
11396
11497
@@ -122,12 +105,6 @@ def _check_command(cmd: str) -> str:
122105 capture_output = True ,
123106 check = False ,
124107 )
125- elif cmd == "kubeadm" :
126- result = run (
127- [cmd , "version" ],
128- capture_output = True ,
129- check = False ,
130- )
131108 else :
132109 result = run (
133110 [cmd , "--version" ],
0 commit comments