Skip to content

Commit 170213c

Browse files
committed
update getting namespace logic
1 parent ac595a3 commit 170213c

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/warnet/k8s.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
import yaml
99
from kubernetes import client, config, watch
1010
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
1213
from kubernetes.dynamic import DynamicClient
1314
from kubernetes.stream import stream
1415

@@ -356,13 +357,25 @@ def get_kubeconfig_value(jsonpath):
356357
return run_command(command)
357358

358359

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]:
360374
"""
361375
Get all namespaces beginning with `prefix`. Returns empty list of no namespaces with the specified prefix are found.
362376
"""
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)]
366379

367380

368381
def get_service_accounts_in_namespace(namespace):

0 commit comments

Comments
 (0)