|
| 1 | +import json |
1 | 2 | import os
|
2 | 3 | import random
|
3 | 4 | import subprocess
|
|
6 | 7 | from pathlib import Path
|
7 | 8 |
|
8 | 9 | import click
|
| 10 | +import inquirer |
9 | 11 | import yaml
|
| 12 | +from inquirer.themes import GreenPassion |
| 13 | + |
| 14 | +from warnet.k8s import get_default_namespace |
| 15 | +from warnet.process import run_command, stream_command |
10 | 16 |
|
11 | 17 | from .admin import admin
|
12 | 18 | from .bitcoin import bitcoin
|
@@ -225,6 +231,53 @@ def auth(kube_config: str) -> None:
|
225 | 231 | )
|
226 | 232 |
|
227 | 233 |
|
| 234 | +@cli.command() |
| 235 | +@click.argument("pod_name", type=str, default="") |
| 236 | +@click.option("--follow", "-f", is_flag=True, default=False, help="Follow logs") |
| 237 | +def logs(pod_name: str, follow: bool): |
| 238 | + """Show the logs of a pod""" |
| 239 | + follow_flag = "--follow" if follow else "" |
| 240 | + namespace = get_default_namespace() |
| 241 | + |
| 242 | + if pod_name: |
| 243 | + try: |
| 244 | + command = f"kubectl logs pod/{pod_name} -n {namespace} {follow_flag}" |
| 245 | + stream_command(command) |
| 246 | + return |
| 247 | + except Exception as e: |
| 248 | + print(f"Could not find the pod {pod_name}: {e}") |
| 249 | + |
| 250 | + try: |
| 251 | + pods = run_command(f"kubectl get pods -n {namespace} -o json") |
| 252 | + pods = json.loads(pods) |
| 253 | + pod_list = [item["metadata"]["name"] for item in pods["items"]] |
| 254 | + except Exception as e: |
| 255 | + print(f"Could not fetch any pods in namespace {namespace}: {e}") |
| 256 | + return |
| 257 | + |
| 258 | + if not pod_list: |
| 259 | + print(f"Could not fetch any pods in namespace {namespace}") |
| 260 | + return |
| 261 | + |
| 262 | + q = [ |
| 263 | + inquirer.List( |
| 264 | + name="pod", |
| 265 | + message="Please choose a pod", |
| 266 | + choices=pod_list, |
| 267 | + ) |
| 268 | + ] |
| 269 | + selected = inquirer.prompt(q, theme=GreenPassion()) |
| 270 | + if selected: |
| 271 | + pod_name = selected["pod"] |
| 272 | + try: |
| 273 | + command = f"kubectl logs pod/{pod_name} -n {namespace} {follow_flag}" |
| 274 | + stream_command(command) |
| 275 | + except Exception as e: |
| 276 | + print(f"Please consider waiting for the pod to become available. Encountered: {e}") |
| 277 | + else: |
| 278 | + pass # cancelled by user |
| 279 | + |
| 280 | + |
228 | 281 | if __name__ == "__main__":
|
229 | 282 | cli()
|
230 | 283 |
|
|
0 commit comments