Basic Python 3 compatibility fixes

This is result of running 2to3 without dict fix (as it seems
unnecessary for most of our cases).

In Python 3 {}.values() returns a view that is not indexable. This
commit uses list() on that to make AddHandler.should_callback compatible
with Python 3.

Change-Id: I354597f43d43630f9fb875dd8c9ab741c35af723
This commit is contained in:
Michał Dulko 2019-11-13 11:49:49 +01:00
parent 28b27c5de2
commit 7ed6e86744
12 changed files with 87 additions and 89 deletions

View File

@ -38,8 +38,8 @@ source_suffix = '.rst'
master_doc = 'index'
# General information about the project.
project = u'kuryr-kubernetes'
copyright = u'2013, OpenStack Foundation'
project = 'kuryr-kubernetes'
copyright = '2013, OpenStack Foundation'
# openstackdocstheme options
repository_name = 'openstack/kuryr-kubernetes'
@ -73,8 +73,8 @@ htmlhelp_basename = '%sdoc' % project
latex_documents = [
('index',
'%s.tex' % project,
u'%s Documentation' % project,
u'OpenStack Foundation', 'manual'),
'%s Documentation' % project,
'OpenStack Foundation', 'manual'),
]
# Example configuration for intersphinx: refer to the Python standard library.

View File

@ -16,8 +16,6 @@
CLI interface for kuryr status commands.
"""
from __future__ import print_function
import copy
import sys
import textwrap

View File

@ -132,7 +132,7 @@ class AddHandler(CNIHandlerBase):
if self._cni.CNI_IFNAME in self._vifs:
self.callback_vif = self._vifs[self._cni.CNI_IFNAME]
else:
self.callback_vif = self._vifs.values()[0]
self.callback_vif = next(iter(self._vifs.values()))
LOG.debug("All VIFs are active, exiting. Will return %s",
self.callback_vif)
return True

View File

@ -22,6 +22,7 @@ from oslo_log import log as logging
from kuryr_kubernetes.cni.binding import base as b_base
from kuryr_kubernetes.cni.plugins import base as base_cni
from kuryr_kubernetes.cni import utils
from kuryr_kubernetes import constants as k_const
from kuryr_kubernetes import exceptions
@ -69,8 +70,7 @@ class K8sCNIRegistryPlugin(base_cni.CNIPlugin):
# Wait for timeout sec, 1 sec between tries, retry when even one
# vif is not active.
@retrying.retry(stop_max_delay=timeout * 1000, wait_fixed=RETRY_DELAY,
retry_on_result=lambda x: any(
map(lambda y: not y.active, x.values())))
retry_on_result=utils.any_vif_inactive)
def wait_for_active(pod_name):
return {
ifname: base.VersionedObject.obj_from_primitive(vif_obj) for

View File

@ -27,6 +27,11 @@ def running_under_container_runtime(proc_one_cg_path=PROC_ONE_CGROUP_PATH):
CONTAINER_RUNTIME_CGROUP_IDS)
def any_vif_inactive(vifs):
"""Return True if there is at least one VIF that's not ACTIVE."""
return any(not vif.active for vif in vifs.values())
class CNIConfig(dict):
def __init__(self, cfg):
super(CNIConfig, self).__init__(cfg)

View File

@ -143,12 +143,12 @@ class NetworkPolicyDriver(base.NetworkPolicyDriver):
default_cidrs.append(utils.get_subnet_cidr(worker_subnet_id))
for cidr in default_cidrs:
default_rule = {
u'security_group_rule': {
u'ethertype': 'IPv4',
u'security_group_id': sg_id,
u'direction': 'ingress',
u'description': 'Kuryr-Kubernetes NetPolicy SG rule',
u'remote_ip_prefix': cidr
'security_group_rule': {
'ethertype': 'IPv4',
'security_group_id': sg_id,
'direction': 'ingress',
'description': 'Kuryr-Kubernetes NetPolicy SG rule',
'remote_ip_prefix': cidr
}}
driver_utils.create_security_group_rule(default_rule)
@ -436,11 +436,11 @@ class NetworkPolicyDriver(base.NetworkPolicyDriver):
def _create_default_sg_rule(self, sg_id, direction, sg_rule_body_list):
default_rule = {
u'security_group_rule': {
u'ethertype': 'IPv4',
u'security_group_id': sg_id,
u'direction': direction,
u'description': 'Kuryr-Kubernetes NetPolicy SG rule',
'security_group_rule': {
'ethertype': 'IPv4',
'security_group_id': sg_id,
'direction': direction,
'description': 'Kuryr-Kubernetes NetPolicy SG rule',
}}
sg_rule_body_list.append(default_rule)

View File

@ -263,19 +263,19 @@ def create_security_group_rule_body(
if not protocol:
protocol = 'TCP'
security_group_rule_body = {
u'security_group_rule': {
u'ethertype': ethertype,
u'security_group_id': security_group_id,
u'description': description,
u'direction': direction,
u'protocol': protocol.lower(),
u'port_range_min': port_range_min,
u'port_range_max': port_range_max,
'security_group_rule': {
'ethertype': ethertype,
'security_group_id': security_group_id,
'description': description,
'direction': direction,
'protocol': protocol.lower(),
'port_range_min': port_range_min,
'port_range_max': port_range_max,
}
}
if cidr:
security_group_rule_body[u'security_group_rule'][
u'remote_ip_prefix'] = cidr
security_group_rule_body['security_group_rule'][
'remote_ip_prefix'] = cidr
if namespace:
security_group_rule_body['namespace'] = namespace
if pods:

View File

@ -79,14 +79,14 @@ class TestNetworkPolicyDriver(test_base.TestCase):
self._e_rules = [{'security_group_rule': {'id': ''}}]
self._policy = {
'apiVersion': u'networking.k8s.io/v1',
'kind': u'NetworkPolicy',
'apiVersion': 'networking.k8s.io/v1',
'kind': 'NetworkPolicy',
'metadata': {
'name': self._policy_name,
'resourceVersion': u'2259309',
'resourceVersion': '2259309',
'generation': 1,
'creationTimestamp': u'2018-09-18T14:09:51Z',
'namespace': u'default',
'creationTimestamp': '2018-09-18T14:09:51Z',
'namespace': 'default',
'annotations': {},
'selfLink': self._policy_link,
'uid': self._policy_uid
@ -110,7 +110,7 @@ class TestNetworkPolicyDriver(test_base.TestCase):
self._crd = {
'metadata': {'name': mock.sentinel.name,
'namespace': u'default',
'namespace': 'default',
'selfLink': mock.sentinel.selfLink},
'spec': {
'egressSgRules': [

View File

@ -304,15 +304,15 @@ class TestNetworkPolicySecurityGroupsDriver(test_base.TestCase):
self._crd_sg_id = mock.sentinel.crd_sg_id
self._sg_rule_body = {
u'security_group_rule': {
u'direction': 'ingress',
u'protocol': u'tcp',
u'description': 'Kuryr-Kubernetes NetPolicy SG rule',
u'ethertype': 'IPv4',
u'port_range_max': 6379,
u'security_group_id': self._crd_sg_id,
u'port_range_min': 6379,
u'remote_ip_prefix': self._pod_ip}}
'security_group_rule': {
'direction': 'ingress',
'protocol': 'tcp',
'description': 'Kuryr-Kubernetes NetPolicy SG rule',
'ethertype': 'IPv4',
'port_range_max': 6379,
'security_group_id': self._crd_sg_id,
'port_range_min': 6379,
'remote_ip_prefix': self._pod_ip}}
self._new_rule_id = mock.sentinel.id
self._crd_with_rule = {

View File

@ -31,24 +31,22 @@ class TestPolicyHandler(test_base.TestCase):
self._pod_sg = mock.sentinel.pod_sg
self._policy = {
u'apiVersion': u'networking.k8s.io/v1',
u'kind': u'NetworkPolicy',
u'metadata': {
u'name': self._policy_name,
u'resourceVersion': u'2259309',
u'generation': 1,
u'creationTimestamp': u'2018-09-18T14:09:51Z',
u'namespace': u'default',
u'annotations': {},
u'selfLink': self._policy_link,
u'uid': self._policy_uid
'apiVersion': 'networking.k8s.io/v1',
'kind': 'NetworkPolicy',
'metadata': {
'name': self._policy_name,
'resourceVersion': '2259309',
'generation': 1,
'creationTimestamp': '2018-09-18T14:09:51Z',
'namespace': 'default',
'annotations': {},
'selfLink': self._policy_link,
'uid': self._policy_uid
},
u'spec': {
u'egress': [{u'ports':
[{u'port': 5978, u'protocol': u'TCP'}]}],
u'ingress': [{u'ports':
[{u'port': 6379, u'protocol': u'TCP'}]}],
u'policyTypes': [u'Ingress', u'Egress']
'spec': {
'egress': [{'ports': [{'port': 5978, 'protocol': 'TCP'}]}],
'ingress': [{'ports': [{'port': 6379, 'protocol': 'TCP'}]}],
'policyTypes': ['Ingress', 'Egress']
}
}

View File

@ -13,10 +13,7 @@
# limitations under the License.
import mock
try:
from BaseHTTPServer import BaseHTTPRequestHandler
except ImportError:
from http.server import BaseHTTPRequestHandler
from http.server import BaseHTTPRequestHandler
from oslo_serialization import jsonutils
@ -118,7 +115,7 @@ class TestRequestHandler(test_base.TestCase):
def test_do_POST_populate(self):
method = 'create'
path = "http://localhost/populatePool"
trunk_ips = [u"10.0.0.6"]
trunk_ips = ["10.0.0.6"]
num_ports = 3
body = jsonutils.dumps({"trunks": trunk_ips,
"num_ports": num_ports})
@ -135,7 +132,7 @@ class TestRequestHandler(test_base.TestCase):
def test_do_POST_populate_exception(self):
method = 'create'
path = "http://localhost/populatePool"
trunk_ips = [u"10.0.0.6"]
trunk_ips = ["10.0.0.6"]
num_ports = 3
body = jsonutils.dumps({"trunks": trunk_ips,
"num_ports": num_ports})
@ -169,7 +166,7 @@ class TestRequestHandler(test_base.TestCase):
def test_do_POST_free(self):
method = 'delete'
path = "http://localhost/freePool"
trunk_ips = [u"10.0.0.6"]
trunk_ips = ["10.0.0.6"]
body = jsonutils.dumps({"trunks": trunk_ips})
headers = {'Content-Type': 'application/json', 'Connection': 'close'}
headers['Content-Length'] = len(body)
@ -184,7 +181,7 @@ class TestRequestHandler(test_base.TestCase):
def test_do_POST_free_exception(self):
method = 'delete'
path = "http://localhost/freePool"
trunk_ips = [u"10.0.0.6"]
trunk_ips = ["10.0.0.6"]
body = jsonutils.dumps({"trunks": trunk_ips})
headers = {'Content-Type': 'application/json', 'Connection': 'close'}
headers['Content-Length'] = len(body)
@ -213,7 +210,7 @@ class TestRequestHandler(test_base.TestCase):
def test_do_POST_wrong_action(self):
method = 'fake'
path = "http://localhost/fakeMethod"
trunk_ips = [u"10.0.0.6"]
trunk_ips = ["10.0.0.6"]
body = jsonutils.dumps({"trunks": trunk_ips})
headers = {'Content-Type': 'application/json', 'Connection': 'close'}
headers['Content-Length'] = len(body)
@ -279,9 +276,9 @@ class TestRequestHandler(test_base.TestCase):
method = 'show'
method_resp = "251f748d-2a0d-4143-bce8-2e616f7a6a4a"
path = "http://localhost/showPool"
pool_key = [u"10.0.0.6", u"9d2b45c4efaa478481c30340b49fd4d2",
[u"00efc78c-f11c-414a-bfcd-a82e16dc07d1",
u"fd6b13dc-7230-4cbe-9237-36b4614bc6b5"]]
pool_key = ["10.0.0.6", "9d2b45c4efaa478481c30340b49fd4d2",
["00efc78c-f11c-414a-bfcd-a82e16dc07d1",
"fd6b13dc-7230-4cbe-9237-36b4614bc6b5"]]
body = jsonutils.dumps({"pool_key": pool_key})
headers = {'Content-Type': 'application/json', 'Connection': 'close'}
headers['Content-Length'] = len(body)
@ -297,9 +294,9 @@ class TestRequestHandler(test_base.TestCase):
method = 'show'
method_resp = "251f748d-2a0d-4143-bce8-2e616f7a6a4a"
path = "http://localhost/showPool"
pool_key = [u"10.0.0.6", u"9d2b45c4efaa478481c30340b49fd4d2",
[u"00efc78c-f11c-414a-bfcd-a82e16dc07d1",
u"fd6b13dc-7230-4cbe-9237-36b4614bc6b5"]]
pool_key = ["10.0.0.6", "9d2b45c4efaa478481c30340b49fd4d2",
["00efc78c-f11c-414a-bfcd-a82e16dc07d1",
"fd6b13dc-7230-4cbe-9237-36b4614bc6b5"]]
body = jsonutils.dumps({"pool_key": pool_key})
headers = {'Content-Type': 'application/json', 'Connection': 'close'}
headers['Content-Length'] = len(body)
@ -315,9 +312,9 @@ class TestRequestHandler(test_base.TestCase):
method = 'show'
method_resp = "Empty pool"
path = "http://localhost/showPool"
pool_key = [u"10.0.0.6", u"9d2b45c4efaa478481c30340b49fd4d2",
[u"00efc78c-f11c-414a-bfcd-a82e16dc07d1",
u"fd6b13dc-7230-4cbe-9237-36b4614bc6b5"]]
pool_key = ["10.0.0.6", "9d2b45c4efaa478481c30340b49fd4d2",
["00efc78c-f11c-414a-bfcd-a82e16dc07d1",
"fd6b13dc-7230-4cbe-9237-36b4614bc6b5"]]
body = jsonutils.dumps({"pool_key": pool_key})
headers = {'Content-Type': 'application/json', 'Connection': 'close'}
headers['Content-Length'] = len(body)
@ -333,7 +330,7 @@ class TestRequestHandler(test_base.TestCase):
method = 'show'
method_resp = ""
path = "http://localhost/showPool"
pool_key = [u"10.0.0.6", u"9d2b45c4efaa478481c30340b49fd4d2"]
pool_key = ["10.0.0.6", "9d2b45c4efaa478481c30340b49fd4d2"]
body = jsonutils.dumps({"pool_key": pool_key})
headers = {'Content-Type': 'application/json', 'Connection': 'close'}
headers['Content-Length'] = len(body)

View File

@ -49,8 +49,8 @@ source_suffix = '.rst'
master_doc = 'index'
# General information about the project.
copyright = u'2017, Kuryr-Kubernetes Developers'
author = u'Kuryr-Kubernetes Developers'
copyright = '2017, Kuryr-Kubernetes Developers'
author = 'Kuryr-Kubernetes Developers'
# Release notes are version independent.
# The short X.Y version.
@ -109,8 +109,8 @@ htmlhelp_basename = 'Kuryr-Kubernetesdoc'
# author, documentclass [howto, manual, or own class]).
latex_documents = [
('index', 'Kuryr-KubernetesReleaseNotes.tex',
u'Kuryr-Kubernetes Release Notes Documentation',
u'Kuryr-Kubernetes Developers', 'manual'),
'Kuryr-Kubernetes Release Notes Documentation',
'Kuryr-Kubernetes Developers', 'manual'),
]
@ -120,8 +120,8 @@ latex_documents = [
# (source start file, name, description, authors, manual section).
man_pages = [
('index', 'kuryr-kubernetesreleasenotes',
u'Kuryr-Kubernetes Release Notes Documentation',
[u'Kuryr-Kubernetes Developers'], 1)
'Kuryr-Kubernetes Release Notes Documentation',
['Kuryr-Kubernetes Developers'], 1)
]
@ -132,8 +132,8 @@ man_pages = [
# dir menu entry, description, category)
texinfo_documents = [
('index', 'Kuryr-KubernetesReleaseNotes',
u'Kuryr-Kubernetes Release Notes Documentation',
u'Kuryr-Kubernetes Developers',
'Kuryr-Kubernetes Release Notes Documentation',
'Kuryr-Kubernetes Developers',
'Kuryr-KubernetesReleaseNotes',
'One line description of project.',
'Miscellaneous'),