Skip to content

Commit f6cd7a9

Browse files
committed
add service-account list
1 parent 82bd6f1 commit f6cd7a9

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/warnet/admin.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from .namespaces import copy_namespaces_defaults, namespaces
1111
from .network import copy_network_defaults
1212
from .process import run_command
13+
from .service_accounts import service_accounts
1314

1415

1516
@click.group(name="admin", hidden=True)
@@ -19,6 +20,7 @@ def admin():
1920

2021

2122
admin.add_command(namespaces)
23+
admin.add_command(service_accounts)
2224

2325

2426
@admin.command()

src/warnet/service_accounts.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import click
2+
3+
from .constants import (
4+
WARGAMES_NAMESPACE_PREFIX,
5+
)
6+
from .k8s import get_static_client
7+
8+
9+
@click.group(name="service-accounts")
10+
def service_accounts():
11+
"""Service account commands"""
12+
13+
14+
@service_accounts.command()
15+
def list():
16+
"""List all service accounts with 'wargames-' prefix"""
17+
# Load the kubeconfig file
18+
sclient = get_static_client()
19+
namespaces = sclient.list_namespace().items
20+
21+
filtered_namespaces = [
22+
ns.metadata.name
23+
for ns in namespaces
24+
if ns.metadata.name.startswith(WARGAMES_NAMESPACE_PREFIX)
25+
]
26+
27+
if len(filtered_namespaces) == 0:
28+
click.secho("Could not find any matching service accounts.", fg="yellow")
29+
30+
for namespace in filtered_namespaces:
31+
click.secho(f"Service accounts in namespace: {namespace}")
32+
service_accounts = sclient.list_namespaced_service_account(namespace=namespace).items
33+
34+
if len(service_accounts) == 0:
35+
click.secho("...Could not find any matching service accounts", fg="yellow")
36+
37+
for sa in service_accounts:
38+
click.secho(f"- {sa.metadata.name}", fg="green")

0 commit comments

Comments
 (0)