diff --git a/ocs_ci/deployment/vmware.py b/ocs_ci/deployment/vmware.py index b2ca2800435b..900c2fbaffac 100644 --- a/ocs_ci/deployment/vmware.py +++ b/ocs_ci/deployment/vmware.py @@ -143,9 +143,9 @@ def __init__(self): self.ocp_version = get_ocp_version() config.ENV_DATA["ocp_version"] = self.ocp_version - config.ENV_DATA["ocp_version_object"] = ( - version.get_semantic_ocp_version_from_config() - ) + config.ENV_DATA[ + "ocp_version_object" + ] = version.get_semantic_ocp_version_from_config() config.ENV_DATA["version_4_9_object"] = version.VERSION_4_9 self.wait_time = 90 @@ -1075,7 +1075,12 @@ def deploy(self, log_cli_level="DEBUG"): os.chdir(self.previous_dir) if not self.sno: - + vsphere = VSPHERE( + config.ENV_DATA["vsphere_server"], + config.ENV_DATA["vsphere_user"], + config.ENV_DATA["vsphere_password"], + ) + vsphere.add_interface_to_vm(vm_name="compute-0") # Update kubeconfig with proxy-url (if client_http_proxy # configured) to redirect client access through proxy server. update_kubeconfig_with_proxy_url_for_client(self.kubeconfig) diff --git a/ocs_ci/utility/vsphere.py b/ocs_ci/utility/vsphere.py index d2a3b2de3ef9..72b20b8f1d9c 100644 --- a/ocs_ci/utility/vsphere.py +++ b/ocs_ci/utility/vsphere.py @@ -29,6 +29,7 @@ VM_DEFAULT_NETWORK, VM_DEFAULT_NETWORK_ADAPTER, ) +from ocs_ci.framework import config from ocs_ci.utility.utils import TimeoutSampler logger = logging.getLogger(__name__) @@ -1742,3 +1743,50 @@ def get_volume_path(self, volume_id, datastore_name, datacenter_name): volume_path = vstorage_object.config.backing.filePath logger.debug(f"File path for volume {volume_id} is `{volume_path}`") return volume_path + + def add_interface_to_vm( + self, vm_name, network_name="VM Network", adapter_type="vmxnet3" + ): + pool = config.ENV_DATA["cluster_name"] + dc = config.ENV_DATA["vsphere_datacenter"] + cluster = config.ENV_DATA["vsphere_cluster"] + vm = self.get_vm_in_pool_by_name(vm_name, dc, cluster, pool) + if not vm: + raise Exception(f"VM '{vm_name}' not found.") + content = self.get_content + container = content.viewManager.CreateContainerView( + content.rootFolder, [vim.Network], True + ) + for conf in container.view: + if conf.name == network_name: + network = conf + break + if not network: + raise Exception(f"Network '{network_name}' not found.") + device_spec = vim.vm.device.VirtualDeviceSpec() + device_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.add + if adapter_type == "vmxnet3": + nic = vim.vm.device.VirtualVmxnet3() + # Set the network backing + nic.backing = vim.vm.device.VirtualEthernetCard.NetworkBackingInfo() + nic.backing.network = network + nic.backing.deviceName = network_name + # Specify the adapter type + nic.key = -100 # Temporary key; vSphere assigns a unique key + nic.deviceInfo = vim.Description() + nic.deviceInfo.summary = f"{adapter_type} adapter connected to {network_name}" + device_spec.device = nic + # Create a VM configuration spec + spec = vim.vm.ConfigSpec() + spec.deviceChange = [device_spec] + # Reconfigure the VM + task = vm.ReconfigVM_Task(spec=spec) + print( + f"Adding {adapter_type} adapter to VM '{vm_name}' on network '{network_name}'..." + ) + result = WaitForTask(task) + if result is None: + raise Exception( + f"Task for configuring network for {vm_name} did not complete successfully." + ) + logger.info(f"Network adapter added to {vm_name} successfully.")