From 390b75817122d84b06bf82e0aff0b082f157c8ec Mon Sep 17 00:00:00 2001 From: Grant Date: Mon, 16 Sep 2024 13:52:53 -0500 Subject: [PATCH 1/2] status: add exception handling Adds exception handling around misconfigured kubeconfig files and also bad network connections. --- src/warnet/status.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/warnet/status.py b/src/warnet/status.py index 1093fe7df..ebbd245d4 100644 --- a/src/warnet/status.py +++ b/src/warnet/status.py @@ -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 @@ -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") From 83b209b9ea45c83fdbb9aeec650f38245b53905c Mon Sep 17 00:00:00 2001 From: Grant Date: Mon, 16 Sep 2024 13:53:43 -0500 Subject: [PATCH 2/2] k8s: add default_namespace exception handling Adds exception handling around the get_default_namespace function, ensuring that if the kubectl invocation fails, we get some kind of error message especially with instruction to install kubectl if for some reason the user has not installed it. --- src/warnet/k8s.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/warnet/k8s.py b/src/warnet/k8s.py index 62a918320..cacf8f65a 100644 --- a/src/warnet/k8s.py +++ b/src/warnet/k8s.py @@ -1,5 +1,6 @@ import json import os +import sys import tempfile from pathlib import Path @@ -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