Skip to content

Commit 5b32a84

Browse files
authored
Always set apiVersion and kind for resources within a resource list (#298) (#302)
* 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 (cherry picked from commit 68cc0e3)
1 parent 26b3c69 commit 5b32a84

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
@@ -510,7 +510,7 @@ def get(self, body=None, **kwargs):
510510
response = copy.deepcopy(body)
511511
namespace = kwargs.pop('namespace', None)
512512
response['items'] = [
513-
self.resource.get(name=item['metadata']['name'], namespace=item['metadata'].get('namespace', namespace), **kwargs)
513+
self.resource.get(name=item['metadata']['name'], namespace=item['metadata'].get('namespace', namespace), **kwargs).to_dict()
514514
for item in body['items']
515515
]
516516
return ResourceInstance(self, response)
@@ -519,15 +519,15 @@ def delete(self, body=None, *args, **kwargs):
519519
response = copy.deepcopy(body)
520520
namespace = kwargs.pop('namespace', None)
521521
response['items'] = [
522-
self.resource.delete(name=item['metadata']['name'], namespace=item['metadata'].get('namespace', namespace), **kwargs)
522+
self.resource.delete(name=item['metadata']['name'], namespace=item['metadata'].get('namespace', namespace), **kwargs).to_dict()
523523
for item in body['items']
524524
]
525525
return ResourceInstance(self, response)
526526

527527
def verb_mapper(self, verb, body=None, **kwargs):
528528
response = copy.deepcopy(body)
529529
response['items'] = [
530-
getattr(self.resource, verb)(body=item, **kwargs)
530+
getattr(self.resource, verb)(body=item, **kwargs).to_dict()
531531
for item in body['items']
532532
]
533533
return ResourceInstance(self, response)
@@ -738,6 +738,17 @@ class ResourceInstance(object):
738738

739739
def __init__(self, resource, instance):
740740
self.resource_type = resource
741+
# If we have a list of resources, then set the apiVersion and kind of
742+
# each resource in 'items'
743+
kind = instance['kind']
744+
if kind.endswith('List') and 'items' in instance:
745+
kind = instance['kind'][:-4]
746+
for item in instance['items']:
747+
if 'apiVersion' not in item:
748+
item['apiVersion'] = instance['apiVersion']
749+
if 'kind' not in item:
750+
item['kind'] = kind
751+
741752
self.attributes = self.__deserialize(instance)
742753

743754
def __deserialize(self, field):

0 commit comments

Comments
 (0)