From fb96e87cb71909e9119358acbfdf60d09bac2d36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Dulko?= Date: Fri, 16 Mar 2018 12:25:00 +0100 Subject: [PATCH] Check for `standard-attr-tag` Neutron extension `tag` and `tag-ext` Neutron extensions are deprecated and scheduled for removal in Rocky. Those are replaced by `standard-attr-tag` extensions and this commit implements its support in kuryr-libnetwork. Change-Id: I295a5b84eb7fa3439561fa009b7499f94d8df4d2 Closes-Bug: 1756305 --- kuryr_libnetwork/controllers.py | 43 +++++++++++++------ kuryr_libnetwork/server.py | 5 +-- kuryr_libnetwork/tests/unit/test_config.py | 49 ++++++++++++---------- 3 files changed, 57 insertions(+), 40 deletions(-) diff --git a/kuryr_libnetwork/controllers.py b/kuryr_libnetwork/controllers.py index 73b39963..7bfd318d 100644 --- a/kuryr_libnetwork/controllers.py +++ b/kuryr_libnetwork/controllers.py @@ -43,6 +43,7 @@ LOG = log.getLogger(__name__) MANDATORY_NEUTRON_EXTENSION = "subnet_allocation" TAG_NEUTRON_EXTENSION = "tag" TAG_EXT_NEUTRON_EXTENSION = "tag-ext" +TAG_STANDARD_NEUTRON_EXTENSION = 'standard-attr-tag' SUBNET_POOLS_V4 = [] SUBNET_POOLS_V6 = [] DEFAULT_DRIVER = driver.get_driver_instance() @@ -83,20 +84,36 @@ def check_for_neutron_ext_support(): .format(MANDATORY_NEUTRON_EXTENSION)) -def check_for_neutron_tag_support(ext_name): - """Validates tag and tag-ext extension support availability in Neutron.""" - if ext_name == TAG_EXT_NEUTRON_EXTENSION: - ext_rename = "tag_ext" - else: - ext_rename = ext_name - setattr(app, ext_rename, True) +def check_for_neutron_tag_support(): + """Validates tagging extensions availability in Neutron. + + Checks if either tag, tag-ext or standard-attr-tag is available and sets + ``app`` properties accordingly. + """ + + app.tag_ext = True + app.tag = True + + # Check for modern extension first. try: - app.neutron.show_extension(ext_name) - except n_exceptions.NeutronClientException as e: - setattr(app, ext_rename, False) - if e.status_code == n_exceptions.NotFound.status_code: - LOG.warning("Neutron extension %s not supported. " - "Continue without using them.", ext_name) + app.neutron.show_extension(TAG_STANDARD_NEUTRON_EXTENSION) + except n_exceptions.NeutronClientException: + pass + else: + # Okay, this means we have functionality of both tag and tag_ext, + # we're good to go! + return + + # Fallback to legacy extensions. + for ext in (TAG_NEUTRON_EXTENSION, TAG_EXT_NEUTRON_EXTENSION): + try: + app.neutron.show_extension(ext) + except n_exceptions.NeutronClientException as e: + ext_param = ext.replace('-', '_') # identifiers cannot have '-' + setattr(app, ext_param, False) + if e.status_code == n_exceptions.NotFound.status_code: + LOG.warning("Neutron extension %s not supported. " + "Continue without using them.", ext) def load_default_subnet_pools(): diff --git a/kuryr_libnetwork/server.py b/kuryr_libnetwork/server.py index 26786b1a..5df58444 100644 --- a/kuryr_libnetwork/server.py +++ b/kuryr_libnetwork/server.py @@ -26,10 +26,7 @@ def configure_app(): log.setup(config.CONF, 'kuryr') controllers.neutron_client() controllers.check_for_neutron_ext_support() - controllers.check_for_neutron_tag_support( - controllers.TAG_NEUTRON_EXTENSION) - controllers.check_for_neutron_tag_support( - controllers.TAG_EXT_NEUTRON_EXTENSION) + controllers.check_for_neutron_tag_support() controllers.load_default_subnet_pools() controllers.load_port_driver() diff --git a/kuryr_libnetwork/tests/unit/test_config.py b/kuryr_libnetwork/tests/unit/test_config.py index a635e290..094c323a 100644 --- a/kuryr_libnetwork/tests/unit/test_config.py +++ b/kuryr_libnetwork/tests/unit/test_config.py @@ -60,10 +60,7 @@ class ConfigurationTest(base.TestKuryrBase): kuryr_uri = parse.urlparse(config.CONF.kuryr_uri) mock_neutron_client.assert_called_once() mock_check_neutron_ext_support.assert_called_once() - mock_check_for_neutron_tag_support.assert_any_call( - controllers.TAG_NEUTRON_EXTENSION) - mock_check_for_neutron_tag_support.assert_any_call( - controllers.TAG_EXT_NEUTRON_EXTENSION) + mock_check_for_neutron_tag_support.assert_called_once_with() mock_run.assert_called_once_with(kuryr_uri.hostname, 23750, ssl_context=None) @@ -81,28 +78,34 @@ class ConfigurationTest(base.TestKuryrBase): mock_extension.assert_called_once_with(ext_alias) @mock.patch('kuryr_libnetwork.controllers.app.neutron.show_extension') - @ddt.data('tag', 'tag-ext') - def test_check_for_neutron_tag_support_with_ex(self, - ext_name, - mock_extension): - err = n_exceptions.NotFound.status_code - ext_not_found_ex = n_exceptions.NeutronClientException( - status_code=err, - message="") - mock_extension.side_effect = ext_not_found_ex - controllers.check_for_neutron_tag_support(ext_name) - mock_extension.assert_called_once_with(ext_name) + def test_check_for_neutron_tag_support_with_modern_ext(self, + mock_extension): + controllers.check_for_neutron_tag_support() + mock_extension.assert_called_once_with('standard-attr-tag') + self.assertTrue(controllers.app.tag) + self.assertTrue(controllers.app.tag_ext) @mock.patch('kuryr_libnetwork.controllers.app.neutron.show_extension') - @ddt.data('fake_ext') - def test_check_for_neutron_tag_support_wrong_ext_name_with_ex( - self, - ext_name, - mock_extension): + @ddt.data({'tag': True, 'tag-ext': True}, + {'tag': True, 'tag-ext': False}, + {'tag': False, 'tag-ext': True}, + {'tag': False, 'tag-ext': False}) + def test_check_for_neutron_tag_support_with_legacy_ext(self, ext_matrix, + mock_extension): err = n_exceptions.NotFound.status_code ext_not_found_ex = n_exceptions.NeutronClientException( status_code=err, message="") - mock_extension.side_effect = ext_not_found_ex - controllers.check_for_neutron_tag_support(ext_name) - mock_extension.assert_called_once_with(ext_name) + + def mock_fn(ext): + if not ext_matrix.get(ext, False): + raise ext_not_found_ex + + mock_extension.side_effect = mock_fn + controllers.check_for_neutron_tag_support() + mock_extension.assert_any_call('standard-attr-tag') + mock_extension.assert_any_call('tag') + mock_extension.assert_any_call('tag-ext') + + self.assertEqual(controllers.app.tag, ext_matrix['tag']) + self.assertEqual(controllers.app.tag_ext, ext_matrix['tag-ext'])