Skip to content

Commit 83b209b

Browse files
committed
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.
1 parent 390b758 commit 83b209b

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/warnet/k8s.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import os
3+
import sys
34
import tempfile
45
from pathlib import Path
56

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

116117
def get_default_namespace() -> str:
117118
command = "kubectl config view --minify -o jsonpath='{..namespace}'"
118-
kubectl_namespace = run_command(command)
119+
try:
120+
kubectl_namespace = run_command(command)
121+
except Exception as e:
122+
print(e)
123+
if str(e).find("command not found"):
124+
print(
125+
"It looks like kubectl is not installed. Please install it to continue: "
126+
"https://kubernetes.io/docs/tasks/tools/"
127+
)
128+
sys.exit(1)
119129
return kubectl_namespace if kubectl_namespace else DEFAULT_NAMESPACE
120130

121131

0 commit comments

Comments
 (0)