Exemple #1
0
def osvif_to_neutron_fixed_ips(subnets):
    fixed_ips = []

    for subnet_id, network in six.iteritems(subnets):
        ips = []
        if len(network.subnets.objects) > 1:
            raise k_exc.IntegrityError(
                _LE("Network object for subnet %(subnet_id)s is invalid, "
                    "must contain a single subnet, but %(num_subnets)s found")
                % {
                    'subnet_id': subnet_id,
                    'num_subnets': len(network.subnets.objects)
                })

        for subnet in network.subnets.objects:
            if subnet.obj_attr_is_set('ips'):
                ips.extend([str(ip.address) for ip in subnet.ips.objects])
        if ips:
            fixed_ips.extend([{
                'subnet_id': subnet_id,
                'ip_address': ip
            } for ip in ips])
        else:
            fixed_ips.append({'subnet_id': subnet_id})

    return fixed_ips
Exemple #2
0
def _make_vif_subnets(neutron_port, subnets):
    """Gets a list of os-vif Subnet objects for port.

    :param neutron_port: dict containing port information as returned by
                         neutron client's 'show_port'
    :param subnets: subnet mapping as returned by PodSubnetsDriver.get_subnets
    :return: list of os-vif Subnet object
    """

    vif_subnets = {}

    for neutron_fixed_ip in neutron_port.get('fixed_ips', []):
        subnet_id = neutron_fixed_ip['subnet_id']
        ip_address = neutron_fixed_ip['ip_address']

        if subnet_id not in subnets:
            continue

        try:
            subnet = vif_subnets[subnet_id]
        except KeyError:
            subnet = _make_vif_subnet(subnets, subnet_id)
            vif_subnets[subnet_id] = subnet

        subnet.ips.objects.append(osv_fixed_ip.FixedIP(address=ip_address))

    if not vif_subnets:
        raise k_exc.IntegrityError(
            _LE("No valid subnets found for port %(port_id)s") %
            {'port_id': neutron_port.get('id')})

    return list(vif_subnets.values())
Exemple #3
0
def _make_vif_network(neutron_port, subnets):
    """Gets a os-vif Network object for port.

    :param neutron_port: dict containing port information as returned by
                         neutron client's 'show_port'
    :param subnets: subnet mapping as returned by PodSubnetsDriver.get_subnets
    :return: os-vif Network object
    """

    try:
        network = next(net.obj_clone() for net in subnets.values()
                       if net.id == neutron_port.get('network_id'))
    except StopIteration:
        raise k_exc.IntegrityError(
            _LE("Port %(port_id)s belongs to network %(network_id)s, "
                "but requested networks are: %(requested_networks)s") % {
                    'port_id': neutron_port.get('id'),
                    'network_id': neutron_port.get('network_id'),
                    'requested_networks': [net.id for net in subnets.values()]
                })

    network.subnets = osv_subnet.SubnetList(
        objects=_make_vif_subnets(neutron_port, subnets))

    return network
Exemple #4
0
    def _remove_from_allowed_address_pairs(self, port, ip_addresses,
                                           mac_address=None):
        if not ip_addresses:
            raise k_exc.IntegrityError(
                "Cannot remove pair from the "
                "allowed_address_pairs of port %s: missing IP address",
                port['id'])

        mac = mac_address if mac_address else port['mac_address']
        address_pairs = port['allowed_address_pairs']
        updated = False

        for ip in ip_addresses:
            try:
                address_pairs.remove({'ip_address': ip, 'mac_address': mac})
                updated = True
            except ValueError:
                LOG.error("No {'ip_address': %s, 'mac_address': %s} pair "
                          "found in the 'allowed_address_pair' list while "
                          "trying to remove it.", ip, mac)

        if updated:
            self._update_port_address_pairs(
                port['id'],
                address_pairs,
                revision_number=port['revision_number'])
Exemple #5
0
    def _add_to_allowed_address_pairs(self, port, ip_addresses,
                                      mac_address=None):
        if not ip_addresses:
            raise k_exc.IntegrityError(
                "Cannot add pair from the "
                "allowed_address_pairs of port %s: missing IP address",
                port['id'])

        mac = mac_address if mac_address else port['mac_address']
        address_pairs = port['allowed_address_pairs']

        # look for duplicates or near-matches
        for pair in address_pairs:
            if pair['ip_address'] in ip_addresses:
                if pair['mac_address'] is mac:
                    raise k_exc.AllowedAddressAlreadyPresent(
                        "Pair %s already "
                        "present in the 'allowed_address_pair' list. This is "
                        "due to a misconfiguration or a bug", pair)
                else:
                    LOG.warning(
                        "A pair with IP %s but different MAC address "
                        "is already present in the 'allowed_address_pair'. "
                        "This could indicate a misconfiguration or a "
                        "bug", pair['ip_address'])

        for ip in ip_addresses:
            address_pairs.append({'ip_address': ip, 'mac_address': mac})

        self._update_port_address_pairs(
            port['id'], address_pairs,
            revision_number=port['revision_number'])

        LOG.debug("Added allowed_address_pair %s %s" %
                  (str(ip_addresses,), mac_address))
Exemple #6
0
def _make_vif_network(neutron_port, subnets):
    """Get an os-vif Network object for port.

    :param neutron_port: dict containing port information as returned by
                         neutron client's 'show_port', or
                         openstack.network.v2.port.Port object
    :param subnets: subnet mapping as returned by PodSubnetsDriver.get_subnets
    :return: os-vif Network object
    """

    # NOTE(gryf): Because we didn't convert macvlan driver, neutron_port can
    # be either a dict or an object
    try:
        network_id = neutron_port.get('network_id')
        port_id = neutron_port.get('id')
    except TypeError:
        network_id = neutron_port.network_id
        port_id = neutron_port.id

    try:
        network = next(net.obj_clone() for net in subnets.values()
                       if net.id == network_id)
    except StopIteration:
        raise k_exc.IntegrityError(
            _("Port %(port_id)s belongs to network %(network_id)s, "
              "but requested networks are: %(requested_networks)s") % {
                  'port_id': port_id,
                  'network_id': network_id,
                  'requested_networks': [net.id for net in subnets.values()]
              })

    network.subnets = osv_subnet.SubnetList(
        objects=_make_vif_subnets(neutron_port, subnets))

    return network
Exemple #7
0
def _get_vhostport_type(vif):
    if vif.mode == osv_fields.VIFVHostUserMode.SERVER:
        return 'dpdkvhostuserclient'
    elif vif.mode == osv_fields.VIFVHostUserMode.CLIENT:
        return 'dpdkvhostuser'
    raise k_exc.IntegrityError(
        _("Unknown vhostuser mode %(mode)s for vif %(vif_id)s")
        % {'mode': vif.mode, 'vif_id': vif.id})
Exemple #8
0
    def _get_service_link(self, endpoints):
        ep_link = endpoints['metadata']['selfLink']
        link_parts = ep_link.split('/')

        if link_parts[-2] != 'endpoints':
            raise k_exc.IntegrityError(
                _("Unsupported endpoints link: %(link)s") % {'link': ep_link})
        link_parts[-2] = 'services'
        return "/".join(link_parts)
Exemple #9
0
    def _get_network_id(self, subnets):
        ids = ovu.osvif_to_neutron_network_ids(subnets)

        if len(ids) != 1:
            raise k_exc.IntegrityError("Subnet mapping %(subnets)s is not "
                "valid: %(num_networks)s unique networks found" %
                {'subnets': subnets, 'num_networks': len(ids)})

        return ids[0]
Exemple #10
0
def get_network_id(subnets):
    ids = list(set(net.id for net in subnets.values()))

    if len(ids) != 1:
        raise k_exc.IntegrityError(
            "Subnet mapping %(subnets)s is not valid: "
            "%(num_networks)s unique networks found" %
            {'subnets': subnets, 'num_networks': len(ids)})

    return ids[0]
Exemple #11
0
def get_endpoints_link(service):
    svc_link = service['metadata']['selfLink']
    link_parts = svc_link.split('/')

    if link_parts[-2] != 'services':
        raise exceptions.IntegrityError(
            _("Unsupported service link: %(link)s") % {'link': svc_link})
    link_parts[-2] = 'endpoints'

    return "/".join(link_parts)
Exemple #12
0
def get_service_link(endpoints):
    endpoints_link = endpoints['metadata']['selfLink']
    link_parts = endpoints_link.split('/')

    if link_parts[-2] != 'endpoints':
        raise exceptions.IntegrityError(
            f"Unsupported endpoints link: {endpoints_link}")
    link_parts[-2] = 'services'

    return "/".join(link_parts)
def get_endpoints_link(service):
    svc_link = get_res_link(service)
    link_parts = svc_link.split('/')

    if link_parts[-2] != 'services':
        raise exceptions.IntegrityError(
            f"Unsupported service link: {svc_link}")
    link_parts[-2] = 'endpoints'

    return "/".join(link_parts)
Exemple #14
0
 def _get_endpoints_link_by_route(self, route_link, ep_name):
     route_link = route_link.replace(ocp_const.OCP_API_BASE,
                                     k_const.K8S_API_BASE)
     link_parts = route_link.split('/')
     if link_parts[-2] != 'routes':
         raise k_exc.IntegrityError(
             _("Unsupported route link: %(link)s") % {'link': route_link})
     link_parts[-2] = 'endpoints'
     link_parts[-1] = ep_name
     return "/".join(link_parts)
Exemple #15
0
    def _get_subnet_id(self, service, project_id, ip):
        subnets_mapping = self._drv_subnets.get_subnets(service, project_id)
        subnet_ids = {
            subnet_id
            for subnet_id, network in subnets_mapping.items()
            for subnet in network.subnets.objects
            if ip in subnet.cidr}

        if len(subnet_ids) != 1:
            raise k_exc.IntegrityError(_(
                "Found %(num)s subnets for service %(link)s IP %(ip)s") % {
                    'link': service['metadata']['selfLink'],
                    'ip': ip,
                    'num': len(subnet_ids)})

        return subnet_ids.pop()
Exemple #16
0
def _make_vif_subnet(subnets, subnet_id):
    """Makes a copy of an os-vif Subnet from subnets mapping.

    :param subnets: subnet mapping as returned by PodSubnetsDriver.get_subnets
    :param subnet_id: ID of the subnet to extract from 'subnets' mapping
    :return: a copy of an os-vif Subnet object matching 'subnet_id'
    """

    network = subnets[subnet_id]

    if len(network.subnets.objects) != 1:
        raise k_exc.IntegrityError(_(
            "Network object for subnet %(subnet_id)s is invalid, "
            "must contain a single subnet, but %(num_subnets)s found") % {
            'subnet_id': subnet_id,
            'num_subnets': len(network.subnets.objects)})

    subnet = network.subnets.objects[0].obj_clone()
    subnet.ips = osv_fixed_ip.FixedIPList(objects=[])
    return subnet