kuryr/kuryr/lib/utils.py
Antoni Segura Puimedon 854a8028b6
binding: Add driver based subsystem
This patch introduces a new hierarchy of drivers to perform the port
binding and unbinding in a similar fashion as how it is done with
Neutron plugins.

The initial three drivers are:
* veth: The one that we have been using up until now and that uses
  the usr/libexec/kuryr/* scripts to bind the host side
* ipvlan: L2 ipvlan motivated mostly container-in-vm use cases so that
  the instance interface will have linked devices that get addresses
  of other ports of the same subnet.
* macvlan: bridged mode ipvlan for OSes that do not support vlan.

Co-Authored-by: Louise Daly <louise.m.daly@intel.com>
Implements: blueprint driver-binding-ipvlan
Change-Id: I1d94ab324ab2a65a6d3e782e23ea6c59b110ff67
2016-10-03 12:28:01 +02:00

94 lines
3.0 KiB
Python

# 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.
import hashlib
import random
import socket
from keystoneauth1 import loading as ks_loading
from neutronclient.v2_0 import client
from oslo_config import cfg
from kuryr.lib import config as kuryr_config
DOCKER_NETNS_BASE = '/var/run/docker/netns'
PORT_POSTFIX = 'port'
def get_neutron_client(*args, **kwargs):
auth_plugin = ks_loading.load_auth_from_conf_options(
cfg.CONF, kuryr_config.neutron_group.name)
session = ks_loading.load_session_from_conf_options(cfg.CONF,
'neutron',
auth=auth_plugin)
return client.Client(session=session,
auth=auth_plugin,
endpoint_type=cfg.CONF.neutron.endpoint_type)
def get_hostname():
"""Returns the host name."""
return socket.gethostname()
def get_neutron_subnetpool_name(subnet_cidr):
"""Returns a Neutron subnetpool name.
:param subnet_cidr: The subnetpool allocation cidr
:returns: the Neutron subnetpool_name name formatted appropriately
"""
name_prefix = cfg.CONF.subnetpool_name_prefix
return '-'.join([name_prefix, subnet_cidr])
def get_dict_format_fixed_ips_from_kv_format(fixed_ips):
"""Returns fixed_ips in dict format.
:param fixed_ips: Format that neutron client expects for list_ports ex,
['subnet_id=5083bda8-1b7c-4625-97f3-1d4c33bfeea8',
'ip_address=192.168.1.2']
:returns: normal dict form,
[{'subnet_id': '5083bda8-1b7c-4625-97f3-1d4c33bfeea8',
'ip_address': '192.168.1.2'}]
"""
new_fixed_ips = []
for fixed_ip in fixed_ips:
if 'subnet_id' == fixed_ip.split('=')[0]:
subnet_id = fixed_ip.split('=')[1]
else:
ip = fixed_ip.split('=')[1]
new_fixed_ips.append({'subnet_id': subnet_id,
'ip_address': ip})
return new_fixed_ips
def getrandbits(bit_size=256):
return str(random.getrandbits(bit_size)).encode('utf-8')
def get_hash(bit_size=256):
return hashlib.sha256(getrandbits(bit_size=bit_size)).hexdigest()
def string_mappings(mapping_list):
"""Make a string out of the mapping list"""
details = ''
if mapping_list:
details = '"' + str(mapping_list) + '"'
return details
def get_random_string(length):
"""Get a random hex string of the specified length."""
return "{0:0{1}x}".format(random.getrandbits(length * 4), length)