Python: impossible to use client Configuration objects with different authorization

Created on 25 Oct 2018  路  5Comments  路  Source: kubernetes-client/python

When trying to create few Configuration objects with different authorization tokens faced the following issue:

In [1]: import kubernetes.client                                                                                                            

In [2]: config1 = kubernetes.client.Configuration()                                                                                         

In [3]: config2 = kubernetes.client.Configuration()                                                                                         

In [4]: config1.api_key                                                                                                                     
Out[4]: {}

In [5]: config2.api_key                                                                                                                     
Out[5]: {}

In [6]: config1.api_key['authorization'] = 'zzzz'                                                                                           

In [7]: config2.api_key                                                                                                                     
Out[7]: {'authorization': 'zzzz'}

In [8]: id(config1.api_key)                                                                                                                 
Out[8]: 140012587374472

In [9]: id(config2.api_key)                                                                                                                 
Out[9]: 140012587374472

TypeWithDefault metaclass makes Configuration work as a singleton.

All 5 comments

Yes, it's intentional. You can create new object using config1 = type.__call__(Configuration).

@tomplus What is the reason for this? It's not documented anywhere that Configuration is a singleton. Instead, i see Configuration is not a singleton object anymore. Please use Configuraion.set_default to change default configuration in the changelog.

The suggested factory for Configuration doesn't look good. It uses the magic method __call__, that is for making objects callable, and not for be called directly by some client code. Why not having something like kubernetes.client.new_config() for the factory?

Yes, it looks complicated. It's not a singleton but a metaclass construction. A feature - 'default configuration' makes it works as a singleton. This library wraps it and it'd better to use config.new_client_from_config like in this example: https://github.com/kubernetes-client/python/blob/master/examples/multiple_clusters.py but I'm not sure if it's a @oliaboo's case...

@tomplus Thank you for the example. I think we're ready to close this issue.

@tomplus, thank you for the explanation! Closing the issue

Was this page helpful?
0 / 5 - 0 ratings