Skip to content

Dynamic Client list pods as PartialObjectMetadataList is throwing exception when there is no matching labels filters #2142

Closed
@dirrao

Description

@dirrao

When using dynamic client to list pods in PartialObjectMetadataList format. When there no matching pods with labels, its throwing exception rather than return empty list.

    # fetching the node api
    api = client.resources.get(api_version="v1", kind="Pod")

    # Creating a custom header
    params = {'header_params': {'Accept': 'application/json;as=PartialObjectMetadataList;v=v1;g=meta.k8s.io'}, 'query_params': [('labelSelector', 'role=xyz')], 'namespace': 'default'}

    resp = api.get(**params)

Traceback (most recent call last):
  File "dynamic_client.py", line 30, in <module>
    main()
  File "dynamic_client.py", line 16, in main
    resp = api.get(**params)
  File "/usr/local/miniconda/lib/python3.7/site-packages/kubernetes/dynamic/client.py", line 112, in get
    return self.request('get', path, **kwargs)
  File "/usr/local/miniconda/lib/python3.7/site-packages/kubernetes/dynamic/client.py", line 62, in inner
    return serializer(self, json.loads(resp.data.decode('utf8')))
  File "/usr/local/miniconda/lib/python3.7/site-packages/kubernetes/dynamic/resource.py", line 291, in __init__
    for item in instance['items']:
TypeError: 'NoneType' object is not iterable

Solution: We should have null check before iterating over the instance items.

Environment:

  • Kubernetes version (``): v1.22.17
  • OS (e.g., MacOS 10.13.6): CentOs 6.0
  • Python version (python --version) 3.7.15
  • Python client version (pip list | grep kubernetes) 3.7.15

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bugCategorizes issue or PR as related to a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions