Refactor DriverBase.get_instance()
scope param is added to allow getting multiple independent instances of a driver. This is particularly useful for using same VIFPoolDriver implementation for multiple PodVIFDrivers (eg. NoopVIFPool may be used for multiple PodVIFDriver types) Also renames driver_alias to specific_driver to better indicate its function. Related-Bug: 1747406 Change-Id: Iea3b65b91f362a18cca6bf9d44d938063a56118b Signed-off-by: Yash Gupta <y.gupta@samsung.com>
This commit is contained in:
parent
f3b8977442
commit
cb2d308f84
@ -51,21 +51,36 @@ class DriverBase(object):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_instance(cls, driver_alias=None):
|
def get_instance(cls, specific_driver=None, scope='default'):
|
||||||
"""Get an implementing driver instance."""
|
"""Get an implementing driver instance.
|
||||||
|
|
||||||
|
:param specific_driver: Loads a specific driver instead of using conf.
|
||||||
|
Uses separate manager entry so that loading of
|
||||||
|
default/other drivers is not affected.
|
||||||
|
:param scope: Loads the driver in the given scope (if independent
|
||||||
|
instances of a driver are required)
|
||||||
|
"""
|
||||||
|
|
||||||
alias = cls.ALIAS
|
alias = cls.ALIAS
|
||||||
driver_name = alias + '_driver' if not driver_alias else driver_alias
|
|
||||||
|
if specific_driver:
|
||||||
|
driver_key = '{}:{}:{}'.format(alias, specific_driver, scope)
|
||||||
|
else:
|
||||||
|
driver_key = '{}:_from_cfg:{}'.format(alias, scope)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
manager = _DRIVER_MANAGERS[driver_name]
|
manager = _DRIVER_MANAGERS[driver_key]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
name = (config.CONF.kubernetes[driver_name] if not driver_alias
|
if specific_driver:
|
||||||
else driver_alias)
|
driver_name = specific_driver
|
||||||
|
else:
|
||||||
|
driver_name = config.CONF.kubernetes[alias + '_driver']
|
||||||
|
|
||||||
manager = stv_driver.DriverManager(
|
manager = stv_driver.DriverManager(
|
||||||
namespace="%s.%s" % (_DRIVER_NAMESPACE_BASE, alias),
|
namespace="%s.%s" % (_DRIVER_NAMESPACE_BASE, alias),
|
||||||
name=name,
|
name=driver_name,
|
||||||
invoke_on_load=True)
|
invoke_on_load=True)
|
||||||
_DRIVER_MANAGERS[driver_name] = manager
|
_DRIVER_MANAGERS[driver_key] = manager
|
||||||
|
|
||||||
driver = manager.driver
|
driver = manager.driver
|
||||||
if not isinstance(driver, cls):
|
if not isinstance(driver, cls):
|
||||||
|
@ -35,7 +35,7 @@ class NPWGMultiVIFDriver(base.MultiVIFDriver):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(NPWGMultiVIFDriver, self).__init__()
|
super(NPWGMultiVIFDriver, self).__init__()
|
||||||
self._drv_vif_pool = base.VIFPoolDriver.get_instance(
|
self._drv_vif_pool = base.VIFPoolDriver.get_instance(
|
||||||
driver_alias='multi_pool')
|
specific_driver='multi_pool')
|
||||||
self._drv_vif_pool.set_vif_driver()
|
self._drv_vif_pool.set_vif_driver()
|
||||||
|
|
||||||
def request_additional_vifs(self, pod, project_id, security_groups):
|
def request_additional_vifs(self, pod, project_id, security_groups):
|
||||||
@ -71,7 +71,7 @@ class NPWGMultiVIFDriver(base.MultiVIFDriver):
|
|||||||
else:
|
else:
|
||||||
alias = config[constants.K8S_ANNOTATION_NPWG_CRD_DRIVER_TYPE]
|
alias = config[constants.K8S_ANNOTATION_NPWG_CRD_DRIVER_TYPE]
|
||||||
vif_drv = base.PodVIFDriver.get_instance(
|
vif_drv = base.PodVIFDriver.get_instance(
|
||||||
driver_alias=alias)
|
specific_driver=alias)
|
||||||
vif = vif_drv.request_vif(pod, project_id, subnet, security_groups)
|
vif = vif_drv.request_vif(pod, project_id, subnet, security_groups)
|
||||||
if vif:
|
if vif:
|
||||||
vifs.append(vif)
|
vifs.append(vif)
|
||||||
|
@ -774,9 +774,10 @@ class MultiVIFPool(base.VIFPoolDriver):
|
|||||||
LOG.error("The pool and pod driver selected are not "
|
LOG.error("The pool and pod driver selected are not "
|
||||||
"compatible. They will be skipped")
|
"compatible. They will be skipped")
|
||||||
raise exceptions.MultiPodDriverPoolConfigurationNotSupported()
|
raise exceptions.MultiPodDriverPoolConfigurationNotSupported()
|
||||||
drv_vif = base.PodVIFDriver.get_instance(driver_alias=pod_driver)
|
drv_vif = base.PodVIFDriver.get_instance(
|
||||||
|
specific_driver=pod_driver)
|
||||||
drv_pool = base.VIFPoolDriver.get_instance(
|
drv_pool = base.VIFPoolDriver.get_instance(
|
||||||
driver_alias=pool_driver)
|
specific_driver=pool_driver)
|
||||||
drv_pool.set_vif_driver(drv_vif)
|
drv_pool.set_vif_driver(drv_vif)
|
||||||
self._vif_drvs[pod_driver] = drv_pool
|
self._vif_drvs[pod_driver] = drv_pool
|
||||||
|
|
||||||
|
@ -35,7 +35,7 @@ class NamespaceHandler(k8s_base.ResourceEventHandler):
|
|||||||
self._drv_subnets = drivers.PodSubnetsDriver.get_instance()
|
self._drv_subnets = drivers.PodSubnetsDriver.get_instance()
|
||||||
self._drv_sg = drivers.PodSecurityGroupsDriver.get_instance()
|
self._drv_sg = drivers.PodSecurityGroupsDriver.get_instance()
|
||||||
self._drv_vif_pool = drivers.VIFPoolDriver.get_instance(
|
self._drv_vif_pool = drivers.VIFPoolDriver.get_instance(
|
||||||
driver_alias='multi_pool')
|
specific_driver='multi_pool')
|
||||||
self._drv_vif_pool.set_vif_driver()
|
self._drv_vif_pool.set_vif_driver()
|
||||||
|
|
||||||
def on_present(self, namespace):
|
def on_present(self, namespace):
|
||||||
|
@ -52,7 +52,7 @@ class VIFHandler(k8s_base.ResourceEventHandler):
|
|||||||
# VIFHandler driver, for now it is aware of the pool driver, but this
|
# VIFHandler driver, for now it is aware of the pool driver, but this
|
||||||
# will be reverted as soon as a mechanism is in place.
|
# will be reverted as soon as a mechanism is in place.
|
||||||
self._drv_vif_pool = drivers.VIFPoolDriver.get_instance(
|
self._drv_vif_pool = drivers.VIFPoolDriver.get_instance(
|
||||||
driver_alias='multi_pool')
|
specific_driver='multi_pool')
|
||||||
self._drv_vif_pool.set_vif_driver()
|
self._drv_vif_pool.set_vif_driver()
|
||||||
self._drv_multi_vif = drivers.MultiVIFDriver.get_enabled_drivers()
|
self._drv_multi_vif = drivers.MultiVIFDriver.get_enabled_drivers()
|
||||||
|
|
||||||
|
@ -51,6 +51,7 @@ class TestDriverBase(test_base.TestCase):
|
|||||||
def test_get_instance_not_loaded(self, m_stv_mgr, m_cfg, m_mgrs):
|
def test_get_instance_not_loaded(self, m_stv_mgr, m_cfg, m_mgrs):
|
||||||
alias = _TestDriver.ALIAS
|
alias = _TestDriver.ALIAS
|
||||||
cfg_name = '%s_driver' % (alias)
|
cfg_name = '%s_driver' % (alias)
|
||||||
|
mgr_key = '%s:_from_cfg:default' % (alias)
|
||||||
drv_name = 'driver_impl'
|
drv_name = 'driver_impl'
|
||||||
namespace = '%s.%s' % (d_base._DRIVER_NAMESPACE_BASE, alias)
|
namespace = '%s.%s' % (d_base._DRIVER_NAMESPACE_BASE, alias)
|
||||||
m_cfg.kubernetes.__getitem__.return_value = drv_name
|
m_cfg.kubernetes.__getitem__.return_value = drv_name
|
||||||
@ -64,7 +65,7 @@ class TestDriverBase(test_base.TestCase):
|
|||||||
m_cfg.kubernetes.__getitem__.assert_called_with(cfg_name)
|
m_cfg.kubernetes.__getitem__.assert_called_with(cfg_name)
|
||||||
m_stv_mgr.assert_called_with(namespace=namespace, name=drv_name,
|
m_stv_mgr.assert_called_with(namespace=namespace, name=drv_name,
|
||||||
invoke_on_load=True)
|
invoke_on_load=True)
|
||||||
m_mgrs.__setitem__.assert_called_once_with(cfg_name, m_mgr)
|
m_mgrs.__setitem__.assert_called_once_with(mgr_key, m_mgr)
|
||||||
|
|
||||||
@mock.patch.object(d_base, '_DRIVER_MANAGERS')
|
@mock.patch.object(d_base, '_DRIVER_MANAGERS')
|
||||||
@mock.patch('kuryr_kubernetes.config.CONF')
|
@mock.patch('kuryr_kubernetes.config.CONF')
|
||||||
|
@ -132,7 +132,7 @@ class TestNPWGMultiVIFDriver(test_base.TestCase):
|
|||||||
m_drv = multi_vif.NPWGMultiVIFDriver()
|
m_drv = multi_vif.NPWGMultiVIFDriver()
|
||||||
self.assertEqual(self._vif_pool_drv, m_drv._drv_vif_pool)
|
self.assertEqual(self._vif_pool_drv, m_drv._drv_vif_pool)
|
||||||
m_get_vif_pool_driver.assert_called_once_with(
|
m_get_vif_pool_driver.assert_called_once_with(
|
||||||
driver_alias='multi_pool')
|
specific_driver='multi_pool')
|
||||||
m_set_vifs_driver.assert_called_once()
|
m_set_vifs_driver.assert_called_once()
|
||||||
|
|
||||||
@mock.patch('kuryr_kubernetes.utils.get_subnet')
|
@mock.patch('kuryr_kubernetes.utils.get_subnet')
|
||||||
|
Loading…
Reference in New Issue
Block a user