From 8f9eeb801a27490b9b3d68a4fe8b00c488c72cb7 Mon Sep 17 00:00:00 2001 From: Hieu LE Date: Wed, 17 Aug 2016 14:27:22 +0700 Subject: [PATCH] Centralize config option: cluster_heat section Centralize config option of Cluster Heat section. Replace oslo_conf cfg to magnum.conf. Change-Id: I9118eeb17061a0aa26269ea9deaba28e79f28b76 Implements: blueprint centralize-config-magnum --- .../conductor/handlers/cluster_conductor.py | 42 +++------------ magnum/conf/__init__.py | 2 + magnum/conf/cluster_heat.py | 52 +++++++++++++++++++ ...592131657ca1_add_coe_column_to_baymodel.py | 17 ++---- magnum/opts.py | 5 +- magnum/tests/functional/python_client_base.py | 9 ++-- .../handlers/test_cluster_conductor.py | 12 +++-- .../handlers/test_mesos_cluster_conductor.py | 6 +-- .../handlers/test_swarm_cluster_conductor.py | 6 +-- 9 files changed, 87 insertions(+), 64 deletions(-) create mode 100644 magnum/conf/cluster_heat.py diff --git a/magnum/conductor/handlers/cluster_conductor.py b/magnum/conductor/handlers/cluster_conductor.py index cb5b51e389..929e4202e4 100644 --- a/magnum/conductor/handlers/cluster_conductor.py +++ b/magnum/conductor/handlers/cluster_conductor.py @@ -16,7 +16,6 @@ import os from heatclient.common import template_utils from heatclient import exc -from oslo_config import cfg from oslo_log import log as logging from oslo_service import loopingcall from oslo_utils import importutils @@ -30,6 +29,7 @@ from magnum.conductor.handlers.common import cert_manager from magnum.conductor.handlers.common import trust_manager from magnum.conductor import scale_manager from magnum.conductor import utils as conductor_utils +import magnum.conf from magnum.drivers.common import template_def from magnum.i18n import _ from magnum.i18n import _LE @@ -37,33 +37,7 @@ from magnum.i18n import _LI from magnum import objects from magnum.objects import fields - -cluster_heat_opts = [ - cfg.IntOpt('max_attempts', - default=2000, - help=('Number of attempts to query the Heat stack for ' - 'finding out the status of the created stack and ' - 'getting template outputs. This value is ignored ' - 'during cluster creation if timeout is set as the poll ' - 'will continue until cluster creation either ends ' - 'or times out.'), - deprecated_group='bay_heat'), - cfg.IntOpt('wait_interval', - default=1, - help=('Sleep time interval between two attempts of querying ' - 'the Heat stack. This interval is in seconds.'), - deprecated_group='bay_heat'), - cfg.IntOpt('create_timeout', - default=60, - help=('The length of time to let cluster creation continue. ' - 'This interval is in minutes. The default is 60 minutes.' - ), - deprecated_group='bay_heat', - deprecated_name='bay_create_timeout') -] - -CONF = cfg.CONF -CONF.register_opts(cluster_heat_opts, group='cluster_heat') +CONF = magnum.conf.CONF LOG = logging.getLogger(__name__) @@ -108,7 +82,7 @@ def _create_stack(context, osc, cluster, create_timeout): else: # no create_timeout value was passed in to the request # so falling back on configuration file value - heat_timeout = cfg.CONF.cluster_heat.create_timeout + heat_timeout = CONF.cluster_heat.create_timeout fields = { 'stack_name': stack_name, 'parameters': heat_params, @@ -270,7 +244,7 @@ class Handler(object): def _poll_and_check(self, osc, cluster): poller = HeatPoller(osc, cluster) lc = loopingcall.FixedIntervalLoopingCall(f=poller.poll_and_check) - lc.start(cfg.CONF.cluster_heat.wait_interval, True) + lc.start(CONF.cluster_heat.wait_interval, True) class HeatPoller(object): @@ -338,18 +312,18 @@ class HeatPoller(object): # the loop will end when the stack completes or the timeout occurs if stack.stack_status == fields.ClusterStatus.CREATE_IN_PROGRESS: if (stack.timeout_mins is None and - self.attempts > cfg.CONF.cluster_heat.max_attempts): + self.attempts > CONF.cluster_heat.max_attempts): LOG.error(_LE('Cluster check exit after %(attempts)s attempts,' 'stack_id: %(id)s, stack_status: %(status)s') % - {'attempts': cfg.CONF.cluster_heat.max_attempts, + {'attempts': CONF.cluster_heat.max_attempts, 'id': self.cluster.stack_id, 'status': stack.stack_status}) raise loopingcall.LoopingCallDone() else: - if self.attempts > cfg.CONF.cluster_heat.max_attempts: + if self.attempts > CONF.cluster_heat.max_attempts: LOG.error(_LE('Cluster check exit after %(attempts)s attempts,' 'stack_id: %(id)s, stack_status: %(status)s') % - {'attempts': cfg.CONF.cluster_heat.max_attempts, + {'attempts': CONF.cluster_heat.max_attempts, 'id': self.cluster.stack_id, 'status': stack.stack_status}) raise loopingcall.LoopingCallDone() diff --git a/magnum/conf/__init__.py b/magnum/conf/__init__.py index a999489f22..19bf4fccac 100644 --- a/magnum/conf/__init__.py +++ b/magnum/conf/__init__.py @@ -20,6 +20,7 @@ from magnum.conf import barbican # from magnum.conf import certificates from magnum.conf import cinder from magnum.conf import cluster +from magnum.conf import cluster_heat from magnum.conf import cluster_templates from magnum.conf import conductor # from magnum.conf import database @@ -38,6 +39,7 @@ api.register_opts(CONF) barbican.register_opts(CONF) cluster.register_opts(CONF) cluster_templates.register_opts(CONF) +cluster_heat.register_opts(CONF) # certificates.register_opts(CONF) cinder.register_opts(CONF) conductor.register_opts(CONF) diff --git a/magnum/conf/cluster_heat.py b/magnum/conf/cluster_heat.py new file mode 100644 index 0000000000..07eb9e5259 --- /dev/null +++ b/magnum/conf/cluster_heat.py @@ -0,0 +1,52 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may not +# use this file except in compliance with the License. You may obtain a copy +# of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from oslo_config import cfg + +cluster_heat_group = cfg.OptGroup(name='cluster_heat', + title='Heat options for Cluster ' + 'configuration') + +cluster_heat_opts = [ + cfg.IntOpt('max_attempts', + default=2000, + help=('Number of attempts to query the Heat stack for ' + 'finding out the status of the created stack and ' + 'getting template outputs. This value is ignored ' + 'during cluster creation if timeout is set as the poll ' + 'will continue until cluster creation either ends ' + 'or times out.'), + deprecated_group='bay_heat'), + cfg.IntOpt('wait_interval', + default=1, + help=('Sleep time interval between two attempts of querying ' + 'the Heat stack. This interval is in seconds.'), + deprecated_group='bay_heat'), + cfg.IntOpt('create_timeout', + default=60, + help=('The length of time to let cluster creation continue. ' + 'This interval is in minutes. The default is 60 minutes.' + ), + deprecated_group='bay_heat', + deprecated_name='bay_create_timeout') +] + + +def register_opts(conf): + conf.register_group(cluster_heat_group) + conf.register_opts(cluster_heat_opts, group=cluster_heat_group) + + +def list_opts(): + return { + cluster_heat_group: cluster_heat_opts + } diff --git a/magnum/db/sqlalchemy/alembic/versions/592131657ca1_add_coe_column_to_baymodel.py b/magnum/db/sqlalchemy/alembic/versions/592131657ca1_add_coe_column_to_baymodel.py index 18f1538193..5fa5159c83 100644 --- a/magnum/db/sqlalchemy/alembic/versions/592131657ca1_add_coe_column_to_baymodel.py +++ b/magnum/db/sqlalchemy/alembic/versions/592131657ca1_add_coe_column_to_baymodel.py @@ -23,20 +23,11 @@ revision = '592131657ca1' down_revision = '4956f03cabad' from alembic import op -from oslo_config import cfg + +import magnum.conf import sqlalchemy as sa -from magnum.i18n import _ - - -cluster_heat_opts = [ - cfg.StrOpt('cluster_coe', - default='kubernetes', - help=_('Container Orchestration Environments are ' - 'kubernetes or swarm.'), - deprecated_group='bay_heat') -] -cfg.CONF.register_opts(cluster_heat_opts, group='cluster_heat') +CONF = magnum.conf.CONF def upgrade(): @@ -48,5 +39,5 @@ def upgrade(): op.execute( baymodel.update().values({ - 'coe': op.inline_literal(cfg.CONF.cluster_heat.cluster_coe)}) + 'coe': op.inline_literal("kubernetes")}) ) diff --git a/magnum/opts.py b/magnum/opts.py index e67a812827..cc53e0c172 100644 --- a/magnum/opts.py +++ b/magnum/opts.py @@ -20,9 +20,10 @@ from magnum.common.cert_manager import local_cert_manager import magnum.common.exception import magnum.common.rpc_service import magnum.common.service +import magnum.common.utils import magnum.common.x509.config -import magnum.conductor.handlers.cluster_conductor import magnum.db +import magnum.drivers.common.template_def def list_opts(): @@ -37,8 +38,6 @@ def list_opts(): ('docker', magnum.common.docker_utils.docker_opts), ('trust', magnum.common.keystone.trust_opts), ('x509', magnum.common.x509.config.x509_opts), - ('cluster_heat', - magnum.conductor.handlers.cluster_conductor.cluster_heat_opts), ('certificates', itertools.chain(magnum.common.cert_manager.cert_manager_opts, local_cert_manager.local_cert_manager_opts, diff --git a/magnum/tests/functional/python_client_base.py b/magnum/tests/functional/python_client_base.py index 063e24a002..ceaac9795d 100644 --- a/magnum/tests/functional/python_client_base.py +++ b/magnum/tests/functional/python_client_base.py @@ -18,7 +18,6 @@ Tests for `magnum` module. """ import os -from oslo_config import cfg import subprocess import tempfile import time @@ -30,7 +29,9 @@ from heatclient import client as heatclient from k8sclient.client import api_client from k8sclient.client.apis import apiv_api from keystoneclient.v2_0 import client as ksclient + from magnum.common.utils import rmtree_without_raise +import magnum.conf from magnum.i18n import _LI from magnum.tests.functional.common import base from magnum.tests.functional.common import utils @@ -38,6 +39,8 @@ from magnumclient.common.apiclient import exceptions from magnumclient.common import cliutils from magnumclient.v1 import client as v1client +CONF = magnum.conf.CONF + class BaseMagnumClient(base.BaseMagnumTest): @@ -268,9 +271,9 @@ extendedKeyUsage = clientAuth test_timeout = int(test_timeout) except ValueError: # If timeout value is invalid, set a default timeout. - test_timeout = cfg.CONF.cluster_heat.create_timeout + test_timeout = CONF.cluster_heat.create_timeout if test_timeout <= 0: - test_timeout = cfg.CONF.cluster_heat.create_timeout + test_timeout = CONF.cluster_heat.create_timeout self.useFixture(fixtures.Timeout(test_timeout, gentle=True)) diff --git a/magnum/tests/unit/conductor/handlers/test_cluster_conductor.py b/magnum/tests/unit/conductor/handlers/test_cluster_conductor.py index 618082f040..caefcfb10d 100644 --- a/magnum/tests/unit/conductor/handlers/test_cluster_conductor.py +++ b/magnum/tests/unit/conductor/handlers/test_cluster_conductor.py @@ -19,12 +19,12 @@ import six from heatclient import exc import mock from mock import patch -from oslo_config import cfg from oslo_service import loopingcall from pycadf import cadftaxonomy as taxonomy from magnum.common import exception from magnum.conductor.handlers import cluster_conductor +import magnum.conf from magnum import objects from magnum.objects.fields import ClusterStatus as cluster_status from magnum.tests import base @@ -32,6 +32,8 @@ from magnum.tests import fake_notifier from magnum.tests.unit.db import base as db_base from magnum.tests.unit.db import utils +CONF = magnum.conf.CONF + class TestHandler(db_base.DbTestCase): @@ -689,14 +691,14 @@ class TestHeatPoller(base.TestCase): mock_heat_stack, cluster, poller = self.setup_poll_test() mock_heat_stack.stack_status = cluster_status.DELETE_IN_PROGRESS - poller.attempts = cfg.CONF.cluster_heat.max_attempts + poller.attempts = CONF.cluster_heat.max_attempts self.assertRaises(loopingcall.LoopingCallDone, poller.poll_and_check) def test_poll_create_in_prog_max_att_reached_no_timeout(self): mock_heat_stack, cluster, poller = self.setup_poll_test() mock_heat_stack.stack_status = cluster_status.CREATE_IN_PROGRESS - poller.attempts = cfg.CONF.cluster_heat.max_attempts + poller.attempts = CONF.cluster_heat.max_attempts mock_heat_stack.timeout_mins = None self.assertRaises(loopingcall.LoopingCallDone, poller.poll_and_check) @@ -704,7 +706,7 @@ class TestHeatPoller(base.TestCase): mock_heat_stack, cluster, poller = self.setup_poll_test() mock_heat_stack.stack_status = cluster_status.CREATE_IN_PROGRESS - poller.attempts = cfg.CONF.cluster_heat.max_attempts + poller.attempts = CONF.cluster_heat.max_attempts mock_heat_stack.timeout_mins = 60 # since the timeout is set the max attempts gets ignored since # the timeout will eventually stop the poller either when @@ -715,7 +717,7 @@ class TestHeatPoller(base.TestCase): mock_heat_stack, cluster, poller = self.setup_poll_test() mock_heat_stack.stack_status = cluster_status.CREATE_FAILED - poller.attempts = cfg.CONF.cluster_heat.max_attempts + poller.attempts = CONF.cluster_heat.max_attempts mock_heat_stack.timeout_mins = 60 self.assertRaises(loopingcall.LoopingCallDone, poller.poll_and_check) diff --git a/magnum/tests/unit/conductor/handlers/test_mesos_cluster_conductor.py b/magnum/tests/unit/conductor/handlers/test_mesos_cluster_conductor.py index f69af100d2..0d452a5da6 100644 --- a/magnum/tests/unit/conductor/handlers/test_mesos_cluster_conductor.py +++ b/magnum/tests/unit/conductor/handlers/test_mesos_cluster_conductor.py @@ -279,11 +279,11 @@ class TestClusterConductorWithMesos(base.TestCase): env_files) @patch('magnum.conductor.utils.retrieve_cluster_template') - @patch('oslo_config.cfg') + @patch('magnum.conf.CONF') @patch('magnum.common.clients.OpenStackClients') - def setup_poll_test(self, mock_openstack_client, cfg, + def setup_poll_test(self, mock_openstack_client, mock_conf, mock_retrieve_cluster_template): - cfg.CONF.cluster_heat.max_attempts = 10 + mock_conf.cluster_heat.max_attempts = 10 cluster = mock.MagicMock() mock_heat_stack = mock.MagicMock() diff --git a/magnum/tests/unit/conductor/handlers/test_swarm_cluster_conductor.py b/magnum/tests/unit/conductor/handlers/test_swarm_cluster_conductor.py index d1acf3cc16..7d1455184a 100644 --- a/magnum/tests/unit/conductor/handlers/test_swarm_cluster_conductor.py +++ b/magnum/tests/unit/conductor/handlers/test_swarm_cluster_conductor.py @@ -389,11 +389,11 @@ class TestClusterConductorWithSwarm(base.TestCase): env_files) @patch('magnum.conductor.utils.retrieve_cluster_template') - @patch('oslo_config.cfg') + @patch('magnum.conf.CONF') @patch('magnum.common.clients.OpenStackClients') - def setup_poll_test(self, mock_openstack_client, cfg, + def setup_poll_test(self, mock_openstack_client, mock_conf, mock_retrieve_cluster_template): - cfg.CONF.cluster_heat.max_attempts = 10 + mock_conf.cluster_heat.max_attempts = 10 cluster = mock.MagicMock() mock_heat_stack = mock.MagicMock()