Browse Source

Update metering agent to use stevedore alias for driver

Currently the metering agent is using the old import method,
use stevedore instead.

DocImpact

Two places in the networking guide should change to
'driver = iptables' from current format.

Partial-Bug: #1504536
Change-Id: I1e6d196a3ada8fbfc2b70d6a983984d8db09bbd0
tags/11.0.0.0b1
Jean-Philippe Evrard 2 years ago
parent
commit
c9d4698409

+ 3
- 3
neutron/services/metering/agents/metering_agent.py View File

@@ -21,9 +21,9 @@ import oslo_messaging
21 21
 from oslo_service import loopingcall
22 22
 from oslo_service import periodic_task
23 23
 from oslo_service import service
24
-from oslo_utils import importutils
25 24
 from oslo_utils import timeutils
26 25
 
26
+from neutron.services.metering.drivers import utils as driverutils
27 27
 from neutron._i18n import _, _LE, _LI, _LW
28 28
 from neutron.agent.common import config
29 29
 from neutron.agent import rpc as agent_rpc
@@ -86,8 +86,8 @@ class MeteringAgent(MeteringPluginRpc, manager.Manager):
86 86
         LOG.info(_LI("Loading Metering driver %s"), self.conf.driver)
87 87
         if not self.conf.driver:
88 88
             raise SystemExit(_('A metering driver must be specified'))
89
-        self.metering_driver = importutils.import_object(
90
-            self.conf.driver, self, self.conf)
89
+        self.metering_driver = driverutils.load_metering_driver(self,
90
+                                                                self.conf)
91 91
 
92 92
     def _metering_notification(self):
93 93
         for label_id, info in self.metering_infos.items():

+ 42
- 0
neutron/services/metering/drivers/utils.py View File

@@ -0,0 +1,42 @@
1
+# Copyright 2017 Red Hat, Inc.
2
+# All Rights Reserved.
3
+#
4
+#    Licensed under the Apache License, Version 2.0 (the "License"); you may
5
+#    not use this file except in compliance with the License. You may obtain
6
+#    a copy of the License at
7
+#
8
+#         http://www.apache.org/licenses/LICENSE-2.0
9
+#
10
+#    Unless required by applicable law or agreed to in writing, software
11
+#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+#    License for the specific language governing permissions and limitations
14
+#    under the License.
15
+
16
+from oslo_log import log as logging
17
+
18
+from neutron._i18n import _LE
19
+from neutron.common import utils as utils
20
+
21
+
22
+LOG = logging.getLogger(__name__)
23
+
24
+METERING_NAMESPACE = 'neutron.services.metering_drivers'
25
+
26
+
27
+def load_metering_driver(plugin, conf):
28
+    """Load metering driver
29
+
30
+    :param plugin: the metering plugin
31
+    :param conf: driver configuration object
32
+    :raises SystemExit of 1 if driver cannot be loaded
33
+    """
34
+
35
+    try:
36
+        loaded_class = utils.load_class_by_alias_or_classname(
37
+                METERING_NAMESPACE, conf.driver)
38
+        return loaded_class(plugin, conf)
39
+    except ImportError:
40
+        LOG.error(_LE("Error loading metering driver '%s'"),
41
+                  conf.driver)
42
+        raise SystemExit(1)

+ 4
- 6
neutron/tests/unit/services/metering/agents/test_metering_agent.py View File

@@ -54,7 +54,7 @@ class TestMeteringOperations(base.BaseTestCase):
54 54
 
55 55
         self.noop_driver = ('neutron.services.metering.drivers.noop.'
56 56
                             'noop_driver.NoopMeteringDriver')
57
-        cfg.CONF.set_override('driver', self.noop_driver)
57
+        cfg.CONF.set_override('driver', 'noop')
58 58
         cfg.CONF.set_override('measure_interval', 0)
59 59
         cfg.CONF.set_override('report_interval', 0)
60 60
 
@@ -230,9 +230,7 @@ class TestMeteringDriver(base.BaseTestCase):
230 230
         super(TestMeteringDriver, self).setUp()
231 231
         metering_agent_config.register_metering_agent_opts()
232 232
 
233
-        self.noop_driver = ('neutron.services.metering.drivers.noop.'
234
-                            'noop_driver.NoopMeteringDriver')
235
-        cfg.CONF.set_override('driver', self.noop_driver)
233
+        cfg.CONF.set_override('driver', 'noop')
236 234
 
237 235
         self.agent = metering_agent.MeteringAgent('my agent', cfg.CONF)
238 236
         self.driver = mock.Mock()
@@ -244,7 +242,7 @@ class TestMeteringDriver(base.BaseTestCase):
244 242
         with mock.patch.object(metering_agent, 'LOG') as log:
245 243
             self.agent.add_metering_label(None, ROUTERS)
246 244
             log.exception.assert_called_with(mock.ANY,
247
-                                             {'driver': self.noop_driver,
245
+                                             {'driver': 'noop',
248 246
                                               'func': 'add_metering_label'})
249 247
 
250 248
     def test_add_metering_label_runtime_error(self):
@@ -253,7 +251,7 @@ class TestMeteringDriver(base.BaseTestCase):
253 251
         with mock.patch.object(metering_agent, 'LOG') as log:
254 252
             self.agent.add_metering_label(None, ROUTERS)
255 253
             log.exception.assert_called_with(mock.ANY,
256
-                                             {'driver': self.noop_driver,
254
+                                             {'driver': 'noop',
257 255
                                               'func':
258 256
                                               'add_metering_label'})
259 257
 

+ 6
- 0
releasenotes/notes/metering-driver-stevedore-alias-2c4fdb0556205a3a.yaml View File

@@ -0,0 +1,6 @@
1
+---
2
+features:
3
+  - The metering agent driver can now be specified with a stevedore
4
+    alias in the ``metering_agent.ini`` file. For example,
5
+    ``driver = iptables`` instead of
6
+    ``driver = neutron.services.metering.iptables.iptables_driver:IptablesMeteringDriver``.

+ 3
- 0
setup.cfg View File

@@ -151,6 +151,9 @@ neutron.agent.firewall_drivers =
151 151
     iptables = neutron.agent.linux.iptables_firewall:IptablesFirewallDriver
152 152
     iptables_hybrid = neutron.agent.linux.iptables_firewall:OVSHybridIptablesFirewallDriver
153 153
     openvswitch = neutron.agent.linux.openvswitch_firewall:OVSFirewallDriver
154
+neutron.services.metering_drivers =
155
+    noop = neutron.services.metering.drivers.noop.noop_driver:NoopMeteringDriver
156
+    iptables = neutron.services.metering.iptables.iptables_driver:IptablesMeteringDriver
154 157
 tempest.test_plugins =
155 158
     neutron_tests = neutron.tests.tempest.plugin:NeutronTempestPlugin
156 159
 

Loading…
Cancel
Save