def _delete_namespace_network_resources(self, subnet_id, net_id): os_net = clients.get_network_client() if subnet_id: router_id = oslo_cfg.CONF.namespace_subnet.pod_router try: clients.handle_neutron_errors( os_net.remove_interface_from_router, router_id, subnet_id=subnet_id) except os_exc.NotFoundException as e: # Nothing to worry about, either router or subnet is no more, # or subnet is already detached. LOG.debug(e.message) pass except os_exc.SDKException: LOG.exception("Error deleting subnet %(subnet)s from router " "%(router)s.", {'subnet': subnet_id, 'router': router_id}) raise try: os_net.delete_network(net_id) except os_exc.ConflictException: LOG.warning("One or more ports in use on the network %s. " "Deleting leftovers ports before retrying", net_id) # NOTE(dulek): '' is there because Neutron seems to unset # device_owner on detach. leftover_ports = [p for p in os_net.ports(network_id=net_id) if p.device_owner in ['', 'trunk:subport', kl_const.DEVICE_OWNER]] c_utils.delete_ports(leftover_ports) raise exceptions.ResourceNotReady(net_id) except os_exc.SDKException: LOG.exception("Error deleting network %s.", net_id) raise
def _delete_namespace_network_resources(self, subnet_id, net_id): os_net = clients.get_network_client() if subnet_id: router_id = oslo_cfg.CONF.namespace_subnet.pod_router try: clients.handle_neutron_errors( os_net.remove_interface_from_router, router_id, subnet_id=subnet_id) except os_exc.NotFoundException as e: # Nothing to worry about, either router or subnet is no more, # or subnet is already detached. LOG.debug(e.message) pass except os_exc.SDKException: LOG.exception("Error deleting subnet %(subnet)s from router " "%(router)s.", {'subnet': subnet_id, 'router': router_id}) raise try: os_net.delete_network(net_id) except os_exc.ConflictException: LOG.exception("One or more ports in use on the network %s. " "Deleting leftovers ports before retrying", net_id) leftover_ports = os_net.ports(status='DOWN', network_id=net_id) for leftover_port in leftover_ports: try: # NOTE(gryf): there is unlikely, that we get an exception # like PortNotFound or something, since openstacksdk # doesn't raise an exception if port doesn't exists nor # return any information. os_net.delete_port(leftover_port.id) except os_exc.SDKException as e: if "currently a subport for trunk" in str(e): LOG.warning("Port %s is in DOWN status but still " "associated to a trunk. This should not " "happen. Trying to delete it from the " "trunk.", leftover_port.id) # Get the trunk_id from the error message trunk_id = ( str(e).split('trunk')[1].split('.')[0].strip()) try: os_net.delete_trunk_subports( trunk_id, [{'port_id': leftover_port.id}]) except os_exc.NotFoundException: LOG.debug("Port %s already removed from trunk %s", leftover_port['id'], trunk_id) else: LOG.exception("Unexpected error deleting leftover " "port %s. Skiping it and continue with " "the other rest.", leftover_port.id) raise exceptions.ResourceNotReady(net_id) except os_exc.SDKException: LOG.exception("Error deleting network %s.", net_id) raise
def rollback_network_resources(self, net_crd_spec, namespace): os_net = clients.get_network_client() try: try: clients.handle_neutron_errors( os_net.remove_interface_from_router, net_crd_spec['routerId'], subnet_id=net_crd_spec['subnetId']) except os_exc.NotFoundException: # Nothing to worry about, either router or subnet is no more, # or subnet is already detached. pass os_net.delete_network(net_crd_spec['netId']) except os_exc.SDKException: LOG.exception("Failed to clean up network resources associated to " "%(net_id)s, created for the namespace: " "%(namespace)s." % {'net_id': net_crd_spec['netId'], 'namespace': namespace})
def create_namespace_network(self, namespace, project_id): os_net = clients.get_network_client() router_id = oslo_cfg.CONF.namespace_subnet.pod_router subnet_pool_id = oslo_cfg.CONF.namespace_subnet.pod_subnet_pool # create network with namespace as name network_name = "ns/" + namespace + "-net" subnet_name = "ns/" + namespace + "-subnet" try: neutron_net = os_net.create_network(name=network_name, project_id=project_id) c_utils.tag_neutron_resources([neutron_net]) # create a subnet within that network try: neutron_subnet = (os_net .create_subnet(network_id=neutron_net.id, ip_version=4, name=subnet_name, enable_dhcp=False, subnetpool_id=subnet_pool_id, project_id=project_id)) except os_exc.ConflictException: LOG.debug("Max number of retries on neutron side achieved, " "raising ResourceNotReady to retry subnet creation " "for %s", subnet_name) raise exceptions.ResourceNotReady(subnet_name) c_utils.tag_neutron_resources([neutron_subnet]) # connect the subnet to the router clients.handle_neutron_errors(os_net.add_interface_to_router, router_id, subnet_id=neutron_subnet.id) except os_exc.SDKException: LOG.exception("Error creating neutron resources for the namespace " "%s", namespace) raise return {'netId': neutron_net.id, 'routerId': router_id, 'subnetId': neutron_subnet.id, 'subnetCIDR': neutron_subnet.cidr}