2017-02-22 10:18:37 +01:00
|
|
|
# Copyright (c) 2017 Red Hat, Inc.
|
|
|
|
# All Rights Reserved.
|
|
|
|
#
|
|
|
|
# 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.
|
2017-02-22 12:44:46 +01:00
|
|
|
|
|
|
|
import abc
|
2017-02-22 11:10:05 +01:00
|
|
|
import collections
|
2017-02-22 12:00:48 +01:00
|
|
|
import eventlet
|
2019-08-21 12:48:59 +02:00
|
|
|
import os
|
2017-02-22 12:44:46 +01:00
|
|
|
import six
|
2017-02-22 12:00:48 +01:00
|
|
|
import time
|
2017-02-22 10:18:37 +01:00
|
|
|
|
2017-02-22 11:10:05 +01:00
|
|
|
from kuryr.lib._i18n import _
|
2017-06-15 13:07:41 +00:00
|
|
|
from kuryr.lib import constants as kl_const
|
2017-02-22 11:10:05 +01:00
|
|
|
from neutronclient.common import exceptions as n_exc
|
2017-12-15 17:56:07 +01:00
|
|
|
from oslo_cache import core as cache
|
2018-08-10 12:14:37 +02:00
|
|
|
from oslo_concurrency import lockutils
|
2017-02-22 11:10:05 +01:00
|
|
|
from oslo_config import cfg as oslo_cfg
|
|
|
|
from oslo_log import log as logging
|
2018-08-27 17:43:27 +09:00
|
|
|
from oslo_log import versionutils
|
2017-09-18 11:22:22 +00:00
|
|
|
from oslo_serialization import jsonutils
|
2017-02-22 11:10:05 +01:00
|
|
|
|
|
|
|
from kuryr_kubernetes import clients
|
2017-09-18 11:22:22 +00:00
|
|
|
from kuryr_kubernetes import config
|
|
|
|
from kuryr_kubernetes import constants
|
2017-02-22 10:18:37 +01:00
|
|
|
from kuryr_kubernetes.controller.drivers import base
|
2018-08-09 02:38:05 -04:00
|
|
|
from kuryr_kubernetes.controller.drivers import utils as c_utils
|
2017-08-29 08:43:34 +02:00
|
|
|
from kuryr_kubernetes.controller.managers import pool
|
2017-02-22 11:10:05 +01:00
|
|
|
from kuryr_kubernetes import exceptions
|
2017-06-15 13:07:41 +00:00
|
|
|
from kuryr_kubernetes import os_vif_util as ovu
|
2017-12-15 17:56:07 +01:00
|
|
|
from kuryr_kubernetes import utils
|
2017-02-22 11:10:05 +01:00
|
|
|
|
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
# Moved out from neutron_default group
|
|
|
|
vif_pool_driver_opts = [
|
|
|
|
oslo_cfg.IntOpt('ports_pool_max',
|
2019-06-06 12:52:59 +03:00
|
|
|
help=_("Set a maximum amount of ports per pool. "
|
2017-06-08 15:53:52 +03:00
|
|
|
"0 to disable"),
|
|
|
|
default=0),
|
2017-02-22 12:00:48 +01:00
|
|
|
oslo_cfg.IntOpt('ports_pool_min',
|
2017-06-08 15:53:52 +03:00
|
|
|
help=_("Set a target minimum size of the pool of ports"),
|
|
|
|
default=5),
|
2017-02-22 12:00:48 +01:00
|
|
|
oslo_cfg.IntOpt('ports_pool_batch',
|
2017-06-08 15:53:52 +03:00
|
|
|
help=_("Number of ports to be created in a bulk request"),
|
|
|
|
default=10),
|
2017-02-22 12:00:48 +01:00
|
|
|
oslo_cfg.IntOpt('ports_pool_update_frequency',
|
2019-06-06 12:52:59 +03:00
|
|
|
help=_("Minimum interval (in seconds) "
|
2017-06-08 15:53:52 +03:00
|
|
|
"between pool updates"),
|
|
|
|
default=20),
|
2017-12-15 17:56:07 +01:00
|
|
|
oslo_cfg.DictOpt('pools_vif_drivers',
|
|
|
|
help=_("Dict with the pool driver and pod driver to be "
|
|
|
|
"used. If not set, it will take them from the "
|
|
|
|
"kubernetes driver options for pool and pod "
|
|
|
|
"drivers respectively"),
|
2018-08-27 17:43:27 +09:00
|
|
|
default={}, deprecated_for_removal=True,
|
|
|
|
deprecated_since="Stein",
|
|
|
|
deprecated_reason=_(
|
|
|
|
"Mapping from pool->vif does not allow different "
|
|
|
|
"vifs to use the same pool driver. "
|
|
|
|
"Use vif_pool_mapping instead.")),
|
|
|
|
oslo_cfg.DictOpt('vif_pool_mapping',
|
|
|
|
help=_("Dict with the pod driver and the corresponding "
|
|
|
|
"pool driver to be used. If not set, it will take "
|
|
|
|
"them from the kubernetes driver options for pool "
|
|
|
|
"and pod drivers respectively"),
|
2017-12-15 17:56:07 +01:00
|
|
|
default={}),
|
2017-02-22 11:10:05 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
oslo_cfg.CONF.register_opts(vif_pool_driver_opts, "vif_pool")
|
2017-02-22 10:18:37 +01:00
|
|
|
|
2017-12-15 17:56:07 +01:00
|
|
|
node_vif_driver_caching_opts = [
|
|
|
|
oslo_cfg.BoolOpt('caching', default=True),
|
|
|
|
oslo_cfg.IntOpt('cache_time', default=3600),
|
|
|
|
]
|
|
|
|
|
|
|
|
oslo_cfg.CONF.register_opts(node_vif_driver_caching_opts,
|
|
|
|
"node_driver_caching")
|
|
|
|
|
|
|
|
cache.configure(oslo_cfg.CONF)
|
|
|
|
node_driver_cache_region = cache.create_region()
|
|
|
|
MEMOIZE = cache.get_memoization_decorator(
|
|
|
|
oslo_cfg.CONF, node_driver_cache_region, "node_driver_caching")
|
|
|
|
|
|
|
|
cache.configure_cache_region(oslo_cfg.CONF, node_driver_cache_region)
|
|
|
|
|
2018-08-11 02:54:02 -04:00
|
|
|
VIF_TYPE_TO_DRIVER_MAPPING = {
|
|
|
|
'VIFOpenVSwitch': 'neutron-vif',
|
|
|
|
'VIFBridge': 'neutron-vif',
|
|
|
|
'VIFVlanNested': 'nested-vlan',
|
2017-10-10 14:30:59 +03:00
|
|
|
'VIFMacvlanNested': 'nested-macvlan',
|
|
|
|
'VIFSriov': 'sriov'
|
2018-08-11 02:54:02 -04:00
|
|
|
}
|
|
|
|
|
2017-02-22 10:18:37 +01:00
|
|
|
|
|
|
|
class NoopVIFPool(base.VIFPoolDriver):
|
|
|
|
"""No pool VIFs for Kubernetes Pods"""
|
|
|
|
|
|
|
|
def set_vif_driver(self, driver):
|
|
|
|
self._drv_vif = driver
|
|
|
|
|
|
|
|
def request_vif(self, pod, project_id, subnets, security_groups):
|
|
|
|
return self._drv_vif.request_vif(pod, project_id, subnets,
|
|
|
|
security_groups)
|
|
|
|
|
2017-02-22 11:10:05 +01:00
|
|
|
def release_vif(self, pod, vif, *argv):
|
2018-08-20 16:50:50 +09:00
|
|
|
self._drv_vif.release_vif(pod, vif, *argv)
|
2017-02-22 10:18:37 +01:00
|
|
|
|
|
|
|
def activate_vif(self, pod, vif):
|
|
|
|
self._drv_vif.activate_vif(pod, vif)
|
2017-02-22 11:10:05 +01:00
|
|
|
|
2018-11-07 18:46:07 +01:00
|
|
|
def update_vif_sgs(self, pod, sgs):
|
|
|
|
self._drv_vif.update_vif_sgs(pod, sgs)
|
|
|
|
|
2019-10-04 13:11:56 +02:00
|
|
|
def remove_sg_from_pools(self, sg_id, net_id):
|
|
|
|
pass
|
|
|
|
|
2018-08-20 17:49:52 +02:00
|
|
|
def sync_pools(self):
|
|
|
|
pass
|
|
|
|
|
2017-02-22 11:10:05 +01:00
|
|
|
|
2017-02-22 12:44:46 +01:00
|
|
|
@six.add_metaclass(abc.ABCMeta)
|
|
|
|
class BaseVIFPool(base.VIFPoolDriver):
|
|
|
|
"""Skeletal pool driver.
|
2017-02-22 11:10:05 +01:00
|
|
|
|
|
|
|
In order to handle the pools of ports, a few dicts are used:
|
|
|
|
_available_ports_pool is a dictionary with the ready to use Neutron ports
|
|
|
|
information. The keys are the 'pool_key' and the values the 'port_id's.
|
|
|
|
_existing_vifs is a dictionary containing the port vif objects. The keys
|
|
|
|
are the 'port_id' and the values are the vif objects.
|
|
|
|
_recyclable_ports is a dictionary with the Neutron ports to be
|
|
|
|
recycled. The keys are the 'port_id' and their values are the 'pool_key'.
|
2017-02-22 12:51:35 +01:00
|
|
|
_last_update is a dictionary with the timestamp of the last population
|
|
|
|
action for each pool. The keys are the pool_keys and the values are the
|
|
|
|
timestamps.
|
2017-02-22 11:10:05 +01:00
|
|
|
|
|
|
|
The following driver configuration options exist:
|
|
|
|
- ports_pool_max: it specifies how many ports can be kept at each pool.
|
|
|
|
If the pool already reached the specified size, the ports to be recycled
|
|
|
|
are deleted instead. If set to 0, the limit is disabled and ports are
|
|
|
|
always recycled.
|
2017-02-22 12:00:48 +01:00
|
|
|
- ports_pool_min: minimum desired number of ready to use ports at populated
|
|
|
|
pools. Should be smaller than ports_pool_max (if enabled).
|
|
|
|
- ports_pool_batch: target number of ports to be created in bulk requests
|
|
|
|
when populating pools.
|
|
|
|
- ports_pool_update_frequency: interval in seconds between ports pool
|
|
|
|
updates, both for populating pools as well as for recycling ports.
|
2017-02-22 11:10:05 +01:00
|
|
|
"""
|
2017-02-22 12:00:48 +01:00
|
|
|
|
2017-02-22 12:51:35 +01:00
|
|
|
def __init__(self):
|
|
|
|
# Note(ltomasbo) Execute the port recycling periodic actions in a
|
|
|
|
# background thread
|
2019-11-26 14:09:32 +01:00
|
|
|
self._recovered_pools = False
|
2017-02-22 12:51:35 +01:00
|
|
|
eventlet.spawn(self._return_ports_to_pool)
|
|
|
|
|
2017-02-22 12:44:46 +01:00
|
|
|
def set_vif_driver(self, driver):
|
|
|
|
self._drv_vif = driver
|
|
|
|
|
|
|
|
def activate_vif(self, pod, vif):
|
|
|
|
self._drv_vif.activate_vif(pod, vif)
|
|
|
|
|
2018-11-07 18:46:07 +01:00
|
|
|
def update_vif_sgs(self, pod, sgs):
|
|
|
|
self._drv_vif.update_vif_sgs(pod, sgs)
|
|
|
|
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
def _get_pool_size(self, pool_key):
|
|
|
|
pool = self._available_ports_pools.get(pool_key, {})
|
|
|
|
pool_members = []
|
|
|
|
for port_list in pool.values():
|
|
|
|
pool_members.extend(port_list)
|
|
|
|
return len(pool_members)
|
2017-02-22 12:44:46 +01:00
|
|
|
|
2017-09-27 17:02:32 +00:00
|
|
|
def _get_host_addr(self, pod):
|
|
|
|
return pod['status']['hostIP']
|
|
|
|
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
def _get_pool_key(self, host, project_id, net_id=None, subnets=None):
|
2018-02-28 17:22:44 +00:00
|
|
|
if not net_id and subnets:
|
|
|
|
net_obj = list(subnets.values())[0]
|
|
|
|
net_id = net_obj.id
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
pool_key = (host, project_id, net_id)
|
2018-02-28 17:22:44 +00:00
|
|
|
return pool_key
|
|
|
|
|
2018-04-25 09:31:10 +00:00
|
|
|
def _get_pool_key_net(self, pool_key):
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
return pool_key[2]
|
2018-04-25 09:31:10 +00:00
|
|
|
|
2017-02-22 11:10:05 +01:00
|
|
|
def request_vif(self, pod, project_id, subnets, security_groups):
|
|
|
|
try:
|
2017-09-27 17:02:32 +00:00
|
|
|
host_addr = self._get_host_addr(pod)
|
2017-02-22 11:10:05 +01:00
|
|
|
except KeyError:
|
2019-08-21 23:09:43 +02:00
|
|
|
return None
|
|
|
|
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
pool_key = self._get_pool_key(host_addr, project_id, None, subnets)
|
2017-02-22 11:10:05 +01:00
|
|
|
|
|
|
|
try:
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
return self._get_port_from_pool(pool_key, pod, subnets,
|
|
|
|
tuple(sorted(security_groups)))
|
2019-01-24 17:34:50 +01:00
|
|
|
except exceptions.ResourceNotReady:
|
2019-10-28 17:48:58 +01:00
|
|
|
LOG.debug("Ports pool does not have available ports: %s",
|
|
|
|
pool_key)
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
eventlet.spawn(self._populate_pool, pool_key, pod, subnets,
|
|
|
|
tuple(sorted(security_groups)))
|
2019-01-24 17:34:50 +01:00
|
|
|
raise
|
2017-02-22 12:00:48 +01:00
|
|
|
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
def _get_port_from_pool(self, pool_key, pod, subnets, security_groups):
|
2017-10-04 09:26:05 +02:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
def _populate_pool(self, pool_key, pod, subnets, security_groups):
|
2017-02-22 12:00:48 +01:00
|
|
|
# REVISIT(ltomasbo): Drop the subnets parameter and get the information
|
|
|
|
# from the pool_key, which will be required when multi-network is
|
|
|
|
# supported
|
2019-11-26 14:09:32 +01:00
|
|
|
if not self._recovered_pools:
|
|
|
|
LOG.info("Kuryr-controller not yet ready to populate pools.")
|
|
|
|
raise exceptions.ResourceNotReady(pod)
|
2017-02-22 12:00:48 +01:00
|
|
|
now = time.time()
|
2019-10-28 17:03:43 +01:00
|
|
|
last_update = 0
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
pool_updates = self._last_update.get(pool_key)
|
|
|
|
if pool_updates:
|
|
|
|
last_update = pool_updates.get(security_groups, 0)
|
|
|
|
try:
|
|
|
|
if (now - oslo_cfg.CONF.vif_pool.ports_pool_update_frequency <
|
|
|
|
last_update):
|
|
|
|
LOG.info("Not enough time since the last pool update")
|
|
|
|
return
|
|
|
|
except AttributeError:
|
|
|
|
LOG.info("Kuryr-controller not yet ready to populate pools")
|
2018-10-29 16:30:17 +01:00
|
|
|
return
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
self._last_update[pool_key] = {security_groups: now}
|
2017-02-22 12:00:48 +01:00
|
|
|
|
|
|
|
pool_size = self._get_pool_size(pool_key)
|
|
|
|
if pool_size < oslo_cfg.CONF.vif_pool.ports_pool_min:
|
|
|
|
num_ports = max(oslo_cfg.CONF.vif_pool.ports_pool_batch,
|
2017-06-08 15:53:52 +03:00
|
|
|
oslo_cfg.CONF.vif_pool.ports_pool_min - pool_size)
|
|
|
|
vifs = self._drv_vif.request_vifs(
|
|
|
|
pod=pod,
|
2017-02-22 12:00:48 +01:00
|
|
|
project_id=pool_key[1],
|
|
|
|
subnets=subnets,
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
security_groups=security_groups,
|
2017-02-22 12:00:48 +01:00
|
|
|
num_ports=num_ports)
|
|
|
|
for vif in vifs:
|
|
|
|
self._existing_vifs[vif.id] = vif
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
self._available_ports_pools.setdefault(
|
|
|
|
pool_key, {}).setdefault(
|
|
|
|
security_groups, []).append(vif.id)
|
2019-10-14 16:22:17 +02:00
|
|
|
if not vifs:
|
|
|
|
self._last_update[pool_key] = {security_groups: last_update}
|
2017-02-22 12:00:48 +01:00
|
|
|
|
2019-10-15 12:12:04 +02:00
|
|
|
def release_vif(self, pod, vif, project_id, security_groups,
|
|
|
|
host_addr=None):
|
|
|
|
if not host_addr:
|
|
|
|
host_addr = self._get_host_addr(pod)
|
2018-02-28 17:22:44 +00:00
|
|
|
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
pool_key = self._get_pool_key(host_addr, project_id, vif.network.id,
|
|
|
|
None)
|
2017-02-22 12:51:35 +01:00
|
|
|
|
2018-10-29 16:30:17 +01:00
|
|
|
try:
|
|
|
|
if not self._existing_vifs.get(vif.id):
|
|
|
|
self._existing_vifs[vif.id] = vif
|
|
|
|
self._recyclable_ports[vif.id] = pool_key
|
|
|
|
except AttributeError:
|
|
|
|
LOG.info("Kuryr-controller is not ready to handle the pools yet.")
|
|
|
|
raise exceptions.ResourceNotReady(pod)
|
2017-02-22 12:51:35 +01:00
|
|
|
|
2017-10-04 09:26:05 +02:00
|
|
|
def _return_ports_to_pool(self):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def _recover_precreated_ports(self):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2017-09-18 11:22:22 +00:00
|
|
|
def _get_in_use_ports(self):
|
|
|
|
kubernetes = clients.get_kubernetes_client()
|
|
|
|
in_use_ports = []
|
|
|
|
running_pods = kubernetes.get(constants.K8S_API_BASE + '/pods')
|
|
|
|
for pod in running_pods['items']:
|
2017-10-04 09:26:05 +02:00
|
|
|
try:
|
|
|
|
annotations = jsonutils.loads(pod['metadata']['annotations'][
|
|
|
|
constants.K8S_ANNOTATION_VIF])
|
2018-08-16 17:34:26 +02:00
|
|
|
pod_state = utils.extract_pod_annotation(annotations)
|
2017-10-04 09:26:05 +02:00
|
|
|
except KeyError:
|
|
|
|
LOG.debug("Skipping pod without kuryr VIF annotation: %s",
|
|
|
|
pod)
|
|
|
|
else:
|
2018-08-16 17:34:26 +02:00
|
|
|
for vif in pod_state.vifs.values():
|
|
|
|
in_use_ports.append(vif.id)
|
2017-09-18 11:22:22 +00:00
|
|
|
return in_use_ports
|
|
|
|
|
2017-09-15 13:36:18 +00:00
|
|
|
def list_pools(self):
|
|
|
|
return self._available_ports_pools
|
|
|
|
|
|
|
|
def show_pool(self, pool_key):
|
|
|
|
return self._available_ports_pools.get(pool_key)
|
|
|
|
|
2018-04-25 09:31:10 +00:00
|
|
|
def delete_network_pools(self, net_id):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2019-10-04 13:11:56 +02:00
|
|
|
def remove_sg_from_pools(self, sg_id, net_id):
|
|
|
|
neutron = clients.get_neutron_client()
|
2020-01-09 11:52:47 +01:00
|
|
|
for pool_key, pool_ports in list(self._available_ports_pools.items()):
|
2019-10-04 13:11:56 +02:00
|
|
|
if self._get_pool_key_net(pool_key) != net_id:
|
|
|
|
continue
|
2020-01-16 11:57:47 +01:00
|
|
|
for sg_key, ports in list(pool_ports.items()):
|
2019-10-04 13:11:56 +02:00
|
|
|
if sg_id not in sg_key:
|
|
|
|
continue
|
|
|
|
# remove the pool associated to that SG
|
|
|
|
del self._available_ports_pools[pool_key][sg_key]
|
|
|
|
for port_id in ports:
|
|
|
|
# remove all SGs from the port to be reused
|
|
|
|
neutron.update_port(
|
|
|
|
port_id,
|
|
|
|
{
|
|
|
|
"port": {
|
|
|
|
'security_groups': []
|
|
|
|
}
|
|
|
|
})
|
|
|
|
# add the port to the default pool
|
|
|
|
self._available_ports_pools[pool_key].setdefault(
|
|
|
|
tuple([]), []).append(port_id)
|
|
|
|
# NOTE(ltomasbo): as this ports were not created for this
|
|
|
|
# pool, ensuring they are used first, marking them as the
|
|
|
|
# most outdated
|
|
|
|
self._last_update[pool_key] = {tuple([]): 0}
|
|
|
|
|
2017-11-08 10:40:32 +01:00
|
|
|
def _create_healthcheck_file(self):
|
|
|
|
# Note(ltomasbo): Create a health check file when the pre-created
|
|
|
|
# ports are loaded into their corresponding pools. This file is used
|
|
|
|
# by the readiness probe when the controller is deployed in
|
|
|
|
# containerized mode. This way the controller pod will not be ready
|
|
|
|
# until all the pre-created ports have been loaded
|
|
|
|
try:
|
|
|
|
with open('/tmp/pools_loaded', 'a'):
|
|
|
|
LOG.debug("Health check file created for readiness probe")
|
|
|
|
except IOError:
|
|
|
|
LOG.exception("I/O error creating the health check file.")
|
|
|
|
|
2018-08-20 17:49:52 +02:00
|
|
|
@lockutils.synchronized('return_to_pool_baremetal')
|
|
|
|
@lockutils.synchronized('return_to_pool_nested')
|
|
|
|
def sync_pools(self):
|
2019-08-21 12:48:59 +02:00
|
|
|
# NOTE(ltomasbo): Ensure readiness probe is not set to true until the
|
|
|
|
# pools sync is completed in case of controller restart
|
|
|
|
try:
|
|
|
|
os.remove('/tmp/pools_loaded')
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
self._available_ports_pools = collections.defaultdict()
|
|
|
|
self._existing_vifs = collections.defaultdict()
|
|
|
|
self._recyclable_ports = collections.defaultdict()
|
|
|
|
self._last_update = collections.defaultdict()
|
2018-08-20 17:49:52 +02:00
|
|
|
|
2018-12-17 17:38:44 +01:00
|
|
|
def _get_trunks_info(self):
|
|
|
|
"""Returns information about trunks and their subports.
|
|
|
|
|
|
|
|
This method searches for parent ports and subports among the active
|
|
|
|
neutron ports.
|
|
|
|
To find the parent ports it filters the ones that have trunk_details,
|
|
|
|
i.e., the ones that are the parent port of a trunk.
|
|
|
|
To find the subports to recover, it filters out the ports that are
|
|
|
|
already in used by running kubernetes pods. It also filters out the
|
|
|
|
ports whose device_owner is not related to subports, i.e., the ports
|
|
|
|
that are not attached to trunks, such as active ports allocated to
|
|
|
|
running VMs.
|
|
|
|
At the same time it collects information about ports subnets to
|
|
|
|
minimize the number of interaction with Neutron API.
|
|
|
|
|
|
|
|
It returns three dictionaries with the needed information about the
|
|
|
|
parent ports, subports and subnets
|
|
|
|
|
|
|
|
:return: 3 dicts with the trunk details (Key: trunk_id; Value: dict
|
|
|
|
containing ip and subports), subport details (Key: port_id; Value:
|
|
|
|
port_object), and subnet details (Key: subnet_id; Value: subnet dict)
|
|
|
|
"""
|
|
|
|
# REVISIT(ltomasbo): there is no need to recover the subports
|
|
|
|
# belonging to trunk ports whose parent port is DOWN as that means no
|
|
|
|
# pods can be scheduled there. We may need to update this if we allow
|
|
|
|
# lively extending the kubernetes cluster with VMs that already have
|
|
|
|
# precreated subports. For instance by shutting down and up a
|
|
|
|
# kubernetes Worker VM with subports already attached, and the
|
|
|
|
# controller is restarted in between.
|
|
|
|
parent_ports = {}
|
|
|
|
subports = {}
|
|
|
|
subnets = {}
|
|
|
|
|
2019-08-05 12:21:50 +02:00
|
|
|
attrs = {'status': 'ACTIVE'}
|
|
|
|
tags = config.CONF.neutron_defaults.resource_tags
|
|
|
|
if tags:
|
|
|
|
attrs['tags'] = tags
|
2019-07-18 16:11:54 +02:00
|
|
|
all_active_ports = c_utils.get_ports_by_attrs(**attrs)
|
2018-12-17 17:38:44 +01:00
|
|
|
in_use_ports = self._get_in_use_ports()
|
|
|
|
|
|
|
|
for port in all_active_ports:
|
|
|
|
trunk_details = port.get('trunk_details')
|
|
|
|
# Parent port
|
|
|
|
if trunk_details:
|
|
|
|
parent_ports[trunk_details['trunk_id']] = {
|
|
|
|
'ip': port['fixed_ips'][0]['ip_address'],
|
|
|
|
'subports': trunk_details['sub_ports']}
|
|
|
|
else:
|
|
|
|
# Filter to only get subports that are not in use
|
|
|
|
if (port['id'] not in in_use_ports and
|
|
|
|
port['device_owner'] in ['trunk:subport',
|
|
|
|
kl_const.DEVICE_OWNER]):
|
|
|
|
subports[port['id']] = port
|
|
|
|
# NOTE(ltomasbo): _get_subnet can be costly as it
|
|
|
|
# needs to call neutron to get network and subnet
|
|
|
|
# information. This ensures it is only called once
|
|
|
|
# per subnet in use
|
|
|
|
subnet_id = port['fixed_ips'][0]['subnet_id']
|
|
|
|
if not subnets.get(subnet_id):
|
|
|
|
subnets[subnet_id] = {subnet_id:
|
|
|
|
utils.get_subnet(
|
|
|
|
subnet_id)}
|
|
|
|
return parent_ports, subports, subnets
|
|
|
|
|
2019-08-05 14:13:13 +02:00
|
|
|
def _cleanup_leftover_ports(self):
|
|
|
|
neutron = clients.get_neutron_client()
|
|
|
|
attrs = {'device_owner': kl_const.DEVICE_OWNER, 'status': 'DOWN'}
|
2019-07-18 16:11:54 +02:00
|
|
|
existing_ports = c_utils.get_ports_by_attrs(**attrs)
|
2019-08-05 14:13:13 +02:00
|
|
|
|
|
|
|
tags = config.CONF.neutron_defaults.resource_tags
|
|
|
|
if tags:
|
|
|
|
nets = neutron.list_networks(tags=tags)['networks']
|
|
|
|
nets_ids = [n['id'] for n in nets]
|
|
|
|
for port in existing_ports:
|
|
|
|
net_id = port['network_id']
|
|
|
|
if net_id in nets_ids:
|
|
|
|
if port.get('binding:host_id'):
|
|
|
|
for tag in tags:
|
|
|
|
if tag not in port.get('tags', []):
|
|
|
|
# delete the port if it has binding details, it
|
|
|
|
# belongs to the deployment subnet and it does
|
|
|
|
# not have the right tags
|
|
|
|
try:
|
|
|
|
neutron.delete_port(port['id'])
|
|
|
|
break
|
|
|
|
except n_exc.NeutronClientException:
|
|
|
|
LOG.debug("Problem deleting leftover port "
|
|
|
|
"%s. Skipping.", port['id'])
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
# delete port if they have no binding but belong to the
|
|
|
|
# deployment networks, regardless of their tagging
|
|
|
|
try:
|
|
|
|
neutron.delete_port(port['id'])
|
|
|
|
except n_exc.NeutronClientException:
|
|
|
|
LOG.debug("Problem deleting leftover port %s. "
|
|
|
|
"Skipping.", port['id'])
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
for port in existing_ports:
|
|
|
|
if not port.get('binding:host_id'):
|
|
|
|
neutron.delete_port(port['id'])
|
|
|
|
|
2017-02-22 12:51:35 +01:00
|
|
|
|
2017-06-09 17:16:34 +02:00
|
|
|
class NeutronVIFPool(BaseVIFPool):
|
2017-02-22 12:51:35 +01:00
|
|
|
"""Manages VIFs for Bare Metal Kubernetes Pods."""
|
|
|
|
|
2017-09-27 17:02:32 +00:00
|
|
|
def _get_host_addr(self, pod):
|
|
|
|
return pod['spec']['nodeName']
|
|
|
|
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
def _get_port_from_pool(self, pool_key, pod, subnets, security_groups):
|
2017-02-22 11:10:05 +01:00
|
|
|
try:
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
pool_ports = self._available_ports_pools[pool_key]
|
|
|
|
except (KeyError, AttributeError):
|
2017-02-22 11:10:05 +01:00
|
|
|
raise exceptions.ResourceNotReady(pod)
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
try:
|
|
|
|
port_id = pool_ports[security_groups].pop()
|
|
|
|
except (KeyError, IndexError):
|
|
|
|
# Get another port from the pool and update the SG to the
|
|
|
|
# appropriate one. It uses a port from the group that was updated
|
|
|
|
# longer ago
|
|
|
|
pool_updates = self._last_update.get(pool_key, {})
|
|
|
|
if not pool_updates:
|
|
|
|
# No pools update info. Selecting a random one
|
2020-01-16 11:57:47 +01:00
|
|
|
for sg_group, ports in list(pool_ports.items()):
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
if len(ports) > 0:
|
|
|
|
port_id = pool_ports[sg_group].pop()
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise exceptions.ResourceNotReady(pod)
|
|
|
|
else:
|
|
|
|
min_date = -1
|
2020-01-16 11:57:47 +01:00
|
|
|
for sg_group, date in list(pool_updates.items()):
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
if pool_ports.get(sg_group):
|
|
|
|
if min_date == -1 or date < min_date:
|
|
|
|
min_date = date
|
|
|
|
min_sg_group = sg_group
|
|
|
|
if min_date == -1:
|
|
|
|
# pool is empty, no port to reuse
|
|
|
|
raise exceptions.ResourceNotReady(pod)
|
|
|
|
port_id = pool_ports[min_sg_group].pop()
|
|
|
|
neutron = clients.get_neutron_client()
|
|
|
|
neutron.update_port(
|
|
|
|
port_id,
|
|
|
|
{
|
|
|
|
"port": {
|
|
|
|
'security_groups': list(security_groups)
|
|
|
|
}
|
|
|
|
})
|
2017-09-18 11:22:22 +00:00
|
|
|
if config.CONF.kubernetes.port_debug:
|
|
|
|
neutron = clients.get_neutron_client()
|
|
|
|
neutron.update_port(
|
|
|
|
port_id,
|
|
|
|
{
|
|
|
|
"port": {
|
2018-08-09 02:38:05 -04:00
|
|
|
'name': c_utils.get_port_name(pod),
|
2017-09-18 11:22:22 +00:00
|
|
|
'device_id': pod['metadata']['uid']
|
|
|
|
}
|
|
|
|
})
|
2017-02-22 12:00:48 +01:00
|
|
|
# check if the pool needs to be populated
|
|
|
|
if (self._get_pool_size(pool_key) <
|
|
|
|
oslo_cfg.CONF.vif_pool.ports_pool_min):
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
eventlet.spawn(self._populate_pool, pool_key, pod, subnets,
|
|
|
|
security_groups)
|
2017-02-22 11:10:05 +01:00
|
|
|
return self._existing_vifs[port_id]
|
|
|
|
|
|
|
|
def _return_ports_to_pool(self):
|
|
|
|
"""Recycle ports to be reused by future pods.
|
|
|
|
|
|
|
|
For each port in the recyclable_ports dict it reaplies
|
2017-09-20 11:40:16 +00:00
|
|
|
security group if they have been changed and it changes the port
|
|
|
|
name to available_port if the port_debug option is enabled.
|
|
|
|
Then the port_id is included in the dict with the available_ports.
|
2017-02-22 11:10:05 +01:00
|
|
|
|
2019-06-06 12:52:59 +03:00
|
|
|
If a maximum number of ports per pool is set, the port will be
|
|
|
|
deleted if the maximum has been already reached.
|
2017-02-22 11:10:05 +01:00
|
|
|
"""
|
2017-02-22 12:00:48 +01:00
|
|
|
while True:
|
2018-04-25 09:31:10 +00:00
|
|
|
eventlet.sleep(oslo_cfg.CONF.vif_pool.ports_pool_update_frequency)
|
2018-08-20 17:49:52 +02:00
|
|
|
self._trigger_return_to_pool()
|
2018-04-25 09:31:10 +00:00
|
|
|
|
2018-08-10 12:14:37 +02:00
|
|
|
@lockutils.synchronized('return_to_pool_baremetal')
|
2018-04-25 09:31:10 +00:00
|
|
|
def _trigger_return_to_pool(self):
|
2018-10-29 16:30:17 +01:00
|
|
|
if not hasattr(self, '_recyclable_ports'):
|
|
|
|
LOG.info("Kuryr-controller not yet ready to return ports to "
|
|
|
|
"pools.")
|
|
|
|
return
|
2018-04-25 09:31:10 +00:00
|
|
|
neutron = clients.get_neutron_client()
|
|
|
|
sg_current = {}
|
|
|
|
if not config.CONF.kubernetes.port_debug:
|
2019-08-05 12:21:50 +02:00
|
|
|
attrs = {'device_owner': kl_const.DEVICE_OWNER}
|
|
|
|
tags = config.CONF.neutron_defaults.resource_tags
|
|
|
|
if tags:
|
|
|
|
attrs['tags'] = tags
|
2019-07-18 16:11:54 +02:00
|
|
|
kuryr_ports = c_utils.get_ports_by_attrs(**attrs)
|
2018-04-25 09:31:10 +00:00
|
|
|
for port in kuryr_ports:
|
2018-12-19 17:30:21 +05:30
|
|
|
if port['id'] in self._recyclable_ports:
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
sg_current[port['id']] = tuple(sorted(
|
|
|
|
port['security_groups']))
|
2018-04-25 09:31:10 +00:00
|
|
|
|
2020-01-09 11:52:47 +01:00
|
|
|
for port_id, pool_key in list(self._recyclable_ports.items()):
|
2018-04-25 09:31:10 +00:00
|
|
|
if (not oslo_cfg.CONF.vif_pool.ports_pool_max or
|
|
|
|
self._get_pool_size(pool_key) <
|
|
|
|
oslo_cfg.CONF.vif_pool.ports_pool_max):
|
|
|
|
port_name = (constants.KURYR_PORT_NAME
|
|
|
|
if config.CONF.kubernetes.port_debug
|
|
|
|
else '')
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
if config.CONF.kubernetes.port_debug:
|
2017-02-22 12:00:48 +01:00
|
|
|
try:
|
2018-04-25 09:31:10 +00:00
|
|
|
neutron.update_port(
|
|
|
|
port_id,
|
|
|
|
{
|
|
|
|
"port": {
|
|
|
|
'name': port_name,
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
'device_id': ''
|
2018-04-25 09:31:10 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
except n_exc.NeutronClientException:
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
LOG.warning("Error changing name for port %s to be "
|
2018-04-25 09:31:10 +00:00
|
|
|
"reused, put back on the cleanable "
|
|
|
|
"pool.", port_id)
|
|
|
|
continue
|
|
|
|
self._available_ports_pools.setdefault(
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
pool_key, {}).setdefault(
|
|
|
|
sg_current.get(port_id), []).append(port_id)
|
2018-04-25 09:31:10 +00:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
del self._existing_vifs[port_id]
|
|
|
|
neutron.delete_port(port_id)
|
|
|
|
except n_exc.PortNotFoundClient:
|
|
|
|
LOG.debug('Unable to release port %s as it no longer '
|
|
|
|
'exists.', port_id)
|
|
|
|
except KeyError:
|
|
|
|
LOG.debug('Port %s is not in the ports list.', port_id)
|
|
|
|
try:
|
2017-02-22 12:00:48 +01:00
|
|
|
del self._recyclable_ports[port_id]
|
2018-04-25 09:31:10 +00:00
|
|
|
except KeyError:
|
|
|
|
LOG.debug('Port already recycled: %s', port_id)
|
2017-02-22 12:44:46 +01:00
|
|
|
|
2019-11-26 14:09:32 +01:00
|
|
|
def sync_pools(self):
|
|
|
|
super(NeutronVIFPool, self).sync_pools()
|
|
|
|
# NOTE(ltomasbo): Ensure previously created ports are recovered into
|
|
|
|
# their respective pools
|
|
|
|
self._cleanup_leftover_ports()
|
|
|
|
self._recover_precreated_ports()
|
|
|
|
self._recovered_pools = True
|
|
|
|
|
2017-06-15 13:07:41 +00:00
|
|
|
def _recover_precreated_ports(self):
|
2019-08-05 12:21:50 +02:00
|
|
|
attrs = {'device_owner': kl_const.DEVICE_OWNER}
|
|
|
|
tags = config.CONF.neutron_defaults.resource_tags
|
|
|
|
if tags:
|
|
|
|
attrs['tags'] = tags
|
|
|
|
|
2017-09-18 11:22:22 +00:00
|
|
|
if config.CONF.kubernetes.port_debug:
|
2019-08-05 12:21:50 +02:00
|
|
|
attrs['name'] = constants.KURYR_PORT_NAME
|
2019-07-18 16:11:54 +02:00
|
|
|
available_ports = c_utils.get_ports_by_attrs(**attrs)
|
2017-09-18 11:22:22 +00:00
|
|
|
else:
|
2019-07-18 16:11:54 +02:00
|
|
|
kuryr_ports = c_utils.get_ports_by_attrs(**attrs)
|
2017-09-18 11:22:22 +00:00
|
|
|
in_use_ports = self._get_in_use_ports()
|
|
|
|
available_ports = [port for port in kuryr_ports
|
|
|
|
if port['id'] not in in_use_ports]
|
2017-09-27 17:02:32 +00:00
|
|
|
|
2018-12-17 17:38:44 +01:00
|
|
|
_, available_subports, _ = self._get_trunks_info()
|
2017-06-15 13:07:41 +00:00
|
|
|
for port in available_ports:
|
2018-12-17 17:38:44 +01:00
|
|
|
# NOTE(ltomasbo): ensure subports are not considered for
|
|
|
|
# recovering in the case of multi pools
|
|
|
|
if available_subports.get(port['id']):
|
|
|
|
continue
|
|
|
|
vif_plugin = self._drv_vif._get_vif_plugin(port)
|
|
|
|
port_host = port['binding:host_id']
|
|
|
|
if not vif_plugin or not port_host:
|
|
|
|
# NOTE(ltomasbo): kuryr-controller is running without the
|
|
|
|
# rights to get the needed information to recover the ports.
|
|
|
|
# Thus, removing the port instead
|
|
|
|
neutron = clients.get_neutron_client()
|
|
|
|
neutron.delete_port(port['id'])
|
|
|
|
continue
|
2017-09-27 17:02:32 +00:00
|
|
|
subnet_id = port['fixed_ips'][0]['subnet_id']
|
|
|
|
subnet = {
|
2018-08-09 17:24:17 +08:00
|
|
|
subnet_id: utils.get_subnet(subnet_id)}
|
2017-09-27 17:02:32 +00:00
|
|
|
vif = ovu.neutron_to_osvif_vif(vif_plugin, port, subnet)
|
2018-02-28 17:22:44 +00:00
|
|
|
net_obj = subnet[subnet_id]
|
2018-12-17 17:38:44 +01:00
|
|
|
pool_key = self._get_pool_key(port_host,
|
2018-02-28 17:22:44 +00:00
|
|
|
port['project_id'],
|
|
|
|
net_obj.id, None)
|
2017-09-27 17:02:32 +00:00
|
|
|
|
|
|
|
self._existing_vifs[port['id']] = vif
|
|
|
|
self._available_ports_pools.setdefault(
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
pool_key, {}).setdefault(
|
|
|
|
tuple(sorted(port['security_groups'])), []).append(
|
|
|
|
port['id'])
|
2017-09-27 17:02:32 +00:00
|
|
|
|
|
|
|
LOG.info("PORTS POOL: pools updated with pre-created ports")
|
2017-11-08 10:40:32 +01:00
|
|
|
self._create_healthcheck_file()
|
2017-06-15 13:07:41 +00:00
|
|
|
|
2018-04-25 09:31:10 +00:00
|
|
|
def delete_network_pools(self, net_id):
|
2019-11-26 14:09:32 +01:00
|
|
|
if not self._recovered_pools:
|
2018-11-21 16:04:19 +08:00
|
|
|
LOG.info("Kuryr-controller not yet ready to delete network "
|
2018-10-29 16:30:17 +01:00
|
|
|
"pools.")
|
|
|
|
raise exceptions.ResourceNotReady(net_id)
|
2018-04-25 09:31:10 +00:00
|
|
|
neutron = clients.get_neutron_client()
|
2018-08-10 12:14:37 +02:00
|
|
|
|
|
|
|
# NOTE(ltomasbo): Note the pods should already be deleted, but their
|
|
|
|
# associated ports may not have been recycled yet, therefore not being
|
|
|
|
# on the available_ports_pools dict. The next call forces it to be on
|
|
|
|
# that dict before cleaning it up
|
2018-04-25 09:31:10 +00:00
|
|
|
self._trigger_return_to_pool()
|
2020-01-09 11:52:47 +01:00
|
|
|
for pool_key, ports in list(self._available_ports_pools.items()):
|
2018-04-25 09:31:10 +00:00
|
|
|
if self._get_pool_key_net(pool_key) != net_id:
|
|
|
|
continue
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
ports_id = []
|
|
|
|
for sg_ports in ports.values():
|
|
|
|
ports_id.extend(sg_ports)
|
2018-04-25 09:31:10 +00:00
|
|
|
for port_id in ports_id:
|
|
|
|
try:
|
|
|
|
del self._existing_vifs[port_id]
|
|
|
|
except KeyError:
|
|
|
|
LOG.debug('Port %s is not in the ports list.', port_id)
|
|
|
|
try:
|
|
|
|
neutron.delete_port(port_id)
|
|
|
|
except n_exc.PortNotFoundClient:
|
2018-08-10 12:14:37 +02:00
|
|
|
LOG.debug('Unable to release port %s as it no longer '
|
2018-04-25 09:31:10 +00:00
|
|
|
'exists.', port_id)
|
2019-06-18 17:14:20 +02:00
|
|
|
self._available_ports_pools[pool_key] = {}
|
2018-04-25 09:31:10 +00:00
|
|
|
|
2017-02-22 12:44:46 +01:00
|
|
|
|
|
|
|
class NestedVIFPool(BaseVIFPool):
|
|
|
|
"""Manages VIFs for nested Kubernetes Pods.
|
|
|
|
|
|
|
|
In order to handle the pools of ports for nested Pods, an extra dict is
|
|
|
|
used:
|
|
|
|
_known_trunk_ids is a dictionary that keeps the trunk port ids associated
|
|
|
|
to each pool_key to skip calls to neutron to get the trunk information.
|
|
|
|
"""
|
2017-02-22 12:51:35 +01:00
|
|
|
_known_trunk_ids = collections.defaultdict(str)
|
2017-02-22 12:44:46 +01:00
|
|
|
|
2017-08-29 08:43:34 +02:00
|
|
|
def __init__(self):
|
|
|
|
super(NestedVIFPool, self).__init__()
|
|
|
|
# Start the pool manager so that pools can be populated/freed on
|
|
|
|
# demand
|
|
|
|
if config.CONF.kubernetes.enable_manager:
|
|
|
|
self._pool_manager = pool.PoolManager()
|
|
|
|
|
|
|
|
def set_vif_driver(self, driver):
|
|
|
|
self._drv_vif = driver
|
|
|
|
|
2019-10-15 12:12:04 +02:00
|
|
|
def _get_parent_port_id(self, vif):
|
|
|
|
neutron = clients.get_neutron_client()
|
|
|
|
args = {}
|
|
|
|
if config.CONF.neutron_defaults.resource_tags:
|
|
|
|
args['tags'] = config.CONF.neutron_defaults.resource_tags
|
|
|
|
trunks = neutron.list_trunks(**args)
|
|
|
|
for trunk in trunks['trunks']:
|
|
|
|
for sp in trunk['sub_ports']:
|
|
|
|
if sp['port_id'] == vif.id:
|
|
|
|
return trunk['port_id']
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
def release_vif(self, pod, vif, project_id, security_groups):
|
|
|
|
try:
|
|
|
|
host_addr = self._get_host_addr(pod)
|
|
|
|
except KeyError:
|
|
|
|
name = pod['metadata']['name']
|
|
|
|
LOG.warning("Pod %s does not have status.hostIP field set when "
|
|
|
|
"getting deleted. This is unusual. Trying to "
|
|
|
|
"determine the IP by calling Neutron.",
|
|
|
|
name)
|
|
|
|
|
|
|
|
parent_id = self._get_parent_port_id(vif)
|
|
|
|
if not parent_id:
|
|
|
|
LOG.warning("Port %s not found, ignoring its release request.",
|
|
|
|
vif.id)
|
|
|
|
return
|
|
|
|
|
|
|
|
host_addr = self._get_parent_port_ip(parent_id)
|
|
|
|
LOG.debug("Determined hostIP for pod %s is %s", name, host_addr)
|
|
|
|
|
|
|
|
super(NestedVIFPool, self).release_vif(
|
|
|
|
pod, vif, project_id, security_groups, host_addr=host_addr)
|
|
|
|
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
def _get_port_from_pool(self, pool_key, pod, subnets, security_groups):
|
2017-02-22 12:44:46 +01:00
|
|
|
try:
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
pool_ports = self._available_ports_pools[pool_key]
|
|
|
|
except (KeyError, AttributeError):
|
2017-02-22 12:44:46 +01:00
|
|
|
raise exceptions.ResourceNotReady(pod)
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
try:
|
|
|
|
port_id = pool_ports[security_groups].pop()
|
|
|
|
except (KeyError, IndexError):
|
|
|
|
# Get another port from the pool and update the SG to the
|
|
|
|
# appropriate one. It uses a port from the group that was updated
|
|
|
|
# longer ago
|
|
|
|
pool_updates = self._last_update.get(pool_key, {})
|
|
|
|
if not pool_updates:
|
|
|
|
# No pools update info. Selecting a random one
|
2020-01-16 11:57:47 +01:00
|
|
|
for sg_group, ports in list(pool_ports.items()):
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
if len(ports) > 0:
|
|
|
|
port_id = pool_ports[sg_group].pop()
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise exceptions.ResourceNotReady(pod)
|
|
|
|
else:
|
|
|
|
min_date = -1
|
2020-01-16 11:57:47 +01:00
|
|
|
for sg_group, date in list(pool_updates.items()):
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
if pool_ports.get(sg_group):
|
|
|
|
if min_date == -1 or date < min_date:
|
|
|
|
min_date = date
|
|
|
|
min_sg_group = sg_group
|
|
|
|
if min_date == -1:
|
|
|
|
# pool is empty, no port to reuse
|
|
|
|
raise exceptions.ResourceNotReady(pod)
|
|
|
|
port_id = pool_ports[min_sg_group].pop()
|
|
|
|
neutron = clients.get_neutron_client()
|
|
|
|
neutron.update_port(
|
|
|
|
port_id,
|
|
|
|
{
|
|
|
|
"port": {
|
|
|
|
'security_groups': list(security_groups)
|
|
|
|
}
|
|
|
|
})
|
2017-09-18 11:22:22 +00:00
|
|
|
if config.CONF.kubernetes.port_debug:
|
|
|
|
neutron = clients.get_neutron_client()
|
|
|
|
neutron.update_port(
|
|
|
|
port_id,
|
|
|
|
{
|
|
|
|
"port": {
|
2018-08-09 02:38:05 -04:00
|
|
|
'name': c_utils.get_port_name(pod),
|
2017-09-18 11:22:22 +00:00
|
|
|
}
|
|
|
|
})
|
2017-02-22 12:51:35 +01:00
|
|
|
# check if the pool needs to be populated
|
|
|
|
if (self._get_pool_size(pool_key) <
|
|
|
|
oslo_cfg.CONF.vif_pool.ports_pool_min):
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
eventlet.spawn(self._populate_pool, pool_key, pod, subnets,
|
|
|
|
security_groups)
|
2017-02-22 12:44:46 +01:00
|
|
|
return self._existing_vifs[port_id]
|
|
|
|
|
|
|
|
def _return_ports_to_pool(self):
|
|
|
|
"""Recycle ports to be reused by future pods.
|
|
|
|
|
|
|
|
For each port in the recyclable_ports dict it reaplies
|
2017-09-20 11:40:16 +00:00
|
|
|
security group if they have been changed and it changes the port
|
|
|
|
name to available_port if the port_debug option is enabled.
|
|
|
|
Then the port_id is included in the dict with the available_ports.
|
2017-02-22 12:44:46 +01:00
|
|
|
|
2019-06-06 12:52:59 +03:00
|
|
|
If a maximum number of ports per pool is set, the port will be
|
|
|
|
deleted if the maximum has been already reached.
|
2017-02-22 12:44:46 +01:00
|
|
|
"""
|
2017-02-22 12:51:35 +01:00
|
|
|
while True:
|
2018-04-25 09:31:10 +00:00
|
|
|
eventlet.sleep(oslo_cfg.CONF.vif_pool.ports_pool_update_frequency)
|
2018-08-20 17:49:52 +02:00
|
|
|
self._trigger_return_to_pool()
|
2018-04-25 09:31:10 +00:00
|
|
|
|
2018-08-10 12:14:37 +02:00
|
|
|
@lockutils.synchronized('return_to_pool_nested')
|
2018-04-25 09:31:10 +00:00
|
|
|
def _trigger_return_to_pool(self):
|
2018-10-29 16:30:17 +01:00
|
|
|
if not hasattr(self, '_recyclable_ports'):
|
|
|
|
LOG.info("Kuryr-controller not yet ready to return ports to "
|
|
|
|
"pools.")
|
|
|
|
return
|
2018-04-25 09:31:10 +00:00
|
|
|
neutron = clients.get_neutron_client()
|
|
|
|
sg_current = {}
|
|
|
|
if not config.CONF.kubernetes.port_debug:
|
2019-08-05 12:21:50 +02:00
|
|
|
attrs = {'device_owner': ['trunk:subport', kl_const.DEVICE_OWNER]}
|
|
|
|
tags = config.CONF.neutron_defaults.resource_tags
|
|
|
|
if tags:
|
|
|
|
attrs['tags'] = tags
|
2019-07-18 16:11:54 +02:00
|
|
|
kuryr_subports = c_utils.get_ports_by_attrs(**attrs)
|
2018-04-25 09:31:10 +00:00
|
|
|
for subport in kuryr_subports:
|
2018-12-19 17:30:21 +05:30
|
|
|
if subport['id'] in self._recyclable_ports:
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
sg_current[subport['id']] = tuple(sorted(
|
|
|
|
subport['security_groups']))
|
2018-04-25 09:31:10 +00:00
|
|
|
|
2020-01-09 11:52:47 +01:00
|
|
|
for port_id, pool_key in list(self._recyclable_ports.items()):
|
2018-04-25 09:31:10 +00:00
|
|
|
if (not oslo_cfg.CONF.vif_pool.ports_pool_max or
|
|
|
|
self._get_pool_size(pool_key) <
|
|
|
|
oslo_cfg.CONF.vif_pool.ports_pool_max):
|
|
|
|
port_name = (constants.KURYR_PORT_NAME
|
|
|
|
if config.CONF.kubernetes.port_debug
|
|
|
|
else '')
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
if config.CONF.kubernetes.port_debug:
|
2017-02-22 12:51:35 +01:00
|
|
|
try:
|
2018-04-25 09:31:10 +00:00
|
|
|
neutron.update_port(
|
|
|
|
port_id,
|
|
|
|
{
|
|
|
|
"port": {
|
|
|
|
'name': port_name,
|
|
|
|
}
|
|
|
|
})
|
2017-02-22 12:51:35 +01:00
|
|
|
except n_exc.NeutronClientException:
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
LOG.warning("Error changing name for port %s to be "
|
2018-04-25 09:31:10 +00:00
|
|
|
"reused, put back on the cleanable "
|
|
|
|
"pool.", port_id)
|
2017-02-22 12:51:35 +01:00
|
|
|
continue
|
2018-04-25 09:31:10 +00:00
|
|
|
self._available_ports_pools.setdefault(
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
pool_key, {}).setdefault(
|
|
|
|
sg_current.get(port_id), []).append(port_id)
|
2018-04-25 09:31:10 +00:00
|
|
|
else:
|
|
|
|
trunk_id = self._get_trunk_id(neutron, pool_key)
|
|
|
|
try:
|
|
|
|
self._drv_vif._remove_subport(neutron, trunk_id,
|
|
|
|
port_id)
|
|
|
|
self._drv_vif._release_vlan_id(
|
|
|
|
self._existing_vifs[port_id].vlan_id)
|
|
|
|
del self._existing_vifs[port_id]
|
|
|
|
neutron.delete_port(port_id)
|
|
|
|
except n_exc.PortNotFoundClient:
|
|
|
|
LOG.debug('Unable to release port %s as it no longer '
|
|
|
|
'exists.', port_id)
|
|
|
|
except KeyError:
|
|
|
|
LOG.debug('Port %s is not in the ports list.', port_id)
|
|
|
|
except n_exc.NeutronClientException:
|
|
|
|
LOG.warning('Error removing the subport %s', port_id)
|
|
|
|
continue
|
|
|
|
try:
|
2017-02-22 12:51:35 +01:00
|
|
|
del self._recyclable_ports[port_id]
|
2018-04-25 09:31:10 +00:00
|
|
|
except KeyError:
|
|
|
|
LOG.debug('Port already recycled: %s', port_id)
|
|
|
|
|
|
|
|
def _get_trunk_id(self, neutron, pool_key):
|
|
|
|
trunk_id = self._known_trunk_ids.get(pool_key, None)
|
|
|
|
if not trunk_id:
|
|
|
|
p_port = self._drv_vif._get_parent_port_by_host_ip(
|
|
|
|
neutron, pool_key[0])
|
|
|
|
trunk_id = self._drv_vif._get_trunk_id(p_port)
|
|
|
|
self._known_trunk_ids[pool_key] = trunk_id
|
|
|
|
return trunk_id
|
2017-06-15 13:07:41 +00:00
|
|
|
|
|
|
|
def _get_parent_port_ip(self, port_id):
|
|
|
|
neutron = clients.get_neutron_client()
|
|
|
|
parent_port = neutron.show_port(port_id).get('port')
|
|
|
|
return parent_port['fixed_ips'][0]['ip_address']
|
|
|
|
|
2019-11-26 14:09:32 +01:00
|
|
|
def sync_pools(self):
|
|
|
|
super(NestedVIFPool, self).sync_pools()
|
|
|
|
# NOTE(ltomasbo): Ensure previously created ports are recovered into
|
|
|
|
# their respective pools
|
|
|
|
self._recover_precreated_ports()
|
|
|
|
self._recovered_pools = True
|
|
|
|
eventlet.spawn(self._cleanup_leftover_ports)
|
|
|
|
|
2017-06-15 13:07:41 +00:00
|
|
|
def _recover_precreated_ports(self):
|
2017-08-28 18:24:04 +02:00
|
|
|
self._precreated_ports(action='recover')
|
2017-09-18 11:22:22 +00:00
|
|
|
LOG.info("PORTS POOL: pools updated with pre-created ports")
|
2017-11-08 10:40:32 +01:00
|
|
|
self._create_healthcheck_file()
|
2017-08-28 18:24:04 +02:00
|
|
|
|
|
|
|
def _remove_precreated_ports(self, trunk_ips=None):
|
|
|
|
self._precreated_ports(action='free', trunk_ips=trunk_ips)
|
|
|
|
|
|
|
|
def _precreated_ports(self, action, trunk_ips=None):
|
|
|
|
"""Removes or recovers pre-created subports at given pools
|
|
|
|
|
|
|
|
This function handles the pre-created ports based on the given action:
|
|
|
|
- If action is `free` it will remove all the subport from the given
|
|
|
|
trunk ports, or from all the trunk ports if no trunk_ips are passed.
|
|
|
|
- If action is `recover` it will discover the existing subports in the
|
|
|
|
given trunk ports (or in all of them if none are passed) and will add
|
|
|
|
them (and the needed information) to the respective pools.
|
|
|
|
"""
|
2017-06-15 13:07:41 +00:00
|
|
|
neutron = clients.get_neutron_client()
|
2017-08-28 11:34:49 +02:00
|
|
|
# Note(ltomasbo): ML2/OVS changes the device_owner to trunk:subport
|
|
|
|
# when a port is attached to a trunk. However, that is not the case
|
|
|
|
# for other ML2 drivers, such as ODL. So we also need to look for
|
|
|
|
# compute:kuryr
|
2017-06-15 13:07:41 +00:00
|
|
|
|
2017-10-06 15:09:00 +00:00
|
|
|
parent_ports, available_subports, subnets = self._get_trunks_info()
|
2017-06-15 13:07:41 +00:00
|
|
|
|
2017-10-06 15:09:00 +00:00
|
|
|
if not available_subports:
|
|
|
|
return
|
2017-06-15 13:07:41 +00:00
|
|
|
|
2019-11-08 12:15:21 +01:00
|
|
|
# FIXME(ltomasbo): Workaround for ports already detached from trunks
|
|
|
|
# whose status is ACTIVE
|
|
|
|
trunks_subports = [subport_id['port_id']
|
|
|
|
for p_port in parent_ports.values()
|
|
|
|
for subport_id in p_port['subports']]
|
|
|
|
port_ids_to_delete = [p_id for p_id in available_subports.keys()
|
|
|
|
if p_id not in trunks_subports]
|
|
|
|
for port_id in port_ids_to_delete:
|
|
|
|
LOG.debug("Deleting port with wrong status: %s", port_id)
|
|
|
|
try:
|
|
|
|
neutron.delete_port(port_id)
|
|
|
|
except n_exc.PortNotFoundClient:
|
|
|
|
LOG.debug('Port already deleted: %s', port_id)
|
|
|
|
except n_exc.NeutronClientException:
|
|
|
|
LOG.exception('Error removing the port %s', port_id)
|
|
|
|
|
2017-10-06 15:09:00 +00:00
|
|
|
for trunk_id, parent_port in parent_ports.items():
|
|
|
|
host_addr = parent_port.get('ip')
|
2017-08-28 18:24:04 +02:00
|
|
|
if trunk_ips and host_addr not in trunk_ips:
|
|
|
|
continue
|
|
|
|
|
2017-10-06 15:09:00 +00:00
|
|
|
for subport in parent_port.get('subports'):
|
|
|
|
kuryr_subport = available_subports.get(subport['port_id'])
|
2017-06-15 13:07:41 +00:00
|
|
|
if kuryr_subport:
|
2018-02-28 17:22:44 +00:00
|
|
|
subnet_id = kuryr_subport['fixed_ips'][0]['subnet_id']
|
|
|
|
subnet = subnets[subnet_id]
|
|
|
|
net_obj = subnet[subnet_id]
|
|
|
|
pool_key = self._get_pool_key(host_addr,
|
|
|
|
kuryr_subport['project_id'],
|
|
|
|
net_obj.id, None)
|
2017-06-15 13:07:41 +00:00
|
|
|
|
2017-08-28 18:24:04 +02:00
|
|
|
if action == 'recover':
|
|
|
|
vif = ovu.neutron_to_osvif_vif_nested_vlan(
|
|
|
|
kuryr_subport, subnet, subport['segmentation_id'])
|
|
|
|
|
2017-10-06 15:09:00 +00:00
|
|
|
self._existing_vifs[kuryr_subport['id']] = vif
|
2017-08-28 18:24:04 +02:00
|
|
|
self._available_ports_pools.setdefault(
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
pool_key, {}).setdefault(tuple(sorted(
|
|
|
|
kuryr_subport['security_groups'])),
|
|
|
|
[]).append(kuryr_subport['id'])
|
2017-10-06 15:09:00 +00:00
|
|
|
|
2017-08-28 18:24:04 +02:00
|
|
|
elif action == 'free':
|
|
|
|
try:
|
2017-10-06 15:09:00 +00:00
|
|
|
self._drv_vif._remove_subport(neutron, trunk_id,
|
|
|
|
kuryr_subport['id'])
|
|
|
|
neutron.delete_port(kuryr_subport['id'])
|
2017-08-28 18:24:04 +02:00
|
|
|
self._drv_vif._release_vlan_id(
|
|
|
|
subport['segmentation_id'])
|
2017-10-06 15:09:00 +00:00
|
|
|
del self._existing_vifs[kuryr_subport['id']]
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
self._available_ports_pools[pool_key][
|
|
|
|
tuple(sorted(kuryr_subport['security_groups']
|
|
|
|
))].remove(kuryr_subport['id'])
|
2017-08-28 18:24:04 +02:00
|
|
|
except n_exc.PortNotFoundClient:
|
|
|
|
LOG.debug('Unable to release port %s as it no '
|
2017-10-06 15:09:00 +00:00
|
|
|
'longer exists.', kuryr_subport['id'])
|
2017-08-28 18:24:04 +02:00
|
|
|
except KeyError:
|
|
|
|
LOG.debug('Port %s is not in the ports list.',
|
2017-10-06 15:09:00 +00:00
|
|
|
kuryr_subport['id'])
|
2017-08-28 18:24:04 +02:00
|
|
|
except n_exc.NeutronClientException:
|
|
|
|
LOG.warning('Error removing the subport %s',
|
2017-10-06 15:09:00 +00:00
|
|
|
kuryr_subport['id'])
|
2017-08-28 18:24:04 +02:00
|
|
|
except ValueError:
|
|
|
|
LOG.debug('Port %s is not in the available ports '
|
2017-10-06 15:09:00 +00:00
|
|
|
'pool.', kuryr_subport['id'])
|
2017-08-28 18:24:04 +02:00
|
|
|
|
2019-05-03 17:54:55 +02:00
|
|
|
@lockutils.synchronized('return_to_pool_nested')
|
2019-07-02 14:02:43 +02:00
|
|
|
def populate_pool(self, trunk_ip, project_id, subnets, security_groups):
|
2019-11-26 14:09:32 +01:00
|
|
|
if not self._recovered_pools:
|
2019-05-03 17:54:55 +02:00
|
|
|
LOG.info("Kuryr-controller not yet ready to populate pools.")
|
|
|
|
raise exceptions.ResourceNotReady(trunk_ip)
|
|
|
|
pool_key = self._get_pool_key(trunk_ip, project_id, None, subnets)
|
|
|
|
pools = self._available_ports_pools.get(pool_key)
|
|
|
|
if not pools:
|
2019-07-02 14:02:43 +02:00
|
|
|
# NOTE(ltomasbo): If the amount of nodes is large the repopulation
|
|
|
|
# actions may take too long. Using half of the batch to prevent
|
|
|
|
# the problem
|
|
|
|
num_ports = max(oslo_cfg.CONF.vif_pool.ports_pool_batch/2,
|
|
|
|
oslo_cfg.CONF.vif_pool.ports_pool_min)
|
2019-05-03 17:54:55 +02:00
|
|
|
self.force_populate_pool(trunk_ip, project_id, subnets,
|
|
|
|
security_groups, num_ports)
|
|
|
|
|
2017-08-28 18:24:04 +02:00
|
|
|
def force_populate_pool(self, trunk_ip, project_id, subnets,
|
2019-05-03 17:54:55 +02:00
|
|
|
security_groups, num_ports=None):
|
2017-08-28 18:24:04 +02:00
|
|
|
"""Create a given amount of subports at a given trunk port.
|
|
|
|
|
|
|
|
This function creates a given amount of subports and attaches them to
|
|
|
|
the specified trunk, adding them to the related subports pool
|
|
|
|
regardless of the amount of subports already available in the pool.
|
|
|
|
"""
|
2019-05-03 17:54:55 +02:00
|
|
|
if not num_ports:
|
|
|
|
num_ports = oslo_cfg.CONF.vif_pool.ports_pool_batch
|
2017-08-28 18:24:04 +02:00
|
|
|
vifs = self._drv_vif.request_vifs(
|
|
|
|
pod=[],
|
|
|
|
project_id=project_id,
|
|
|
|
subnets=subnets,
|
|
|
|
security_groups=security_groups,
|
|
|
|
num_ports=num_ports,
|
|
|
|
trunk_ip=trunk_ip)
|
|
|
|
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
pool_key = self._get_pool_key(trunk_ip, project_id, None, subnets)
|
2017-08-28 18:24:04 +02:00
|
|
|
for vif in vifs:
|
|
|
|
self._existing_vifs[vif.id] = vif
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
self._available_ports_pools.setdefault(pool_key, {}).setdefault(
|
|
|
|
tuple(sorted(security_groups)), []).append(vif.id)
|
2017-08-28 18:24:04 +02:00
|
|
|
|
|
|
|
def free_pool(self, trunk_ips=None):
|
|
|
|
"""Removes subports from the pool and deletes neutron port resource.
|
|
|
|
|
|
|
|
This function empties the pool of available subports and removes the
|
|
|
|
neutron port resources of the specified trunk port (or all of them if
|
|
|
|
no trunk is specified).
|
|
|
|
"""
|
|
|
|
self._remove_precreated_ports(trunk_ips)
|
2017-12-15 17:56:07 +01:00
|
|
|
|
2018-04-25 09:31:10 +00:00
|
|
|
def delete_network_pools(self, net_id):
|
2019-11-26 14:09:32 +01:00
|
|
|
if not self._recovered_pools:
|
2018-11-21 16:04:19 +08:00
|
|
|
LOG.info("Kuryr-controller not yet ready to delete network "
|
2018-10-29 16:30:17 +01:00
|
|
|
"pools.")
|
|
|
|
raise exceptions.ResourceNotReady(net_id)
|
2018-04-25 09:31:10 +00:00
|
|
|
neutron = clients.get_neutron_client()
|
2018-08-10 12:14:37 +02:00
|
|
|
# NOTE(ltomasbo): Note the pods should already be deleted, but their
|
|
|
|
# associated ports may not have been recycled yet, therefore not being
|
|
|
|
# on the available_ports_pools dict. The next call forces it to be on
|
|
|
|
# that dict before cleaning it up
|
2018-04-25 09:31:10 +00:00
|
|
|
self._trigger_return_to_pool()
|
2020-01-09 11:52:47 +01:00
|
|
|
for pool_key, ports in list(self._available_ports_pools.items()):
|
2018-04-25 09:31:10 +00:00
|
|
|
if self._get_pool_key_net(pool_key) != net_id:
|
|
|
|
continue
|
|
|
|
trunk_id = self._get_trunk_id(neutron, pool_key)
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
ports_id = [p_id for sg_ports in ports.values()
|
|
|
|
for p_id in sg_ports]
|
2018-04-25 09:31:10 +00:00
|
|
|
try:
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
self._drv_vif._remove_subports(neutron, trunk_id, ports_id)
|
2018-04-25 09:31:10 +00:00
|
|
|
except n_exc.NeutronClientException:
|
|
|
|
LOG.exception('Error removing subports from trunk: %s',
|
|
|
|
trunk_id)
|
|
|
|
continue
|
|
|
|
|
Pools support with Network Policies
This patch adapts the pools support to the use of Network Policies.
Unlike with the other drivers, when Network Policies are applied the
pods' ports changes their security groups while being used. That means
their original pool will not fit them anymore with the next two
consequences:
1.- Ports will have their original SG reapplied when pods are deleted,
with the consequent performance impact do to increasing the number of
calls to neutron
2.- Original pools may become useless, as different SGs are being used,
therefore wasting neutron ports
To accomodate for network policies, this patch removes the SG ids from
the pool key, merging all the pools with same network/project/host ids
but with different security groups into the same pool. This will not
change the behavior of the other drivers as there was a unique pool per
network/project/host ids already, i.e., the same SG ids were being used.
However, this will helps to avoid problem 1) as it is no longer
re-applying the SG, but simply putting the port back into its current
pool. And it will fix problem 2) as it will pick a port for an existing
pool that matches network/project/host ids. First it will search for one
with already matching SGs, and if not found, it will recycle one of the
others by reapplying the needed SGs (note it picks a port from one of
the pools that are less frequently used -- assumes they may belong to
a deleted NP that it is not needed anymore, thus removing the port
wastage problem)
Partially Implements: blueprint k8s-network-policies
Change-Id: I2c1e47fd5112c64b8e9984e5ac5d8572d91ac202
2019-02-04 12:32:12 +01:00
|
|
|
for port_id in ports_id:
|
2018-04-25 09:31:10 +00:00
|
|
|
try:
|
|
|
|
self._drv_vif._release_vlan_id(
|
|
|
|
self._existing_vifs[port_id].vlan_id)
|
|
|
|
del self._existing_vifs[port_id]
|
|
|
|
except KeyError:
|
|
|
|
LOG.debug('Port %s is not in the ports list.', port_id)
|
|
|
|
try:
|
|
|
|
neutron.delete_port(port_id)
|
|
|
|
except n_exc.PortNotFoundClient:
|
|
|
|
LOG.debug('Unable to delete subport %s as it no longer '
|
|
|
|
'exists.', port_id)
|
2019-06-18 17:14:20 +02:00
|
|
|
self._available_ports_pools[pool_key] = {}
|
2018-04-25 09:31:10 +00:00
|
|
|
|
2017-12-15 17:56:07 +01:00
|
|
|
|
|
|
|
class MultiVIFPool(base.VIFPoolDriver):
|
|
|
|
"""Manages pools with different VIF types.
|
|
|
|
|
|
|
|
It manages hybrid deployments containing both Bare Metal and Nested
|
|
|
|
Kubernetes Pods. To do that it creates a pool per node with a different
|
|
|
|
pool driver depending on the vif driver that the node is using.
|
|
|
|
|
|
|
|
It assumes a label pod_vif is added to each node to inform about the
|
|
|
|
driver set for that node. If no label is added, it assumes the default pod
|
|
|
|
vif: the one specified at kuryr.conf
|
|
|
|
"""
|
|
|
|
|
|
|
|
def set_vif_driver(self):
|
|
|
|
self._vif_drvs = {}
|
2018-08-27 17:43:27 +09:00
|
|
|
vif_pool_mapping = self._get_vif_pool_mapping()
|
|
|
|
|
|
|
|
if not vif_pool_mapping:
|
2017-12-15 17:56:07 +01:00
|
|
|
pod_vif = oslo_cfg.CONF.kubernetes.pod_vif_driver
|
|
|
|
drv_vif = base.PodVIFDriver.get_instance()
|
|
|
|
drv_pool = base.VIFPoolDriver.get_instance()
|
|
|
|
drv_pool.set_vif_driver(drv_vif)
|
|
|
|
self._vif_drvs[pod_vif] = drv_pool
|
|
|
|
return
|
2018-08-27 17:43:27 +09:00
|
|
|
for pod_driver, pool_driver in vif_pool_mapping.items():
|
2017-12-15 17:56:07 +01:00
|
|
|
if not utils.check_suitable_multi_pool_driver_opt(pool_driver,
|
|
|
|
pod_driver):
|
2019-07-10 10:01:55 +03:00
|
|
|
LOG.error("The pool(%s) and pod(%s) driver selected are not "
|
|
|
|
"compatible.", pool_driver, pod_driver)
|
2017-12-15 17:56:07 +01:00
|
|
|
raise exceptions.MultiPodDriverPoolConfigurationNotSupported()
|
2018-08-27 17:09:38 +09:00
|
|
|
drv_vif = base.PodVIFDriver.get_instance(
|
|
|
|
specific_driver=pod_driver)
|
2017-12-15 17:56:07 +01:00
|
|
|
drv_pool = base.VIFPoolDriver.get_instance(
|
2018-08-27 17:43:27 +09:00
|
|
|
specific_driver=pool_driver, scope='for:{}'.format(pod_driver))
|
2017-12-15 17:56:07 +01:00
|
|
|
drv_pool.set_vif_driver(drv_vif)
|
|
|
|
self._vif_drvs[pod_driver] = drv_pool
|
|
|
|
|
|
|
|
def request_vif(self, pod, project_id, subnets, security_groups):
|
|
|
|
pod_vif_type = self._get_pod_vif_type(pod)
|
|
|
|
return self._vif_drvs[pod_vif_type].request_vif(
|
|
|
|
pod, project_id, subnets, security_groups)
|
|
|
|
|
|
|
|
def release_vif(self, pod, vif, *argv):
|
2018-08-11 02:54:02 -04:00
|
|
|
vif_drv_alias = self._get_vif_drv_alias(vif)
|
|
|
|
self._vif_drvs[vif_drv_alias].release_vif(pod, vif, *argv)
|
2017-12-15 17:56:07 +01:00
|
|
|
|
|
|
|
def activate_vif(self, pod, vif):
|
2018-08-11 02:54:02 -04:00
|
|
|
vif_drv_alias = self._get_vif_drv_alias(vif)
|
|
|
|
self._vif_drvs[vif_drv_alias].activate_vif(pod, vif)
|
2017-12-15 17:56:07 +01:00
|
|
|
|
2018-11-07 18:46:07 +01:00
|
|
|
def update_vif_sgs(self, pod, sgs):
|
|
|
|
pod_vif_type = self._get_pod_vif_type(pod)
|
|
|
|
self._vif_drvs[pod_vif_type].update_vif_sgs(pod, sgs)
|
|
|
|
|
2019-10-04 13:11:56 +02:00
|
|
|
def remove_sg_from_pools(self, sg_id, net_id):
|
|
|
|
for vif_drv in self._vif_drvs.values():
|
|
|
|
if str(vif_drv) == 'NoopVIFPool':
|
|
|
|
continue
|
|
|
|
vif_drv.remove_sg_from_pools(sg_id, net_id)
|
|
|
|
|
2018-04-25 09:31:10 +00:00
|
|
|
def delete_network_pools(self, net_id):
|
|
|
|
for vif_drv in self._vif_drvs.values():
|
|
|
|
if str(vif_drv) == 'NoopVIFPool':
|
|
|
|
continue
|
|
|
|
vif_drv.delete_network_pools(net_id)
|
|
|
|
|
2018-08-20 17:49:52 +02:00
|
|
|
def sync_pools(self):
|
|
|
|
for vif_drv in self._vif_drvs.values():
|
|
|
|
vif_drv.sync_pools()
|
|
|
|
|
2017-12-15 17:56:07 +01:00
|
|
|
def _get_pod_vif_type(self, pod):
|
|
|
|
node_name = pod['spec']['nodeName']
|
|
|
|
return self._get_node_vif_driver(node_name)
|
|
|
|
|
|
|
|
@MEMOIZE
|
|
|
|
def _get_node_vif_driver(self, node_name):
|
|
|
|
kubernetes = clients.get_kubernetes_client()
|
|
|
|
node_info = kubernetes.get(
|
|
|
|
constants.K8S_API_BASE + '/nodes/' + node_name)
|
|
|
|
|
|
|
|
labels = node_info['metadata'].get('labels', None)
|
|
|
|
if labels:
|
|
|
|
pod_vif = labels.get('pod_vif',
|
|
|
|
oslo_cfg.CONF.kubernetes.pod_vif_driver)
|
|
|
|
return pod_vif
|
|
|
|
return oslo_cfg.CONF.kubernetes.pod_vif_driver
|
2018-08-11 02:54:02 -04:00
|
|
|
|
|
|
|
def _get_vif_drv_alias(self, vif):
|
|
|
|
vif_type_name = type(vif).__name__
|
|
|
|
return VIF_TYPE_TO_DRIVER_MAPPING[vif_type_name]
|
2018-08-27 17:43:27 +09:00
|
|
|
|
|
|
|
def _get_vif_pool_mapping(self):
|
|
|
|
vif_pool_mapping = oslo_cfg.CONF.vif_pool.vif_pool_mapping
|
|
|
|
|
|
|
|
if not vif_pool_mapping:
|
|
|
|
pools_vif_drivers = oslo_cfg.CONF.vif_pool.pools_vif_drivers
|
|
|
|
|
|
|
|
if pools_vif_drivers:
|
|
|
|
msg = ("Config option vif_pool.pools_vif_drivers is "
|
|
|
|
"deprecated in favour of vif_pool.vif_pool_mapping, "
|
|
|
|
"and will be removed in a future release")
|
|
|
|
versionutils.report_deprecated_feature(LOG, msg)
|
|
|
|
|
|
|
|
for pool_driver, pod_driver in pools_vif_drivers.items():
|
|
|
|
vif_pool_mapping[pod_driver] = pool_driver
|
|
|
|
|
|
|
|
return vif_pool_mapping
|
2019-05-03 17:54:55 +02:00
|
|
|
|
|
|
|
def populate_pool(self, node_ip, project_id, subnets, sg_id):
|
|
|
|
for vif_drv in self._vif_drvs.values():
|
|
|
|
if str(vif_drv) == 'NestedVIFPool':
|
|
|
|
vif_drv.populate_pool(node_ip, project_id, subnets, sg_id)
|