Skip to content

Commit e2fea79

Browse files
committed
get_default_namespace: remove kubectl
1 parent 5c3d516 commit e2fea79

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
@@ -178,18 +178,24 @@ def delete_pod(
178178

179179

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

194200

195201
def snapshot_bitcoin_datadir(

0 commit comments

Comments
 (0)