Skip to content

Commit f89941f

Browse files
committed
k8s: add ns, sa, and config helper funcs
1 parent 38c3ad1 commit f89941f

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/warnet/k8s.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,3 +351,25 @@ def write_file_to_container(pod_name, container_name, dst_path, data):
351351
return True
352352
except Exception as e:
353353
print(f"Failed to copy data to {pod_name}({container_name}):{dst_path}:\n{e}")
354+
def get_kubeconfig_value(jsonpath):
355+
command = f"kubectl config view --minify -o jsonpath={jsonpath}"
356+
return run_command(command)
357+
358+
359+
def get_namespaces_by_prefix(prefix: str):
360+
"""
361+
Get all namespaces beginning with `prefix`. Returns empty list of no namespaces with the specified prefix are found.
362+
"""
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)]
366+
367+
368+
def get_service_accounts_in_namespace(namespace):
369+
"""
370+
Get all service accounts in a namespace. Returns an empty list if no service accounts are found in the specified namespace.
371+
"""
372+
command = f"kubectl get serviceaccounts -n {namespace} -o jsonpath={{.items[*].metadata.name}}"
373+
# skip the default service account created by k8s
374+
service_accounts = run_command(command).split()
375+
return [sa for sa in service_accounts if sa != "default"]

0 commit comments

Comments
 (0)