From 1eb988c4492e66b713b48aec22ccb4f9508cc5bb Mon Sep 17 00:00:00 2001 From: Chance Zibolski Date: Wed, 22 May 2019 16:39:22 -0700 Subject: [PATCH 1/2] Always set apiVersion and kind for resources within a resource list When the Kubernetes REST API returns a list of objects, it returns a resource list object with an apiVersion and kind set to "KindList" where "Kind" is the Kind of object queried, rather than an simple array of objects. The `items` property contains the array of resources returned. Each resource in this list does not contain the GVK information, only the resource list has an apiVersion or kind property set. This ensures when a resource list is returned, each resource within the list has it's apiVersion and kind set, to make it similar to other Kubernetes clients, and easier to work with the returned resources. --- openshift/dynamic/client.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/openshift/dynamic/client.py b/openshift/dynamic/client.py index b126bdf4..c8e31c6c 100644 --- a/openshift/dynamic/client.py +++ b/openshift/dynamic/client.py @@ -979,6 +979,17 @@ class ResourceInstance(object): def __init__(self, resource, instance): self.resource_type = resource + # If we have a list of resources, then set the apiVersion and kind of + # each resource in 'items' + kind = instance['kind'] + if kind.endswith('List') and 'items' in instance: + kind = instance['kind'][:-4] + for item in instance['items']: + if 'apiVersion' not in item: + item['apiVersion'] = instance['apiVersion'] + if 'kind' not in item: + item['kind'] = kind + self.attributes = self.__deserialize(instance) def __deserialize(self, field): From 7ab5ce80d9cf34155a80c4c1b5541edaaa01882b Mon Sep 17 00:00:00 2001 From: Fabian von Feilitzsch Date: Fri, 24 May 2019 11:23:58 -0400 Subject: [PATCH 2/2] Just do one big serialization at the end of a ResourceList operation --- openshift/dynamic/client.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/openshift/dynamic/client.py b/openshift/dynamic/client.py index c8e31c6c..4f8cfacc 100644 --- a/openshift/dynamic/client.py +++ b/openshift/dynamic/client.py @@ -473,7 +473,7 @@ def get(self, body, name=None, namespace=None, **kwargs): response = copy.deepcopy(body) response['items'] = [ - item['resource'].get(name=item['name'], namespace=item['namespace'] or namespace, **kwargs) + item['resource'].get(name=item['name'], namespace=item['namespace'] or namespace, **kwargs).to_dict() for item in resource_list['items'] ] return ResourceInstance(self, response) @@ -485,7 +485,7 @@ def delete(self, body, name=None, namespace=None, **kwargs): response = copy.deepcopy(body) response['items'] = [ - item['resource'].delete(name=item['name'], namespace=item['namespace'] or namespace, **kwargs) + item['resource'].delete(name=item['name'], namespace=item['namespace'] or namespace, **kwargs).to_dict() for item in resource_list['items'] ] return ResourceInstance(self, response) @@ -494,7 +494,7 @@ def verb_mapper(self, verb, body, **kwargs): resource_list = self._items_to_resources(body) response = copy.deepcopy(body) response['items'] = [ - getattr(item['resource'], verb)(body=item['definition'], **kwargs) + getattr(item['resource'], verb)(body=item['definition'], **kwargs).to_dict() for item in resource_list['items'] ] return ResourceInstance(self, response)