Skip to content

Commit 1fa3073

Browse files
committed
get_default_namespace: remove kubectl
1 parent 8744ecb commit 1fa3073

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

src/warnet/k8s.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -172,18 +172,24 @@ def delete_pod(
172172

173173

174174
def get_default_namespace() -> str:
175-
command = "kubectl config view --minify -o jsonpath='{..namespace}'"
176-
try:
177-
kubectl_namespace = run_command(command)
178-
except Exception as e:
179-
print(e)
180-
if str(e).find("command not found"):
181-
print(
182-
"It looks like kubectl is not installed. Please install it to continue: "
183-
"https://kubernetes.io/docs/tasks/tools/"
184-
)
185-
sys.exit(1)
186-
return kubectl_namespace if kubectl_namespace else DEFAULT_NAMESPACE
175+
with open(KUBECONFIG) as file:
176+
kubeconfig_data = yaml.safe_load(file)
177+
178+
current_context_name = kubeconfig_data.get("current-context")
179+
if not current_context_name:
180+
raise ValueError("No current context set in kubeconfig.")
181+
182+
context_entry = None
183+
for context in kubeconfig_data.get("contexts", []):
184+
if context["name"] == current_context_name:
185+
context_entry = context
186+
break
187+
188+
if not context_entry:
189+
raise ValueError(f"Context '{current_context_name}' not found in kubeconfig.")
190+
191+
namespace = context_entry["context"]["namespace"]
192+
return namespace
187193

188194

189195
def snapshot_bitcoin_datadir(

0 commit comments

Comments
 (0)