Skip to content

Commit a5b1637

Browse files
committed
k8s: add open and write fns for KUBECONFIG
1 parent a2d172d commit a5b1637

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/warnet/k8s.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,3 +417,24 @@ def wait_for_pod(pod_name, timeout_seconds=10):
417417
return
418418
sleep(1)
419419
timeout_seconds -= 1
420+
421+
422+
def open_kubeconfig(kubeconfig_path: str = KUBECONFIG) -> dict:
423+
try:
424+
with open(kubeconfig_path) as file:
425+
return yaml.safe_load(file)
426+
except FileNotFoundError as e:
427+
raise K8sError(f"Kubeconfig file {kubeconfig_path} not found.") from e
428+
except yaml.YAMLError as e:
429+
raise K8sError(f"Error parsing kubeconfig: {e}") from e
430+
431+
432+
def write_kubeconfig(kube_config: dict) -> None:
433+
dir_name = os.path.dirname(KUBECONFIG)
434+
try:
435+
with tempfile.NamedTemporaryFile("w", dir=dir_name, delete=False) as temp_file:
436+
yaml.safe_dump(kube_config, temp_file)
437+
os.replace(temp_file.name, KUBECONFIG)
438+
except Exception as e:
439+
os.remove(temp_file.name)
440+
raise K8sError(f"Error writing kubeconfig: {KUBECONFIG}") from e

0 commit comments

Comments
 (0)