Merge "Add container_id to connect method of BaseBindingDriver"

This commit is contained in:
Zuul 2018-09-21 13:01:43 +00:00 committed by Gerrit Code Review
commit 7a07816b9b
6 changed files with 31 additions and 23 deletions

View File

@ -30,11 +30,11 @@ class BaseBindingDriver(object):
"""Interface to attach ports to pods."""
@abc.abstractmethod
def connect(self, vif, ifname, netns):
def connect(self, vif, ifname, netns, container_id):
raise NotImplementedError()
@abc.abstractmethod
def disconnect(self, vif, ifname, netns):
def disconnect(self, vif, ifname, netns, container_id):
raise NotImplementedError()
@ -92,19 +92,19 @@ def _configure_l3(vif, ifname, netns, is_default_gateway):
def connect(vif, instance_info, ifname, netns=None, report_health=None,
is_default_gateway=True):
is_default_gateway=True, container_id=None):
driver = _get_binding_driver(vif)
if report_health:
report_health(driver.is_healthy())
os_vif.plug(vif, instance_info)
driver.connect(vif, ifname, netns)
driver.connect(vif, ifname, netns, container_id)
_configure_l3(vif, ifname, netns, is_default_gateway)
def disconnect(vif, instance_info, ifname, netns=None, report_health=None,
**kwargs):
container_id=None, **kwargs):
driver = _get_binding_driver(vif)
if report_health:
report_health(driver.is_healthy())
driver.disconnect(vif, ifname, netns)
driver.disconnect(vif, ifname, netns, container_id)
os_vif.unplug(vif, instance_info)

View File

@ -29,7 +29,7 @@ class BaseBridgeDriver(health.HealthHandler, b_base.BaseBindingDriver):
def __init__(self):
super(BaseBridgeDriver, self).__init__()
def connect(self, vif, ifname, netns):
def connect(self, vif, ifname, netns, container_id):
host_ifname = vif.vif_name
with b_base.get_ipdb() as h_ipdb:
@ -67,7 +67,7 @@ class BaseBridgeDriver(health.HealthHandler, b_base.BaseBindingDriver):
h_iface.mtu = interface_mtu
h_iface.up()
def disconnect(self, vif, ifname, netns):
def disconnect(self, vif, ifname, netns, container_id):
pass
@ -75,8 +75,8 @@ class BridgeDriver(BaseBridgeDriver):
def __init__(self):
super(BridgeDriver, self).__init__()
def connect(self, vif, ifname, netns):
super(BridgeDriver, self).connect(vif, ifname, netns)
def connect(self, vif, ifname, netns, container_id):
super(BridgeDriver, self).connect(vif, ifname, netns, container_id)
host_ifname = vif.vif_name
bridge_name = vif.bridge_name
@ -84,7 +84,7 @@ class BridgeDriver(BaseBridgeDriver):
with h_ipdb.interfaces[bridge_name] as h_br:
h_br.add_port(host_ifname)
def disconnect(self, vif, ifname, netns):
def disconnect(self, vif, ifname, netns, container_id):
# NOTE(ivc): veth pair is destroyed automatically along with the
# container namespace
pass
@ -95,16 +95,18 @@ class VIFOpenVSwitchDriver(BaseBridgeDriver):
def __init__(self):
super(VIFOpenVSwitchDriver, self).__init__()
def connect(self, vif, ifname, netns):
super(VIFOpenVSwitchDriver, self).connect(vif, ifname, netns)
def connect(self, vif, ifname, netns, container_id):
super(VIFOpenVSwitchDriver, self).connect(vif, ifname, netns,
container_id)
# FIXME(irenab) use pod_id (neutron port device_id)
instance_id = 'kuryr'
net_utils.create_ovs_vif_port(vif.bridge_name, vif.vif_name,
vif.port_profile.interface_id,
vif.address, instance_id)
def disconnect(self, vif, ifname, netns):
super(VIFOpenVSwitchDriver, self).disconnect(vif, ifname, netns)
def disconnect(self, vif, ifname, netns, container_id):
super(VIFOpenVSwitchDriver, self).disconnect(vif, ifname, netns,
container_id)
net_utils.delete_ovs_vif_port(vif.bridge_name, vif.vif_name)
def is_healthy(self):

View File

@ -35,7 +35,7 @@ class NestedDriver(health.HealthHandler, b_base.BaseBindingDriver):
def _get_iface_create_args(self, vif):
raise NotImplementedError()
def connect(self, vif, ifname, netns):
def connect(self, vif, ifname, netns, container_id):
with b_base.get_ipdb() as h_ipdb:
# NOTE(vikasc): Ideally 'ifname' should be used here but instead a
# temporary name is being used while creating the device for
@ -62,7 +62,7 @@ class NestedDriver(health.HealthHandler, b_base.BaseBindingDriver):
iface.address = str(vif.address)
iface.up()
def disconnect(self, vif, ifname, netns):
def disconnect(self, vif, ifname, netns, container_id):
# NOTE(vikasc): device will get deleted with container namespace, so
# nothing to be done here.
pass

View File

@ -111,7 +111,8 @@ class AddHandler(CNIHandlerBase):
is_default_gateway = (ifname == self._cni.CNI_IFNAME)
b_base.connect(_vif, self._get_inst(pod),
ifname, self._cni.CNI_NETNS,
is_default_gateway=is_default_gateway)
is_default_gateway=is_default_gateway,
container_id=self._cni.CNI_CONTAINERID)
def should_callback(self, pod, vifs):
"""Called after all vifs have been processed
@ -147,7 +148,8 @@ class DelHandler(CNIHandlerBase):
def on_vif(self, pod, vif, ifname):
b_base.disconnect(vif, self._get_inst(pod),
self._cni.CNI_IFNAME, self._cni.CNI_NETNS)
self._cni.CNI_IFNAME, self._cni.CNI_NETNS,
container_id=self._cni.CNI_CONTAINERID)
def should_callback(self, pod, vifs):
"""Called after all vifs have been processed

View File

@ -131,7 +131,8 @@ class K8sCNIRegistryPlugin(base_cni.CNIPlugin):
is_default_gateway = (ifname == params.CNI_IFNAME)
fn(vif, self._get_inst(pod), ifname, params.CNI_NETNS,
report_health=self.report_drivers_health,
is_default_gateway=is_default_gateway)
is_default_gateway=is_default_gateway,
container_id=params.CNI_CONTAINERID)
return vifs
def _get_inst(self, pod):

View File

@ -45,7 +45,8 @@ class TestK8sCNIRegistryPlugin(base.TestCase):
m_lock.assert_called_with('default/foo', external=True)
m_connect.assert_called_with(mock.ANY, mock.ANY, 'eth0', 123,
report_health=mock.ANY,
is_default_gateway=mock.ANY)
is_default_gateway=mock.ANY,
container_id='cont_id')
self.assertEqual('cont_id',
self.plugin.registry['default/foo']['containerid'])
@ -55,7 +56,8 @@ class TestK8sCNIRegistryPlugin(base.TestCase):
m_disconnect.assert_called_with(mock.ANY, mock.ANY, 'eth0', 123,
report_health=mock.ANY,
is_default_gateway=mock.ANY)
is_default_gateway=mock.ANY,
container_id='cont_id')
@mock.patch('kuryr_kubernetes.cni.binding.base.disconnect')
def test_del_wrong_container_id(self, m_disconnect):
@ -88,7 +90,8 @@ class TestK8sCNIRegistryPlugin(base.TestCase):
'containerid': 'cont_id'})
m_connect.assert_called_with(mock.ANY, mock.ANY, 'eth0', 123,
report_health=mock.ANY,
is_default_gateway=mock.ANY)
is_default_gateway=mock.ANY,
container_id='cont_id')
@mock.patch('time.sleep', mock.Mock())
def test_add_not_present(self):