Skip to content

Commit 7a26302

Browse files
authored
Merge pull request #2570 from penguinolog/clean-up_kubernetes_base
Not generated code: clean up python 2.7 support artefacts
2 parents 7574c84 + ae86fe9 commit 7a26302

22 files changed

Lines changed: 71 additions & 131 deletions

examples/cronjob_crud.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/python3
2-
# -*- coding:utf-8 -*-
32

43
import json
54
import time

examples/duration-gep2257.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/python3
2-
# -*- coding:utf-8 -*-
32

43
"""
54
This example uses kubernetes.utils.duration to parse and display

examples/pod_portforward.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
import select
2020
import socket
2121
import time
22-
23-
import six.moves.urllib.request as urllib_request
22+
from urllib import request as urllib_request
2423

2524
from kubernetes import config
2625
from kubernetes.client import Configuration

kubernetes/base/config/exec_provider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from .config_exception import ConfigException
2020

2121

22-
class ExecProvider(object):
22+
class ExecProvider:
2323
"""
2424
Implementation of the proposal for out-of-tree client
2525
authentication providers as described here --

kubernetes/base/config/incluster_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def _join_host_port(host, port):
3434
return template % (host, port)
3535

3636

37-
class InClusterConfigLoader(object):
37+
class InClusterConfigLoader:
3838
def __init__(self,
3939
token_filename,
4040
cert_filename,

kubernetes/base/config/kube_config.py

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@
2222
import platform
2323
import subprocess
2424
import tempfile
25-
import time
2625
from collections import namedtuple
2726

2827
import oauthlib.oauth2
2928
import urllib3
3029
import yaml
3130
from requests_oauthlib import OAuth2Session
32-
from six import PY3
3331

3432
from kubernetes.client import ApiClient, Configuration
3533
from kubernetes.config.exec_provider import ExecProvider
@@ -85,7 +83,7 @@ def _is_expired(expiry):
8583
datetime.datetime.now(tz=UTC))
8684

8785

88-
class FileOrData(object):
86+
class FileOrData:
8987
"""Utility class to read content of obj[%data_key_name] or file's
9088
content of obj[%file_key_name] and represent it as file or data.
9189
Note that the data is preferred. The obj[%file_key_name] will be used iff
@@ -151,7 +149,7 @@ def _write_file(self, force_rewrite=False):
151149
self._data, self._temp_file_path, force_recreate=force_rewrite)
152150

153151

154-
class CommandTokenSource(object):
152+
class CommandTokenSource:
155153
def __init__(self, cmd, args, tokenKey, expiryKey):
156154
self._cmd = cmd
157155
self._args = args
@@ -191,7 +189,7 @@ def token(self):
191189
expiry=parse_rfc3339(data['credential']['token_expiry']))
192190

193191

194-
class KubeConfigLoader(object):
192+
class KubeConfigLoader:
195193

196194
def __init__(self, config_dict, active_context=None,
197195
get_google_credentials=None,
@@ -363,14 +361,9 @@ def _load_oid_token(self, provider):
363361
# https://tools.ietf.org/html/rfc7515#appendix-C
364362
return
365363

366-
if PY3:
367-
jwt_attributes = json.loads(
368-
base64.urlsafe_b64decode(parts[1] + padding).decode('utf-8')
369-
)
370-
else:
371-
jwt_attributes = json.loads(
372-
base64.b64decode(parts[1] + padding)
373-
)
364+
jwt_attributes = json.loads(
365+
base64.urlsafe_b64decode(parts[1] + padding).decode('utf-8')
366+
)
374367

375368
expire = jwt_attributes.get('exp')
376369

@@ -392,14 +385,9 @@ def _refresh_oidc(self, provider):
392385
if 'idp-certificate-authority-data' in provider['config']:
393386
ca_cert = tempfile.NamedTemporaryFile(delete=True)
394387

395-
if PY3:
396-
cert = base64.b64decode(
397-
provider['config']['idp-certificate-authority-data']
398-
).decode('utf-8')
399-
else:
400-
cert = base64.b64decode(
401-
provider['config']['idp-certificate-authority-data'] + "=="
402-
)
388+
cert = base64.b64decode(
389+
provider['config']['idp-certificate-authority-data']
390+
).decode('utf-8')
403391

404392
with open(ca_cert.name, 'w') as fh:
405393
fh.write(cert)
@@ -565,7 +553,7 @@ def current_context(self):
565553
return self._current_context.value
566554

567555

568-
class ConfigNode(object):
556+
class ConfigNode:
569557
"""Remembers each config key's path and construct a relevant exception
570558
message in case of missing keys. The assumption is all access keys are
571559
present in a well-formed kube-config."""

kubernetes/base/config/kube_config_test.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
from unittest import mock
2727
import yaml
28-
from six import PY3, next
2928

3029
from kubernetes.client import Configuration
3130

@@ -300,7 +299,7 @@ class TestConfigNode(BaseTestCase):
300299
]}
301300

302301
def setUp(self):
303-
super(TestConfigNode, self).setUp()
302+
super().setUp()
304303
self.node = ConfigNode("test_obj", self.test_obj)
305304

306305
def test_normal_map_array_operations(self):
@@ -1274,12 +1273,8 @@ def test_list_kube_config_contexts(self):
12741273
config_file=config_file)
12751274
self.assertDictEqual(self.TEST_KUBE_CONFIG['contexts'][0],
12761275
active_context)
1277-
if PY3:
1278-
self.assertCountEqual(self.TEST_KUBE_CONFIG['contexts'],
1279-
contexts)
1280-
else:
1281-
self.assertItemsEqual(self.TEST_KUBE_CONFIG['contexts'],
1282-
contexts)
1276+
self.assertCountEqual(self.TEST_KUBE_CONFIG['contexts'],
1277+
contexts)
12831278

12841279
def test_new_client_from_config(self):
12851280
config_file = self._create_temp_file(

kubernetes/base/dynamic/client.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import six
1615
import json
1716

1817
from kubernetes import watch
@@ -57,19 +56,15 @@ def inner(self, *args, **kwargs):
5756
raise api_exception(e)
5857
if serialize_response:
5958
try:
60-
if six.PY2:
61-
return serializer(self, json.loads(resp.data))
6259
return serializer(self, json.loads(resp.data.decode('utf8')))
6360
except ValueError:
64-
if six.PY2:
65-
return resp.data
6661
return resp.data.decode('utf8')
6762
return resp
6863

6964
return inner
7065

7166

72-
class DynamicClient(object):
67+
class DynamicClient:
7368
""" A kubernetes client that dynamically discovers and interacts with
7469
the kubernetes API
7570
"""
@@ -258,7 +253,7 @@ def request(self, method, path, body=None, **params):
258253
local_var_files = {}
259254

260255
# Checking Accept header.
261-
new_header_params = dict((key.lower(), value) for key, value in header_params.items())
256+
new_header_params = {key.lower(): value for key, value in header_params.items()}
262257
if not 'accept' in new_header_params:
263258
header_params['Accept'] = self.client.select_header_accept([
264259
'application/json',

kubernetes/base/dynamic/discovery.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# limitations under the License.
1414

1515
import os
16-
import six
1716
import json
1817
import logging
1918
import hashlib
@@ -33,7 +32,7 @@
3332
DISCOVERY_PREFIX = 'apis'
3433

3534

36-
class Discoverer(object):
35+
class Discoverer:
3736
"""
3837
A convenient container for storing discovered API resources. Allows
3938
easy searching and retrieval of specific resources.
@@ -43,9 +42,7 @@ class Discoverer(object):
4342

4443
def __init__(self, client, cache_file):
4544
self.client = client
46-
default_cache_id = self.client.configuration.host
47-
if six.PY3:
48-
default_cache_id = default_cache_id.encode('utf-8')
45+
default_cache_id = self.client.configuration.host.encode('utf-8')
4946
try:
5047
default_cachefile_name = 'osrcp-{0}.json'.format(hashlib.md5(default_cache_id, usedforsecurity=False).hexdigest())
5148
except TypeError:
@@ -60,7 +57,7 @@ def __init_cache(self, refresh=False):
6057
refresh = True
6158
else:
6259
try:
63-
with open(self.__cache_file, 'r') as f:
60+
with open(self.__cache_file) as f:
6461
self._cache = json.load(f, cls=partial(CacheDecoder, self.client))
6562
if self._cache.get('library_version') != __version__:
6663
# Version mismatch, need to refresh cache
@@ -313,7 +310,7 @@ def __iter__(self):
313310
prefix, group, version, rg.preferred)
314311
self._cache['resources'][prefix][group][version] = rg
315312
self.__update_cache = True
316-
for _, resource in six.iteritems(rg.resources):
313+
for _, resource in rg.resources.items():
317314
yield resource
318315
self.__maybe_write_cache()
319316

@@ -397,7 +394,7 @@ def __iter__(self):
397394
yield resource
398395

399396

400-
class ResourceGroup(object):
397+
class ResourceGroup:
401398
"""Helper class for Discoverer container"""
402399
def __init__(self, preferred, resources=None):
403400
self.preferred = preferred

kubernetes/base/dynamic/resource.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from pprint import pformat
2020

2121

22-
class Resource(object):
22+
class Resource:
2323
""" Represents an API resource type, containing the information required to build urls for requests """
2424

2525
def __init__(self, prefix=None, group=None, api_version=None, kind=None,
@@ -278,7 +278,7 @@ def to_dict(self):
278278
return d
279279

280280

281-
class ResourceInstance(object):
281+
class ResourceInstance:
282282
""" A parsed instance of an API resource. It exists solely to
283283
ease interaction with API objects by allowing attributes to
284284
be accessed with '.' notation.
@@ -338,14 +338,14 @@ def __repr__(self):
338338

339339
def __getattr__(self, name):
340340
if not '_ResourceInstance__initialised' in self.__dict__:
341-
return super(ResourceInstance, self).__getattr__(name)
341+
return super().__getattr__(name)
342342
return getattr(self.attributes, name)
343343

344344
def __setattr__(self, name, value):
345345
if not '_ResourceInstance__initialised' in self.__dict__:
346-
return super(ResourceInstance, self).__setattr__(name, value)
346+
return super().__setattr__(name, value)
347347
elif name in self.__dict__:
348-
return super(ResourceInstance, self).__setattr__(name, value)
348+
return super().__setattr__(name, value)
349349
else:
350350
self.attributes[name] = value
351351

@@ -359,7 +359,7 @@ def __dir__(self):
359359
return dir(type(self)) + list(self.attributes.__dict__.keys())
360360

361361

362-
class ResourceField(object):
362+
class ResourceField:
363363
""" A parsed instance of an API resource attribute. It exists
364364
solely to ease interaction with API objects by allowing
365365
attributes to be accessed with '.' notation
@@ -389,8 +389,7 @@ def __dir__(self):
389389
return dir(type(self)) + list(self.__dict__.keys())
390390

391391
def __iter__(self):
392-
for k, v in self.__dict__.items():
393-
yield (k, v)
392+
yield from self.__dict__.items()
394393

395394
def to_dict(self):
396395
return self.__serialize(self)

0 commit comments

Comments
 (0)