Skip to content

Commit

Permalink
k8s: add default_namespace exception handling
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mplsgrant committed Sep 16, 2024
1 parent 390b758 commit 83b209b
Showing 1 changed file with 11 additions and 1 deletion.
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

0 comments on commit 83b209b

Please sign in to comment.