Skip to content
This repository was archived by the owner on Mar 13, 2022. It is now read-only.

Commit 298f21a

Browse files
authored
Merge pull request #251 from jamesgetx/fix_to_dict
fix: field extra_args recursive growth caused by Resource and Subreso…
2 parents 6023e11 + bd944a5 commit 298f21a

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

dynamic/resource.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def __init__(self, prefix=None, group=None, api_version=None, kind=None,
4848
self.extra_args = kwargs
4949

5050
def to_dict(self):
51-
return {
51+
d = {
5252
'_type': 'Resource',
5353
'prefix': self.prefix,
5454
'group': self.group,
@@ -58,12 +58,13 @@ def to_dict(self):
5858
'verbs': self.verbs,
5959
'name': self.name,
6060
'preferred': self.preferred,
61-
'singular_name': self.singular_name,
62-
'short_names': self.short_names,
61+
'singularName': self.singular_name,
62+
'shortNames': self.short_names,
6363
'categories': self.categories,
6464
'subresources': {k: sr.to_dict() for k, sr in self.subresources.items()},
65-
'extra_args': self.extra_args,
6665
}
66+
d.update(self.extra_args)
67+
return d
6768

6869
@property
6970
def group_version(self):
@@ -236,7 +237,7 @@ def __init__(self, parent, **kwargs):
236237
self.api_version = parent.api_version
237238
self.kind = kwargs.pop('kind')
238239
self.name = kwargs.pop('name')
239-
self.subresource = self.name.split('/')[1]
240+
self.subresource = kwargs.pop('subresource', None) or self.name.split('/')[1]
240241
self.namespaced = kwargs.pop('namespaced', False)
241242
self.verbs = kwargs.pop('verbs', None)
242243
self.extra_args = kwargs
@@ -262,14 +263,15 @@ def __getattr__(self, name):
262263
return partial(getattr(self.parent.client, name), self)
263264

264265
def to_dict(self):
265-
return {
266+
d = {
266267
'kind': self.kind,
267268
'name': self.name,
268269
'subresource': self.subresource,
269270
'namespaced': self.namespaced,
270-
'verbs': self.verbs,
271-
'extra_args': self.extra_args,
271+
'verbs': self.verbs
272272
}
273+
d.update(self.extra_args)
274+
return d
273275

274276

275277
class ResourceInstance(object):

dynamic/test_discovery.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,24 @@ def test_init_cache_from_file(self):
3838

3939
# test no Discoverer._write_cache called
4040
self.assertTrue(mtime1 == mtime2)
41+
42+
def test_cache_decoder_resource_and_subresource(self):
43+
client = DynamicClient(api_client.ApiClient(configuration=self.config))
44+
# first invalidate cache
45+
client.resources.invalidate_cache()
46+
47+
# do Discoverer.__init__
48+
client = DynamicClient(api_client.ApiClient(configuration=self.config))
49+
# the resources of client will use _cache['resources'] in memory
50+
deploy1 = client.resources.get(kind='Deployment')
51+
52+
# do Discoverer.__init__
53+
client = DynamicClient(api_client.ApiClient(configuration=self.config))
54+
# the resources of client will use _cache['resources'] decode from cache file
55+
deploy2 = client.resources.get(kind='Deployment')
56+
57+
# test Resource is the same
58+
self.assertTrue(deploy1 == deploy2)
59+
60+
# test Subresource is the same
61+
self.assertTrue(deploy1.status == deploy2.status)

0 commit comments

Comments
 (0)