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