Don't register cli opts on import

A recent change in oslo.policy has made it register its cli opts on
the global config object. This was done to fix a bug where the opts
passed to the oslo.policy cli tools would get lost once it called
into project code because it was previously using a private config
object.

Octavia had already fixed this bug in a different way by filtering
the args in the enforcer code, which should no longer be necessary
now that the oslo.policy fix has merged.

However, the use of the global config object by the policy cli has
introduced a new problem, which is that after the config object is
initialized you can't register more cli opts. Because Octavia was
registering cli opts on import, this means that when the policy
tools call the Octavia policy entrypoint those opts get registered
and cause a failure.

To fix that, this change moves the cli opt registration into a
function that gets called from config.init so they will only get
registered when running an actual Octavia service. A separate
function was needed because they also need to be registered in
unit tests, and we don't want to actually initialize the entire
config object there. This way they can be initialized properly
in both scenarios.

Change-Id: I48ae260335f67e8ab1a188a94e44a7f1968e6fe9
This commit is contained in:
Ben Nemec 2020-03-31 21:18:38 +00:00 committed by Michael Johnson
parent cd176e55c5
commit 19d80f11a4
3 changed files with 8 additions and 18 deletions

View File

@ -735,9 +735,7 @@ cfg.CONF.register_opts(controller_worker_opts, group='controller_worker')
cfg.CONF.register_opts(keepalived_vrrp_opts, group='keepalived_vrrp')
cfg.CONF.register_opts(task_flow_opts, group='task_flow')
cfg.CONF.register_opts(house_keeping_opts, group='house_keeping')
cfg.CONF.register_cli_opts(core_cli_opts)
cfg.CONF.register_opts(certificate_opts, group='certificates')
cfg.CONF.register_cli_opts(healthmanager_opts, group='health_manager')
cfg.CONF.register_opts(nova_opts, group='nova')
cfg.CONF.register_opts(cinder_opts, group='cinder')
cfg.CONF.register_opts(glance_opts, group='glance')
@ -757,13 +755,18 @@ _SQL_CONNECTION_DEFAULT = 'sqlite://'
db_options.set_defaults(cfg.CONF, connection=_SQL_CONNECTION_DEFAULT,
max_pool_size=10, max_overflow=20, pool_timeout=10)
logging.register_options(cfg.CONF)
ks_loading.register_auth_conf_options(cfg.CONF, constants.SERVICE_AUTH)
ks_loading.register_session_conf_options(cfg.CONF, constants.SERVICE_AUTH)
def register_cli_opts():
cfg.CONF.register_cli_opts(core_cli_opts)
cfg.CONF.register_cli_opts(healthmanager_opts, group='health_manager')
logging.register_options(cfg.CONF)
def init(args, **kwargs):
register_cli_opts()
cfg.CONF(args=args, project='octavia',
version='%%prog %s' % version.version_info.release_string(),
**kwargs)

View File

@ -11,15 +11,11 @@
# under the License.
"""Policy Engine For Octavia."""
import sys
from oslo_config import cfg
from oslo_log import log as logging
from oslo_policy import policy as oslo_policy
from oslo_utils import excutils
from octavia.common import config
from octavia.common import exceptions
from octavia import policies
@ -153,14 +149,4 @@ class IsAdminCheck(oslo_policy.Check):
# This is used for the oslopolicy-policy-generator tool
def get_no_context_enforcer():
# oslo.config needs access to the --config-dir and --config-file
# command line args
filtered_args = ['--config-dir', '--config-file']
# Start at 1 because cfg.CONF expects the equivalent of sys.argv[1:]
conf_args = [arg for idx, arg in enumerate(sys.argv[1:])
if (arg in filtered_args or
sys.argv[idx] in filtered_args)]
config.init(conf_args)
return Policy()

View File

@ -30,6 +30,7 @@ class TestCase(testtools.TestCase):
def setUp(self):
super(TestCase, self).setUp()
config.register_cli_opts()
self.addCleanup(mock.patch.stopall)
self.addCleanup(self.clean_caches)