openshift/openshift-restclient-python

How to query an appliedclusterresourcequota?

machineglow opened this issue · 1 comments

Hi, I've created a clusterresourcequota and applied it to several projects via the selector and I'm trying to query the appliedclusterresourcequota resource type for specific projects but I'm not sure how to do that. Whenever I try to specify a namespace, it complains that there's no namespace attribute.

>>> v1_aclquotas = dyn_client.resources.get(api_version='v1', kind='AppliedClusterResourceQuota', namespace='default')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\mnhwong\AppData\Local\Programs\Python\Python38\lib\site-packages\openshift\dynamic\discovery.py", line 230, in get
    results = self.search(**kwargs)
  File "C:\Users\mnhwong\AppData\Local\Programs\Python\Python38\lib\site-packages\openshift\dynamic\discovery.py", line 275, in search
    results = self.__search(self.__build_search(**kwargs), self.__resources, [])
  File "C:\Users\mnhwong\AppData\Local\Programs\Python\Python38\lib\site-packages\openshift\dynamic\discovery.py", line 317, in __search
    matches.extend(self.__search([key] + parts[1:], resources, reqParams))
  File "C:\Users\mnhwong\AppData\Local\Programs\Python\Python38\lib\site-packages\openshift\dynamic\discovery.py", line 303, in __search
    return self.__search(parts[1:], resourcePart, reqParams + [part] )
  File "C:\Users\mnhwong\AppData\Local\Programs\Python\Python38\lib\site-packages\openshift\dynamic\discovery.py", line 317, in __search
    matches.extend(self.__search([key] + parts[1:], resources, reqParams))
  File "C:\Users\mnhwong\AppData\Local\Programs\Python\Python38\lib\site-packages\openshift\dynamic\discovery.py", line 303, in __search
    return self.__search(parts[1:], resourcePart, reqParams + [part] )
  File "C:\Users\mnhwong\AppData\Local\Programs\Python\Python38\lib\site-packages\openshift\dynamic\discovery.py", line 299, in __search
    return self.__search(parts[1:], resourcePart.resources, reqParams)
  File "C:\Users\mnhwong\AppData\Local\Programs\Python\Python38\lib\site-packages\openshift\dynamic\discovery.py", line 308, in __search
    if getattr(_resource, term) == value:
  File "C:\Users\mnhwong\AppData\Local\Programs\Python\Python38\lib\site-packages\openshift\dynamic\resource.py", line 90, in __getattr__
    return partial(getattr(self.client, name), self)
AttributeError: 'DynamicClient' object has no attribute 'namespace'
>>>

If i omit the namespace, the code works but I don't get anything back

v1_aclquotas = dyn_client.resources.get(api_version='v1', kind='AppliedClusterResourceQuota')
print(v1_aclquotas.get())

ResourceInstance[AppliedClusterResourceQuotaList]:
  apiVersion: quota.openshift.io/v1
  items: []
  kind: AppliedClusterResourceQuotaList
  metadata:
    selfLink: /apis/quota.openshift.io/v1/appliedclusterresourcequotas

And here's the query to the regular clusterresourcequota kind that pulls back the expected clusterresourcequota I defined.

>>> v1_clquotas = dyn_client.resources.get(api_version='v1', kind='ClusterResourceQuota')
>>> print(v1_clquotas.get())
ResourceInstance[ClusterResourceQuotaList]:
  apiVersion: quota.openshift.io/v1
  items:
  - apiVersion: quota.openshift.io/v1
    kind: ClusterResourceQuota
    metadata:
      annotations:
<omitting>

Any advice would be appreciated. Thanks!

It's a little confusing but the dyn_client.resources.get is querying the available API resources, not instances of an API resource. So the proper way to query for instances of the resource in a namespace is

# This returns the API client for the AppliedClusterResourceQuota type
v1_aclquotas = dyn_client.resources.get(api_version='v1', kind='AppliedClusterResourceQuota')

# This uses the API client to get instances of the v1.AppliedResourceQuota in the default namespace
instances = v1_aclquotas.get(namespace='default')
print(instances)