2018-04-18 11:03:27 +00:00
|
|
|
# Copyright 2018 Red Hat, Inc.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
from kuryr.lib._i18n import _
|
|
|
|
from oslo_config import cfg as oslo_cfg
|
|
|
|
from oslo_log import log as logging
|
|
|
|
|
|
|
|
from kuryr_kubernetes import clients
|
|
|
|
from kuryr_kubernetes import constants
|
|
|
|
from kuryr_kubernetes.controller.drivers import default_subnet
|
2019-02-13 10:04:08 +01:00
|
|
|
from kuryr_kubernetes.controller.drivers import utils as c_utils
|
2018-04-18 11:03:27 +00:00
|
|
|
from kuryr_kubernetes import exceptions
|
2018-08-09 17:24:17 +08:00
|
|
|
from kuryr_kubernetes import utils
|
2018-04-18 11:03:27 +00:00
|
|
|
|
2019-11-29 07:45:25 +01:00
|
|
|
from openstack import exceptions as os_exc
|
2018-04-18 11:03:27 +00:00
|
|
|
|
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
namespace_subnet_driver_opts = [
|
|
|
|
oslo_cfg.StrOpt('pod_router',
|
|
|
|
help=_("Default Neutron router ID where pod subnet(s) is "
|
|
|
|
"connected")),
|
|
|
|
oslo_cfg.StrOpt('pod_subnet_pool',
|
|
|
|
help=_("Default Neutron subnet pool ID where pod subnets "
|
|
|
|
"get their cidr from")),
|
|
|
|
]
|
|
|
|
|
|
|
|
oslo_cfg.CONF.register_opts(namespace_subnet_driver_opts, "namespace_subnet")
|
|
|
|
|
|
|
|
|
|
|
|
class NamespacePodSubnetDriver(default_subnet.DefaultPodSubnetDriver):
|
|
|
|
"""Provides subnet for Pod port based on a Pod's namespace."""
|
|
|
|
|
|
|
|
def get_subnets(self, pod, project_id):
|
|
|
|
pod_namespace = pod['metadata']['namespace']
|
2019-05-03 17:54:55 +02:00
|
|
|
return self.get_namespace_subnet(pod_namespace)
|
2018-04-18 11:03:27 +00:00
|
|
|
|
2019-05-03 17:54:55 +02:00
|
|
|
def get_namespace_subnet(self, namespace, subnet_id=None):
|
|
|
|
if not subnet_id:
|
|
|
|
subnet_id = self._get_namespace_subnet_id(namespace)
|
2018-08-09 17:24:17 +08:00
|
|
|
return {subnet_id: utils.get_subnet(subnet_id)}
|
2018-04-18 11:03:27 +00:00
|
|
|
|
2019-05-03 17:54:55 +02:00
|
|
|
def _get_namespace_subnet_id(self, namespace):
|
2018-04-18 11:03:27 +00:00
|
|
|
kubernetes = clients.get_kubernetes_client()
|
|
|
|
try:
|
|
|
|
ns = kubernetes.get('%s/namespaces/%s' % (constants.K8S_API_BASE,
|
|
|
|
namespace))
|
2019-10-11 13:47:23 +02:00
|
|
|
except exceptions.K8sResourceNotFound:
|
|
|
|
LOG.warning("Namespace %s not found", namespace)
|
|
|
|
raise
|
2018-04-18 11:03:27 +00:00
|
|
|
except exceptions.K8sClientException:
|
|
|
|
LOG.exception("Kubernetes Client Exception.")
|
|
|
|
raise exceptions.ResourceNotReady(namespace)
|
|
|
|
|
|
|
|
try:
|
|
|
|
annotations = ns['metadata']['annotations']
|
2018-04-18 11:31:48 +00:00
|
|
|
net_crd_name = annotations[constants.K8S_ANNOTATION_NET_CRD]
|
2018-04-18 11:03:27 +00:00
|
|
|
except KeyError:
|
2019-10-11 15:57:41 +02:00
|
|
|
LOG.debug("Namespace missing CRD annotations for selecting "
|
|
|
|
"the corresponding subnet.")
|
2018-04-18 11:03:27 +00:00
|
|
|
raise exceptions.ResourceNotReady(namespace)
|
|
|
|
|
|
|
|
try:
|
|
|
|
net_crd = kubernetes.get('%s/kuryrnets/%s' % (
|
2018-04-18 11:31:48 +00:00
|
|
|
constants.K8S_API_CRD, net_crd_name))
|
2019-10-07 13:58:43 +02:00
|
|
|
except exceptions.K8sResourceNotFound:
|
2019-10-11 15:57:41 +02:00
|
|
|
LOG.debug("Kuryrnet resource not yet created, retrying...")
|
2019-10-07 13:58:43 +02:00
|
|
|
raise exceptions.ResourceNotReady(net_crd_name)
|
2018-04-18 11:31:48 +00:00
|
|
|
except exceptions.K8sClientException:
|
2018-04-18 11:03:27 +00:00
|
|
|
LOG.exception("Kubernetes Client Exception.")
|
2018-04-18 11:31:48 +00:00
|
|
|
raise
|
2018-04-18 11:03:27 +00:00
|
|
|
|
|
|
|
return net_crd['spec']['subnetId']
|
|
|
|
|
2018-06-29 14:38:18 +02:00
|
|
|
def delete_namespace_subnet(self, net_crd):
|
2018-04-18 11:31:48 +00:00
|
|
|
subnet_id = net_crd['spec']['subnetId']
|
|
|
|
net_id = net_crd['spec']['netId']
|
|
|
|
|
2019-07-18 16:11:54 +02:00
|
|
|
self._delete_namespace_network_resources(subnet_id, net_id)
|
|
|
|
|
|
|
|
def _delete_namespace_network_resources(self, subnet_id, net_id):
|
2019-11-29 07:45:25 +01:00
|
|
|
os_net = clients.get_network_client()
|
2019-07-18 16:11:54 +02:00
|
|
|
if subnet_id:
|
|
|
|
router_id = oslo_cfg.CONF.namespace_subnet.pod_router
|
|
|
|
try:
|
2019-11-29 07:45:25 +01:00
|
|
|
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
|
2020-02-06 10:09:48 +01:00
|
|
|
except os_exc.SDKException:
|
|
|
|
LOG.exception("Error deleting subnet %(subnet)s from router "
|
|
|
|
"%(router)s.",
|
|
|
|
{'subnet': subnet_id, 'router': router_id})
|
|
|
|
raise
|
2018-06-05 13:21:16 +02:00
|
|
|
|
|
|
|
try:
|
2019-11-29 07:45:25 +01:00
|
|
|
os_net.delete_network(net_id)
|
|
|
|
except os_exc.ConflictException:
|
|
|
|
|
2019-07-18 16:11:54 +02:00
|
|
|
LOG.exception("One or more ports in use on the network %s. "
|
|
|
|
"Deleting leftovers ports before retrying", net_id)
|
2019-11-29 07:45:25 +01:00
|
|
|
leftover_ports = os_net.ports(status='DOWN', network_id=net_id)
|
2019-07-18 16:11:54 +02:00
|
|
|
for leftover_port in leftover_ports:
|
|
|
|
try:
|
2019-11-29 07:45:25 +01:00
|
|
|
# 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:
|
2019-10-18 14:26:45 +02:00
|
|
|
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 "
|
2019-11-29 07:45:25 +01:00
|
|
|
"trunk.", leftover_port.id)
|
2019-10-18 14:26:45 +02:00
|
|
|
# Get the trunk_id from the error message
|
|
|
|
trunk_id = (
|
|
|
|
str(e).split('trunk')[1].split('.')[0].strip())
|
2019-11-26 20:57:40 +00:00
|
|
|
try:
|
2019-11-29 07:45:25 +01:00
|
|
|
os_net.delete_trunk_subports(
|
|
|
|
trunk_id, [{'port_id': leftover_port.id}])
|
|
|
|
except os_exc.NotFoundException:
|
2019-11-26 20:57:40 +00:00
|
|
|
LOG.debug("Port %s already removed from trunk %s",
|
|
|
|
leftover_port['id'], trunk_id)
|
2019-10-18 14:26:45 +02:00
|
|
|
else:
|
|
|
|
LOG.exception("Unexpected error deleting leftover "
|
|
|
|
"port %s. Skiping it and continue with "
|
2019-11-29 07:45:25 +01:00
|
|
|
"the other rest.", leftover_port.id)
|
|
|
|
|
2018-06-05 13:21:16 +02:00
|
|
|
raise exceptions.ResourceNotReady(net_id)
|
2019-11-29 07:45:25 +01:00
|
|
|
except os_exc.SDKException:
|
2018-06-05 13:21:16 +02:00
|
|
|
LOG.exception("Error deleting network %s.", net_id)
|
2018-04-18 11:31:48 +00:00
|
|
|
raise
|
|
|
|
|
2018-07-05 08:25:40 +02:00
|
|
|
def create_namespace_network(self, namespace, project_id):
|
2019-11-29 07:45:25 +01:00
|
|
|
os_net = clients.get_network_client()
|
2018-04-18 11:03:27 +00:00
|
|
|
|
|
|
|
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:
|
2019-11-29 07:45:25 +01:00
|
|
|
neutron_net = os_net.create_network(name=network_name,
|
|
|
|
project_id=project_id)
|
2020-01-27 14:19:41 +01:00
|
|
|
c_utils.tag_neutron_resources([neutron_net])
|
2018-04-18 11:03:27 +00:00
|
|
|
|
|
|
|
# create a subnet within that network
|
2019-09-04 11:02:04 +02:00
|
|
|
try:
|
2019-11-29 07:45:25 +01:00
|
|
|
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:
|
2019-09-04 11:02:04 +02:00
|
|
|
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)
|
2020-01-27 14:19:41 +01:00
|
|
|
c_utils.tag_neutron_resources([neutron_subnet])
|
2018-04-18 11:03:27 +00:00
|
|
|
|
|
|
|
# connect the subnet to the router
|
2019-11-29 07:45:25 +01:00
|
|
|
clients.handle_neutron_errors(os_net.add_interface_to_router,
|
|
|
|
router_id,
|
|
|
|
subnet_id=neutron_subnet.id)
|
|
|
|
except os_exc.SDKException:
|
2019-01-24 17:34:50 +01:00
|
|
|
LOG.exception("Error creating neutron resources for the namespace "
|
|
|
|
"%s", namespace)
|
|
|
|
raise
|
2019-11-29 07:45:25 +01:00
|
|
|
return {'netId': neutron_net.id,
|
2018-06-29 14:38:18 +02:00
|
|
|
'routerId': router_id,
|
2019-11-29 07:45:25 +01:00
|
|
|
'subnetId': neutron_subnet.id,
|
|
|
|
'subnetCIDR': neutron_subnet.cidr}
|
2018-04-18 11:03:27 +00:00
|
|
|
|
2018-06-29 14:38:18 +02:00
|
|
|
def rollback_network_resources(self, net_crd_spec, namespace):
|
2019-11-29 07:45:25 +01:00
|
|
|
os_net = clients.get_network_client()
|
2018-04-18 11:03:27 +00:00
|
|
|
try:
|
2019-11-29 07:45:25 +01:00
|
|
|
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:
|
2018-04-18 11:03:27 +00:00
|
|
|
LOG.exception("Failed to clean up network resources associated to "
|
|
|
|
"%(net_id)s, created for the namespace: "
|
2018-06-29 14:38:18 +02:00
|
|
|
"%(namespace)s." % {'net_id': net_crd_spec['netId'],
|
2018-04-18 11:03:27 +00:00
|
|
|
'namespace': namespace})
|
2019-07-18 16:11:54 +02:00
|
|
|
|
|
|
|
def cleanup_namespace_networks(self, namespace):
|
2019-11-29 07:45:25 +01:00
|
|
|
os_net = clients.get_network_client()
|
2019-07-18 16:11:54 +02:00
|
|
|
net_name = 'ns/' + namespace + '-net'
|
|
|
|
tags = oslo_cfg.CONF.neutron_defaults.resource_tags
|
|
|
|
if tags:
|
2019-11-29 07:45:25 +01:00
|
|
|
networks = os_net.networks(name=net_name, tags=tags)
|
|
|
|
else:
|
|
|
|
networks = os_net.networks(name=net_name)
|
|
|
|
|
|
|
|
for net in networks:
|
|
|
|
net_id = net.id
|
|
|
|
subnets = net.subnet_ids
|
|
|
|
subnet_id = None
|
|
|
|
if subnets:
|
|
|
|
# NOTE(ltomasbo): Each network created by kuryr only has
|
|
|
|
# one subnet
|
|
|
|
subnet_id = subnets[0]
|
|
|
|
self._delete_namespace_network_resources(subnet_id, net_id)
|