-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
51 lines (41 loc) · 1.81 KB
/
Copy pathmain.py
File metadata and controls
51 lines (41 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
import sys
import argparse
from balancer import NodeBalancer
from kubernetes.node_manager import NodeManager
class CustomArgumentParser(argparse.ArgumentParser):
"""Custom ArgumentParser that provides more helpful error messages."""
def error(self, message):
"""Override error method to print help when an error occurs."""
sys.stderr.write(f'error: {message}\n')
self.print_help()
sys.exit(2)
def main():
"""Main entry point for the application."""
# Get available node groups for help message
node_manager = NodeManager()
available_groups = node_manager.get_available_groups()
# Create custom argument parser
parser = CustomArgumentParser(description="Balance nodes in a Kubernetes cluster")
# Add arguments
parser.add_argument("node_group", help=f"Node group to balance. Available groups: {', '.join(available_groups)}")
parser.add_argument("--threshold", "-t", type=int, default=10, help="Threshold for node balancing (default: 10)")
parser.add_argument("--dry-run", "-d", action="store_true", help="Perform a dry run without making actual changes")
try:
# Parse arguments
args = parser.parse_args()
# Validate node group
if args.node_group not in available_groups:
print(f"Error: '{args.node_group}' is not a valid node group.")
print(f"Available node groups: {', '.join(available_groups)}")
sys.exit(1)
balancer = NodeBalancer(args.dry_run)
balancer.balance_nodes(args.node_group, threshold=args.threshold)
except ValueError as e:
print(f"Error: {e}")
sys.exit(1)
except Exception as e:
print(f"Unexpected error: {e}")
sys.exit(1)
if __name__ == "__main__":
main()