|
1 | 1 | """Status command handler.""" |
2 | 2 |
|
3 | 3 | import shutil |
4 | | -import subprocess |
5 | | -from pathlib import Path |
| 4 | +from collections.abc import Callable |
6 | 5 |
|
7 | | -from kube_galaxy.pkg.utils.logging import info, print_dict, section |
| 6 | +import typer |
8 | 7 |
|
| 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 |
| 17 | +from kube_galaxy.pkg.utils.logging import error, info, print_dict, section, success, warning |
| 18 | +from kube_galaxy.pkg.utils.shell import run |
9 | 19 |
|
10 | | -def status() -> None: |
11 | | - """Display project status including dependencies and file counts.""" |
| 20 | + |
| 21 | +def status(wait: bool = False, timeout: int = 300) -> None: |
| 22 | + """Display project status and optionally verify cluster health.""" |
12 | 23 | section("Kubernetes Galaxy Test - Project Status") |
13 | 24 |
|
14 | | - # Check dependencies |
| 25 | + _print_dependency_status() |
| 26 | + _print_cluster_context() |
| 27 | + |
| 28 | + if wait: |
| 29 | + _verify_cluster_health(timeout) |
| 30 | + success("Cluster is healthy") |
| 31 | + |
| 32 | + |
| 33 | +def _print_dependency_status() -> None: |
| 34 | + """Print required command dependency status.""" |
15 | 35 | info("Dependencies:") |
16 | 36 | deps = { |
17 | | - "kubectl": check_command("kubectl"), |
18 | | - "kubeadm": check_command("kubeadm"), |
19 | | - "spread": check_command("spread"), |
| 37 | + "kubectl": _check_command("kubectl"), |
| 38 | + "spread": _check_command("spread"), |
20 | 39 | } |
21 | 40 | print_dict(deps) |
22 | 41 |
|
23 | | - # Count project files |
| 42 | + |
| 43 | +def _print_cluster_context() -> None: |
| 44 | + """Print active cluster context and current node table if available.""" |
| 45 | + if not shutil.which("kubectl"): |
| 46 | + warning("kubectl not available; skipping cluster checks") |
| 47 | + return |
| 48 | + |
24 | 49 | info("") |
25 | | - info("Project Files:") |
26 | | - file_counts = { |
27 | | - "Manifests": len(list(Path("manifests").glob("*.yaml"))) |
28 | | - if Path("manifests").exists() |
29 | | - else 0, |
30 | | - "Workflows": len( |
31 | | - list(Path(".github/workflows").glob("*.yml")) |
32 | | - + list(Path(".github/workflows").glob("*.yaml")) |
33 | | - ) |
34 | | - if Path(".github/workflows").exists() |
35 | | - else 0, |
36 | | - "Actions": len( |
37 | | - list(Path(".github/actions").glob("*/action.yml")) |
38 | | - + list(Path(".github/actions").glob("*/action.yaml")) |
39 | | - ) |
40 | | - if Path(".github/actions").exists() |
41 | | - else 0, |
42 | | - "Tests": len(list(Path("tests").glob("*.yaml")) + list(Path("tests").glob("*.yml"))) |
43 | | - if Path("tests").exists() |
44 | | - else 0, |
45 | | - } |
46 | | - print_dict(file_counts) |
| 50 | + try: |
| 51 | + context = get_context() |
| 52 | + info(f"Active Cluster: {context}") |
| 53 | + nodes_output = get_nodes() |
| 54 | + if nodes_output: |
| 55 | + lines = nodes_output.strip().split("\n") |
| 56 | + info(f"Cluster Nodes: {len(lines) - 1}") |
| 57 | + for line in lines[1:]: |
| 58 | + if line: |
| 59 | + info(f" {line}") |
| 60 | + except ClusterError: |
| 61 | + info("Active Cluster: error checking") |
47 | 62 |
|
48 | | - # Show kubeadm cluster nodes |
49 | | - if shutil.which("kubectl"): |
50 | | - info("") |
51 | | - try: |
52 | | - result = subprocess.run( |
53 | | - ["kubectl", "get", "nodes"], |
54 | | - capture_output=True, |
55 | | - text=True, |
56 | | - check=False, |
57 | | - ) |
58 | | - if result.returncode == 0 and result.stdout: |
59 | | - lines = result.stdout.strip().split("\n") |
60 | | - info(f"Cluster Nodes: {len(lines) - 1}") # Subtract header |
61 | | - for line in lines[1:]: # Skip header |
62 | | - if line: |
63 | | - info(f" {line}") |
64 | | - except Exception: |
65 | | - pass |
66 | 63 |
|
67 | | - # Show active cluster |
68 | | - if shutil.which("kubectl"): |
69 | | - info("") |
70 | | - try: |
71 | | - result = subprocess.run( |
72 | | - ["kubectl", "config", "current-context"], |
73 | | - capture_output=True, |
74 | | - text=True, |
75 | | - check=False, |
76 | | - ) |
77 | | - if result.returncode == 0: |
78 | | - context = result.stdout.strip() |
79 | | - info(f"Active Cluster: {context}") |
80 | | - else: |
81 | | - info("Active Cluster: none") |
82 | | - except Exception: |
83 | | - info("Active Cluster: error checking") |
| 64 | +def _verify_cluster_health(timeout: int) -> None: |
| 65 | + """Wait for cluster readiness and print summary tables.""" |
| 66 | + if not shutil.which("kubectl"): |
| 67 | + error("kubectl is required for --wait health checks", show_traceback=False) |
| 68 | + raise typer.Exit(code=1) |
| 69 | + |
| 70 | + section("Cluster Health Verification") |
| 71 | + info("Waiting for nodes to be Ready...") |
84 | 72 |
|
| 73 | + try: |
| 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) |
| 78 | + error("Cluster readiness checks failed", show_traceback=False) |
| 79 | + raise typer.Exit(code=1) from exc |
85 | 80 |
|
86 | | -def check_command(cmd: str) -> str: |
| 81 | + _print_command_output(get_cluster_info, "Cluster Info") |
| 82 | + _print_command_output(get_nodes, "Nodes") |
| 83 | + _print_command_output(get_pods, "Pods") |
| 84 | + |
| 85 | + |
| 86 | +def _print_command_output(command: Callable[[], str], title: str) -> None: |
| 87 | + """Run command and print its output with a section label.""" |
| 88 | + info("") |
| 89 | + info(f"{title}:") |
| 90 | + try: |
| 91 | + if output := command().strip(): |
| 92 | + info(output) |
| 93 | + except ClusterError as exc: |
| 94 | + error(f"Failed to run: {title}", show_traceback=False) |
| 95 | + raise typer.Exit(code=1) from exc |
| 96 | + |
| 97 | + |
| 98 | +def _check_command(cmd: str) -> str: |
87 | 99 | """Check if a command is installed and return status.""" |
88 | 100 | if shutil.which(cmd): |
89 | 101 | try: |
90 | 102 | if cmd == "kubectl": |
91 | | - result = subprocess.run( |
| 103 | + result = run( |
92 | 104 | [cmd, "version", "--client"], |
93 | 105 | capture_output=True, |
94 | | - text=True, |
95 | | - check=False, |
96 | | - ) |
97 | | - elif cmd == "kubeadm": |
98 | | - result = subprocess.run( |
99 | | - [cmd, "version"], |
100 | | - capture_output=True, |
101 | | - text=True, |
102 | 106 | check=False, |
103 | 107 | ) |
104 | 108 | else: |
105 | | - result = subprocess.run( |
| 109 | + result = run( |
106 | 110 | [cmd, "--version"], |
107 | 111 | capture_output=True, |
108 | | - text=True, |
109 | 112 | check=False, |
110 | 113 | ) |
111 | 114 |
|
|
0 commit comments