openshift/openshift-restclient-python

In-cluster config support? object has no attribute 'configuration'

mygithubthrowaway opened this issue · 3 comments

Hi there,

I created a simple script, if I use it from my workstation everything works properly, because the configuration is loaded from my client config (KUBECONFIG).
I use the following snippet as readme:

k8s_client = config.new_client_from_config()
dyn_client = DynamicClient(k8s_client)

But when I tried to run this script in the ocp cluster using the "in cluster config" from kubernetes api client(example here: https://github.com/kubernetes-client/python/blob/master/examples/in_cluster_config.py#L55) like this:

k8s_client = config.load_incluster_config()
dyn_client = DynamicClient(k8s_client)

I receive an error:

Traceback (most recent call last):
  File "/usr/src/app/app.py", line 24, in <module>
    dyn_client = DynamicClient(k8s_client)
  File "/usr/local/lib/python3.8/site-packages/openshift/dynamic/client.py", line 70, in __init__
    self.configuration = client.configuration
AttributeError: 'NoneType' object has no attribute 'configuration'

Is in-cluster config supported? basically to run something from inside a pod

I tend to use ansible's k8s module work as a reference:

the outer lines of this code block should do the trick: https://github.com/ansible-collections/community.kubernetes/blob/master/plugins/module_utils/common.py#L198-L207

@mygithubthrowaway The issue with the code you posted is that the DynamicClient object takes a kubernetes client object to instantiate. The config.new_client_from_config() function first runs the config.load_kube_config() function, and then ApiClient(configuration=client_config). The load_incluster_config() function that you are using in your second snippet does not return an ApiClient object, it just loads the config. This snippet should work:

import kubernetes
kubernetes.config.load_incluster_config()
k8s_client = kubernetes.client.ApiClient()
dyn_client = DynamicClient(k8s_client)

@willthames thanks for the tips

@fabianvf the snippet works fine, thanks!