Skip to content

Commit 494e0ba

Browse files
committed
kubectl_context: switch up this functional
This function is not used as far as I can tell, and it is not tested. Perhaps consider removing or integrating somehow into the namespace work if needed.
1 parent ddc6d4f commit 494e0ba

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

src/warnet/k8s.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from .constants import DEFAULT_NAMESPACE, KUBECONFIG
1717
from .process import run_command, stream_command
18+
from .constants import KUBECONFIG
1819

1920

2021
def get_static_client() -> CoreV1Api:
@@ -94,17 +95,30 @@ def create_kubernetes_object(
9495
return obj
9596

9697

97-
def set_kubectl_context(namespace: str) -> bool:
98+
def set_context_namespace(namespace: str):
9899
"""
99-
Set the default kubectl context to the specified namespace.
100+
Set the default kubeconfig context to the specified namespace.
100101
"""
101-
command = f"kubectl config set-context --current --namespace={namespace}"
102-
result = stream_command(command)
103-
if result:
104-
print(f"Kubectl context set to namespace: {namespace}")
105-
else:
106-
print(f"Failed to set kubectl context to namespace: {namespace}")
107-
return result
102+
with open(KUBECONFIG) as file:
103+
kubeconfig_data = yaml.safe_load(file)
104+
105+
current_context_name = kubeconfig_data.get("current-context")
106+
if not current_context_name:
107+
raise ValueError("No current context set in kubeconfig.")
108+
109+
context_entry = None
110+
for context in kubeconfig_data.get("contexts", []):
111+
if context["name"] == current_context_name:
112+
context_entry = context
113+
break
114+
115+
if not context_entry:
116+
raise ValueError(f"Context '{current_context_name}' not found in kubeconfig.")
117+
118+
context_entry["context"]["namespace"] = namespace
119+
120+
with open(KUBECONFIG, "w") as file:
121+
yaml.safe_dump(kubeconfig_data, file)
108122

109123

110124
def apply_kubernetes_yaml(yaml_file: str) -> bool:

0 commit comments

Comments
 (0)