Skip to content

Commit

Permalink
Merge pull request #604 from mplsgrant/2024-09-status-exception-handling
Browse files Browse the repository at this point in the history
Add `warnet status` exception handling
  • Loading branch information
pinheadmz authored Sep 17, 2024
2 parents e0773da + 83b209b commit 917ee4a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/warnet/k8s.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import os
import sys
import tempfile
from pathlib import Path

Expand Down Expand Up @@ -115,7 +116,16 @@ def delete_pod(pod_name: str) -> bool:

def get_default_namespace() -> str:
command = "kubectl config view --minify -o jsonpath='{..namespace}'"
kubectl_namespace = run_command(command)
try:
kubectl_namespace = run_command(command)
except Exception as e:
print(e)
if str(e).find("command not found"):
print(
"It looks like kubectl is not installed. Please install it to continue: "
"https://kubernetes.io/docs/tasks/tools/"
)
sys.exit(1)
return kubectl_namespace if kubectl_namespace else DEFAULT_NAMESPACE


Expand Down
28 changes: 26 additions & 2 deletions src/warnet/status.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import sys

import click
from kubernetes.config.config_exception import ConfigException
from rich.console import Console
from rich.panel import Panel
from rich.table import Table
from rich.text import Text
from urllib3.exceptions import MaxRetryError

from .k8s import get_mission
from .network import _connected
Expand All @@ -13,8 +17,28 @@ def status():
"""Display the unified status of the Warnet network and active scenarios"""
console = Console()

tanks = _get_tank_status()
scenarios = _get_deployed_scenarios()
try:
tanks = _get_tank_status()
scenarios = _get_deployed_scenarios()
except ConfigException as e:
print(e)
print(
"The kubeconfig file has not been properly set. This may mean that you need to "
"authorize with a cluster such as by starting minikube, starting docker-desktop, or "
"authorizing with a configuration file provided by a cluster administrator."
)
sys.exit(1)
except MaxRetryError as e:
print(e)
print(
"Warnet cannot get the status of a Warnet network. To resolve this, you may need to "
"confirm you have access to a Warnet cluster. Start by checking your network "
"connection. Then, if running a local cluster, check that minikube or docker-desktop "
"is running properly. If you are trying to connect to a remote cluster, check that "
"the relevant authorization file has been configured properly as instructed by a "
"cluster administrator."
)
sys.exit(1)

# Create a unified table
table = Table(title="Warnet Status", show_header=True, header_style="bold magenta")
Expand Down

0 comments on commit 917ee4a

Please sign in to comment.