|
8 | 8 | import yaml
|
9 | 9 | from kubernetes import client, config, watch
|
10 | 10 | from kubernetes.client import CoreV1Api
|
11 |
| -from kubernetes.client.models import V1PodList |
| 11 | +from kubernetes.client.exceptions import ApiException |
| 12 | +from kubernetes.client.models import V1Namespace, V1PodList |
12 | 13 | from kubernetes.dynamic import DynamicClient
|
13 | 14 | from kubernetes.stream import stream
|
14 | 15 |
|
@@ -356,13 +357,25 @@ def get_kubeconfig_value(jsonpath):
|
356 | 357 | return run_command(command)
|
357 | 358 |
|
358 | 359 |
|
359 |
| -def get_namespaces_by_prefix(prefix: str): |
| 360 | +def get_namespaces() -> list[V1Namespace]: |
| 361 | + sclient = get_static_client() |
| 362 | + try: |
| 363 | + return sclient.list_namespace().items |
| 364 | + |
| 365 | + except ApiException as e: |
| 366 | + if e.status == 403: |
| 367 | + ns = sclient.read_namespace(name=get_default_namespace()) |
| 368 | + return [ns] |
| 369 | + else: |
| 370 | + return [] |
| 371 | + |
| 372 | + |
| 373 | +def get_namespaces_by_prefix(prefix: str) -> list[V1Namespace]: |
360 | 374 | """
|
361 | 375 | Get all namespaces beginning with `prefix`. Returns empty list of no namespaces with the specified prefix are found.
|
362 | 376 | """
|
363 |
| - command = "kubectl get namespaces -o jsonpath={.items[*].metadata.name}" |
364 |
| - namespaces = run_command(command).split() |
365 |
| - return [ns for ns in namespaces if ns.startswith(prefix)] |
| 377 | + namespaces = get_namespaces() |
| 378 | + return [ns for ns in namespaces if ns.metadata.name.startswith(prefix)] |
366 | 379 |
|
367 | 380 |
|
368 | 381 | def get_service_accounts_in_namespace(namespace):
|
|
0 commit comments