kubexec 0.2.0__tar.gz → 0.6.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {kubexec-0.2.0 → kubexec-0.6.0}/PKG-INFO +1 -1
- {kubexec-0.2.0 → kubexec-0.6.0}/kubexec/__init__.py +1 -1
- {kubexec-0.2.0 → kubexec-0.6.0}/kubexec/cli.py +6 -0
- kubexec-0.6.0/kubexec/kuberlist.py +293 -0
- {kubexec-0.2.0 → kubexec-0.6.0}/kubexec.egg-info/PKG-INFO +1 -1
- {kubexec-0.2.0 → kubexec-0.6.0}/kubexec.egg-info/SOURCES.txt +1 -0
- {kubexec-0.2.0 → kubexec-0.6.0}/kubexec.egg-info/entry_points.txt +1 -0
- {kubexec-0.2.0 → kubexec-0.6.0}/setup.py +2 -1
- {kubexec-0.2.0 → kubexec-0.6.0}/README.md +0 -0
- {kubexec-0.2.0 → kubexec-0.6.0}/kubexec/config.py +0 -0
- {kubexec-0.2.0 → kubexec-0.6.0}/kubexec/exceptions.py +0 -0
- {kubexec-0.2.0 → kubexec-0.6.0}/kubexec/executor.py +0 -0
- {kubexec-0.2.0 → kubexec-0.6.0}/kubexec/k8s_client.py +0 -0
- {kubexec-0.2.0 → kubexec-0.6.0}/kubexec/templates.py +0 -0
- {kubexec-0.2.0 → kubexec-0.6.0}/kubexec/utils.py +0 -0
- {kubexec-0.2.0 → kubexec-0.6.0}/kubexec.egg-info/dependency_links.txt +0 -0
- {kubexec-0.2.0 → kubexec-0.6.0}/kubexec.egg-info/requires.txt +0 -0
- {kubexec-0.2.0 → kubexec-0.6.0}/kubexec.egg-info/top_level.txt +0 -0
- {kubexec-0.2.0 → kubexec-0.6.0}/setup.cfg +0 -0
- {kubexec-0.2.0 → kubexec-0.6.0}/tests/__init__.py +0 -0
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
"""List pods CLI for kubexec"""
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import logging
|
|
5
|
+
import sys
|
|
6
|
+
from datetime import datetime, timezone
|
|
7
|
+
from typing import Optional
|
|
8
|
+
from rich.console import Console
|
|
9
|
+
from rich.table import Table
|
|
10
|
+
from rich.text import Text
|
|
11
|
+
from rich import box
|
|
12
|
+
from .config import Config
|
|
13
|
+
from .k8s_client import KubernetesClient
|
|
14
|
+
from .exceptions import KubeExecError
|
|
15
|
+
|
|
16
|
+
console = Console()
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def setup_logging(verbose: bool = False) -> None:
|
|
20
|
+
"""Setup logging configuration"""
|
|
21
|
+
level = logging.DEBUG if verbose else logging.INFO
|
|
22
|
+
logging.basicConfig(
|
|
23
|
+
level=level,
|
|
24
|
+
format='%(message)s'
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def create_parser() -> argparse.ArgumentParser:
|
|
29
|
+
"""Create command-line argument parser"""
|
|
30
|
+
parser = argparse.ArgumentParser(
|
|
31
|
+
prog='kuberlist',
|
|
32
|
+
description='List Kubernetes pods in current namespace',
|
|
33
|
+
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
34
|
+
epilog="""
|
|
35
|
+
Examples:
|
|
36
|
+
kuberlist # List all pods
|
|
37
|
+
kuberlist --all # List all pods with more details
|
|
38
|
+
kuberlist --running # Only running pods
|
|
39
|
+
kuberlist --kubexec # Only kubexec jobs
|
|
40
|
+
kuberlist --watch # Watch pod status changes
|
|
41
|
+
"""
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
parser.add_argument(
|
|
45
|
+
'-n', '--namespace',
|
|
46
|
+
help='Kubernetes namespace (auto-detected if not specified)'
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
parser.add_argument(
|
|
50
|
+
'--context',
|
|
51
|
+
help='Kubernetes context to use'
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
parser.add_argument(
|
|
55
|
+
'--all',
|
|
56
|
+
action='store_true',
|
|
57
|
+
help='Show all pod details including node and age'
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
parser.add_argument(
|
|
61
|
+
'--running',
|
|
62
|
+
action='store_true',
|
|
63
|
+
help='Show only running pods'
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
parser.add_argument(
|
|
67
|
+
'--kubexec',
|
|
68
|
+
action='store_true',
|
|
69
|
+
help='Show only kubexec jobs'
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
parser.add_argument(
|
|
73
|
+
'--watch',
|
|
74
|
+
action='store_true',
|
|
75
|
+
help='Watch for pod status changes (press Ctrl+C to stop)'
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
parser.add_argument(
|
|
79
|
+
'--verbose',
|
|
80
|
+
action='store_true',
|
|
81
|
+
help='Verbose output'
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
parser.add_argument(
|
|
85
|
+
'--version',
|
|
86
|
+
action='version',
|
|
87
|
+
version=f'%(prog)s {__import__("kubexec").__version__}'
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
return parser
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def format_age(created_time) -> str:
|
|
94
|
+
"""Format pod age in human readable format"""
|
|
95
|
+
if not created_time:
|
|
96
|
+
return "unknown"
|
|
97
|
+
|
|
98
|
+
now = datetime.now(timezone.utc)
|
|
99
|
+
age = now - created_time
|
|
100
|
+
|
|
101
|
+
days = age.days
|
|
102
|
+
hours, remainder = divmod(age.seconds, 3600)
|
|
103
|
+
minutes, _ = divmod(remainder, 60)
|
|
104
|
+
|
|
105
|
+
if days > 0:
|
|
106
|
+
return f"{days}d{hours}h"
|
|
107
|
+
elif hours > 0:
|
|
108
|
+
return f"{hours}h{minutes}m"
|
|
109
|
+
else:
|
|
110
|
+
return f"{minutes}m"
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def get_status_color(status: str, restart_count: int = 0) -> str:
|
|
114
|
+
"""Get color for pod status"""
|
|
115
|
+
if status == "Running":
|
|
116
|
+
return "green" if restart_count == 0 else "yellow"
|
|
117
|
+
elif status == "Pending":
|
|
118
|
+
return "yellow"
|
|
119
|
+
elif status == "Succeeded":
|
|
120
|
+
return "blue"
|
|
121
|
+
elif status in ["Failed", "Error"]:
|
|
122
|
+
return "red"
|
|
123
|
+
else:
|
|
124
|
+
return "white"
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def list_pods(
|
|
128
|
+
k8s_client: KubernetesClient,
|
|
129
|
+
namespace: str,
|
|
130
|
+
show_all: bool = False,
|
|
131
|
+
running_only: bool = False,
|
|
132
|
+
kubexec_only: bool = False
|
|
133
|
+
) -> None:
|
|
134
|
+
"""List pods in namespace"""
|
|
135
|
+
try:
|
|
136
|
+
# Set up label selector
|
|
137
|
+
label_selector = None
|
|
138
|
+
if kubexec_only:
|
|
139
|
+
label_selector = "app=kubexec"
|
|
140
|
+
|
|
141
|
+
pods = k8s_client.core_v1.list_namespaced_pod(
|
|
142
|
+
namespace=namespace,
|
|
143
|
+
label_selector=label_selector
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
if not pods.items:
|
|
147
|
+
filter_desc = ""
|
|
148
|
+
if kubexec_only:
|
|
149
|
+
filter_desc = " kubexec"
|
|
150
|
+
elif running_only:
|
|
151
|
+
filter_desc = " running"
|
|
152
|
+
console.print(f"[yellow]No{filter_desc} pods found in namespace '{namespace}'[/yellow]")
|
|
153
|
+
return
|
|
154
|
+
|
|
155
|
+
# Filter running pods if requested
|
|
156
|
+
if running_only:
|
|
157
|
+
pods.items = [pod for pod in pods.items if pod.status.phase == "Running"]
|
|
158
|
+
|
|
159
|
+
# Create Rich table
|
|
160
|
+
table = Table(title=f"Pods in namespace: [bold cyan]{namespace}[/bold cyan]", box=box.ROUNDED)
|
|
161
|
+
|
|
162
|
+
# Add columns
|
|
163
|
+
table.add_column("NAME", style="bold", no_wrap=True)
|
|
164
|
+
table.add_column("STATUS", justify="center")
|
|
165
|
+
table.add_column("RESTARTS", justify="center")
|
|
166
|
+
table.add_column("AGE", justify="center")
|
|
167
|
+
|
|
168
|
+
if show_all:
|
|
169
|
+
table.add_column("NODE", style="dim")
|
|
170
|
+
table.add_column("IMAGE", style="dim", max_width=30)
|
|
171
|
+
|
|
172
|
+
# Add rows
|
|
173
|
+
for pod in pods.items:
|
|
174
|
+
name = pod.metadata.name
|
|
175
|
+
status = pod.status.phase
|
|
176
|
+
|
|
177
|
+
# Get restart count
|
|
178
|
+
restart_count = 0
|
|
179
|
+
if pod.status.container_statuses:
|
|
180
|
+
restart_count = sum(cs.restart_count for cs in pod.status.container_statuses)
|
|
181
|
+
|
|
182
|
+
# Format age
|
|
183
|
+
age = format_age(pod.metadata.creation_timestamp)
|
|
184
|
+
|
|
185
|
+
# Color status based on state
|
|
186
|
+
status_color = get_status_color(status, restart_count)
|
|
187
|
+
status_text = Text(status, style=status_color)
|
|
188
|
+
|
|
189
|
+
# Color restart count (red if > 0)
|
|
190
|
+
restart_style = "red" if restart_count > 0 else "green"
|
|
191
|
+
|
|
192
|
+
if show_all:
|
|
193
|
+
node = pod.spec.node_name or "pending"
|
|
194
|
+
|
|
195
|
+
# Get first container image
|
|
196
|
+
image = "unknown"
|
|
197
|
+
if pod.spec.containers:
|
|
198
|
+
image = pod.spec.containers[0].image
|
|
199
|
+
# Shorten long image names
|
|
200
|
+
if len(image) > 30:
|
|
201
|
+
parts = image.split('/')
|
|
202
|
+
image = '/'.join(parts[-2:]) if len(parts) > 1 else image[:30] + "..."
|
|
203
|
+
|
|
204
|
+
table.add_row(
|
|
205
|
+
name,
|
|
206
|
+
status_text,
|
|
207
|
+
Text(str(restart_count), style=restart_style),
|
|
208
|
+
age,
|
|
209
|
+
node,
|
|
210
|
+
image
|
|
211
|
+
)
|
|
212
|
+
else:
|
|
213
|
+
table.add_row(
|
|
214
|
+
name,
|
|
215
|
+
status_text,
|
|
216
|
+
Text(str(restart_count), style=restart_style),
|
|
217
|
+
age
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
console.print(table)
|
|
221
|
+
|
|
222
|
+
except Exception as e:
|
|
223
|
+
raise KubeExecError(f"Failed to list pods: {e}")
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
def watch_pods(
|
|
227
|
+
k8s_client: KubernetesClient,
|
|
228
|
+
namespace: str,
|
|
229
|
+
kubexec_only: bool = False
|
|
230
|
+
) -> None:
|
|
231
|
+
"""Watch pod status changes"""
|
|
232
|
+
import time
|
|
233
|
+
|
|
234
|
+
console.print(f"[bold blue]Watching pods in namespace '{namespace}'[/bold blue] (press Ctrl+C to stop)")
|
|
235
|
+
console.print()
|
|
236
|
+
|
|
237
|
+
try:
|
|
238
|
+
while True:
|
|
239
|
+
# Clear screen
|
|
240
|
+
console.clear()
|
|
241
|
+
|
|
242
|
+
# Show timestamp
|
|
243
|
+
timestamp = datetime.now().strftime('%H:%M:%S')
|
|
244
|
+
console.print(f"[dim]Last updated: {timestamp}[/dim]")
|
|
245
|
+
console.print()
|
|
246
|
+
|
|
247
|
+
list_pods(k8s_client, namespace, show_all=True, kubexec_only=kubexec_only)
|
|
248
|
+
|
|
249
|
+
time.sleep(5)
|
|
250
|
+
|
|
251
|
+
except KeyboardInterrupt:
|
|
252
|
+
console.print("\n[yellow]Watch stopped.[/yellow]")
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
def main() -> int:
|
|
256
|
+
"""Main CLI entry point"""
|
|
257
|
+
parser = create_parser()
|
|
258
|
+
args = parser.parse_args()
|
|
259
|
+
|
|
260
|
+
# Setup logging
|
|
261
|
+
setup_logging(args.verbose)
|
|
262
|
+
|
|
263
|
+
try:
|
|
264
|
+
# Load configuration to get namespace
|
|
265
|
+
config = Config()
|
|
266
|
+
namespace = args.namespace or config.get('namespace')
|
|
267
|
+
|
|
268
|
+
# Initialize Kubernetes client
|
|
269
|
+
k8s_client = KubernetesClient(args.context)
|
|
270
|
+
|
|
271
|
+
if args.watch:
|
|
272
|
+
watch_pods(k8s_client, namespace, args.kubexec)
|
|
273
|
+
else:
|
|
274
|
+
list_pods(k8s_client, namespace, args.all, args.running, args.kubexec)
|
|
275
|
+
|
|
276
|
+
return 0
|
|
277
|
+
|
|
278
|
+
except KubeExecError as e:
|
|
279
|
+
logging.error(f"kuberlist error: {e}")
|
|
280
|
+
return 1
|
|
281
|
+
except KeyboardInterrupt:
|
|
282
|
+
logging.info("Interrupted by user")
|
|
283
|
+
return 130
|
|
284
|
+
except Exception as e:
|
|
285
|
+
logging.error(f"Unexpected error: {e}")
|
|
286
|
+
if args.verbose:
|
|
287
|
+
import traceback
|
|
288
|
+
traceback.print_exc()
|
|
289
|
+
return 1
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
if __name__ == '__main__':
|
|
293
|
+
sys.exit(main())
|
|
@@ -20,7 +20,7 @@ except FileNotFoundError:
|
|
|
20
20
|
|
|
21
21
|
setup(
|
|
22
22
|
name="kubexec",
|
|
23
|
-
version="0.
|
|
23
|
+
version="0.6.0",
|
|
24
24
|
author="kubexec team",
|
|
25
25
|
description="Execute commands and scripts on Kubernetes pods with Docker access",
|
|
26
26
|
long_description=long_description,
|
|
@@ -42,6 +42,7 @@ setup(
|
|
|
42
42
|
entry_points={
|
|
43
43
|
"console_scripts": [
|
|
44
44
|
"kubexec=kubexec.cli:main",
|
|
45
|
+
"kuberlist=kubexec.kuberlist:main",
|
|
45
46
|
],
|
|
46
47
|
},
|
|
47
48
|
)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|