Skip to content

Commit

Permalink
Prevent API groups from growing stale (#264)
Browse files Browse the repository at this point in the history
* Make api_groups a property

* Prevent api_groups from growing stale by re-requesting resources every time

* Add back v1.List

* Typo
  • Loading branch information
fabianvf authored Feb 12, 2019
1 parent 66c8670 commit 31c96ef
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions openshift/dynamic/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,15 +640,15 @@ def default_groups(self, request_resources=False):
'v1': (ResourceGroup( True, resources=self.get_resources_for_api_version('oapi', '', 'v1', True) )
if request_resources else ResourceGroup(True))
}}

groups[DISCOVERY_PREFIX] = {'': {
'v1': ResourceGroup(True, resources = {"List": ResourceList(self.client)})
}}
groups[DISCOVERY_PREFIX] = {'': {
'v1': ResourceGroup(True, resources = {"List": ResourceList(self.client)})
}}
return groups

def parse_api_groups(self, request_resources=False):
def parse_api_groups(self, request_resources=False, update=False):
""" Discovers all API groups present in the cluster """
if not self._cache.get('resources'):
if not self._cache.get('resources') or update:
self._cache['resources'] = self._cache.get('resources', {})
groups_response = load_json(self.client.request('GET', '/{}'.format(DISCOVERY_PREFIX)))['groups']

groups = self.default_groups(request_resources=request_resources)
Expand All @@ -657,13 +657,16 @@ def parse_api_groups(self, request_resources=False):
new_group = {}
for version_raw in group['versions']:
version = version_raw['version']
resource_group = self._cache.get('resources', {}).get(DISCOVERY_PREFIX, {}).get(group['name'], {}).get(version)
preferred = version_raw == group['preferredVersion']
resources = {}
resources = resource_group.resources if resource_group else {}
if request_resources:
resources = self.get_resources_for_api_version(DISCOVERY_PREFIX, group['name'], version, preferred)
new_group[version] = ResourceGroup(preferred, resources=resources)
groups[DISCOVERY_PREFIX][group['name']] = new_group
self._cache['resources'] = groups
self._cache['resources'].update(groups)
self._write_cache()

return self._cache['resources']

def _load_server_info(self):
Expand Down Expand Up @@ -751,8 +754,9 @@ def __maybe_write_cache(self):
self._write_cache()
self.__update_cache = False

@property
def api_groups(self):
return self.__resources['apis'].keys()
return self.parse_api_groups(request_resources=False, update=True)['apis'].keys()

def search(self, **kwargs):
results = self.__search(self.__build_search(**kwargs), self.__resources, [])
Expand Down Expand Up @@ -817,6 +821,7 @@ def __iter__(self):
yield resource
self.__maybe_write_cache()


class EagerDiscoverer(Discoverer):
""" A convenient container for storing discovered API resources. Allows
easy searching and retrieval of specific resources.
Expand All @@ -836,7 +841,7 @@ def discover(self):
@property
def api_groups(self):
""" list available api groups """
return self.__resources['apis'].keys()
return self.parse_api_groups(request_resources=True, update=True)['apis'].keys()


def search(self, **kwargs):
Expand Down

0 comments on commit 31c96ef

Please sign in to comment.