Skip to content

Commit f84777a

Browse files
committed
operate on a namespace group
1 parent e3f517e commit f84777a

File tree

5 files changed

+33
-15
lines changed

5 files changed

+33
-15
lines changed

resources/charts/namespaces/templates/namespace.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ apiVersion: v1
22
kind: Namespace
33
metadata:
44
name: {{ .Values.namespaceName | default .Release.Name }}
5+
labels:
6+
type: {{ .Values.type }}

resources/charts/namespaces/values.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
type: "assets"
12
users:
23
- name: warnet-user
34
roles:
@@ -37,4 +38,4 @@ roles:
3738
verbs: ["get", "list"]
3839
- apiGroups: [""]
3940
resources: ["events"]
40-
verbs: ["get"]
41+
verbs: ["get"]

src/warnet/constants.py

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
LOGGING_NAMESPACE = "warnet-logging"
1616
INGRESS_NAMESPACE = "ingress"
1717
HELM_COMMAND = "helm upgrade --install --create-namespace"
18+
WARNET_ASSETS = "assets"
1819

1920
# Directories and files for non-python assets, e.g., helm charts, example scenarios, default configs
2021
SRC_DIR = files("warnet")

src/warnet/control.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,9 @@ def delete_pod(pod_name, namespace):
132132

133133
# Delete remaining pods
134134
pods = get_pods()
135-
for pod in pods.items:
136-
futures.append(executor.submit(delete_pod, pod.metadata.name, pod.metadata.namespace))
135+
for pod_list in pods:
136+
for pod in pod_list.items:
137+
futures.append(executor.submit(delete_pod, pod.metadata.name, pod.metadata.namespace))
137138

138139
# Wait for all tasks to complete and print results
139140
for future in as_completed(futures):
@@ -159,9 +160,10 @@ def get_active_network(namespace):
159160

160161

161162
@click.command(context_settings={"ignore_unknown_options": True})
163+
@click.option("--namespace", "-n", type=str, help="Namespace to run scenario in (overrides the current namespace in kubectl)")
162164
@click.argument("scenario_file", type=click.Path(exists=True, file_okay=True, dir_okay=False))
163165
@click.argument("additional_args", nargs=-1, type=click.UNPROCESSED)
164-
def run(scenario_file: str, additional_args: tuple[str]):
166+
def run(namespace: str, scenario_file: str, additional_args: tuple[str]):
165167
"""
166168
Run a scenario from a file.
167169
Pass `-- --help` to get individual scenario help
@@ -173,7 +175,7 @@ def run(scenario_file: str, additional_args: tuple[str]):
173175
scenario_data = base64.b64encode(file.read()).decode()
174176

175177
name = f"commander-{scenario_name.replace('_', '')}-{int(time.time())}"
176-
namespace = get_default_namespace()
178+
ns = namespace if namespace else get_default_namespace()
177179
tankpods = get_mission("tank")
178180
tanks = [
179181
{
@@ -198,7 +200,7 @@ def run(scenario_file: str, additional_args: tuple[str]):
198200
"upgrade",
199201
"--install",
200202
"--namespace",
201-
namespace,
203+
ns,
202204
"--set",
203205
f"fullnameOverride={name}",
204206
"--set",

src/warnet/k8s.py

+21-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
import yaml
88
from kubernetes import client, config, watch
9-
from kubernetes.client.models import CoreV1Event, V1PodList
9+
from kubernetes.client.models import CoreV1Event, V1PodList, V1NamespaceList, V1Pod
10+
from kubernetes.client.rest import ApiException
1011
from kubernetes.dynamic import DynamicClient
1112
from kubernetes.stream import stream
1213

@@ -16,6 +17,7 @@
1617
INGRESS_NAMESPACE,
1718
KUBECONFIG,
1819
LOGGING_NAMESPACE,
20+
WARNET_ASSETS,
1921
)
2022
from .process import run_command, stream_command
2123

@@ -32,19 +34,23 @@ def get_dynamic_client() -> DynamicClient:
3234

3335
def get_pods() -> V1PodList:
3436
sclient = get_static_client()
35-
try:
36-
pod_list: V1PodList = sclient.list_namespaced_pod(get_default_namespace())
37-
except Exception as e:
38-
raise e
39-
return pod_list
37+
pods = []
38+
namespaces: V1NamespaceList = get_namespaces_by_warnet_type(WARNET_ASSETS)
39+
for ns in namespaces.items:
40+
try:
41+
pods.append(sclient.list_namespaced_pod(ns.metadata.name))
42+
except Exception as e:
43+
raise e
44+
return pods
4045

4146

4247
def get_mission(mission: str) -> list[V1PodList]:
4348
pods = get_pods()
4449
crew = []
45-
for pod in pods.items:
46-
if "mission" in pod.metadata.labels and pod.metadata.labels["mission"] == mission:
47-
crew.append(pod)
50+
for pod_list in pods:
51+
for pod in pod_list.items:
52+
if "mission" in pod.metadata.labels and pod.metadata.labels["mission"] == mission:
53+
crew.append(pod)
4854
return crew
4955

5056

@@ -306,3 +312,9 @@ def get_service_accounts_in_namespace(namespace):
306312
# skip the default service account created by k8s
307313
service_accounts = run_command(command).split()
308314
return [sa for sa in service_accounts if sa != "default"]
315+
316+
317+
def get_namespaces_by_warnet_type(warnet_type: str) -> list[V1NamespaceList]:
318+
sclient = get_static_client()
319+
namespaces = sclient.list_namespace(label_selector=f"type={warnet_type}")
320+
return namespaces

0 commit comments

Comments
 (0)