def allocate_vip(self, load_balancer): if not load_balancer.vip.port_id and not load_balancer.vip.subnet_id: raise base.AllocateVIPException('Cannot allocate a vip ' 'without a port_id or ' 'a subnet_id.') if load_balancer.vip.port_id: LOG.info(_LI('Port %s already exists. Nothing to be done.'), load_balancer.vip.port_id) port = self.get_port(load_balancer.vip.port_id) return self._port_to_vip(port, load_balancer) # Must retrieve the network_id from the subnet subnet = self.get_subnet(load_balancer.vip.subnet_id) # It can be assumed that network_id exists port = { 'port': { 'name': 'octavia-lb-' + load_balancer.id, 'network_id': subnet.network_id, 'admin_state_up': False, 'device_id': 'lb-{0}'.format(load_balancer.id), 'device_owner': OCTAVIA_OWNER } } try: new_port = self.neutron_client.create_port(port) except Exception: message = _LE('Error creating neutron port on network ' '{network_id}.').format(network_id=subnet.network_id) LOG.exception(message) raise base.AllocateVIPException(message) new_port = utils.convert_port_dict_to_model(new_port) return self._port_to_vip(new_port, load_balancer)
def allocate_vip(self, load_balancer): if load_balancer.vip.port_id: LOG.info('Port %s already exists. Nothing to be done.', load_balancer.vip.port_id) port = self.get_port(load_balancer.vip.port_id) return self._port_to_vip(port, load_balancer) # It can be assumed that network_id exists port = { 'port': { 'name': 'octavia-lb-' + load_balancer.id, 'network_id': load_balancer.vip.network_id, 'admin_state_up': False, 'device_id': 'lb-{0}'.format(load_balancer.id), 'device_owner': OCTAVIA_OWNER } } try: new_port = self.neutron_client.create_port(port) except Exception: message = _('Error creating neutron port on network ' '{network_id}.').format( network_id=load_balancer.vip.network_id) LOG.exception(message) raise base.AllocateVIPException(message) new_port = utils.convert_port_dict_to_model(new_port) return self._port_to_vip(new_port, load_balancer)
def test_create_vip_port_failed(self, mock_get_net_driver): mock_net_driver = mock.MagicMock() mock_get_net_driver.return_value = mock_net_driver mock_net_driver.allocate_vip.side_effect = ( network_base.AllocateVIPException()) self.assertRaises(exceptions.DriverError, self.amp_driver.create_vip_port, self.sample_data.lb_id, self.sample_data.project_id, self.sample_data.provider_vip_dict)
def allocate_vrid_fip(self, vrid, network_id, amphorae, fixed_ip=None): fixed_ip_json = {} if vrid.subnet_id: fixed_ip_json['subnet_id'] = vrid.subnet_id if fixed_ip: fixed_ip_json['ip_address'] = fixed_ip # Make sure we are backward compatible with older neutron if self._check_extension_enabled(aap.PROJECT_ID_ALIAS): project_id_key = 'project_id' else: project_id_key = 'tenant_id' # It can be assumed that network_id exists port = { 'port': { 'name': 'octavia-vrid-fip-' + vrid.id, 'network_id': network_id, 'admin_state_up': False, 'device_id': 'vrid-{0}'.format(vrid.id), 'device_owner': aap.OCTAVIA_OWNER, project_id_key: vrid.owner } } if fixed_ip_json: port['port']['fixed_ips'] = [fixed_ip_json] try: new_port = self.neutron_client.create_port(port) except Exception as e: message = _('Error creating neutron port on network ' '{network_id}.').format(network_id=network_id) LOG.exception(message) raise base.AllocateVIPException( message, orig_msg=getattr(e, 'message', None), orig_code=getattr(e, 'status_code', None), ) new_port = utils.convert_port_dict_to_model(new_port) fixed_ip = new_port.fixed_ips[0].ip_address for amphora in filter( lambda amp: amp.status == constants.AMPHORA_ALLOCATED, amphorae): interface = self._get_plugged_interface(amphora.compute_id, network_id, amphora.lb_network_ip) self._add_allowed_address_pair_to_port(interface.port_id, fixed_ip) return new_port
def allocate_vip(self, load_balancer): if load_balancer.vip.port_id: LOG.info('Port %s already exists. Nothing to be done.', load_balancer.vip.port_id) port = self.get_port(load_balancer.vip.port_id) return self._port_to_vip(port, load_balancer) fixed_ip = {} if load_balancer.vip.subnet_id: fixed_ip['subnet_id'] = load_balancer.vip.subnet_id if load_balancer.vip.ip_address: fixed_ip['ip_address'] = load_balancer.vip.ip_address # Make sure we are backward compatible with older neutron if self._check_extension_enabled(PROJECT_ID_ALIAS): project_id_key = 'project_id' else: project_id_key = 'tenant_id' # It can be assumed that network_id exists port = { 'port': { 'name': 'octavia-lb-' + load_balancer.id, 'network_id': load_balancer.vip.network_id, 'admin_state_up': False, 'device_id': 'lb-{0}'.format(load_balancer.id), 'device_owner': OCTAVIA_OWNER, project_id_key: load_balancer.project_id } } if fixed_ip: port['port']['fixed_ips'] = [fixed_ip] try: new_port = self.neutron_client.create_port(port) except Exception as e: message = _('Error creating neutron port on network ' '{network_id}.').format( network_id=load_balancer.vip.network_id) LOG.exception(message) raise base.AllocateVIPException( message, orig_msg=getattr(e, 'message', None), orig_code=getattr(e, 'status_code', None), ) new_port = utils.convert_port_dict_to_model(new_port) return self._port_to_vip(new_port, load_balancer)
def allocate_vip(self, load_balancer): if load_balancer.vip.port_id: try: port = self.get_port(load_balancer.vip.port_id) fixed_ip_found = self._validate_fixed_ip( port.fixed_ips, load_balancer.vip.subnet_id, load_balancer.vip.ip_address) if (port.network_id == load_balancer.vip.network_id and fixed_ip_found): LOG.info('Port %s already exists. Nothing to be done.', load_balancer.vip.port_id) return self._port_to_vip(port, load_balancer) LOG.error( 'Neutron VIP mis-match. Expected ip %s on ' 'subnet %s in network %s. Neutron has fixed_ips %s ' 'in network %s. Deleting and recreating the VIP ' 'port.', load_balancer.vip.ip_address, load_balancer.vip.subnet_id, load_balancer.vip.network_id, self._fixed_ips_to_list_of_dicts(port.fixed_ips), port.network_id) if load_balancer.vip.octavia_owned: self.delete_port(load_balancer.vip.port_id) else: raise base.AllocateVIPException( 'VIP port {0} is broken, but is owned by project {1} ' 'so will not be recreated. Aborting VIP allocation.'. format(port.id, port.project_id)) except base.AllocateVIPException as e: # Catch this explicitly because otherwise we blame Neutron LOG.error(getattr(e, constants.MESSAGE, None)) raise except base.PortNotFound: LOG.warning('VIP port %s is missing from neutron. Rebuilding.', load_balancer.vip.port_id) except Exception as e: message = _('Neutron is failing to service requests due to: ' '{}. Aborting.').format(str(e)) LOG.error(message) raise base.AllocateVIPException( message, orig_msg=getattr(e, constants.MESSAGE, None), orig_code=getattr(e, constants.STATUS_CODE, None), ) fixed_ip = {} if load_balancer.vip.subnet_id: fixed_ip['subnet_id'] = load_balancer.vip.subnet_id if load_balancer.vip.ip_address: fixed_ip[constants.IP_ADDRESS] = load_balancer.vip.ip_address # Make sure we are backward compatible with older neutron if self._check_extension_enabled(PROJECT_ID_ALIAS): project_id_key = 'project_id' else: project_id_key = 'tenant_id' # It can be assumed that network_id exists port = { constants.PORT: { constants.NAME: 'octavia-lb-' + load_balancer.id, constants.NETWORK_ID: load_balancer.vip.network_id, constants.ADMIN_STATE_UP: False, 'device_id': 'lb-{0}'.format(load_balancer.id), constants.DEVICE_OWNER: OCTAVIA_OWNER, project_id_key: load_balancer.project_id } } if fixed_ip: port[constants.PORT][constants.FIXED_IPS] = [fixed_ip] try: new_port = self.neutron_client.create_port(port) except Exception as e: message = _('Error creating neutron port on network ' '{network_id} due to {e}.').format( network_id=load_balancer.vip.network_id, e=str(e)) LOG.exception(message) raise base.AllocateVIPException( message, orig_msg=getattr(e, constants.MESSAGE, None), orig_code=getattr(e, constants.STATUS_CODE, None), ) new_port = utils.convert_port_dict_to_model(new_port) return self._port_to_vip(new_port, load_balancer, octavia_owned=True)
def allocate_vip(self, load_balancer): port_id = load_balancer.vip.port_id if port_id: LOG.info('Port %s already exists. Nothing to be done.', port_id) port = self.get_port(port_id) return self._port_to_vip(port, load_balancer) fixed_ip = {} if load_balancer.vip.subnet_id: fixed_ip['subnet_id'] = load_balancer.vip.subnet_id if load_balancer.vip.ip_address: fixed_ip['ip_address'] = load_balancer.vip.ip_address # Make sure we are backward compatible with older neutron if self._check_extension_enabled(PROJECT_ID_ALIAS): project_id_key = 'project_id' else: project_id_key = 'tenant_id' # select a candidate to schedule to try: session = db_apis.get_session() candidate = self.amp_repo.get_candidates(session)[0] except ValueError as e: message = _('Scheduling failed, no ready candidates found') LOG.exception(message) raise base.AllocateVIPException( message, orig_msg=getattr(e, 'message', None), orig_code=getattr(e, 'status_code', None), ) LOG.debug("Found candidates for new LB %s: %s", load_balancer.id, candidate) # It can be assumed that network_id exists port = { 'port': { 'name': 'loadbalancer-{}'.format(load_balancer.id), 'network_id': load_balancer.vip.network_id, 'admin_state_up': True, 'device_id': load_balancer.id, 'device_owner': constants.DEVICE_OWNER_LISTENER, 'binding:host_id': candidate, project_id_key: load_balancer.project_id } } if fixed_ip: port['port']['fixed_ips'] = [fixed_ip] try: new_port = self.neutron_client.create_port(port) except Exception as e: message = _('Error creating neutron port on network ' '{network_id}.').format( network_id=load_balancer.vip.network_id) LOG.exception(message) raise base.AllocateVIPException( message, orig_msg=getattr(e, 'message', None), orig_code=getattr(e, 'status_code', None), ) new_port = utils.convert_port_dict_to_model(new_port) return self._port_to_vip(new_port, load_balancer)