Don't register config options on imports

Importing some modules lead to registering config options that may
collide with config options from a project that calls the import. This
patch wraps the side effect that registers config options into a
function that needs to be called in case the caller wants to register
the options.

This solution is also not perfect as it guards the common options to be
registered only once even if the function is called multiple times. This
is to solve problems in unittests, ideally we should always call the
function just once even in our testing suites.

Resolves-Bug: #1968606
Change-Id: Ic1532eb8de887ff1b1085206df11f53e22f7f524
Signed-off-by: Jakub Libosvar <libosvar@redhat.com>
This commit is contained in:
Jakub Libosvar 2022-04-11 21:45:49 +00:00
parent fe2bba45d0
commit 4d3a274765
32 changed files with 76 additions and 29 deletions

View File

@ -30,6 +30,7 @@ from neutron import service as neutron_service
def register_options(conf):
common_config.register_common_config_options()
config.register_interface_driver_opts_helper(conf)
config.register_agent_state_opts_helper(conf)
config.register_availability_zone_opts_helper(conf)

View File

@ -166,6 +166,7 @@ class MonitorDaemon(daemon.Daemon):
def configure(conf):
config.register_common_config_options()
config.init(sys.argv[1:])
conf.set_override('log_dir', cfg.CONF.conf_dir)
conf.set_override('debug', True)

View File

@ -31,6 +31,7 @@ from neutron import service as neutron_service
def register_opts(conf):
common_config.register_common_config_options()
l3_config.register_l3_agent_config_opts(l3_config.OPTS, conf)
ha_conf.register_l3_agent_ha_opts(conf)
meta_conf.register_meta_conf_opts(meta_conf.SHARED_OPTS, conf)

View File

@ -29,6 +29,7 @@ LOG = logging.getLogger(__name__)
def main():
config.register_common_config_options()
meta.register_meta_conf_opts(meta.SHARED_OPTS)
meta.register_meta_conf_opts(meta.UNIX_DOMAIN_METADATA_PROXY_OPTS)
meta.register_meta_conf_opts(meta.METADATA_PROXY_HANDLER_OPTS)

View File

@ -26,6 +26,7 @@ LOG = logging.getLogger(__name__)
def main():
config.register_common_config_options()
ovn_meta.register_meta_conf_opts(meta.SHARED_OPTS)
ovn_meta.register_meta_conf_opts(meta.UNIX_DOMAIN_METADATA_PROXY_OPTS)
ovn_meta.register_meta_conf_opts(meta.METADATA_PROXY_HANDLER_OPTS)

View File

@ -78,6 +78,7 @@ class PatchPortCleaner(object):
def main():
common_config.register_common_config_options()
common_config.init(sys.argv[1:])
ovs_conf.register_ovs_agent_opts()
common_config.setup_logging()

View File

@ -29,6 +29,7 @@ from neutron import manager
def main():
config.register_common_config_options()
config.init(sys.argv[1:])
config.setup_logging()

View File

@ -32,6 +32,7 @@ def setup_conf():
from the main config that do not apply during clean-up.
"""
conf = cfg.CONF
config.register_common_config_options()
agent_config.register_root_helper(conf=conf)
agent_config.setup_privsep()
command.register_cmd_opts(command.ip_opts, conf)

View File

@ -16,6 +16,7 @@ from neutron_lib.utils import helpers
from oslo_config import cfg
from oslo_log import log as logging
from neutron.common import config as common_config
from neutron.conf.agent import common as config
from neutron.plugins.ml2.drivers.linuxbridge.agent \
import linuxbridge_neutron_agent
@ -67,6 +68,7 @@ def main():
This tool should not be called during an instance create, migrate, etc. as
it can delete a linux bridge about to be used by nova.
"""
common_config.register_common_config_options()
cfg.CONF(sys.argv[1:])
config.setup_logging()
config.setup_privsep()

View File

@ -66,6 +66,7 @@ def setup_conf():
"""
conf = cfg.CONF
config.register_common_config_options()
cmd.register_cmd_opts(cmd.netns_opts, conf)
agent_config.register_interface_driver_opts_helper(conf)
dhcp_config.register_agent_dhcp_opts(conf)

View File

@ -20,6 +20,7 @@ from oslo_config import cfg
from oslo_db import options as db_options
from oslo_log import log as logging
from neutron.common import config as common_config
from neutron.conf.agent import securitygroups_rpc
from neutron.conf.plugins.ml2.drivers.ovn import ovn_conf
from neutron import manager
@ -137,6 +138,7 @@ class AgentNotifierApi(object):
def setup_conf():
conf = cfg.CONF
common_config.register_common_config_options()
ml2_group, ml2_opts = neutron_options.list_ml2_conf_opts()[0]
cfg.CONF.register_cli_opts(ml2_opts, ml2_group)
cfg.CONF.register_cli_opts(securitygroups_rpc.security_group_opts,

View File

@ -39,6 +39,7 @@ def setup_conf():
"""
conf = cfg.CONF
config.register_common_config_options()
cmd.register_cmd_opts(cmd.ovs_opts, conf)
l3_config.register_l3_agent_config_opts(l3_config.OPTS, conf)
agent_config.register_interface_driver_opts_helper(conf)

View File

@ -19,6 +19,7 @@ from oslo_db import options as db_options
from oslo_log import log as logging
from neutron.api import converters as n_converters
from neutron.common import config as common_config
from neutron.objects import ports as port_obj
from neutron.objects.qos import binding as qos_binding_obj
from neutron.objects.qos import rule as qos_rule_obj
@ -29,6 +30,7 @@ LOG = logging.getLogger(__name__)
def setup_conf():
conf = cfg.CONF
common_config.register_common_config_options()
db_group, neutron_db_opts = db_options.list_opts()[0]
cfg.CONF.register_cli_opts(neutron_db_opts, db_group)
conf()

View File

@ -17,6 +17,7 @@ from oslo_config import cfg
from oslo_db import options as db_options
from oslo_log import log as logging
from neutron.common import config as common_config
from neutron.db import models_v2
@ -25,6 +26,7 @@ LOG = logging.getLogger(__name__)
def setup_conf():
conf = cfg.CONF
common_config.register_common_config_options()
db_group, neutron_db_opts = db_options.list_opts()[0]
cfg.CONF.register_cli_opts(neutron_db_opts, db_group)
conf()

View File

@ -37,6 +37,7 @@ LOG = logging.getLogger(__name__)
def setup_conf():
config.register_common_config_options()
ovs_conf.register_ovs_agent_opts(cfg.CONF)
lb_conf.register_linuxbridge_opts(cfg.CONF)
sriov_conf.register_agent_sriov_nic_opts(cfg.CONF)

View File

@ -18,6 +18,7 @@ from oslo_db import options as db_options
from oslo_log import log as logging
from oslo_upgradecheck import upgradecheck
from neutron.common import config as common_config
from neutron.conf import common as neutron_conf_base
from neutron.conf import service as neutron_conf_service
@ -50,7 +51,7 @@ def setup_conf(conf=cfg.CONF):
Use separate setup_conf for the utility because there are many options
from the main config that do not apply during checks.
"""
common_config.register_common_config_options()
neutron_conf_base.register_core_common_config_opts(conf)
neutron_conf_service.register_service_opts(
neutron_conf_service.SERVICE_OPTS, cfg.CONF)

View File

@ -37,6 +37,8 @@ from neutron import version
LOG = logging.getLogger(__name__)
_COMMON_OPTIONS_ALREADY_REGISTERED = False
# Jam here any extra log level default you care about. This helps keep
# Neutron logs lean.
EXTRA_LOG_LEVEL_DEFAULTS = [
@ -46,40 +48,46 @@ EXTRA_LOG_LEVEL_DEFAULTS = [
'os_ken.controller.controller=INFO',
]
# Register the configuration options
common_config.register_core_common_config_opts()
# Ensure that the control exchange is set correctly
oslo_messaging.set_transport_defaults(control_exchange='neutron')
def register_common_config_options():
global _COMMON_OPTIONS_ALREADY_REGISTERED
if _COMMON_OPTIONS_ALREADY_REGISTERED:
return
# Register the configuration options
common_config.register_core_common_config_opts()
NOVA_CONF_SECTION = 'nova'
# Ensure that the control exchange is set correctly
oslo_messaging.set_transport_defaults(control_exchange='neutron')
ks_loading.register_auth_conf_options(cfg.CONF, NOVA_CONF_SECTION)
ks_loading.register_session_conf_options(cfg.CONF, NOVA_CONF_SECTION)
ks_loading.register_auth_conf_options(
cfg.CONF, common_config.NOVA_CONF_SECTION)
ks_loading.register_session_conf_options(
cfg.CONF, common_config.NOVA_CONF_SECTION)
# Register the nova configuration options
common_config.register_nova_opts()
# Register the nova configuration options
common_config.register_nova_opts()
ks_loading.register_auth_conf_options(cfg.CONF,
common_config.PLACEMENT_CONF_SECTION)
ks_loading.register_session_conf_options(
cfg.CONF, common_config.PLACEMENT_CONF_SECTION)
ks_loading.register_auth_conf_options(cfg.CONF,
common_config.PLACEMENT_CONF_SECTION)
ks_loading.register_session_conf_options(cfg.CONF,
common_config.PLACEMENT_CONF_SECTION)
# Register the placement configuration options
common_config.register_placement_opts()
# Register the placement configuration options
common_config.register_placement_opts()
logging.register_options(cfg.CONF)
logging.register_options(cfg.CONF)
# Register the ironic configuration options
ks_loading.register_auth_conf_options(cfg.CONF,
common_config.IRONIC_CONF_SECTION)
ks_loading.register_session_conf_options(cfg.CONF,
common_config.IRONIC_CONF_SECTION)
ks_loading.register_adapter_conf_options(cfg.CONF,
common_config.IRONIC_CONF_SECTION)
common_config.register_ironic_opts()
# Register the ironic configuration options
ks_loading.register_auth_conf_options(cfg.CONF,
common_config.IRONIC_CONF_SECTION)
ks_loading.register_session_conf_options(cfg.CONF,
common_config.IRONIC_CONF_SECTION)
ks_loading.register_adapter_conf_options(cfg.CONF,
common_config.IRONIC_CONF_SECTION)
common_config.register_ironic_opts()
_COMMON_OPTIONS_ALREADY_REGISTERED = True
def init(args, default_config_files=None, **kwargs):

View File

@ -27,6 +27,7 @@ from oslo_utils import fileutils
from oslo_utils import importutils
from neutron._i18n import _
from neutron.common import config as common_config
from neutron.conf.db import migration_cli
from neutron.db import migration
from neutron.db.migration.connection import DBConnection
@ -651,6 +652,7 @@ def get_engine_config():
def main():
common_config.register_common_config_options()
# Interpret the config file for Python logging.
# This line sets up loggers basically.
logging_config.fileConfig(neutron_alembic_ini)

View File

@ -20,6 +20,7 @@ from oslo_utils import importutils
from neutron._i18n import _
from neutron.agent.common import utils
from neutron.common import config as common_config
from neutron.conf.agent import common as config
from neutron.conf.plugins.ml2.drivers import ovs_conf
from neutron.debug import debug_agent
@ -86,5 +87,6 @@ class NeutronDebugShell(shell.NeutronShell):
def main(argv=None):
common_config.register_common_config_options()
return NeutronDebugShell(shell.NEUTRON_API_VERSION).run(
argv or sys.argv[1:])

View File

@ -1012,6 +1012,7 @@ class LinuxBridgeRpcCallbacks(sg_rpc.SecurityGroupAgentRpcCallbackMixin,
def main():
common_config.register_common_config_options()
common_config.init(sys.argv[1:])
common_config.setup_logging()

View File

@ -206,6 +206,7 @@ def validate_firewall_driver():
def main():
common_config.register_common_config_options()
common_config.init(sys.argv[1:])
common_config.setup_logging()

View File

@ -537,6 +537,7 @@ class SriovNicAgentConfigParser(object):
def main():
common_config.register_common_config_options()
common_config.init(sys.argv[1:])
common_config.setup_logging()

View File

@ -30,6 +30,7 @@ cfg.CONF.import_group('OVS', 'neutron.plugins.ml2.drivers.openvswitch.agent.'
def main():
common_config.register_common_config_options()
common_config.init(sys.argv[1:])
of_main.init_config()
common_config.setup_logging()

View File

@ -646,7 +646,9 @@ class OVSNeutronAgent(l2population_rpc.L2populationRpcCallBackTunnelMixin,
def _add_port_to_updated_smartnic_ports(self, mac, vif_name, iface_id,
vif_type, vm_uuid='',
mtu=plugin_utils.get_deployment_physnet_mtu()):
mtu=None):
if mtu is None:
mtu = plugin_utils.get_deployment_physnet_mtu()
self.updated_smartnic_ports.append({
'mac': mac,
'vm_uuid': vm_uuid,

View File

@ -52,7 +52,7 @@ def _get_config_files(env=None):
def _init_configuration():
# the configuration will be read into the cfg.CONF global data structure
conf_files = _get_config_files()
config.register_common_config_options()
config.init(sys.argv[1:], default_config_files=conf_files)
config.setup_logging()
config.set_config_defaults()

View File

@ -395,6 +395,7 @@ class MeteringAgentWithStateReport(MeteringAgent):
def main():
conf = cfg.CONF
common_config.register_common_config_options()
metering_agent.register_metering_agent_opts()
config.register_agent_state_opts_helper(conf)
common_config.init(sys.argv[1:])

View File

@ -62,7 +62,6 @@ from neutron.tests import post_mortem_debug
CONF = cfg.CONF
CONF.import_opt('state_path', 'neutron.conf.common')
ROOTDIR = os.path.dirname(__file__)
ETCDIR = os.path.join(ROOTDIR, 'etc')
@ -383,6 +382,7 @@ class BaseTestCase(DietTestCase):
self.useFixture(lockutils.ExternalLockFixture())
self.useFixture(fixture.APIDefinitionFixture())
config.register_common_config_options()
cfg.CONF.set_override('state_path', self.get_default_temp_dir().path)
self.addCleanup(CONF.reset)

View File

@ -22,6 +22,7 @@ from oslo_utils import uuidutils
from neutron.agent.linux import dhcp as linux_dhcp
from neutron.cmd.eventlet.agents import dhcp as dhcp_agent
from neutron.common import config
OPTS = [
@ -76,6 +77,7 @@ def monkeypatch_linux_dhcp():
def main():
config.register_common_config_options()
cfg.CONF.register_opts(OPTS)
monkeypatch_linux_dhcp()
dhcp_agent.main()

View File

@ -17,10 +17,12 @@ import sys
from oslo_config import cfg # noqa
from neutron.common import config
from neutron.common import eventlet_utils
from neutron.tests.common.agents import l3_agent
eventlet_utils.monkey_patch()
if __name__ == "__main__":
config.register_common_config_options()
sys.exit(l3_agent.main())

View File

@ -21,6 +21,7 @@ from oslo_config import cfg
from neutron.agent.common import ovs_lib
from neutron.agent.common import polling
from neutron.agent.l2.extensions import qos as qos_extension
from neutron.common import config
from neutron.services.trunk.drivers.openvswitch.agent \
import driver as trunk_driver
from neutron.tests.common.agents import ovs_agent
@ -61,6 +62,7 @@ def main():
# https://review.opendev.org/#/c/506722/ will be merged and ovsdb-server
# ovs-vswitchd processes for each test will be isolated in separate
# namespace
config.register_common_config_options()
monkeypatch_init_handler()
monkeypatch_qos()
monkeypatch_event_filtering()

View File

@ -135,6 +135,7 @@ class FakePlacement(object):
if __name__ == "__main__":
common_config.register_common_config_options()
common_config.init(sys.argv[1:])
common_config.setup_logging()
placement_port = cfg.CONF.placement_port

View File

@ -27,6 +27,7 @@ import sqlalchemy
from sqlalchemy import event # noqa
from sqlalchemy.sql import ddl as sqla_ddl
from neutron.common import config
from neutron.db import migration as migration_root
from neutron.db.migration.alembic_migrations import external
from neutron.db.migration import cli as migration
@ -35,7 +36,6 @@ from neutron.tests import base as test_base
from neutron.tests.functional import base as functional_base
from neutron.tests.unit import testlib_api
cfg.CONF.import_opt('core_plugin', 'neutron.conf.common')
CREATION_OPERATIONS = {
'sqla': (sqla_ddl.CreateIndex,
@ -143,6 +143,7 @@ class _TestModelsMigrations(test_migrations.ModelsMigrationsSync):
TIMEOUT_SCALING_FACTOR = 4
def setUp(self):
config.register_common_config_options()
super(_TestModelsMigrations, self).setUp()
self.cfg = self.useFixture(config_fixture.Config())
self.cfg.config(core_plugin='ml2')