Merge "Makes use of Neutron tags optional"

This commit is contained in:
Jenkins 2016-04-21 08:18:24 +00:00 committed by Gerrit Code Review
commit b822c22c8f
5 changed files with 145 additions and 65 deletions

View File

@ -34,6 +34,7 @@ from kuryr import utils
MANDATORY_NEUTRON_EXTENSION = "subnet_allocation"
TAG_NEUTRON_EXTENSION = "tag"
def _get_cloud_config(cloud='devstack-admin'):
@ -122,6 +123,18 @@ def check_for_neutron_ext_support():
.format(MANDATORY_NEUTRON_EXTENSION))
def check_for_neutron_ext_tag():
"""Validates for mandatory extension support availability in neutron."""
app.tag = True
try:
app.neutron.show_extension(TAG_NEUTRON_EXTENSION)
except n_exceptions.NeutronClientException as e:
app.tag = False
if e.status_code == n_exceptions.NotFound.status_code:
app.logger.warn(_LW("Neutron tags not supported. "
"Continue without using them."))
# TODO(tfukushima): Retrieve the following subnet names from the config file.
SUBNET_POOLS_V4 = [
p.strip() for p in os.environ.get('SUBNET_POOLS_V4', 'kuryr').split(',')]
@ -331,10 +344,11 @@ def _neutron_net_add_tag(netid, tag):
app.neutron.add_tag('networks', netid, tag)
def _neutron_net_add_tags(netid, tag):
tags = utils.create_net_tags(tag)
for tag in tags:
_neutron_net_add_tag(netid, tag)
def _neutron_net_add_tags(netid, tag, tags=True):
if tags:
tags = utils.create_net_tags(tag)
for tag in tags:
_neutron_net_add_tag(netid, tag)
def _neutron_net_remove_tag(netid, tag):
@ -347,6 +361,18 @@ def _neutron_net_remove_tags(netid, tag):
_neutron_net_remove_tag(netid, tag)
def _make_net_identifier(network_id, tags=True):
if tags:
return utils.make_net_tags(network_id)
return network_id
def _get_networks_by_identifier(identifier):
if app.tag:
return _get_networks_by_attrs(tags=identifier)
return _get_networks_by_attrs(name=identifier)
@app.route('/Plugin.Activate', methods=['POST'])
def plugin_activate():
"""Returns the list of the implemented drivers.
@ -452,9 +478,8 @@ def network_driver_create_network():
app.logger.debug("Received JSON data {0} for"
" /NetworkDriver.CreateNetwork".format(json_data))
jsonschema.validate(json_data, schemata.NETWORK_CREATE_SCHEMA)
container_net_id = json_data['NetworkID']
neutron_network_name = utils.make_net_name(container_net_id)
neutron_network_name = utils.make_net_name(container_net_id, tags=app.tag)
pool_cidr = json_data['IPv4Data'][0]['Pool']
gateway_ip = ''
if 'Gateway' in json_data['IPv4Data'][0]:
@ -477,7 +502,8 @@ def network_driver_create_network():
{'network': {'name': neutron_network_name,
"admin_state_up": True}})
network_id = network['network']['id']
_neutron_net_add_tags(network['network']['id'], container_net_id)
_neutron_net_add_tags(network['network']['id'], container_net_id,
tags=app.tag)
app.logger.info(_LI("Created a new network with name {0}"
" successfully: {1}")
@ -493,8 +519,15 @@ def network_driver_create_network():
app.logger.error(_LE("Error happened during listing "
"Neutron networks: {0}").format(ex))
raise
_neutron_net_add_tags(network_id, container_net_id)
_neutron_net_add_tag(network_id, const.KURYR_EXISTING_NEUTRON_NET)
if app.tag:
_neutron_net_add_tags(network_id, container_net_id, tags=app.tag)
_neutron_net_add_tag(network_id, const.KURYR_EXISTING_NEUTRON_NET)
else:
network = app.neutron.update_network(
neutron_uuid, {'network': {'name': neutron_network_name}})
app.logger.info(_LI("Updated the network with new name {0}"
" successfully: {1}")
.format(neutron_network_name, network))
app.logger.info(_LI("Using existing network {0} successfully")
.format(neutron_uuid))
@ -540,36 +573,40 @@ def network_driver_delete_network():
jsonschema.validate(json_data, schemata.NETWORK_DELETE_SCHEMA)
container_net_id = json_data['NetworkID']
neutron_network_tags = utils.make_net_tags(container_net_id)
existing_network_tags = neutron_network_tags + ','
existing_network_tags += const.KURYR_EXISTING_NEUTRON_NET
try:
existing_networks = _get_networks_by_attrs(tags=existing_network_tags)
except n_exceptions.NeutronClientException as ex:
app.logger.error(_LE("Error happened during listing "
"Neutron networks: {0}").format(ex))
raise
neutron_network_identifier = _make_net_identifier(container_net_id,
tags=app.tag)
if app.tag:
existing_network_identifier = neutron_network_identifier + ','
existing_network_identifier += const.KURYR_EXISTING_NEUTRON_NET
try:
existing_networks = _get_networks_by_identifier(
existing_network_identifier)
except n_exceptions.NeutronClientException as ex:
app.logger.error(_LE("Error happened during listing "
"Neutron networks: {0}").format(ex))
raise
if existing_networks:
app.logger.warn(_LW("Network is a pre existing Neutron network, "
"not deleting in Neutron. removing tags: {0}")
.format(existing_network_tags))
neutron_net_id = existing_networks[0]['id']
_neutron_net_remove_tags(neutron_net_id, container_net_id)
_neutron_net_remove_tag(neutron_net_id,
const.KURYR_EXISTING_NEUTRON_NET)
return flask.jsonify(const.SCHEMA['SUCCESS'])
if existing_networks:
app.logger.warn(_LW("Network is a pre existing Neutron network, "
"not deleting in Neutron. removing tags: {0}")
.format(existing_network_identifier))
neutron_net_id = existing_networks[0]['id']
_neutron_net_remove_tags(neutron_net_id, container_net_id)
_neutron_net_remove_tag(neutron_net_id,
const.KURYR_EXISTING_NEUTRON_NET)
return flask.jsonify(const.SCHEMA['SUCCESS'])
try:
filtered_networks = _get_networks_by_attrs(tags=neutron_network_tags)
filtered_networks = _get_networks_by_identifier(
neutron_network_identifier)
except n_exceptions.NeutronClientException as ex:
app.logger.error(_LE("Error happened during listing "
"Neutron networks: {0}").format(ex))
raise
if not filtered_networks:
app.logger.warn(_LW("Network with tags {0} cannot be found")
.format(neutron_network_tags))
app.logger.warn(_LW("Network with identifier {0} cannot be found")
.format(neutron_network_identifier))
else:
neutron_network_id = filtered_networks[0]['id']
filtered_subnets = _get_subnets_by_attrs(
@ -650,14 +687,15 @@ def network_driver_create_endpoint():
.format(json_data))
jsonschema.validate(json_data, schemata.ENDPOINT_CREATE_SCHEMA)
neutron_network_tags = utils.make_net_tags(json_data['NetworkID'])
neutron_network_identifier = _make_net_identifier(json_data['NetworkID'],
tags=app.tag)
endpoint_id = json_data['EndpointID']
filtered_networks = _get_networks_by_attrs(tags=neutron_network_tags)
filtered_networks = _get_networks_by_identifier(neutron_network_identifier)
if not filtered_networks:
return flask.jsonify({
'Err': "Neutron network associated with tags {0} doesn't exist."
.format(neutron_network_tags)
'Err': "Neutron net associated with identifier {0} doesn't exist."
.format(neutron_network_identifier)
})
else:
neutron_network_id = filtered_networks[0]['id']
@ -747,14 +785,15 @@ def network_driver_join():
.format(json_data))
jsonschema.validate(json_data, schemata.JOIN_SCHEMA)
neutron_network_tags = utils.make_net_tags(json_data['NetworkID'])
neutron_network_identifier = _make_net_identifier(json_data['NetworkID'],
tags=app.tag)
endpoint_id = json_data['EndpointID']
filtered_networks = _get_networks_by_attrs(tags=neutron_network_tags)
filtered_networks = _get_networks_by_identifier(neutron_network_identifier)
if not filtered_networks:
return flask.jsonify({
'Err': "Neutron network associated with tags {0} doesn't exit."
.format(neutron_network_tags)
'Err': "Neutron net associated with identifier {0} doesn't exit."
.format(neutron_network_identifier)
})
else:
neutron_network_id = filtered_networks[0]['id']
@ -831,14 +870,15 @@ def network_driver_leave():
.format(json_data))
jsonschema.validate(json_data, schemata.LEAVE_SCHEMA)
neutron_network_tags = utils.make_net_tags(json_data['NetworkID'])
neutron_network_identifier = _make_net_identifier(json_data['NetworkID'],
tags=app.tag)
endpoint_id = json_data['EndpointID']
filtered_networks = _get_networks_by_attrs(tags=neutron_network_tags)
filtered_networks = _get_networks_by_identifier(neutron_network_identifier)
if not filtered_networks:
return flask.jsonify({
'Err': "Neutron network associated with tags {0} doesn't exit."
.format(neutron_network_tags)
'Err': "Neutron net associated with identifier {0} doesn't exit."
.format(neutron_network_identifier)
})
else:
neutron_port_name = utils.get_neutron_port_name(endpoint_id)

View File

@ -23,6 +23,7 @@ def start():
from kuryr import app
from kuryr import controllers
controllers.check_for_neutron_ext_support()
controllers.check_for_neutron_ext_tag()
app.debug = config.CONF.debug
log.setup(config.CONF, 'Kuryr')

View File

@ -27,6 +27,7 @@ class TestCase(test_cli20.CLITestV20Base):
app.config['TESTING'] = True
self.app = app.test_client()
self.app.neutron = self.client
app.tag = True
class TestKuryrBase(TestCase):

View File

@ -13,7 +13,7 @@
import hashlib
import uuid
from ddt import ddt
import ddt
from oslo_serialization import jsonutils
from kuryr import app
@ -22,7 +22,7 @@ from kuryr.tests.unit import base
from kuryr import utils
@ddt
@ddt.ddt
class TestKuryrNetworkPreExisting(base.TestKuryrBase):
def _ids(self):
@ -46,19 +46,32 @@ class TestKuryrNetworkPreExisting(base.TestKuryrBase):
}
return docker_network_id, fake_neutron_net_id, fake_response
def test_create_network_pre_existing(self):
@ddt.data(
(True), (False))
def test_create_network_pre_existing(self, use_tags):
if not use_tags:
self.mox.StubOutWithMock(app, "tag")
app.tag = use_tags
docker_network_id, fake_neutron_net_id, fake_response = self._ids()
self.mox.StubOutWithMock(app.neutron, "list_networks")
app.neutron.list_networks(id=fake_neutron_net_id).AndReturn(
fake_response)
self.mox.StubOutWithMock(app.neutron, "add_tag")
tags = utils.create_net_tags(docker_network_id)
for tag in tags:
app.neutron.add_tag('networks', fake_neutron_net_id, tag)
app.neutron.add_tag('networks', fake_neutron_net_id,
const.KURYR_EXISTING_NEUTRON_NET)
if app.tag:
self.mox.StubOutWithMock(app.neutron, "add_tag")
tags = utils.create_net_tags(docker_network_id)
for tag in tags:
app.neutron.add_tag('networks', fake_neutron_net_id, tag)
app.neutron.add_tag('networks', fake_neutron_net_id,
const.KURYR_EXISTING_NEUTRON_NET)
else:
self.mox.StubOutWithMock(app.neutron, "update_network")
app.neutron.update_network(
fake_neutron_net_id, {'network':
{'name': docker_network_id}}).AndReturn(
fake_response)
self.mox.StubOutWithMock(app.neutron, 'list_subnets')
fake_existing_subnets_response = {
@ -120,21 +133,44 @@ class TestKuryrNetworkPreExisting(base.TestKuryrBase):
decoded_json = jsonutils.loads(response.data)
self.assertEqual(const.SCHEMA['SUCCESS'], decoded_json)
def test_delete_network_pre_existing(self):
@ddt.data(
(True), (False))
def test_delete_network_pre_existing(self, use_tags):
if not use_tags:
self.mox.StubOutWithMock(app, "tag")
app.tag = use_tags
docker_network_id, fake_neutron_net_id, fake_response = self._ids()
self.mox.StubOutWithMock(app.neutron, 'list_networks')
t = utils.make_net_tags(docker_network_id)
te = t + ',' + const.KURYR_EXISTING_NEUTRON_NET
app.neutron.list_networks(tags=te).AndReturn(
fake_response)
if app.tag:
self.mox.StubOutWithMock(app.neutron, 'list_networks')
t = utils.make_net_tags(docker_network_id)
te = t + ',' + const.KURYR_EXISTING_NEUTRON_NET
app.neutron.list_networks(tags=te).AndReturn(
fake_response)
self.mox.StubOutWithMock(app.neutron, "remove_tag")
tags = utils.create_net_tags(docker_network_id)
for tag in tags:
app.neutron.remove_tag('networks', fake_neutron_net_id, tag)
app.neutron.remove_tag('networks', fake_neutron_net_id,
const.KURYR_EXISTING_NEUTRON_NET)
self.mox.StubOutWithMock(app.neutron, "remove_tag")
tags = utils.create_net_tags(docker_network_id)
for tag in tags:
app.neutron.remove_tag('networks', fake_neutron_net_id, tag)
app.neutron.remove_tag('networks', fake_neutron_net_id,
const.KURYR_EXISTING_NEUTRON_NET)
else:
self.mox.StubOutWithMock(app.neutron, 'list_networks')
app.neutron.list_networks(name=docker_network_id).AndReturn(
fake_response)
self.mox.StubOutWithMock(app.neutron, 'list_subnets')
fake_existing_subnets_response = {
"subnets": []
}
app.neutron.list_subnets(
network_id=fake_neutron_net_id).AndReturn(
fake_existing_subnets_response)
self.mox.StubOutWithMock(app.neutron, 'delete_network')
app.neutron.delete_network(fake_neutron_net_id).AndReturn(None)
self.mox.ReplayAll()
data = {'NetworkID': docker_network_id}

View File

@ -163,5 +163,7 @@ def make_net_tags(tag):
return ','.join(map(str, tags))
def make_net_name(netid):
return const.NET_NAME_PREFIX + netid[:8]
def make_net_name(netid, tags=True):
if tags:
return const.NET_NAME_PREFIX + netid[:8]
return netid