13
13
# limitations under the License.
14
14
15
15
import os
16
+ import datetime
16
17
17
18
from kubernetes .client import Configuration
18
19
@@ -40,10 +41,11 @@ def __init__(self, token_filename,
40
41
self ._token_filename = token_filename
41
42
self ._cert_filename = cert_filename
42
43
self ._environ = environ
44
+ self ._token_refresh_period = datetime .timedelta (minutes = 5 )
43
45
44
- def load_and_set (self ):
46
+ def load_and_set (self , refresh_token = True ):
45
47
self ._load_config ()
46
- self ._set_config ()
48
+ self ._set_config (refresh_token = refresh_token )
47
49
48
50
def _load_config (self ):
49
51
if (SERVICE_HOST_ENV_NAME not in self ._environ or
@@ -61,10 +63,7 @@ def _load_config(self):
61
63
if not os .path .isfile (self ._token_filename ):
62
64
raise ConfigException ("Service token file does not exists." )
63
65
64
- with open (self ._token_filename ) as f :
65
- self .token = f .read ()
66
- if not self .token :
67
- raise ConfigException ("Token file exists but empty." )
66
+ self ._read_token_file ()
68
67
69
68
if not os .path .isfile (self ._cert_filename ):
70
69
raise ConfigException (
@@ -76,19 +75,38 @@ def _load_config(self):
76
75
77
76
self .ssl_ca_cert = self ._cert_filename
78
77
79
- def _set_config (self ):
78
+ def _set_config (self , refresh_token ):
80
79
configuration = Configuration ()
81
80
configuration .host = self .host
82
81
configuration .ssl_ca_cert = self .ssl_ca_cert
83
82
configuration .api_key ['authorization' ] = "bearer " + self .token
84
83
Configuration .set_default (configuration )
84
+ if not refresh_token :
85
+ return
86
+ self .token_expires_at = datetime .datetime .now () + self ._token_refresh_period
87
+ def wrap (f ):
88
+ in_cluster_config = self
89
+ def wrapped (self , identifier ):
90
+ if identifier == 'authorization' and in_cluster_config .token_expires_at <= datetime .datetime .now ():
91
+ in_cluster_config ._read_token_file ()
92
+ in_cluster_config .token_expires_at = datetime .datetime .now () + in_cluster_config ._token_refresh_period
93
+ self .api_key [identifier ] = "bearer " + in_cluster_config .token
94
+ return f (self , identifier )
95
+ return wrapped
96
+ Configuration .get_api_key_with_prefix = wrap (Configuration .get_api_key_with_prefix )
97
+
98
+ def _read_token_file (self ):
99
+ with open (self ._token_filename ) as f :
100
+ self .token = f .read ()
101
+ if not self .token :
102
+ raise ConfigException ("Token file exists but empty." )
85
103
86
104
87
- def load_incluster_config ():
105
+ def load_incluster_config (refresh_token = True ):
88
106
"""
89
107
Use the service account kubernetes gives to pods to connect to kubernetes
90
108
cluster. It's intended for clients that expect to be running inside a pod
91
109
running on kubernetes. It will raise an exception if called from a process
92
110
not running in a kubernetes environment."""
93
111
InClusterConfigLoader (token_filename = SERVICE_TOKEN_FILENAME ,
94
- cert_filename = SERVICE_CERT_FILENAME ).load_and_set ()
112
+ cert_filename = SERVICE_CERT_FILENAME ).load_and_set (refresh_token = refresh_token )
0 commit comments