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

View File

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

View File

@ -132,7 +132,7 @@ class AddHandler(CNIHandlerBase):
if self._cni.CNI_IFNAME in self._vifs: if self._cni.CNI_IFNAME in self._vifs:
self.callback_vif = self._vifs[self._cni.CNI_IFNAME] self.callback_vif = self._vifs[self._cni.CNI_IFNAME]
else: 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", LOG.debug("All VIFs are active, exiting. Will return %s",
self.callback_vif) self.callback_vif)
return True 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.binding import base as b_base
from kuryr_kubernetes.cni.plugins import base as base_cni 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 constants as k_const
from kuryr_kubernetes import exceptions 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 # Wait for timeout sec, 1 sec between tries, retry when even one
# vif is not active. # vif is not active.
@retrying.retry(stop_max_delay=timeout * 1000, wait_fixed=RETRY_DELAY, @retrying.retry(stop_max_delay=timeout * 1000, wait_fixed=RETRY_DELAY,
retry_on_result=lambda x: any( retry_on_result=utils.any_vif_inactive)
map(lambda y: not y.active, x.values())))
def wait_for_active(pod_name): def wait_for_active(pod_name):
return { return {
ifname: base.VersionedObject.obj_from_primitive(vif_obj) for 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) 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): class CNIConfig(dict):
def __init__(self, cfg): def __init__(self, cfg):
super(CNIConfig, self).__init__(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)) default_cidrs.append(utils.get_subnet_cidr(worker_subnet_id))
for cidr in default_cidrs: for cidr in default_cidrs:
default_rule = { default_rule = {
u'security_group_rule': { 'security_group_rule': {
u'ethertype': 'IPv4', 'ethertype': 'IPv4',
u'security_group_id': sg_id, 'security_group_id': sg_id,
u'direction': 'ingress', 'direction': 'ingress',
u'description': 'Kuryr-Kubernetes NetPolicy SG rule', 'description': 'Kuryr-Kubernetes NetPolicy SG rule',
u'remote_ip_prefix': cidr 'remote_ip_prefix': cidr
}} }}
driver_utils.create_security_group_rule(default_rule) 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): def _create_default_sg_rule(self, sg_id, direction, sg_rule_body_list):
default_rule = { default_rule = {
u'security_group_rule': { 'security_group_rule': {
u'ethertype': 'IPv4', 'ethertype': 'IPv4',
u'security_group_id': sg_id, 'security_group_id': sg_id,
u'direction': direction, 'direction': direction,
u'description': 'Kuryr-Kubernetes NetPolicy SG rule', 'description': 'Kuryr-Kubernetes NetPolicy SG rule',
}} }}
sg_rule_body_list.append(default_rule) sg_rule_body_list.append(default_rule)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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