diff --git a/kuryr_kubernetes/controller/drivers/base.py b/kuryr_kubernetes/controller/drivers/base.py index 411a480cc..8976ddb92 100644 --- a/kuryr_kubernetes/controller/drivers/base.py +++ b/kuryr_kubernetes/controller/drivers/base.py @@ -51,21 +51,36 @@ class DriverBase(object): """ @classmethod - def get_instance(cls, driver_alias=None): - """Get an implementing driver instance.""" + def get_instance(cls, specific_driver=None, scope='default'): + """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 - 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: - manager = _DRIVER_MANAGERS[driver_name] + manager = _DRIVER_MANAGERS[driver_key] except KeyError: - name = (config.CONF.kubernetes[driver_name] if not driver_alias - else driver_alias) + if specific_driver: + driver_name = specific_driver + else: + driver_name = config.CONF.kubernetes[alias + '_driver'] + manager = stv_driver.DriverManager( namespace="%s.%s" % (_DRIVER_NAMESPACE_BASE, alias), - name=name, + name=driver_name, invoke_on_load=True) - _DRIVER_MANAGERS[driver_name] = manager + _DRIVER_MANAGERS[driver_key] = manager driver = manager.driver if not isinstance(driver, cls): diff --git a/kuryr_kubernetes/controller/drivers/multi_vif.py b/kuryr_kubernetes/controller/drivers/multi_vif.py index 9ce9e39b2..4258607fe 100644 --- a/kuryr_kubernetes/controller/drivers/multi_vif.py +++ b/kuryr_kubernetes/controller/drivers/multi_vif.py @@ -35,7 +35,7 @@ class NPWGMultiVIFDriver(base.MultiVIFDriver): def __init__(self): super(NPWGMultiVIFDriver, self).__init__() self._drv_vif_pool = base.VIFPoolDriver.get_instance( - driver_alias='multi_pool') + specific_driver='multi_pool') self._drv_vif_pool.set_vif_driver() def request_additional_vifs(self, pod, project_id, security_groups): @@ -71,7 +71,7 @@ class NPWGMultiVIFDriver(base.MultiVIFDriver): else: alias = config[constants.K8S_ANNOTATION_NPWG_CRD_DRIVER_TYPE] vif_drv = base.PodVIFDriver.get_instance( - driver_alias=alias) + specific_driver=alias) vif = vif_drv.request_vif(pod, project_id, subnet, security_groups) if vif: vifs.append(vif) diff --git a/kuryr_kubernetes/controller/drivers/vif_pool.py b/kuryr_kubernetes/controller/drivers/vif_pool.py index faf16b4f3..46182647b 100644 --- a/kuryr_kubernetes/controller/drivers/vif_pool.py +++ b/kuryr_kubernetes/controller/drivers/vif_pool.py @@ -774,9 +774,10 @@ class MultiVIFPool(base.VIFPoolDriver): LOG.error("The pool and pod driver selected are not " "compatible. They will be skipped") 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( - driver_alias=pool_driver) + specific_driver=pool_driver) drv_pool.set_vif_driver(drv_vif) self._vif_drvs[pod_driver] = drv_pool diff --git a/kuryr_kubernetes/controller/handlers/namespace.py b/kuryr_kubernetes/controller/handlers/namespace.py index f21bf86b7..ff572d375 100644 --- a/kuryr_kubernetes/controller/handlers/namespace.py +++ b/kuryr_kubernetes/controller/handlers/namespace.py @@ -35,7 +35,7 @@ class NamespaceHandler(k8s_base.ResourceEventHandler): self._drv_subnets = drivers.PodSubnetsDriver.get_instance() self._drv_sg = drivers.PodSecurityGroupsDriver.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() def on_present(self, namespace): diff --git a/kuryr_kubernetes/controller/handlers/vif.py b/kuryr_kubernetes/controller/handlers/vif.py index fb279c8a9..22538c2e9 100644 --- a/kuryr_kubernetes/controller/handlers/vif.py +++ b/kuryr_kubernetes/controller/handlers/vif.py @@ -52,7 +52,7 @@ class VIFHandler(k8s_base.ResourceEventHandler): # VIFHandler driver, for now it is aware of the pool driver, but this # will be reverted as soon as a mechanism is in place. 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_multi_vif = drivers.MultiVIFDriver.get_enabled_drivers() diff --git a/kuryr_kubernetes/tests/unit/controller/drivers/test_base.py b/kuryr_kubernetes/tests/unit/controller/drivers/test_base.py index 501d646d9..c16c9d58b 100644 --- a/kuryr_kubernetes/tests/unit/controller/drivers/test_base.py +++ b/kuryr_kubernetes/tests/unit/controller/drivers/test_base.py @@ -51,6 +51,7 @@ class TestDriverBase(test_base.TestCase): def test_get_instance_not_loaded(self, m_stv_mgr, m_cfg, m_mgrs): alias = _TestDriver.ALIAS cfg_name = '%s_driver' % (alias) + mgr_key = '%s:_from_cfg:default' % (alias) drv_name = 'driver_impl' namespace = '%s.%s' % (d_base._DRIVER_NAMESPACE_BASE, alias) 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_stv_mgr.assert_called_with(namespace=namespace, name=drv_name, 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('kuryr_kubernetes.config.CONF') diff --git a/kuryr_kubernetes/tests/unit/controller/drivers/test_multi_vif.py b/kuryr_kubernetes/tests/unit/controller/drivers/test_multi_vif.py index a7c637c0f..83cc64b5a 100644 --- a/kuryr_kubernetes/tests/unit/controller/drivers/test_multi_vif.py +++ b/kuryr_kubernetes/tests/unit/controller/drivers/test_multi_vif.py @@ -132,7 +132,7 @@ class TestNPWGMultiVIFDriver(test_base.TestCase): m_drv = multi_vif.NPWGMultiVIFDriver() self.assertEqual(self._vif_pool_drv, m_drv._drv_vif_pool) m_get_vif_pool_driver.assert_called_once_with( - driver_alias='multi_pool') + specific_driver='multi_pool') m_set_vifs_driver.assert_called_once() @mock.patch('kuryr_kubernetes.utils.get_subnet')