Skip to content

Commit b943c81

Browse files
committed
k8s: add open and write fns for KUBECONFIG
1 parent 333e277 commit b943c81

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/warnet/k8s.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,3 +460,23 @@ def write_file_to_container(pod_name, container_name, dst_path, data):
460460
return True
461461
except Exception as e:
462462
print(f"Failed to copy data to {pod_name}({container_name}):{dst_path}:\n{e}")
463+
464+
def open_kubeconfig(kubeconfig_path: str = KUBECONFIG) -> dict:
465+
try:
466+
with open(kubeconfig_path) as file:
467+
return yaml.safe_load(file)
468+
except FileNotFoundError as e:
469+
raise K8sError(f"Kubeconfig file {kubeconfig_path} not found.") from e
470+
except yaml.YAMLError as e:
471+
raise K8sError(f"Error parsing kubeconfig: {e}") from e
472+
473+
474+
def write_kubeconfig(kube_config: dict) -> None:
475+
dir_name = os.path.dirname(KUBECONFIG)
476+
try:
477+
with tempfile.NamedTemporaryFile("w", dir=dir_name, delete=False) as temp_file:
478+
yaml.safe_dump(kube_config, temp_file)
479+
os.replace(temp_file.name, KUBECONFIG)
480+
except Exception as e:
481+
os.remove(temp_file.name)
482+
raise K8sError(f"Error writing kubeconfig: {KUBECONFIG}") from e

0 commit comments

Comments
 (0)