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

fix: field extra_args recursive growth caused by Resource and Subreso… #251

Merged
merged 1 commit into from
Oct 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions dynamic/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __init__(self, prefix=None, group=None, api_version=None, kind=None,
self.extra_args = kwargs

def to_dict(self):
return {
d = {
'_type': 'Resource',
'prefix': self.prefix,
'group': self.group,
Expand All @@ -58,12 +58,13 @@ def to_dict(self):
'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):
Expand Down Expand Up @@ -236,7 +237,7 @@ def __init__(self, parent, **kwargs):
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
Expand All @@ -262,14 +263,15 @@ def __getattr__(self, name):
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):
Expand Down
21 changes: 21 additions & 0 deletions dynamic/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,24 @@ def test_init_cache_from_file(self):

# 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)