Add "host" field to ZunNetwork

Change-Id: I2ae825712c59917af9d31a851fd14353a2fa848d
This commit is contained in:
Hongbin Lu 2023-12-18 11:49:19 +00:00
parent 397edf53c1
commit da8e6850fc
9 changed files with 62 additions and 11 deletions

View File

@ -0,0 +1,38 @@
# 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.
"""add host to ZunNetwork
Revision ID: 3f2b36231bee
Revises: f979327df44b
Create Date: 2023-12-18 10:47:27.164812
"""
# revision identifiers, used by Alembic.
revision = '3f2b36231bee'
down_revision = 'f979327df44b'
branch_labels = None
depends_on = None
from alembic import op
import sqlalchemy as sa
def upgrade():
op.add_column('network',
sa.Column('host', sa.String(length=255), nullable=True))
op.drop_constraint(constraint_name='uniq_network0neutron_net_id',
table_name='network', type_='unique')
op.create_unique_constraint(
constraint_name='uniq_network0neutron_net_id_host',
table_name='network', columns=['neutron_net_id', 'host'])

View File

@ -1198,7 +1198,8 @@ class Connection(object):
return result
def _add_networks_filters(self, query, filters):
filter_names = ['name', 'neutron_net_id', 'project_id', 'user_id']
filter_names = ['name', 'neutron_net_id', 'project_id', 'user_id',
'host']
return self._add_filters(query, models.Network, filters=filters,
filter_names=filter_names)

View File

@ -561,13 +561,14 @@ class Network(Base):
__tablename__ = 'network'
__table_args__ = (
schema.UniqueConstraint('uuid', name='uniq_network0uuid'),
schema.UniqueConstraint('neutron_net_id',
name='uniq_network0neutron_net_id'),
schema.UniqueConstraint('neutron_net_id', 'host',
name='uniq_network0neutron_net_id_host'),
table_args()
)
id = Column(Integer, primary_key=True)
name = Column(String(255))
neutron_net_id = Column(String(255))
host = Column(String(255))
network_id = Column(String(255))
project_id = Column(String(255))
user_id = Column(String(255))

View File

@ -111,6 +111,7 @@ class KuryrNetwork(network.Network):
network_dict['user_id'] = self.context.user_id
network_dict['name'] = name
network_dict['neutron_net_id'] = neutron_net_id
network_dict['host'] = CONF.host
network = objects.ZunNetwork(self.context, **network_dict)
for attempt in (1, 2, 3):
@ -138,10 +139,12 @@ class KuryrNetwork(network.Network):
networks = objects.ZunNetwork.list(
self.context,
filters={'neutron_net_id': network.neutron_net_id})
LOG.debug("network objects with 'neutron_net_id' as '%(net_id)s': "
"%(networks)s",
filters={'neutron_net_id': network.neutron_net_id,
'host': CONF.host})
LOG.debug("network objects with 'neutron_net_id' as '%(net_id)s'"
"at host %(host)s: %(networks)s",
{"net_id": network.neutron_net_id,
"host": CONF.host,
"networks": networks})
docker_networks = self.docker.networks(names=[network.name])
LOG.debug("docker networks with name matching '%(name)s': "

View File

@ -20,7 +20,8 @@ from zun.objects import base
class ZunNetwork(base.ZunPersistentObject, base.ZunObject):
# Version 1.0: Initial version
# Version 1.1: Add destroy method
VERSION = '1.1'
# Version 1.2: Add 'host' attribute
VERSION = '1.2'
fields = {
'id': fields.IntegerField(),
@ -30,6 +31,7 @@ class ZunNetwork(base.ZunPersistentObject, base.ZunObject):
'name': fields.StringField(nullable=True),
'network_id': fields.StringField(nullable=True),
'neutron_net_id': fields.StringField(nullable=True),
'host': fields.StringField(nullable=True),
}
@staticmethod

View File

@ -31,14 +31,15 @@ class DbNetworkTestCase(base.DbTestCase):
def test_create_network_already_exists(self):
utils.create_test_network(context=self.context,
uuid='123', neutron_net_id='456')
uuid='123', neutron_net_id='456',
host='fake_host')
with self.assertRaisesRegex(exception.NetworkAlreadyExists,
'A network with UUID 123.*'):
utils.create_test_network(context=self.context, uuid='123')
with self.assertRaisesRegex(exception.NetworkAlreadyExists,
'A network with neutron_net_id 456.*'):
utils.create_test_network(context=self.context,
neutron_net_id='456')
neutron_net_id='456', host='fake_host')
def test_list_networks(self):
uuids = []

View File

@ -533,6 +533,7 @@ def get_test_network(**kwargs):
'created_at': kwargs.get('created_at'),
'updated_at': kwargs.get('updated_at'),
'neutron_net_id': kwargs.get('neutron_net_id', 'bar'),
'host': kwargs.get('host', 'fake_host'),
}

View File

@ -18,6 +18,7 @@ from unittest import mock
from neutronclient.common import exceptions as n_exc
from zun.common import exception
from zun import conf
from zun.network import kuryr_network
from zun.objects.container import Container
from zun.objects.zun_network import ZunNetwork
@ -239,6 +240,8 @@ class KuryrNetworkTestCase(base.TestCase):
@mock.patch('zun.network.neutron.NeutronAPI')
def test_create_network_already_exist(
self, mock_neutron_api_cls, mock_list, mock_save, mock_create):
fake_host = 'host1'
conf.CONF.set_override('host', fake_host)
mock_neutron_api_cls.return_value = self.network_driver.neutron_api
neutron_net_id = 'fake-net-id'
docker_net_id = 'docker-net'
@ -253,7 +256,8 @@ class KuryrNetworkTestCase(base.TestCase):
network = self.network_driver.create_network(neutron_net_id)
self.assertEqual(docker_net_id, network.network_id)
mock_list.assert_called_once_with(
self.context, filters={'neutron_net_id': neutron_net_id})
self.context, filters={'neutron_net_id': neutron_net_id,
'host': fake_host})
mock_list_network.assert_called_once_with(names=[neutron_net_id])
def test_remove_network(self):

View File

@ -368,7 +368,7 @@ object_data = {
'ContainerPCIRequests': '1.0-7b8f7f044661fe4e24e6949c035af2c4',
'ContainerAction': '1.2-4ae05fe3d1576c211c2425e4db190ef2',
'ContainerActionEvent': '1.0-2974d0a6f5d4821fd4e223a88c10181a',
'ZunNetwork': '1.1-26e8d37a54e5fc905ede657744a221d9',
'ZunNetwork': '1.2-f0a65c31e98868ac64bd30c09245b516',
'ExecInstance': '1.0-59464e7b96db847c0abb1e96d3cec30a',
'Registry': '1.0-9fddfae03f3ca052cc26c924642b9268',
'RequestGroup': '1.0-5e08d68d0a63b729778340d608ec4eae',