Objects with no namespaces cannot be fetched #2

Open
opened 8 months ago by guhcampos · 1 comments

Hello! I have just stumbled into your project coming from pytest-kind but there's a subtle yet show-stopping bug in the handling of non-namespaced objects.

I have a CustomResourceDefinition which I need to retrieve with:

    crd = (
        pykube.CustomResourceDefinition.objects(kind_cluster.api)
        .filter(namespace=pykube.all)
        .get(name="mycrd.mydomain.com")
    )

It can definitely be listed as it works fine up until I introduce the call to get, then I receive a:

E           AttributeError: 'object' object has no attribute 'strip'

Which seems related to the fact that namespaces is a placeholder object, given the CRDs are not namespaced:

tests/kind/test_kind.py:7:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../../../../.cache/pypoetry/virtualenvs/operator-IUAFUnlB-py3.8/lib/python3.8/site-packages/pykube/query.py:124: in get
    return self.get_by_name(kwargs["name"])
../../../../.cache/pypoetry/virtualenvs/operator-IUAFUnlB-py3.8/lib/python3.8/site-packages/pykube/query.py:112: in get_by_name
    r = self.api.get(**kwargs)
../../../../.cache/pypoetry/virtualenvs/operator-IUAFUnlB-py3.8/lib/python3.8/site-packages/pykube/http.py:431: in get
    return self.session.get(*args, **self.get_kwargs(**kwargs))
../../../../.cache/pypoetry/virtualenvs/operator-IUAFUnlB-py3.8/lib/python3.8/site-packages/pykube/http.py:390: in get_kwargs
    kwargs["url"] = self.url + join_url_path(*bits, join_empty=True)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

join_empty = True, components = ('/apis', 'apiextensions.k8s.io/v1', 'namespaces', <object object at 0x7efe54da72c0>, 'customresourcedefinitions/mycrd.mydomain.com')
new_comps = ['apis', 'apiextensions.k8s.io/v1', 'namespaces'], comp = <object object at 0x7efe54da72c0>

    def join_url_path(*components, join_empty: bool = False) -> str:
        """Join given URL path components and return absolute path starting with '/'."""
        new_comps: List[str] = []
        for comp in components:
>           comp = comp.strip("/")
E           AttributeError: 'object' object has no attribute 'strip'

../../../../.cache/pypoetry/virtualenvs/operator-IUAFUnlB-py3.8/lib/python3.8/site-packages/pykube/utils.py:72: AttributeError

note the 'namespaces', <object object at 0x7efe54da72c0>,

Version is:

poetry show | grep pykube
pykube-ng               20.10.0    Python client library for Kubernetes
Hello! I have just stumbled into your project coming from `pytest-kind` but there's a subtle yet show-stopping bug in the handling of non-namespaced objects. I have a `CustomResourceDefinition` which I need to retrieve with: ```python crd = ( pykube.CustomResourceDefinition.objects(kind_cluster.api) .filter(namespace=pykube.all) .get(name="mycrd.mydomain.com") ) ``` It can definitely be listed as it works fine up until I introduce the call to `get`, then I receive a: ```python E AttributeError: 'object' object has no attribute 'strip' ``` Which seems related to the fact that `namespaces` is a placeholder object, given the CRDs are not namespaced: ``` tests/kind/test_kind.py:7: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ../../../../.cache/pypoetry/virtualenvs/operator-IUAFUnlB-py3.8/lib/python3.8/site-packages/pykube/query.py:124: in get return self.get_by_name(kwargs["name"]) ../../../../.cache/pypoetry/virtualenvs/operator-IUAFUnlB-py3.8/lib/python3.8/site-packages/pykube/query.py:112: in get_by_name r = self.api.get(**kwargs) ../../../../.cache/pypoetry/virtualenvs/operator-IUAFUnlB-py3.8/lib/python3.8/site-packages/pykube/http.py:431: in get return self.session.get(*args, **self.get_kwargs(**kwargs)) ../../../../.cache/pypoetry/virtualenvs/operator-IUAFUnlB-py3.8/lib/python3.8/site-packages/pykube/http.py:390: in get_kwargs kwargs["url"] = self.url + join_url_path(*bits, join_empty=True) _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ join_empty = True, components = ('/apis', 'apiextensions.k8s.io/v1', 'namespaces', <object object at 0x7efe54da72c0>, 'customresourcedefinitions/mycrd.mydomain.com') new_comps = ['apis', 'apiextensions.k8s.io/v1', 'namespaces'], comp = <object object at 0x7efe54da72c0> def join_url_path(*components, join_empty: bool = False) -> str: """Join given URL path components and return absolute path starting with '/'.""" new_comps: List[str] = [] for comp in components: > comp = comp.strip("/") E AttributeError: 'object' object has no attribute 'strip' ../../../../.cache/pypoetry/virtualenvs/operator-IUAFUnlB-py3.8/lib/python3.8/site-packages/pykube/utils.py:72: AttributeError ``` note the `'namespaces', <object object at 0x7efe54da72c0>,` Version is: ```bash poetry show | grep pykube pykube-ng 20.10.0 Python client library for Kubernetes ```
Owner

Thanks for reporting!

You can simply remove the filter(namespace..) and it works:

Pykube v20.10.0, loaded "/home/hjacobs/.kube/config" with context "myclustername".

    Example commands:
      [d.name for d in Deployment.objects(api)]              # get names of deployments in default namespace
      list(DaemonSet.objects(api, namespace='kube-system'))  # list daemonsets in "kube-system"
      Pod.objects(api).get_by_name('mypod').labels           # labels of pod "mypod"

    Use Ctrl-D to exit

>>> CustomResourceDefinition.objects(api).get(name="awsiamroles.zalando.org")
<CustomResourceDefinition awsiamroles.zalando.org>

I'm using Custom Resource Definitions in kube-web-view and did not have any problems. We might want to improve this case to fail more gracefully with filter(namespace=..) (or don't fail at all).

Thanks for reporting! You can simply remove the `filter(namespace..)` and it works: ``` Pykube v20.10.0, loaded "/home/hjacobs/.kube/config" with context "myclustername". Example commands: [d.name for d in Deployment.objects(api)] # get names of deployments in default namespace list(DaemonSet.objects(api, namespace='kube-system')) # list daemonsets in "kube-system" Pod.objects(api).get_by_name('mypod').labels # labels of pod "mypod" Use Ctrl-D to exit >>> CustomResourceDefinition.objects(api).get(name="awsiamroles.zalando.org") <CustomResourceDefinition awsiamroles.zalando.org> ``` I'm using Custom Resource Definitions in [kube-web-view](https://codeberg.org/hjacobs/kube-web-view/) and did not have any problems. We might want to improve this case to fail more gracefully with `filter(namespace=..)` (or don't fail at all).
Sign in to join this conversation.
No Label
No Milestone
No Assignees
2 Participants
Notifications
Due Date

No due date set.

Dependencies

This issue currently doesn't have any dependencies.

Loading…
There is no content yet.