|
| 1 | +# Copyright 2025 The Krkn Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +import queue |
| 15 | +import time |
| 16 | +from typing import Tuple |
| 17 | + |
| 18 | +from krkn_lib.telemetry.ocp import KrknTelemetryOpenshift |
| 19 | +from krkn_lib.utils import get_random_string |
| 20 | + |
| 21 | +from krkn.scenario_plugins.network_chaos_ng.models import ( |
| 22 | + NetworkChaosScenarioType, |
| 23 | + BaseNetworkChaosConfig, |
| 24 | + NetworkChaosConfig, |
| 25 | +) |
| 26 | +from krkn.scenario_plugins.network_chaos_ng.modules.abstract_network_chaos_module import ( |
| 27 | + AbstractNetworkChaosModule, |
| 28 | +) |
| 29 | +from krkn.scenario_plugins.network_chaos_ng.modules.utils import ( |
| 30 | + log_info, |
| 31 | + log_error, |
| 32 | + deploy_network_chaos_ng_pod, |
| 33 | + get_pod_default_interface, |
| 34 | +) |
| 35 | +from krkn.scenario_plugins.network_chaos_ng.modules.utils_network_chaos import ( |
| 36 | + common_set_limit_rules, |
| 37 | + common_delete_limit_rules, |
| 38 | +) |
| 39 | + |
| 40 | + |
| 41 | +class VmiNetworkChaosModule(AbstractNetworkChaosModule): |
| 42 | + |
| 43 | + def __init__(self, config: NetworkChaosConfig, kubecli: KrknTelemetryOpenshift): |
| 44 | + super().__init__(config, kubecli) |
| 45 | + self.config = config |
| 46 | + |
| 47 | + def run(self, target: str, error_queue: queue.Queue = None): |
| 48 | + # target is "namespace/vmi-name" as produced by get_vmi_targets() |
| 49 | + parallel = False |
| 50 | + if error_queue: |
| 51 | + parallel = True |
| 52 | + try: |
| 53 | + namespace, vmi_name = target.split("/", 1) |
| 54 | + network_chaos_pod_name = f"vmi-network-chaos-{get_random_string(5)}" |
| 55 | + container_name = f"fedora-container-{get_random_string(5)}" |
| 56 | + |
| 57 | + log_info( |
| 58 | + f"creating workload to inject network chaos in VMI {vmi_name} " |
| 59 | + f"latency:{str(self.config.latency) if self.config.latency else '0'}, " |
| 60 | + f"packet drop:{str(self.config.loss) if self.config.loss else '0'} " |
| 61 | + f"bandwidth restriction:{str(self.config.bandwidth) if self.config.bandwidth else '0'} ", |
| 62 | + parallel, |
| 63 | + network_chaos_pod_name, |
| 64 | + ) |
| 65 | + |
| 66 | + # Resolve which node the VMI is running on |
| 67 | + vmi = self.kubecli.get_lib_kubernetes().get_vmi(vmi_name, namespace) |
| 68 | + if not vmi: |
| 69 | + raise Exception( |
| 70 | + f"VMI {vmi_name} not found in namespace {namespace}" |
| 71 | + ) |
| 72 | + |
| 73 | + node_name = vmi.get("status", {}).get("nodeName") |
| 74 | + if not node_name: |
| 75 | + raise Exception( |
| 76 | + f"unable to determine node for VMI {vmi_name} in namespace {namespace}; " |
| 77 | + "VMI may not be in Running phase" |
| 78 | + ) |
| 79 | + |
| 80 | + log_info( |
| 81 | + f"VMI {vmi_name} is running on node {node_name}", |
| 82 | + parallel, |
| 83 | + network_chaos_pod_name, |
| 84 | + ) |
| 85 | + |
| 86 | + # The virt-launcher pod carries the VMI's network namespace. |
| 87 | + # It is labelled kubevirt.io/domain=<vmi-name>. |
| 88 | + virt_launcher_pods = self.kubecli.get_lib_kubernetes().list_pods( |
| 89 | + namespace, label_selector=f"kubevirt.io/domain={vmi_name}" |
| 90 | + ) |
| 91 | + if not virt_launcher_pods: |
| 92 | + raise Exception( |
| 93 | + f"no virt-launcher pod found for VMI {vmi_name} in namespace {namespace}" |
| 94 | + ) |
| 95 | + virt_launcher_pod_name = virt_launcher_pods[0] |
| 96 | + |
| 97 | + log_info( |
| 98 | + f"resolved virt-launcher pod {virt_launcher_pod_name} for VMI {vmi_name}", |
| 99 | + parallel, |
| 100 | + network_chaos_pod_name, |
| 101 | + ) |
| 102 | + |
| 103 | + # Deploy the privileged chaos pod onto the VMI's node. |
| 104 | + # host_network=False so that tc rules are applied via nsenter into |
| 105 | + # the virt-launcher pod's network namespace rather than the node's. |
| 106 | + deploy_network_chaos_ng_pod( |
| 107 | + self.config, |
| 108 | + node_name, |
| 109 | + network_chaos_pod_name, |
| 110 | + self.kubecli.get_lib_kubernetes(), |
| 111 | + container_name, |
| 112 | + host_network=False, |
| 113 | + ) |
| 114 | + |
| 115 | + # Detect the default network interface. When host_network=False the |
| 116 | + # chaos pod has its own network namespace; the interface name returned |
| 117 | + # (typically "eth0") is used as the target interface inside the |
| 118 | + # virt-launcher's network namespace via nsenter, which also uses "eth0" |
| 119 | + # as its primary interface. |
| 120 | + if len(self.config.interfaces) == 0: |
| 121 | + interfaces = [ |
| 122 | + get_pod_default_interface( |
| 123 | + network_chaos_pod_name, |
| 124 | + self.config.namespace, |
| 125 | + self.kubecli.get_lib_kubernetes(), |
| 126 | + ) |
| 127 | + ] |
| 128 | + if not interfaces[0]: |
| 129 | + log_error( |
| 130 | + "no network interface detected; impossible to execute the network chaos scenario", |
| 131 | + parallel, |
| 132 | + network_chaos_pod_name, |
| 133 | + ) |
| 134 | + self.kubecli.get_lib_kubernetes().delete_pod( |
| 135 | + network_chaos_pod_name, self.config.namespace |
| 136 | + ) |
| 137 | + return |
| 138 | + log_info( |
| 139 | + f"detected default interface: {interfaces[0]}", |
| 140 | + parallel, |
| 141 | + network_chaos_pod_name, |
| 142 | + ) |
| 143 | + else: |
| 144 | + interfaces = self.config.interfaces |
| 145 | + |
| 146 | + # Retrieve the container IDs of the virt-launcher pod so we can |
| 147 | + # identify the cgroup and resolve the host-visible PIDs via /proc. |
| 148 | + container_ids = self.kubecli.get_lib_kubernetes().get_container_ids( |
| 149 | + virt_launcher_pod_name, namespace |
| 150 | + ) |
| 151 | + if not container_ids: |
| 152 | + raise Exception( |
| 153 | + f"impossible to resolve container ID for virt-launcher pod " |
| 154 | + f"{virt_launcher_pod_name} in namespace {namespace}" |
| 155 | + ) |
| 156 | + |
| 157 | + log_info( |
| 158 | + f"targeting virt-launcher container {container_ids[0]}", |
| 159 | + parallel, |
| 160 | + network_chaos_pod_name, |
| 161 | + ) |
| 162 | + |
| 163 | + # Resolve the host PIDs for the virt-launcher container so that |
| 164 | + # nsenter can enter its network namespace. |
| 165 | + pids = self.kubecli.get_lib_kubernetes().get_pod_pids( |
| 166 | + base_pod_name=network_chaos_pod_name, |
| 167 | + base_pod_namespace=self.config.namespace, |
| 168 | + base_pod_container_name=container_name, |
| 169 | + pod_name=virt_launcher_pod_name, |
| 170 | + pod_namespace=namespace, |
| 171 | + pod_container_id=container_ids[0], |
| 172 | + ) |
| 173 | + if not pids: |
| 174 | + raise Exception( |
| 175 | + f"impossible to resolve PIDs for virt-launcher pod {virt_launcher_pod_name}" |
| 176 | + ) |
| 177 | + |
| 178 | + log_info( |
| 179 | + f"resolved PIDs {pids} on node {node_name} for VMI {vmi_name}", |
| 180 | + parallel, |
| 181 | + network_chaos_pod_name, |
| 182 | + ) |
| 183 | + |
| 184 | + common_set_limit_rules( |
| 185 | + self.config.egress, |
| 186 | + self.config.ingress, |
| 187 | + interfaces, |
| 188 | + self.config.bandwidth, |
| 189 | + self.config.latency, |
| 190 | + self.config.loss, |
| 191 | + parallel, |
| 192 | + network_chaos_pod_name, |
| 193 | + self.kubecli.get_lib_kubernetes(), |
| 194 | + network_chaos_pod_name, |
| 195 | + self.config.namespace, |
| 196 | + pids, |
| 197 | + ) |
| 198 | + |
| 199 | + time.sleep(self.config.test_duration) |
| 200 | + |
| 201 | + log_info("removing tc rules", parallel, network_chaos_pod_name) |
| 202 | + |
| 203 | + common_delete_limit_rules( |
| 204 | + self.config.egress, |
| 205 | + self.config.ingress, |
| 206 | + interfaces, |
| 207 | + network_chaos_pod_name, |
| 208 | + self.config.namespace, |
| 209 | + self.kubecli.get_lib_kubernetes(), |
| 210 | + pids, |
| 211 | + parallel, |
| 212 | + network_chaos_pod_name, |
| 213 | + ) |
| 214 | + |
| 215 | + self.kubecli.get_lib_kubernetes().delete_pod( |
| 216 | + network_chaos_pod_name, self.config.namespace |
| 217 | + ) |
| 218 | + |
| 219 | + except Exception as e: |
| 220 | + if error_queue is None: |
| 221 | + raise e |
| 222 | + else: |
| 223 | + error_queue.put(str(e)) |
| 224 | + |
| 225 | + def get_config(self) -> Tuple[NetworkChaosScenarioType, BaseNetworkChaosConfig]: |
| 226 | + return NetworkChaosScenarioType.VMI, self.config |
| 227 | + |
| 228 | + def get_targets(self) -> list[str]: |
| 229 | + return self.get_vmi_targets(self.config) |
0 commit comments