diff --git a/README.md b/README.md index 085fe5d7..82c158a4 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,11 @@ # Overview -This principle charm provides the OpenStack Neutron API service which was previously provided by the nova-cloud-controller charm. +This principle charm provides the OpenStack Neutron API service which +was previously provided by the nova-cloud-controller charm. -When this charm is related to the nova-cloud-controller charm the nova-cloud controller charm will shutdown its api service, de-register it from keystone and inform the compute nodes of the new neutron url. +When this charm is related to the nova-cloud-controller charm the +nova-cloud controller charm will shutdown its api service, de-register +it from keystone and inform the compute nodes of the new neutron url. # Usage @@ -16,7 +19,8 @@ To deploy (partial deployment only): juju add-relation neutron-api neutron-openvswitch juju add-relation neutron-api nova-cloud-controller -This charm also supports scale out and high availability using the hacluster charm: +This charm also supports scale out and high availability using the +hacluster charm: juju deploy hacluster neutron-hacluster juju add-unit neutron-api @@ -25,36 +29,38 @@ This charm also supports scale out and high availability using the hacluster cha ## HA/Clustering -There are two mutually exclusive high availability options: using virtual -IP(s) or DNS. In both cases, a relationship to hacluster is required which -provides the corosync back end HA functionality. +There are two mutually exclusive high availability options: using +virtual IP(s) or DNS. In both cases, a relationship to hacluster is +required which provides the corosync back end HA functionality. -To use virtual IP(s) the clustered nodes must be on the same subnet such that -the VIP is a valid IP on the subnet for one of the node's interfaces and each -node has an interface in said subnet. The VIP becomes a highly-available API -endpoint. +To use virtual IP(s) the clustered nodes must be on the same subnet +such that the VIP is a valid IP on the subnet for one of the node's +interfaces and each node has an interface in said subnet. The VIP +becomes a highly-available API endpoint. -At a minimum, the config option 'vip' must be set in order to use virtual IP -HA. If multiple networks are being used, a VIP should be provided for each -network, separated by spaces. Optionally, vip_iface or vip_cidr may be -specified. +At a minimum, the config option 'vip' must be set in order to use +virtual IP HA. If multiple networks are being used, a VIP should be +provided for each network, separated by spaces. Optionally, vip_iface +or vip_cidr may be specified. -To use DNS high availability there are several prerequisites. However, DNS HA -does not require the clustered nodes to be on the same subnet. +To use DNS high availability there are several prerequisites. However, +DNS HA does not require the clustered nodes to be on the same subnet. Currently the DNS HA feature is only available for MAAS 2.0 or greater -environments. MAAS 2.0 requires Juju 2.0 or greater. The clustered nodes must -have static or "reserved" IP addresses registered in MAAS. The DNS hostname(s) -must be pre-registered in MAAS before use with DNS HA. +environments. MAAS 2.0 requires Juju 2.0 or greater. The clustered +nodes must have static or "reserved" IP addresses registered in MAAS. +The DNS hostname(s) must be pre-registered in MAAS before use with DNS +HA. -At a minimum, the config option 'dns-ha' must be set to true and at least one -of 'os-public-hostname', 'os-internal-hostname' or 'os-internal-hostname' must -be set in order to use DNS HA. One or more of the above hostnames may be set. +At a minimum, the config option 'dns-ha' must be set to true and at +least one of 'os-public-hostname', 'os-internal-hostname' or +'os-internal-hostname' must be set in order to use DNS HA. One or more +of the above hostnames may be set. The charm will throw an exception in the following circumstances: -If neither 'vip' nor 'dns-ha' is set and the charm is related to hacluster -If both 'vip' and 'dns-ha' are set as they are mutually exclusive -If 'dns-ha' is set and none of the os-{admin,internal,public}-hostname(s) are -set +If neither 'vip' nor 'dns-ha' is set and the charm is related to +hacluster If both 'vip' and 'dns-ha' are set as they are mutually +exclusive. If 'dns-ha' is set and none of the +os-{admin,internal,public}-hostname(s) are set # Restrictions @@ -62,17 +68,22 @@ This charm only support deployment with OpenStack Icehouse or better. # Network Space support -This charm supports the use of Juju Network Spaces, allowing the charm to be bound to network space configurations managed directly by Juju. This is only supported with Juju 2.0 and above. +This charm supports the use of Juju Network Spaces, allowing the charm +to be bound to network space configurations managed directly by Juju. +This is only supported with Juju 2.0 and above. -API endpoints can be bound to distinct network spaces supporting the network separation of public, internal and admin endpoints. +API endpoints can be bound to distinct network spaces supporting the +network separation of public, internal and admin endpoints. -Access to the underlying MySQL instance can also be bound to a specific space using the shared-db relation. +Access to the underlying MySQL instance can also be bound to a specific +space using the shared-db relation. To use this feature, use the --bind option when deploying the charm: juju deploy neutron-api --bind "public=public-space internal=internal-space admin=admin-space shared-db=internal-space" -alternatively these can also be provided as part of a juju native bundle configuration: +alternatively these can also be provided as part of a juju native +bundle configuration: neutron-api: charm: cs:xenial/neutron-api @@ -83,6 +94,43 @@ alternatively these can also be provided as part of a juju native bundle configu internal: internal-space shared-db: internal-space -NOTE: Spaces must be configured in the underlying provider prior to attempting to use them. +NOTE: Spaces must be configured in the underlying provider prior to +attempting to use them. -NOTE: Existing deployments using os-*-network configuration options will continue to function; these options are preferred over any network space binding provided if set. +NOTE: Existing deployments using os-*-network configuration options +will continue to function; these options are preferred over any network +space binding provided if set. + +# Additional Middleware Requests by Neutron Plugin Charms + +Some neutron plugins may require additional middleware to be added +to api-paste.ini. In order to support that a subordinate may pass +extra_middleware via the neutron-plugin-api-subordinate relation. + +Relation data to be set by subordinates: + {'extra_middleware': [{ + 'type': 'middleware_type', + 'name': 'middleware_name', + 'config': { + 'setting_1': 'value_1', + 'setting_2': 'value_2'}}]} + +It would not be correct to do that from your own plugin as this +requires the neutron-api service restart which should be handled in +this charm. + +The developer guide for Neutron contains a description of the startup +process which makes it clear that api-paste.ini is parsed only once +in neutron-api's lifetime (see the "WSGI Application" section): + +https://git.openstack.org/cgit/openstack/neutron/tree/doc/source/devref/api_layer.rst#n49 + +For the api-paste.ini format in general, please consult PasteDeploy +repository docs/index.txt, "Config Format" section: +https://bitbucket.org/ianb/pastedeploy + +Classes in loadwsgi.py contain config_prefixes that can be used for +middleware types - these are the prefixes the charm code validates +passed data against: + +https://bitbucket.org/ianb/pastedeploy/src/4b27133a2a7db58b213ae55b580039c11d2055c0/paste/deploy/loadwsgi.py?at=default&fileviewer=file-view-default diff --git a/hooks/neutron_api_context.py b/hooks/neutron_api_context.py index 503f07ae..149d6c7c 100644 --- a/hooks/neutron_api_context.py +++ b/hooks/neutron_api_context.py @@ -12,6 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +import ast + from collections import OrderedDict from charmhelpers.core.hookenv import ( @@ -432,7 +434,88 @@ class NeutronApiSDNConfigFileContext(context.OSContextGenerator): return {'config': '/etc/neutron/plugins/ml2/ml2_conf.ini'} +class NeutronApiApiPasteContext(context.OSContextGenerator): + interfaces = ['neutron-plugin-api-subordinate'] + + def __validate_middleware(self, middleware): + ''' + Accepts a list of dicts of the following format: + { + 'type': 'middleware_type', + 'name': 'middleware_name', + 'config': { + option_1: value_1, + # ... + option_n: value_n + } + This validator was meant to be minimalistic - PasteDeploy's + validator will take care of the rest while our purpose here + is mainly config rendering - not imposing additional validation + logic which does not belong here. + ''' + # types taken from PasteDeploy's wsgi loader + VALID_TYPES = ['filter', 'filter-app', + 'app', 'application', + 'composite', 'composit', 'pipeline'] + + def types_valid(t, n, c): + return all((type(t) is str, + type(n) is str, + type(c is dict))) + + def mtype_valid(t): + return t in VALID_TYPES + + for m in middleware: + t, n, c = [m.get(v) for v in ['type', 'name', 'config']] + # note that dict has to be non-empty + if not types_valid(t, n, c): + raise ValueError('Extra middleware key type(s) are' + ' invalid: {}'.format(repr(m))) + if not mtype_valid(t): + raise ValueError('Extra middleware type key is not' + ' a valid PasteDeploy middleware ' + 'type {}'.format(repr(t))) + if not c: + raise ValueError('Extra middleware config dictionary' + ' is empty') + + def __process_unit(self, rid, unit): + rdata = relation_get(rid=rid, unit=unit) + # update extra middleware for all possible plugins + rdata_middleware = rdata.get('extra_middleware') + if rdata_middleware: + try: + middleware = ast.literal_eval(rdata_middleware) + except: + import traceback + log(traceback.format_exc()) + raise ValueError('Invalid extra middleware data' + ' - check the subordinate charm') + if middleware: + return middleware + else: + log('extra_middleware specified but not' + 'populated by unit {}, ' + 'relation: {}, value: {}'.format( + unit, rid, repr(middleware))) + raise ValueError('Invalid extra middleware' + 'specified by a subordinate') + # no extra middleware + return list() + + def __call__(self): + extra_middleware = [] + for rid in relation_ids('neutron-plugin-api-subordinate'): + for unit in related_units(rid): + extra_middleware.extend(self.__process_unit(rid, unit)) + self.__validate_middleware(extra_middleware) + return {'extra_middleware': extra_middleware}\ + if extra_middleware else {} + + class MidonetContext(context.OSContextGenerator): + def __init__(self, rel_name='midonet'): self.rel_name = rel_name self.interfaces = [rel_name] diff --git a/hooks/neutron_api_hooks.py b/hooks/neutron_api_hooks.py index f01965db..6c0ee32b 100755 --- a/hooks/neutron_api_hooks.py +++ b/hooks/neutron_api_hooks.py @@ -63,26 +63,27 @@ from charmhelpers.contrib.openstack.utils import ( ) from neutron_api_utils import ( - CLUSTER_RES, - NEUTRON_CONF, + additional_install_locations, + API_PASTE_INI, api_port, + assess_status, + CLUSTER_RES, determine_packages, determine_ports, do_openstack_upgrade, + dvr_router_present, + force_etcd_restart, + get_topics, git_install, is_api_ready, - dvr_router_present, l3ha_router_present, migrate_neutron_database, + NEUTRON_CONF, neutron_ready, register_configs, restart_map, services, setup_ipv6, - get_topics, - additional_install_locations, - force_etcd_restart, - assess_status, ) from neutron_api_context import ( get_dvr, @@ -613,13 +614,23 @@ def zeromq_configuration_relation_joined(relid=None): users="neutron") -@hooks.hook('neutron-plugin-api-subordinate-relation-joined') +@hooks.hook('neutron-plugin-api-subordinate-relation-joined', + 'neutron-plugin-api-subordinate-relation-changed') +@restart_on_change(restart_map(), stopstart=True) def neutron_plugin_api_subordinate_relation_joined(relid=None): + ''' + -changed handles relation data set by a subordinate. + ''' relation_data = {'neutron-api-ready': 'no'} if is_api_ready(CONFIGS): relation_data['neutron-api-ready'] = "yes" relation_set(relation_id=relid, **relation_data) + # there is no race condition with the neutron service restart + # as juju propagates the changes done in relation_set only after + # the hook exists + CONFIGS.write(API_PASTE_INI) + @hooks.hook('zeromq-configuration-relation-changed', 'neutron-plugin-api-subordinate-relation-changed') diff --git a/hooks/neutron_api_utils.py b/hooks/neutron_api_utils.py index 1f8772c3..f5df40b6 100755 --- a/hooks/neutron_api_utils.py +++ b/hooks/neutron_api_utils.py @@ -156,6 +156,7 @@ APACHE_24_CONF = '/etc/apache2/sites-available/openstack_https_frontend.conf' NEUTRON_DEFAULT = '/etc/default/neutron-server' CA_CERT_PATH = '/usr/local/share/ca-certificates/keystone_juju_ca_cert.crt' MEMCACHED_CONF = '/etc/memcached.conf' +API_PASTE_INI = '%s/api-paste.ini' % NEUTRON_CONF_DIR BASE_RESOURCE_MAP = OrderedDict([ (NEUTRON_CONF, { @@ -183,6 +184,10 @@ BASE_RESOURCE_MAP = OrderedDict([ 'services': ['neutron-server'], 'contexts': [neutron_api_context.NeutronCCContext()], }), + (API_PASTE_INI, { + 'services': ['neutron-server'], + 'contexts': [neutron_api_context.NeutronApiApiPasteContext()], + }), (APACHE_CONF, { 'contexts': [neutron_api_context.ApacheSSLContext()], 'services': ['apache2'], @@ -393,6 +398,7 @@ def resource_map(release=None): resource_map[MEMCACHED_CONF] = { 'contexts': [context.MemcacheContext()], 'services': ['memcached']} + return resource_map diff --git a/templates/api-paste.ini b/templates/api-paste.ini new file mode 100644 index 00000000..8bc7fb23 --- /dev/null +++ b/templates/api-paste.ini @@ -0,0 +1,37 @@ +[composite:neutron] +use = egg:Paste#urlmap +/: neutronversions +/v2.0: neutronapi_v2_0 + +[composite:neutronapi_v2_0] +use = call:neutron.auth:pipeline_factory +noauth = request_id catch_errors extensions neutronapiapp_v2_0 +keystone = {% for m in extra_middleware %}{{ m.name }} {% endfor %}request_id catch_errors authtoken keystonecontext extensions neutronapiapp_v2_0 + +{% for m in extra_middleware -%} +[{{ m.type }}:{{ m.name }}] +{% for k, v in m.config.iteritems() -%} +{{ k }} = {{ v }} +{% endfor %} +{% endfor -%} + +[filter:request_id] +paste.filter_factory = neutron.openstack.common.middleware.request_id:RequestIdMiddleware.factory + +[filter:catch_errors] +paste.filter_factory = neutron.openstack.common.middleware.catch_errors:CatchErrorsMiddleware.factory + +[filter:keystonecontext] +paste.filter_factory = neutron.auth:NeutronKeystoneContext.factory + +[filter:authtoken] +paste.filter_factory = keystoneclient.middleware.auth_token:filter_factory + +[filter:extensions] +paste.filter_factory = neutron.api.extensions:plugin_aware_extension_middleware_factory + +[app:neutronversions] +paste.app_factory = neutron.api.versions:Versions.factory + +[app:neutronapiapp_v2_0] +paste.app_factory = neutron.api.v2.router:APIRouter.factory diff --git a/templates/kilo/api-paste.ini b/templates/kilo/api-paste.ini new file mode 100644 index 00000000..973d0e24 --- /dev/null +++ b/templates/kilo/api-paste.ini @@ -0,0 +1,37 @@ +[composite:neutron] +use = egg:Paste#urlmap +/: neutronversions +/v2.0: neutronapi_v2_0 + +[composite:neutronapi_v2_0] +use = call:neutron.auth:pipeline_factory +noauth = request_id catch_errors extensions neutronapiapp_v2_0 +keystone = {% for m in extra_middleware %}{{ m.name }} {% endfor %}request_id catch_errors authtoken keystonecontext extensions neutronapiapp_v2_0 + +{% for m in extra_middleware -%} +[{{ m.type }}:{{ m.name }}] +{% for k, v in m.config.iteritems() -%} +{{ k }} = {{ v }} +{% endfor %} +{% endfor -%} + +[filter:request_id] +paste.filter_factory = oslo.middleware:RequestId.factory + +[filter:catch_errors] +paste.filter_factory = oslo.middleware:CatchErrors.factory + +[filter:keystonecontext] +paste.filter_factory = neutron.auth:NeutronKeystoneContext.factory + +[filter:authtoken] +paste.filter_factory = keystonemiddleware.auth_token:filter_factory + +[filter:extensions] +paste.filter_factory = neutron.api.extensions:plugin_aware_extension_middleware_factory + +[app:neutronversions] +paste.app_factory = neutron.api.versions:Versions.factory + +[app:neutronapiapp_v2_0] +paste.app_factory = neutron.api.v2.router:APIRouter.factory diff --git a/templates/mitaka/api-paste.ini b/templates/mitaka/api-paste.ini new file mode 100644 index 00000000..31f57977 --- /dev/null +++ b/templates/mitaka/api-paste.ini @@ -0,0 +1,41 @@ +[composite:neutron] +use = egg:Paste#urlmap +/: neutronversions +/v2.0: neutronapi_v2_0 + +[composite:neutronapi_v2_0] +use = call:neutron.auth:pipeline_factory +noauth = cors request_id catch_errors extensions neutronapiapp_v2_0 +keystone = {% for m in extra_middleware %}{{ m.name }} {% endfor %}cors request_id catch_errors authtoken keystonecontext extensions neutronapiapp_v2_0 + +{% for m in extra_middleware -%} +[{{ m.type }}:{{ m.name }}] +{% for k, v in m.config.iteritems() -%} +{{ k }} = {{ v }} +{% endfor %} +{% endfor -%} + +[filter:request_id] +paste.filter_factory = oslo_middleware:RequestId.factory + +[filter:catch_errors] +paste.filter_factory = oslo_middleware:CatchErrors.factory + +[filter:cors] +paste.filter_factory = oslo_middleware.cors:filter_factory +oslo_config_project = neutron + +[filter:keystonecontext] +paste.filter_factory = neutron.auth:NeutronKeystoneContext.factory + +[filter:authtoken] +paste.filter_factory = keystonemiddleware.auth_token:filter_factory + +[filter:extensions] +paste.filter_factory = neutron.api.extensions:plugin_aware_extension_middleware_factory + +[app:neutronversions] +paste.app_factory = neutron.api.versions:Versions.factory + +[app:neutronapiapp_v2_0] +paste.app_factory = neutron.api.v2.router:APIRouter.factory diff --git a/templates/newton/api-paste.ini b/templates/newton/api-paste.ini new file mode 100644 index 00000000..eba39515 --- /dev/null +++ b/templates/newton/api-paste.ini @@ -0,0 +1,52 @@ +[composite:neutron] +use = egg:Paste#urlmap +/: neutronversions_composite +/v2.0: neutronapi_v2_0 + +[composite:neutronapi_v2_0] +use = call:neutron.auth:pipeline_factory +noauth = cors http_proxy_to_wsgi request_id catch_errors extensions neutronapiapp_v2_0 +keystone = {% for m in extra_middleware %}{{ m.name }} {% endfor %}cors http_proxy_to_wsgi request_id catch_errors authtoken keystonecontext extensions neutronapiapp_v2_0 + +{% for m in extra_middleware -%} +[{{ m.type }}:{{ m.name }}] +{% for k, v in m.config.iteritems() -%} +{{ k }} = {{ v }} +{% endfor %} +{% endfor -%} + +[composite:neutronversions_composite] +use = call:neutron.auth:pipeline_factory +noauth = cors http_proxy_to_wsgi neutronversions +keystone = cors http_proxy_to_wsgi neutronversions + +[filter:request_id] +paste.filter_factory = oslo_middleware:RequestId.factory + +[filter:catch_errors] +paste.filter_factory = oslo_middleware:CatchErrors.factory + +[filter:cors] +paste.filter_factory = oslo_middleware.cors:filter_factory +oslo_config_project = neutron + +[filter:http_proxy_to_wsgi] +paste.filter_factory = oslo_middleware.http_proxy_to_wsgi:HTTPProxyToWSGI.factory + +[filter:keystonecontext] +paste.filter_factory = neutron.auth:NeutronKeystoneContext.factory + +[filter:authtoken] +paste.filter_factory = keystonemiddleware.auth_token:filter_factory + +[filter:extensions] +paste.filter_factory = neutron.api.extensions:plugin_aware_extension_middleware_factory + +[app:neutronversions] +paste.app_factory = neutron.api.versions:Versions.factory + +[app:neutronapiapp_v2_0] +paste.app_factory = neutron.api.v2.router:APIRouter.factory + +[filter:osprofiler] +paste.filter_factory = osprofiler.web:WsgiMiddleware.factory diff --git a/unit_tests/test_neutron_api_context.py b/unit_tests/test_neutron_api_context.py index 035a735d..48190ac3 100644 --- a/unit_tests/test_neutron_api_context.py +++ b/unit_tests/test_neutron_api_context.py @@ -774,6 +774,95 @@ class NeutronApiSDNConfigFileContextTest(CharmTestCase): }) +class NeutronApiApiPasteContextTest(CharmTestCase): + + def setUp(self): + super(NeutronApiApiPasteContextTest, self).setUp( + context, TO_PATCH) + self.relation_get.side_effect = self.test_relation.get + + def tearDown(self): + super(NeutronApiApiPasteContextTest, self).tearDown() + + def test_default(self): + middleware = [] + + self.test_relation.set({ + 'extra_middleware': repr(middleware) + }) + + self.relation_ids.return_value = ['rid'] + self.related_units.return_value = ['testunit'] + + self.assertRaises(ValueError, context.NeutronApiApiPasteContext()) + + def test_string(self): + self.test_relation.set({'extra_middleware': 'n42'}) + + self.relation_ids.return_value = ['rid'] + self.related_units.return_value = ['testunit'] + + self.assertRaises(ValueError, context.NeutronApiApiPasteContext()) + + def test_dict(self): + self.test_relation.set({'extra_middleware': + {'dict_with': 'something'}}) + + self.relation_ids.return_value = ['rid'] + self.related_units.return_value = ['testunit'] + + self.assertRaises(ValueError, context.NeutronApiApiPasteContext()) + + def test_configset(self): + middleware = [{'name': 'middleware_1', + 'type': 'filter', + 'config': {'setting_1': 'value_1'}}, + {'name': 'middleware_2', + 'type': 'app', + 'config': {'setting_2': 'value_2'}} + ] + + # note repr is needed to simulate charm-helpers behavior + # with regards to object serialization - the context + # implementation should safely eval the string instead + # of just using it + self.test_relation.set({ + 'extra_middleware': repr(middleware) + }) + self.relation_ids.return_value = ['rid2'] + self.related_units.return_value = ['unit1'] + napiapipaste_ctxt = context.NeutronApiApiPasteContext()() + self.assertEquals(napiapipaste_ctxt, {'extra_middleware': middleware}) + + def __test_arg(self, key): + middleware = [{'name': 'middleware_1', + 'type': 'filter', + 'config': {'setting_1': 'value_1'}}, + {'name': 'middleware_2', + 'type': 'composite', + 'config': {'setting_2': 'value_2'}}] + # invalidate a key + middleware[0][key] = None + + self.test_relation.set({ + 'extra_middleware': repr(middleware) + }) + + self.relation_ids.return_value = ['rid'] + self.related_units.return_value = ['testunit'] + + self.assertRaises(ValueError, context.NeutronApiApiPasteContext()) + + def test_no_type(self): + self.__test_arg('type') + + def test_no_name(self): + self.__test_arg('name') + + def test_no_config(self): + self.__test_arg('config') + + class MidonetContextTest(CharmTestCase): def setUp(self): diff --git a/unit_tests/test_neutron_api_utils.py b/unit_tests/test_neutron_api_utils.py index 2a3f857d..839e8772 100644 --- a/unit_tests/test_neutron_api_utils.py +++ b/unit_tests/test_neutron_api_utils.py @@ -227,6 +227,9 @@ class TestNeutronAPIUtils(CharmTestCase): (nutils.NEUTRON_DEFAULT, { 'services': ['neutron-server'], }), + (nutils.API_PASTE_INI, { + 'services': ['neutron-server'], + }), (ML2CONF, { 'services': ['neutron-server'], }), @@ -255,6 +258,7 @@ class TestNeutronAPIUtils(CharmTestCase): templating.OSConfigRenderer.side_effect = _mock_OSConfigRenderer _regconfs = nutils.register_configs() confs = ['/etc/neutron/neutron.conf', + '/etc/neutron/api-paste.ini', '/etc/default/neutron-server', '/etc/neutron/plugins/ml2/ml2_conf.ini', '/etc/apache2/sites-available/openstack_https_frontend',