Skip to content

Commit

Permalink
multus for Vsphere
Browse files Browse the repository at this point in the history
Signed-off-by: Shay Rozen <[email protected]>
  • Loading branch information
shyRozen committed Nov 21, 2024
1 parent 14b7e85 commit e016d66
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
13 changes: 9 additions & 4 deletions ocs_ci/deployment/vmware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
48 changes: 48 additions & 0 deletions ocs_ci/utility/vsphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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.")

0 comments on commit e016d66

Please sign in to comment.