|
15 | 15 |
|
16 | 16 | from .constants import DEFAULT_NAMESPACE, KUBECONFIG
|
17 | 17 | from .process import run_command, stream_command
|
| 18 | +from .constants import KUBECONFIG |
18 | 19 |
|
19 | 20 |
|
20 | 21 | def get_static_client() -> CoreV1Api:
|
@@ -94,17 +95,30 @@ def create_kubernetes_object(
|
94 | 95 | return obj
|
95 | 96 |
|
96 | 97 |
|
97 |
| -def set_kubectl_context(namespace: str) -> bool: |
| 98 | +def set_context_namespace(namespace: str): |
98 | 99 | """
|
99 |
| - Set the default kubectl context to the specified namespace. |
| 100 | + Set the default kubeconfig context to the specified namespace. |
100 | 101 | """
|
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) |
108 | 122 |
|
109 | 123 |
|
110 | 124 | def apply_kubernetes_yaml(yaml_file: str) -> bool:
|
|
0 commit comments