Skip to content

Commit

Permalink
operate on a namespace group
Browse files Browse the repository at this point in the history
  • Loading branch information
josibake committed Sep 26, 2024
1 parent e3f517e commit f84777a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 15 deletions.
2 changes: 2 additions & 0 deletions resources/charts/namespaces/templates/namespace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ apiVersion: v1
kind: Namespace
metadata:
name: {{ .Values.namespaceName | default .Release.Name }}
labels:
type: {{ .Values.type }}
3 changes: 2 additions & 1 deletion resources/charts/namespaces/values.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
type: "assets"
users:
- name: warnet-user
roles:
Expand Down Expand Up @@ -37,4 +38,4 @@ roles:
verbs: ["get", "list"]
- apiGroups: [""]
resources: ["events"]
verbs: ["get"]
verbs: ["get"]
1 change: 1 addition & 0 deletions src/warnet/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
LOGGING_NAMESPACE = "warnet-logging"
INGRESS_NAMESPACE = "ingress"
HELM_COMMAND = "helm upgrade --install --create-namespace"
WARNET_ASSETS = "assets"

# Directories and files for non-python assets, e.g., helm charts, example scenarios, default configs
SRC_DIR = files("warnet")
Expand Down
12 changes: 7 additions & 5 deletions src/warnet/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ def delete_pod(pod_name, namespace):

# Delete remaining pods
pods = get_pods()
for pod in pods.items:
futures.append(executor.submit(delete_pod, pod.metadata.name, pod.metadata.namespace))
for pod_list in pods:
for pod in pod_list.items:
futures.append(executor.submit(delete_pod, pod.metadata.name, pod.metadata.namespace))

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


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

name = f"commander-{scenario_name.replace('_', '')}-{int(time.time())}"
namespace = get_default_namespace()
ns = namespace if namespace else get_default_namespace()
tankpods = get_mission("tank")
tanks = [
{
Expand All @@ -198,7 +200,7 @@ def run(scenario_file: str, additional_args: tuple[str]):
"upgrade",
"--install",
"--namespace",
namespace,
ns,
"--set",
f"fullnameOverride={name}",
"--set",
Expand Down
30 changes: 21 additions & 9 deletions src/warnet/k8s.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

import yaml
from kubernetes import client, config, watch
from kubernetes.client.models import CoreV1Event, V1PodList
from kubernetes.client.models import CoreV1Event, V1PodList, V1NamespaceList, V1Pod
from kubernetes.client.rest import ApiException
from kubernetes.dynamic import DynamicClient
from kubernetes.stream import stream

Expand All @@ -16,6 +17,7 @@
INGRESS_NAMESPACE,
KUBECONFIG,
LOGGING_NAMESPACE,
WARNET_ASSETS,
)
from .process import run_command, stream_command

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

def get_pods() -> V1PodList:
sclient = get_static_client()
try:
pod_list: V1PodList = sclient.list_namespaced_pod(get_default_namespace())
except Exception as e:
raise e
return pod_list
pods = []
namespaces: V1NamespaceList = get_namespaces_by_warnet_type(WARNET_ASSETS)
for ns in namespaces.items:
try:
pods.append(sclient.list_namespaced_pod(ns.metadata.name))
except Exception as e:
raise e
return pods


def get_mission(mission: str) -> list[V1PodList]:
pods = get_pods()
crew = []
for pod in pods.items:
if "mission" in pod.metadata.labels and pod.metadata.labels["mission"] == mission:
crew.append(pod)
for pod_list in pods:
for pod in pod_list.items:
if "mission" in pod.metadata.labels and pod.metadata.labels["mission"] == mission:
crew.append(pod)
return crew


Expand Down Expand Up @@ -306,3 +312,9 @@ def get_service_accounts_in_namespace(namespace):
# skip the default service account created by k8s
service_accounts = run_command(command).split()
return [sa for sa in service_accounts if sa != "default"]


def get_namespaces_by_warnet_type(warnet_type: str) -> list[V1NamespaceList]:
sclient = get_static_client()
namespaces = sclient.list_namespace(label_selector=f"type={warnet_type}")
return namespaces

0 comments on commit f84777a

Please sign in to comment.