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
|
|
|
|
from kuryr_kubernetes import exceptions
|
|
|
|
|
|
|
|
from neutronclient.common import exceptions as n_exc
|
|
|
|
|
|
|
|
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']
|
|
|
|
subnet_id = self._get_namespace_subnet(pod_namespace)
|
|
|
|
|
|
|
|
return {subnet_id: default_subnet._get_subnet(subnet_id)}
|
|
|
|
|
|
|
|
def _get_namespace_subnet(self, namespace):
|
|
|
|
kubernetes = clients.get_kubernetes_client()
|
|
|
|
try:
|
|
|
|
ns = kubernetes.get('%s/namespaces/%s' % (constants.K8S_API_BASE,
|
|
|
|
namespace))
|
|
|
|
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:
|
|
|
|
LOG.exception("Namespace missing CRD annotations for selecting "
|
|
|
|
"the corresponding subnet.")
|
|
|
|
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))
|
|
|
|
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
|
|
|
neutron = clients.get_neutron_client()
|
|
|
|
|
2018-06-29 14:38:18 +02:00
|
|
|
router_id = oslo_cfg.CONF.namespace_subnet.pod_router
|
2018-04-18 11:31:48 +00:00
|
|
|
subnet_id = net_crd['spec']['subnetId']
|
|
|
|
net_id = net_crd['spec']['netId']
|
|
|
|
|
|
|
|
try:
|
|
|
|
neutron.remove_interface_router(router_id,
|
|
|
|
{"subnet_id": subnet_id})
|
2018-06-05 13:21:16 +02:00
|
|
|
except n_exc.NotFound:
|
|
|
|
LOG.debug("Subnet %(subnet)s not attached to router %(router)s",
|
|
|
|
{'subnet': subnet_id, 'router': router_id})
|
|
|
|
except n_exc.NeutronClientException:
|
|
|
|
LOG.exception("Error deleting subnet %(subnet)s from router "
|
|
|
|
"%(router)s.", {'subnet': subnet_id, 'router':
|
|
|
|
router_id})
|
|
|
|
raise
|
|
|
|
|
|
|
|
try:
|
2018-04-18 11:31:48 +00:00
|
|
|
neutron.delete_network(net_id)
|
2018-06-05 13:21:16 +02:00
|
|
|
except n_exc.NotFound:
|
|
|
|
LOG.debug("Neutron Network not found: %s", net_id)
|
|
|
|
except n_exc.NetworkInUseClient:
|
|
|
|
LOG.exception("One or more ports in use on the network %s.",
|
|
|
|
net_id)
|
|
|
|
raise exceptions.ResourceNotReady(net_id)
|
2018-04-18 11:31:48 +00:00
|
|
|
except n_exc.NeutronClientException:
|
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):
|
2018-04-18 11:03:27 +00:00
|
|
|
neutron = clients.get_neutron_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 = neutron.create_network(
|
|
|
|
{
|
2018-07-05 08:25:40 +02:00
|
|
|
"network": {
|
|
|
|
"name": network_name,
|
|
|
|
"project_id": project_id
|
|
|
|
}
|
2018-04-18 11:03:27 +00:00
|
|
|
}).get('network')
|
|
|
|
|
|
|
|
# create a subnet within that network
|
|
|
|
neutron_subnet = neutron.create_subnet(
|
|
|
|
{
|
|
|
|
"subnet": {
|
|
|
|
"network_id": neutron_net['id'],
|
|
|
|
"ip_version": 4,
|
|
|
|
"name": subnet_name,
|
|
|
|
"enable_dhcp": False,
|
|
|
|
"subnetpool_id": subnet_pool_id,
|
2018-07-05 08:25:40 +02:00
|
|
|
"project_id": project_id
|
2018-04-18 11:03:27 +00:00
|
|
|
}
|
|
|
|
}).get('subnet')
|
|
|
|
|
|
|
|
# connect the subnet to the router
|
|
|
|
neutron.add_interface_router(router_id,
|
|
|
|
{"subnet_id": neutron_subnet['id']})
|
|
|
|
except n_exc.NeutronClientException as ex:
|
|
|
|
LOG.error("Error creating neutron resources for the namespace "
|
|
|
|
"%s: %s", namespace, ex)
|
|
|
|
raise ex
|
2018-06-29 14:38:18 +02:00
|
|
|
return {'netId': neutron_net['id'],
|
|
|
|
'routerId': router_id,
|
2018-07-10 18:29:07 +02: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):
|
2018-04-18 11:03:27 +00:00
|
|
|
neutron = clients.get_neutron_client()
|
|
|
|
try:
|
2018-06-29 14:38:18 +02:00
|
|
|
neutron.remove_interface_router(net_crd_spec['routerId'],
|
|
|
|
{'subnet_id':
|
|
|
|
net_crd_spec['subnetId']})
|
|
|
|
neutron.delete_network(net_crd_spec['netId'])
|
2018-04-18 11:03:27 +00:00
|
|
|
except n_exc.NeutronClientException:
|
|
|
|
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})
|