From 90c52f907fea74b619089930b8df72f64d3a6ce8 Mon Sep 17 00:00:00 2001 From: Paige Patton <64206430+paigerube14@users.noreply.github.com> Date: Fri, 15 Aug 2025 11:13:42 -0400 Subject: [PATCH] regex to tools pod names (#886) Signed-off-by: Paige Patton --- .../native/network/ingress_shaping.py | 37 ++++++++------ .../native/network/pod_interface.j2 | 2 +- .../native/network/pod_module.j2 | 2 +- .../native/pod_network_outage/pod_module.j2 | 2 +- .../pod_network_outage_plugin.py | 51 ++++++++++--------- .../network_chaos_scenario_plugin.py | 10 ++-- krkn/scenario_plugins/network_chaos/pod.j2 | 2 +- 7 files changed, 59 insertions(+), 47 deletions(-) diff --git a/krkn/scenario_plugins/native/network/ingress_shaping.py b/krkn/scenario_plugins/native/network/ingress_shaping.py index ef15726e..0c4466de 100644 --- a/krkn/scenario_plugins/native/network/ingress_shaping.py +++ b/krkn/scenario_plugins/native/network/ingress_shaping.py @@ -5,6 +5,7 @@ import time import sys import os import re +import random from traceback import format_exc from jinja2 import Environment, FileSystemLoader from . import kubernetes_functions as kube_helper @@ -168,14 +169,14 @@ def get_default_interface(node: str, pod_template, cli: CoreV1Api, image: str) - Returns: Default interface (string) belonging to the node """ - - pod_body = yaml.safe_load(pod_template.render(nodename=node, image=image)) + pod_name_regex = str(random.randint(0, 10000)) + pod_body = yaml.safe_load(pod_template.render(regex_name=pod_name_regex,nodename=node, image=image)) logging.info("Creating pod to query interface on node %s" % node) kube_helper.create_pod(cli, pod_body, "default", 300) - + pod_name = f"fedtools-{pod_name_regex}" try: cmd = ["ip", "r"] - output = kube_helper.exec_cmd_in_pod(cli, cmd, "fedtools", "default") + output = kube_helper.exec_cmd_in_pod(cli, cmd, pod_name, "default") if not output: logging.error("Exception occurred while executing command in pod") @@ -191,7 +192,7 @@ def get_default_interface(node: str, pod_template, cli: CoreV1Api, image: str) - finally: logging.info("Deleting pod to query interface on node") - kube_helper.delete_pod(cli, "fedtools", "default") + kube_helper.delete_pod(cli, pod_name, "default") return interfaces @@ -220,13 +221,15 @@ def verify_interface( Returns: The interface list for the node """ - pod_body = yaml.safe_load(pod_template.render(nodename=node, image=image)) + pod_name_regex = str(random.randint(0, 10000)) + pod_body = yaml.safe_load(pod_template.render(regex_name=pod_name_regex,nodename=node, image=image)) logging.info("Creating pod to query interface on node %s" % node) kube_helper.create_pod(cli, pod_body, "default", 300) + pod_name = f"fedtools-{pod_name_regex}" try: if input_interface_list == []: cmd = ["ip", "r"] - output = kube_helper.exec_cmd_in_pod(cli, cmd, "fedtools", "default") + output = kube_helper.exec_cmd_in_pod(cli, cmd, pod_name, "default") if not output: logging.error("Exception occurred while executing command in pod") @@ -242,7 +245,7 @@ def verify_interface( else: cmd = ["ip", "-br", "addr", "show"] - output = kube_helper.exec_cmd_in_pod(cli, cmd, "fedtools", "default") + output = kube_helper.exec_cmd_in_pod(cli, cmd, pod_name, "default") if not output: logging.error("Exception occurred while executing command in pod") @@ -265,7 +268,7 @@ def verify_interface( ) finally: logging.info("Deleting pod to query interface on node") - kube_helper.delete_pod(cli, "fedtools", "default") + kube_helper.delete_pod(cli, pod_name, "default") return input_interface_list @@ -431,16 +434,18 @@ def create_virtual_interfaces( - The YAML template used to instantiate a pod to create virtual interfaces on the node """ - pod_body = yaml.safe_load(pod_template.render(nodename=node, image=image)) + pod_name_regex = str(random.randint(0, 10000)) + pod_body = yaml.safe_load(pod_template.render(regex_name=pod_name_regex,nodename=node, image=image)) kube_helper.create_pod(cli, pod_body, "default", 300) logging.info( "Creating {0} virtual interfaces on node {1} using a pod".format( len(interface_list), node ) ) - create_ifb(cli, len(interface_list), "modtools") + pod_name = f"modtools-{pod_name_regex}" + create_ifb(cli, len(interface_list), pod_name) logging.info("Deleting pod used to create virtual interfaces") - kube_helper.delete_pod(cli, "modtools", "default") + kube_helper.delete_pod(cli, pod_name, "default") def delete_virtual_interfaces( @@ -467,11 +472,13 @@ def delete_virtual_interfaces( """ for node in node_list: - pod_body = yaml.safe_load(pod_template.render(nodename=node, image=image)) + pod_name_regex = str(random.randint(0, 10000)) + pod_body = yaml.safe_load(pod_template.render(regex_name=pod_name_regex,nodename=node, image=image)) kube_helper.create_pod(cli, pod_body, "default", 300) logging.info("Deleting all virtual interfaces on node {0}".format(node)) - delete_ifb(cli, "modtools") - kube_helper.delete_pod(cli, "modtools", "default") + pod_name = f"modtools-{pod_name_regex}" + delete_ifb(cli, pod_name) + kube_helper.delete_pod(cli, pod_name, "default") def create_ifb(cli: CoreV1Api, number: int, pod_name: str): diff --git a/krkn/scenario_plugins/native/network/pod_interface.j2 b/krkn/scenario_plugins/native/network/pod_interface.j2 index d70ae0d3..9318bf3f 100644 --- a/krkn/scenario_plugins/native/network/pod_interface.j2 +++ b/krkn/scenario_plugins/native/network/pod_interface.j2 @@ -1,7 +1,7 @@ apiVersion: v1 kind: Pod metadata: - name: fedtools + name: fedtools-{{regex_name}} spec: hostNetwork: true nodeName: {{nodename}} diff --git a/krkn/scenario_plugins/native/network/pod_module.j2 b/krkn/scenario_plugins/native/network/pod_module.j2 index 34cc2b2f..6d1bdd19 100644 --- a/krkn/scenario_plugins/native/network/pod_module.j2 +++ b/krkn/scenario_plugins/native/network/pod_module.j2 @@ -1,7 +1,7 @@ apiVersion: v1 kind: Pod metadata: - name: modtools + name: modtools-{{regex_name}} spec: nodeName: {{nodename}} containers: diff --git a/krkn/scenario_plugins/native/pod_network_outage/pod_module.j2 b/krkn/scenario_plugins/native/pod_network_outage/pod_module.j2 index 34cc2b2f..6d1bdd19 100644 --- a/krkn/scenario_plugins/native/pod_network_outage/pod_module.j2 +++ b/krkn/scenario_plugins/native/pod_network_outage/pod_module.j2 @@ -1,7 +1,7 @@ apiVersion: v1 kind: Pod metadata: - name: modtools + name: modtools-{{regex_name}} spec: nodeName: {{nodename}} containers: diff --git a/krkn/scenario_plugins/native/pod_network_outage/pod_network_outage_plugin.py b/krkn/scenario_plugins/native/pod_network_outage/pod_network_outage_plugin.py index 42e2cd28..2fd1b3d0 100755 --- a/krkn/scenario_plugins/native/pod_network_outage/pod_network_outage_plugin.py +++ b/krkn/scenario_plugins/native/pod_network_outage/pod_network_outage_plugin.py @@ -537,7 +537,7 @@ def get_egress_cmd( def create_virtual_interfaces( - kubecli: KrknKubernetes, nummber: int, node: str, pod_template, image: str, + kubecli: KrknKubernetes, number: int, node: str, pod_template, image: str, ) -> None: """ Function that creates a privileged pod and uses it to create @@ -561,14 +561,16 @@ def create_virtual_interfaces( image (string) - Image of network chaos tool """ - pod_body = yaml.safe_load(pod_template.render(nodename=node, image=image)) + pod_name_regex = str(random.randint(0, 10000)) + pod_body = yaml.safe_load(pod_template.render(regex_name=pod_name_regex, nodename=node, image=image)) kubecli.create_pod(pod_body, "default", 300) + pod_name = f"modtools-{pod_name_regex}" logging.info( - "Creating {0} virtual interfaces on node {1} using a pod".format(nummber, node) + "Creating {0} virtual interfaces on node {1} using a pod".format(number, node) ) - create_ifb(kubecli, nummber, "modtools") + create_ifb(kubecli, number, pod_name) logging.info("Deleting pod used to create virtual interfaces") - kubecli.delete_pod("modtools", "default") + kubecli.delete_pod(pod_name, "default") def delete_virtual_interfaces( @@ -598,11 +600,12 @@ def delete_virtual_interfaces( """ for node in node_list: - pod_body = yaml.safe_load(pod_template.render(nodename=node, image=image)) + pod_name_regex = str(random.randint(0, 10000)) + pod_body = yaml.safe_load(pod_template.render(regex_name=pod_name_regex, nodename=node, image=image)) kubecli.create_pod(pod_body, "default", 300) logging.info("Deleting all virtual interfaces on node {0}".format(node)) - delete_ifb(kubecli, "modtools") - kubecli.delete_pod("modtools", "default") + delete_ifb(kubecli, "modtools-" + pod_name_regex) + kubecli.delete_pod("modtools-" + pod_name_regex, "default") def create_ifb(kubecli: KrknKubernetes, number: int, pod_name: str): @@ -652,15 +655,15 @@ def list_bridges(node: str, pod_template, kubecli: KrknKubernetes, image: str) - Returns: List of bridges on the node. """ - - pod_body = yaml.safe_load(pod_template.render(nodename=node, image=image)) + pod_name_regex = str(random.randint(0, 10000)) + pod_body = yaml.safe_load(pod_template.render(regex_name=pod_name_regex, nodename=node, image=image)) logging.info("Creating pod to query bridge on node %s" % node) kubecli.create_pod(pod_body, "default", 300) - + pod_name = f"modtools-{pod_name_regex}" try: cmd = ["/host", "ovs-vsctl", "list-br"] output = kubecli.exec_cmd_in_pod( - cmd, "modtools", "default", base_command="chroot" + cmd, pod_name, "default", base_command="chroot" ) if not output: @@ -671,7 +674,7 @@ def list_bridges(node: str, pod_template, kubecli: KrknKubernetes, image: str) - finally: logging.info("Deleting pod to query interface on node") - kubecli.delete_pod("modtools", "default") + kubecli.delete_pod(pod_name, "default") return bridges @@ -704,11 +707,11 @@ def check_cookie( Returns Returns the matching flow rules """ - - pod_body = yaml.safe_load(pod_template.render(nodename=node, image=image)) + pod_name_regex = str(random.randint(0, 10000)) + pod_body = yaml.safe_load(pod_template.render(regex_name = pod_name_regex,nodename=node, image=image)) logging.info("Creating pod to query duplicate rules on node %s" % node) kubecli.create_pod(pod_body, "default", 300) - + pod_name = f"modtools-{pod_name_regex}" try: cmd = [ "chroot", @@ -721,7 +724,7 @@ def check_cookie( f"cookie={cookie}/-1", ] output = kubecli.exec_cmd_in_pod( - cmd, "modtools", "default", base_command="chroot" + cmd, pod_name, "default", base_command="chroot" ) if not output: @@ -732,7 +735,7 @@ def check_cookie( finally: logging.info("Deleting pod to query interface on node") - kubecli.delete_pod("modtools", "default") + kubecli.delete_pod(pod_name, "default") return flow_list @@ -763,12 +766,12 @@ def get_pod_interface( Returns Returns the pod interface name """ - - pod_body = yaml.safe_load(pod_template.render(nodename=node, image=image)) + pod_name_regex = str(random.randint(0, 10000)) + pod_body = yaml.safe_load(pod_template.render(regex_name=pod_name_regex, nodename=node, image=image)) logging.info("Creating pod to query pod interface on node %s" % node) kubecli.create_pod(pod_body, "default", 300) inf = "" - + pod_name = f"modtools-{pod_name_regex}" try: if br_name == "br-int": find_ip = f"external-ids:ip_addresses={ip}/23" @@ -786,12 +789,12 @@ def get_pod_interface( ] output = kubecli.exec_cmd_in_pod( - cmd, "modtools", "default", base_command="chroot" + cmd, pod_name, "default", base_command="chroot" ) if not output: cmd = ["/host", "ip", "addr", "show"] output = kubecli.exec_cmd_in_pod( - cmd, "modtools", "default", base_command="chroot" + cmd, pod_name, "default", base_command="chroot" ) for if_str in output.split("\n"): if re.search(ip, if_str): @@ -800,7 +803,7 @@ def get_pod_interface( inf = output finally: logging.info("Deleting pod to query interface on node") - kubecli.delete_pod("modtools", "default") + kubecli.delete_pod(pod_name, "default") return inf diff --git a/krkn/scenario_plugins/network_chaos/network_chaos_scenario_plugin.py b/krkn/scenario_plugins/network_chaos/network_chaos_scenario_plugin.py index e39e0299..d2b7a10f 100644 --- a/krkn/scenario_plugins/network_chaos/network_chaos_scenario_plugin.py +++ b/krkn/scenario_plugins/network_chaos/network_chaos_scenario_plugin.py @@ -161,17 +161,19 @@ class NetworkChaosScenarioPlugin(AbstractScenarioPlugin): self, test_interface, nodelst, template, kubecli: KrknKubernetes, image: str ): pod_index = random.randint(0, len(nodelst) - 1) - pod_body = yaml.safe_load(template.render(nodename=nodelst[pod_index], image=image)) + pod_name_regex = str(random.randint(0, 10000)) + pod_body = yaml.safe_load(template.render(regex_name=pod_name_regex,nodename=nodelst[pod_index], image=image)) logging.info("Creating pod to query interface on node %s" % nodelst[pod_index]) kubecli.create_pod(pod_body, "default", 300) + pod_name = f"fedtools-{pod_name_regex}" try: if test_interface == []: cmd = "ip r | grep default | awk '/default/ {print $5}'" - output = kubecli.exec_cmd_in_pod(cmd, "fedtools", "default") + output = kubecli.exec_cmd_in_pod(cmd, pod_name, "default") test_interface = [output.replace("\n", "")] else: cmd = "ip -br addr show|awk -v ORS=',' '{print $1}'" - output = kubecli.exec_cmd_in_pod(cmd, "fedtools", "default") + output = kubecli.exec_cmd_in_pod(cmd, pod_name, "default") interface_lst = output[:-1].split(",") for interface in test_interface: if interface not in interface_lst: @@ -183,7 +185,7 @@ class NetworkChaosScenarioPlugin(AbstractScenarioPlugin): return test_interface finally: logging.info("Deleting pod to query interface on node") - kubecli.delete_pod("fedtools", "default") + kubecli.delete_pod(pod_name, "default") # krkn_lib def get_job_pods(self, api_response, kubecli: KrknKubernetes): diff --git a/krkn/scenario_plugins/network_chaos/pod.j2 b/krkn/scenario_plugins/network_chaos/pod.j2 index bf143483..f2306d83 100644 --- a/krkn/scenario_plugins/network_chaos/pod.j2 +++ b/krkn/scenario_plugins/network_chaos/pod.j2 @@ -1,7 +1,7 @@ apiVersion: v1 kind: Pod metadata: - name: fedtools + name: fedtools-{{regex_name}} spec: hostNetwork: true nodeName: {{nodename}}