-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[vSphere Provider] Fix vc conn timout issue #40516
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,51 @@ | ||
import atexit | ||
import ssl | ||
|
||
from pyVim.connect import Disconnect, SmartConnect | ||
from pyVim.connect import Disconnect, SmartStubAdapter, VimSessionOrientedStub | ||
from pyVmomi import vim | ||
|
||
from ray.autoscaler._private.vsphere.utils import Constants | ||
|
||
|
||
class PyvmomiSdkProvider: | ||
def __init__(self, server, user, password, session_type: Constants.SessionType): | ||
def __init__( | ||
self, | ||
server, | ||
user, | ||
password, | ||
session_type: Constants.SessionType, | ||
port: int = 443, | ||
): | ||
# Instance variables | ||
self.server = server | ||
self.user = user | ||
self.password = password | ||
self.session_type = session_type | ||
self.port = port | ||
|
||
# Instance parameters | ||
if self.session_type == Constants.SessionType.UNVERIFIED: | ||
context_obj = ssl._create_unverified_context() | ||
self.timeout = 0 | ||
|
||
smart_connect_obj = SmartConnect( | ||
# Connect using a session oriented connection | ||
# Ref. https://github.com/vmware/pyvmomi/issues/347 | ||
self.pyvmomi_sdk_client = None | ||
credentials = VimSessionOrientedStub.makeUserLoginMethod(user, password) | ||
smart_stub = SmartStubAdapter( | ||
host=server, | ||
user=user, | ||
pwd=password, | ||
port=port, | ||
sslContext=context_obj, | ||
connectionPoolTimeout=self.timeout, | ||
) | ||
atexit.register(Disconnect, smart_connect_obj) | ||
self.pyvmomi_sdk_client = smart_connect_obj.content | ||
self.session_stub = VimSessionOrientedStub(smart_stub, credentials) | ||
self.pyvmomi_sdk_client = vim.ServiceInstance( | ||
"ServiceInstance", self.session_stub | ||
) | ||
|
||
if not self.pyvmomi_sdk_client: | ||
raise ValueError("Could not connect to the specified host") | ||
atexit.register(Disconnect, self.pyvmomi_sdk_client) | ||
|
||
def get_pyvmomi_obj_by_moid(self, vimtype, moid): | ||
""" | ||
|
@@ -35,52 +57,49 @@ def get_pyvmomi_obj_by_moid(self, vimtype, moid): | |
""" | ||
obj = None | ||
if self.pyvmomi_sdk_client is None: | ||
raise ValueError("Must init pyvmomi_sdk_client first.") | ||
raise RuntimeError("Must init pyvmomi_sdk_client first.") | ||
|
||
if not moid: | ||
raise ValueError("Invalid argument for moid") | ||
|
||
container = self.pyvmomi_sdk_client.viewManager.CreateContainerView( | ||
self.pyvmomi_sdk_client.rootFolder, vimtype, True | ||
container = self.pyvmomi_sdk_client.content.viewManager.CreateContainerView( | ||
self.pyvmomi_sdk_client.content.rootFolder, vimtype, True | ||
) | ||
|
||
for c in container.view: | ||
if moid: | ||
if moid in str(c): | ||
obj = c | ||
break | ||
else: | ||
if moid in str(c): | ||
obj = c | ||
break | ||
if not obj: | ||
raise RuntimeError( | ||
f"Unexpected: cannot find vSphere object {vimtype} with moid: {moid}" | ||
) | ||
|
||
return obj | ||
|
||
def get_pyvmomi_obj_by_name(self, vimtype, name): | ||
def get_pyvmomi_obj(self, vimtype, name=None, obj_id=None): | ||
""" | ||
This function finds the vSphere object by the object name and the object type. | ||
The object type can be "VM", "Host", "Datastore", etc. | ||
The object name is a unique name under the vCenter server. | ||
This function will return the vSphere object. | ||
The argument for `vimtype` can be "vim.VM", "vim.Host", "vim.Datastore", etc. | ||
Then either the name or the object id need to be provided. | ||
To check all such object information, you can go to the managed object board | ||
page of your vCenter Server, such as: https://<your_vc_ip/mob | ||
""" | ||
obj = None | ||
if not name and not obj_id: | ||
# Raise runtime error because this is not user fault | ||
raise RuntimeError("Either name or obj id must be provided") | ||
if self.pyvmomi_sdk_client is None: | ||
raise ValueError("Must init pyvmomi_sdk_client first.") | ||
raise RuntimeError("Must init pyvmomi_sdk_client first") | ||
|
||
container = self.pyvmomi_sdk_client.viewManager.CreateContainerView( | ||
self.pyvmomi_sdk_client.rootFolder, vimtype, True | ||
container = self.pyvmomi_sdk_client.content.viewManager.CreateContainerView( | ||
self.pyvmomi_sdk_client.content.rootFolder, vimtype, True | ||
) | ||
|
||
for c in container.view: | ||
if name: | ||
# If both name and moid are provided we will prioritize name. | ||
if name: | ||
for c in container.view: | ||
if c.name == name: | ||
obj = c | ||
break | ||
else: | ||
obj = c | ||
break | ||
if not obj: | ||
raise RuntimeError( | ||
f"Unexpected: cannot find vSphere object {vimtype} with name: {name}" | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed the same code changes needed to be made in two places; I still think it would be a bit better to unify the functions, maybe like |
||
return obj | ||
return c | ||
elif obj_id: | ||
for c in container.view: | ||
if str(c) == obj_id: | ||
return c | ||
raise ValueError( | ||
f"Cannot find the object with type {vimtype} on vSphere with" | ||
f"name={name} and obj_id={obj_id}" | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
get_pyvmomi_obj_by_*
could not returnNone
(instead it would raise an exception), but now it can return None. Do the callers need to handle the case where it'sNone
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.