Skip to content

Commit 34b27ab

Browse files
[release-0.9] Always set apiVersion and kind for resources within a resource list (#301)
* 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. * Just do one big serialization at the end of a ResourceList operation
1 parent 437342a commit 34b27ab

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

openshift/dynamic/client.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ def get(self, body, name=None, namespace=None, **kwargs):
473473
response = copy.deepcopy(body)
474474

475475
response['items'] = [
476-
item['resource'].get(name=item['name'], namespace=item['namespace'] or namespace, **kwargs)
476+
item['resource'].get(name=item['name'], namespace=item['namespace'] or namespace, **kwargs).to_dict()
477477
for item in resource_list['items']
478478
]
479479
return ResourceInstance(self, response)
@@ -485,7 +485,7 @@ def delete(self, body, name=None, namespace=None, **kwargs):
485485
response = copy.deepcopy(body)
486486

487487
response['items'] = [
488-
item['resource'].delete(name=item['name'], namespace=item['namespace'] or namespace, **kwargs)
488+
item['resource'].delete(name=item['name'], namespace=item['namespace'] or namespace, **kwargs).to_dict()
489489
for item in resource_list['items']
490490
]
491491
return ResourceInstance(self, response)
@@ -494,7 +494,7 @@ def verb_mapper(self, verb, body, **kwargs):
494494
resource_list = self._items_to_resources(body)
495495
response = copy.deepcopy(body)
496496
response['items'] = [
497-
getattr(item['resource'], verb)(body=item['definition'], **kwargs)
497+
getattr(item['resource'], verb)(body=item['definition'], **kwargs).to_dict()
498498
for item in resource_list['items']
499499
]
500500
return ResourceInstance(self, response)
@@ -979,6 +979,17 @@ class ResourceInstance(object):
979979

980980
def __init__(self, resource, instance):
981981
self.resource_type = resource
982+
# If we have a list of resources, then set the apiVersion and kind of
983+
# each resource in 'items'
984+
kind = instance['kind']
985+
if kind.endswith('List') and 'items' in instance:
986+
kind = instance['kind'][:-4]
987+
for item in instance['items']:
988+
if 'apiVersion' not in item:
989+
item['apiVersion'] = instance['apiVersion']
990+
if 'kind' not in item:
991+
item['kind'] = kind
992+
982993
self.attributes = self.__deserialize(instance)
983994

984995
def __deserialize(self, field):

0 commit comments

Comments
 (0)