@@ -37,15 +37,16 @@ def _join_host_port(host, port):
37
37
class InClusterConfigLoader (object ):
38
38
39
39
def __init__ (self , token_filename ,
40
- cert_filename , environ = os .environ ):
40
+ cert_filename , try_refresh_token , environ = os .environ ):
41
41
self ._token_filename = token_filename
42
42
self ._cert_filename = cert_filename
43
43
self ._environ = environ
44
+ self ._try_refresh_token = try_refresh_token
44
45
self ._token_refresh_period = datetime .timedelta (minutes = 1 )
45
46
46
- def load_and_set (self , refresh_token = True ):
47
+ def load_and_set (self , client_configuration ):
47
48
self ._load_config ()
48
- self ._set_config (refresh_token = refresh_token )
49
+ self ._set_config (client_configuration )
49
50
50
51
def _load_config (self ):
51
52
if (SERVICE_HOST_ENV_NAME not in self ._environ or
@@ -75,37 +76,41 @@ def _load_config(self):
75
76
76
77
self .ssl_ca_cert = self ._cert_filename
77
78
78
- def _set_config (self , refresh_token ):
79
- configuration = Configuration ()
80
- configuration .host = self .host
81
- configuration .ssl_ca_cert = self .ssl_ca_cert
82
- configuration .api_key ['authorization' ] = "bearer " + self .token
83
- Configuration .set_default (configuration )
84
- if not refresh_token :
79
+ def _set_config (self , client_configuration ):
80
+ client_configuration .host = self .host
81
+ client_configuration .ssl_ca_cert = self .ssl_ca_cert
82
+ if self .token is not None :
83
+ client_configuration .api_key ['authorization' ] = self .token
84
+ if not self ._try_refresh_token :
85
85
return
86
- def wrap (f ):
87
- in_cluster_config = self
88
- def wrapped (self , identifier ):
89
- if identifier == 'authorization' and identifier in self .api_key and in_cluster_config .token_expires_at <= datetime .datetime .now ():
90
- in_cluster_config ._read_token_file ()
91
- self .api_key [identifier ] = "bearer " + in_cluster_config .token
92
- return f (self , identifier )
93
- return wrapped
94
- Configuration .get_api_key_with_prefix = wrap (Configuration .get_api_key_with_prefix )
86
+ def load_token_from_file (* args ):
87
+ if self .token_expires_at <= datetime .datetime .now ():
88
+ self ._read_token_file ()
89
+ return self .token
90
+ client_configuration .get_api_key_with_prefix = load_token_from_file
95
91
96
92
def _read_token_file (self ):
97
93
with open (self ._token_filename ) as f :
98
- self .token = f .read ()
99
- self .token_expires_at = datetime .datetime .now () + self ._token_refresh_period
100
- if not self .token :
94
+ content = f .read ()
95
+ if not content :
101
96
raise ConfigException ("Token file exists but empty." )
97
+ self .token = "bearer " + content
98
+ self .token_expires_at = datetime .datetime .now () + self ._token_refresh_period
102
99
103
100
104
- def load_incluster_config (refresh_token = True ):
101
+ def load_incluster_config (client_configuration = None , try_refresh_token = True ):
105
102
"""
106
103
Use the service account kubernetes gives to pods to connect to kubernetes
107
104
cluster. It's intended for clients that expect to be running inside a pod
108
105
running on kubernetes. It will raise an exception if called from a process
109
106
not running in a kubernetes environment."""
110
- InClusterConfigLoader (token_filename = SERVICE_TOKEN_FILENAME ,
111
- cert_filename = SERVICE_CERT_FILENAME ).load_and_set (refresh_token = refresh_token )
107
+ loader = InClusterConfigLoader (token_filename = SERVICE_TOKEN_FILENAME ,
108
+ cert_filename = SERVICE_CERT_FILENAME ,
109
+ try_refresh_token = try_refresh_token )
110
+
111
+ if client_configuration is None :
112
+ config = type .__call__ (Configuration )
113
+ loader .load_and_set (config )
114
+ Configuration .set_default (config )
115
+ else :
116
+ loader .load_and_set (client_configuration )
0 commit comments