|
21 | 21 | get_mission,
|
22 | 22 | get_pods,
|
23 | 23 | snapshot_bitcoin_datadir,
|
| 24 | + pod_log |
24 | 25 | )
|
25 | 26 | from .process import run_command, stream_command
|
26 | 27 |
|
@@ -235,46 +236,39 @@ def run(scenario_file: str, additional_args: tuple[str]):
|
235 | 236 | @click.option("--follow", "-f", is_flag=True, default=False, help="Follow logs")
|
236 | 237 | def logs(pod_name: str, follow: bool):
|
237 | 238 | """Show the logs of a pod"""
|
238 |
| - follow_flag = "--follow" if follow else "" |
239 | 239 | namespace = get_default_namespace()
|
240 | 240 |
|
241 |
| - if pod_name: |
| 241 | + if pod_name == "": |
242 | 242 | try:
|
243 |
| - command = f"kubectl logs pod/{pod_name} -n {namespace} {follow_flag}" |
244 |
| - stream_command(command) |
245 |
| - return |
| 243 | + pods = get_pods() |
| 244 | + pod_list = [item.metadata.name for item in pods.items] |
246 | 245 | except Exception as e:
|
247 |
| - print(f"Could not find the pod {pod_name}: {e}") |
| 246 | + print(f"Could not fetch any pods in namespace {namespace}: {e}") |
| 247 | + return |
248 | 248 |
|
249 |
| - try: |
250 |
| - pods = run_command(f"kubectl get pods -n {namespace} -o json") |
251 |
| - pods = json.loads(pods) |
252 |
| - pod_list = [item["metadata"]["name"] for item in pods["items"]] |
253 |
| - except Exception as e: |
254 |
| - print(f"Could not fetch any pods in namespace {namespace}: {e}") |
255 |
| - return |
| 249 | + if not pod_list: |
| 250 | + print(f"Could not fetch any pods in namespace {namespace}") |
| 251 | + return |
256 | 252 |
|
257 |
| - if not pod_list: |
258 |
| - print(f"Could not fetch any pods in namespace {namespace}") |
259 |
| - return |
| 253 | + q = [ |
| 254 | + inquirer.List( |
| 255 | + name="pod", |
| 256 | + message="Please choose a pod", |
| 257 | + choices=pod_list, |
| 258 | + ) |
| 259 | + ] |
| 260 | + selected = inquirer.prompt(q, theme=GreenPassion()) |
| 261 | + if selected: |
| 262 | + pod_name = selected["pod"] |
| 263 | + else: |
| 264 | + return # cancelled by user |
260 | 265 |
|
261 |
| - q = [ |
262 |
| - inquirer.List( |
263 |
| - name="pod", |
264 |
| - message="Please choose a pod", |
265 |
| - choices=pod_list, |
266 |
| - ) |
267 |
| - ] |
268 |
| - selected = inquirer.prompt(q, theme=GreenPassion()) |
269 |
| - if selected: |
270 |
| - pod_name = selected["pod"] |
271 |
| - try: |
272 |
| - command = f"kubectl logs pod/{pod_name} -n {namespace} {follow_flag}" |
273 |
| - stream_command(command) |
274 |
| - except Exception as e: |
275 |
| - print(f"Please consider waiting for the pod to become available. Encountered: {e}") |
276 |
| - else: |
277 |
| - pass # cancelled by user |
| 266 | + try: |
| 267 | + stream = pod_log(pod_name, container_name=None, follow=follow) |
| 268 | + for line in stream.stream(): |
| 269 | + print(line.decode('utf-8'), end=None) |
| 270 | + except Exception as e: |
| 271 | + print(e) |
278 | 272 |
|
279 | 273 |
|
280 | 274 | @click.command()
|
|
0 commit comments