openshift/openshift-restclient-python

GET on DeploymentConfig resutls in 404

rummens opened this issue · 10 comments

Hi there,

I am trying to retrieve a list of DeploymentConfigs of a project, but it failes with a Not Found 404. I tried calling the OpenShift REST API manually and it works. I guess something inside this tool calls the wrong API endpoint?

Here is the code:

v1_resources = self.dyn_client.resources.get(api_version='v1', kind="DeploymentConfig")
        try:
                # Lists all Services in the cluster
                return v1_resources.get(namespace=project)

Any suggestions?

Thanks
Kind Regards
Marcel

How are you instantiating the client? From the README's usage section:

To work with the dynamic client, you will need an instantiated Kubernetes
client object. The Kubernetes client object requires a Kubernetes Config
that can be set in the Config
class

or using a helper utility. All of the examples that follow make use of the
new_client_from_config() helper utility provided by the Kubernetes Client
Config

that returns an API client to be used with any API object.
There are plenty of Kubernetes Client
examples
to
examine other ways of accessing Kubernetes Clusters.

When you say "calling the Openshift REST API manually" do you mean oc get or curl?

I am using this code snippet to create the k8 client:

 # Define the barer token we are going to use to authenticate.
        # See here to create the token:
        # https://kubernetes.io/docs/tasks/access-application-cluster/access-cluster/
        aToken = token

        # Create a configuration object
        aConfiguration = client.Configuration()

        # Specify the endpoint of your Kube cluster
        aConfiguration.host = host

        # Security part.
        # In this simple example we are not going to verify the SSL certificate of
        # the remote cluster (for simplicity reason)
        aConfiguration.verify_ssl = verify_ssl
        # Nevertheless if you want to do it you can with these 2 parameters
        # configuration.verify_ssl=True
        # ssl_ca_cert is the filepath to the file that contains the certificate.
        # configuration.ssl_ca_cert="certificate"

        aConfiguration.api_key = {"authorization": "Bearer " + aToken}

        # Create a ApiClient with our config
        return client.ApiClient(aConfiguration)

Then this to init the dynamic client:
self.dyn_client = DynamicClient(self.k8s_client)

I was using Postman, so calling the REST API directly. Basically curl as GUI. The same GET function of the python client works for other Resources, e.g. Service, Route or BuildConfig. Only DeploymentConfig results in an error.

can you print the output of self.dyn_client.api_groups? That should at least tell what API groups it's finding in your cluster.

I was able to narrow the problem down to some issue with the label selector (using only app label in the moment). It works as excepted when I use the name of the deployment config. From my understanding the search with the labels should return an empty list if it does not find any resources (similar to Service).

@fabianvf I cannot give you the data, it seems dyn_client has to api_groups attribute or function? Returns with AttributeError: 'DynamicClient' object has no attribute 'api_groups'

Oh oops, dyn_client.resources.api_groups, apologies. If you aren't passing a name to get it should definitely be returning an empty list, not 404ing, I'll dig into that ASAP.

Hi,
this is the response:
dict_keys(['apps', 'authentication.k8s.io', 'autoscaling', 'batch', 'certificates.k8s.io', 'extensions', 'policy', 'storage.k8s.io'])

I found a workaorund for now, using the name and catching the exception. But it is not best practice :-) Thanks for your help

Cool, would you mind also posting the output of dyn_client.version?

Can I see the request you're making? I was able to perform a search with various combinations of namespace, label_selector, and name, and I got back empty lists as long as I did not provide name (if you provide name then it 404s, but that is expected).

Hello, Exactly same issue here :

Openshift version : 3.6

dyn_client.resources.api_groups :

dict_keys(['authentication.k8s.io', 'authorization.k8s.io', 'autoscaling', 'batch', 'certificates.k8s.io', 'extensions', 'policy', 'rbac.authorization.k8s.io', 'storage.k8s.io', 'apps', 'security.openshift.io', 'network.openshift.io', 'route.openshift.io', 'image.openshift.io', 'authorization.openshift.io', 'template.openshift.io', 'oauth.openshift.io', 'project.openshift.io', 'build.openshift.io', 'quota.openshift.io', 'user.openshift.io', 'apps.openshift.io'])

dyn_client.version :

{'kubernetes': {'major': '1', 'minor': '6', 'gitVersion': 'v1.6.1+5115d708d7', 'gitCommit': 'fff65cf', 'gitTreeState': 'clean', 'buildDate': '2018-11-25T08:15:14Z', 'goVersion': 'go1.7.6', 'compiler': 'gc', 'platform': 'linux/amd64'}, 'openshift': {'major': '3', 'minor': '6', 'gitCommit': '9686d5294b', 'gitVersion': 'v3.6.173.0.140', 'buildDate': '2018-11-25T08:15:14Z'}}

code :

dc = dyn_client.resources.get(api_version='v1', kind='DeploymentConfig')
list = dc.get(namespace='mynamespace')

Error :

openshift.dynamic.exceptions.NotFoundError: 404
Reason: Not Found
HTTP response headers: HTTPHeaderDict({'Cache-Control': 'no-store', 'Content-Type': 'application/json', 'Date': 'Fri, 15 Feb 2019 16:25:56 GMT', 'Content-Length': '174'})
HTTP response body: b'{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"the server could not find the requested resource","reason":"NotFound","details":{},"code":404}\n'

Note that the same issue occur when i specify a name like :

list = dc.get(name='mydeploymentconfig', namespace='mynamespace')

The same GET function of the python client works for other Resources, e.g. Service.
Any suggestion of how to retrieve all deploymentconfig in my openshift namespace ?

Thanks.

my fault, I misconfigured the client because I have to use the openshift as the version of the API.
the code below works perfectly well

dc = client.resources.get(api_version='apps.openshift.io/v1', kind='DeploymentConfig')
list = dc.get(name=name, namespace=namespace, label_selector=label_selector)

I think this comment can solve this issue ? @rummens can you confirm ?

Anyway, thank you for this wonderful client !