|
9 | 9 | import yaml
|
10 | 10 | from kubernetes import client, config, watch
|
11 | 11 | from kubernetes.client import CoreV1Api
|
12 |
| -from kubernetes.client.models import V1Pod, V1PodList |
| 12 | +from kubernetes.client.models import V1Namespace, V1Pod, V1PodList |
13 | 13 | from kubernetes.client.rest import ApiException
|
14 | 14 | from kubernetes.dynamic import DynamicClient
|
15 | 15 | from kubernetes.stream import stream
|
@@ -360,18 +360,32 @@ def write_file_to_container(pod_name, container_name, dst_path, data):
|
360 | 360 | return True
|
361 | 361 | except Exception as e:
|
362 | 362 | print(f"Failed to copy data to {pod_name}({container_name}):{dst_path}:\n{e}")
|
| 363 | + |
| 364 | + |
363 | 365 | def get_kubeconfig_value(jsonpath):
|
364 | 366 | command = f"kubectl config view --minify -o jsonpath={jsonpath}"
|
365 | 367 | return run_command(command)
|
366 | 368 |
|
367 | 369 |
|
368 |
| -def get_namespaces_by_prefix(prefix: str): |
| 370 | +def get_namespaces() -> list[V1Namespace]: |
| 371 | + sclient = get_static_client() |
| 372 | + try: |
| 373 | + return sclient.list_namespace().items |
| 374 | + |
| 375 | + except ApiException as e: |
| 376 | + if e.status == 403: |
| 377 | + ns = sclient.read_namespace(name=get_default_namespace()) |
| 378 | + return [ns] |
| 379 | + else: |
| 380 | + return [] |
| 381 | + |
| 382 | + |
| 383 | +def get_namespaces_by_prefix(prefix: str) -> list[V1Namespace]: |
369 | 384 | """
|
370 | 385 | Get all namespaces beginning with `prefix`. Returns empty list of no namespaces with the specified prefix are found.
|
371 | 386 | """
|
372 |
| - command = "kubectl get namespaces -o jsonpath={.items[*].metadata.name}" |
373 |
| - namespaces = run_command(command).split() |
374 |
| - return [ns for ns in namespaces if ns.startswith(prefix)] |
| 387 | + namespaces = get_namespaces() |
| 388 | + return [ns for ns in namespaces if ns.metadata.name.startswith(prefix)] |
375 | 389 |
|
376 | 390 |
|
377 | 391 | def get_service_accounts_in_namespace(namespace):
|
|
0 commit comments