From cb2ffa547333b0b21e85b34576a746d38eb08b95 Mon Sep 17 00:00:00 2001 From: Shih-Hao Li Date: Tue, 20 Sep 2016 11:34:08 -0700 Subject: [PATCH] NSX|V3: ensure that octavia ports receive DHCP addresses Change-Id: Ibca37f98e1dae95a37c31123c38b7cd13908c16a --- vmware_nsx/common/utils.py | 7 +++++++ vmware_nsx/plugins/nsx_v3/plugin.py | 14 +++++++------- .../admin/plugins/nsxv3/resources/dhcp_binding.py | 5 ++--- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/vmware_nsx/common/utils.py b/vmware_nsx/common/utils.py index 4ec5ffc302..0a62feed47 100644 --- a/vmware_nsx/common/utils.py +++ b/vmware_nsx/common/utils.py @@ -20,6 +20,7 @@ import hashlib import eventlet from neutron import version as n_version from neutron_lib.api import validators +from neutron_lib import constants from neutron_lib import exceptions from oslo_config import cfg from oslo_context import context as common_context @@ -281,6 +282,12 @@ def is_ipv4_ip_address(addr): return True +def is_port_dhcp_configurable(port): + owner = port.get('device_owner') + return (owner and + not owner.startswith(constants.DEVICE_OWNER_NETWORK_PREFIX)) + + def spawn_n(func, *args, **kwargs): """Passthrough method for eventlet.spawn_n. diff --git a/vmware_nsx/plugins/nsx_v3/plugin.py b/vmware_nsx/plugins/nsx_v3/plugin.py index e9cbfe9654..3c9962ccc6 100644 --- a/vmware_nsx/plugins/nsx_v3/plugin.py +++ b/vmware_nsx/plugins/nsx_v3/plugin.py @@ -1408,8 +1408,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, return ips def _add_dhcp_binding(self, context, port): - if not port["device_owner"].startswith( - const.DEVICE_OWNER_COMPUTE_PREFIX): + if not utils.is_port_dhcp_configurable(port): return dhcp_service = nsx_db.get_nsx_service_binding( context.session, port['network_id'], nsx_constants.SERVICE_DHCP) @@ -1499,9 +1498,11 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, # Note that Neutron allows a port with multiple IPs in the # same subnet. But backend DHCP server may not support that. - if old_port["device_owner"] != new_port["device_owner"]: - if old_port["device_owner"].startswith( - const.DEVICE_OWNER_COMPUTE_PREFIX): + if (utils.is_port_dhcp_configurable(old_port) != + utils.is_port_dhcp_configurable(new_port)): + # Note that the device_owner could be changed, + # but still needs DHCP binding. + if utils.is_port_dhcp_configurable(old_port): self._delete_dhcp_binding(context, old_port) else: self._add_dhcp_binding(context, new_port) @@ -1545,8 +1546,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, "DHCP server %(server)s"), {'ip': new_ip, 'server': dhcp_service['nsx_service_id']}) - elif old_port["device_owner"].startswith( - const.DEVICE_OWNER_COMPUTE_PREFIX): + elif utils.is_port_dhcp_configurable(old_port): # Update static DHCP bindings for a compute port. bindings = nsx_db.get_nsx_dhcp_bindings(context.session, old_port['id']) diff --git a/vmware_nsx/shell/admin/plugins/nsxv3/resources/dhcp_binding.py b/vmware_nsx/shell/admin/plugins/nsxv3/resources/dhcp_binding.py index 03bfec6877..6771fb4156 100644 --- a/vmware_nsx/shell/admin/plugins/nsxv3/resources/dhcp_binding.py +++ b/vmware_nsx/shell/admin/plugins/nsxv3/resources/dhcp_binding.py @@ -39,8 +39,7 @@ def list_dhcp_bindings(resource, event, trigger, **kwargs): """List DHCP bindings in Neutron.""" comp_ports = [port for port in neutron_client.get_ports() - if port['device_owner'].startswith( - const.DEVICE_OWNER_COMPUTE_PREFIX)] + if nsx_utils.is_port_dhcp_configurable(port)] LOG.info(formatters.output_formatter(constants.DHCP_BINDING, comp_ports, ['id', 'mac_address', 'fixed_ips'])) @@ -77,7 +76,7 @@ def nsx_update_dhcp_bindings(resource, event, trigger, **kwargs): for port in ports: device_owner = port['device_owner'] if (device_owner != const.DEVICE_OWNER_DHCP and - not device_owner.startswith(const.DEVICE_OWNER_COMPUTE_PREFIX)): + not nsx_utils.is_port_dhcp_configurable(port)): continue for fixed_ip in port['fixed_ips']: if netaddr.IPNetwork(fixed_ip['ip_address']).version == 6: