fix: field extra_args recursive growth caused by Resource and Subresource to_dict method when cache with CacheDecoder

This commit is contained in:
jamesgetx 2021-09-03 17:30:56 +08:00
parent dd15ac6263
commit bd944a58a3
No known key found for this signature in database
GPG Key ID: 2A809BA26FBB7CE3
2 changed files with 31 additions and 8 deletions

View File

@ -48,7 +48,7 @@ class Resource(object):
self.extra_args = kwargs
def to_dict(self):
return {
d = {
'_type': 'Resource',
'prefix': self.prefix,
'group': self.group,
@ -58,12 +58,13 @@ class Resource(object):
'verbs': self.verbs,
'name': self.name,
'preferred': self.preferred,
'singular_name': self.singular_name,
'short_names': self.short_names,
'singularName': self.singular_name,
'shortNames': self.short_names,
'categories': self.categories,
'subresources': {k: sr.to_dict() for k, sr in self.subresources.items()},
'extra_args': self.extra_args,
}
d.update(self.extra_args)
return d
@property
def group_version(self):
@ -236,7 +237,7 @@ class Subresource(Resource):
self.api_version = parent.api_version
self.kind = kwargs.pop('kind')
self.name = kwargs.pop('name')
self.subresource = self.name.split('/')[1]
self.subresource = kwargs.pop('subresource', None) or self.name.split('/')[1]
self.namespaced = kwargs.pop('namespaced', False)
self.verbs = kwargs.pop('verbs', None)
self.extra_args = kwargs
@ -262,14 +263,15 @@ class Subresource(Resource):
return partial(getattr(self.parent.client, name), self)
def to_dict(self):
return {
d = {
'kind': self.kind,
'name': self.name,
'subresource': self.subresource,
'namespaced': self.namespaced,
'verbs': self.verbs,
'extra_args': self.extra_args,
'verbs': self.verbs
}
d.update(self.extra_args)
return d
class ResourceInstance(object):

View File

@ -38,3 +38,24 @@ class TestDiscoverer(unittest.TestCase):
# test no Discoverer._write_cache called
self.assertTrue(mtime1 == mtime2)
def test_cache_decoder_resource_and_subresource(self):
client = DynamicClient(api_client.ApiClient(configuration=self.config))
# first invalidate cache
client.resources.invalidate_cache()
# do Discoverer.__init__
client = DynamicClient(api_client.ApiClient(configuration=self.config))
# the resources of client will use _cache['resources'] in memory
deploy1 = client.resources.get(kind='Deployment')
# do Discoverer.__init__
client = DynamicClient(api_client.ApiClient(configuration=self.config))
# the resources of client will use _cache['resources'] decode from cache file
deploy2 = client.resources.get(kind='Deployment')
# test Resource is the same
self.assertTrue(deploy1 == deploy2)
# test Subresource is the same
self.assertTrue(deploy1.status == deploy2.status)