Skip to content
This repository was archived by the owner on Mar 13, 2022. It is now read-only.

Commit d4455d5

Browse files
committed
allow incluster to accept pass-in config
1 parent bf5c599 commit d4455d5

File tree

2 files changed

+40
-39
lines changed

2 files changed

+40
-39
lines changed

Diff for: config/incluster_config.py

+30-25
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,16 @@ def _join_host_port(host, port):
3737
class InClusterConfigLoader(object):
3838

3939
def __init__(self, token_filename,
40-
cert_filename, environ=os.environ):
40+
cert_filename, try_refresh_token, environ=os.environ):
4141
self._token_filename = token_filename
4242
self._cert_filename = cert_filename
4343
self._environ = environ
44+
self._try_refresh_token = try_refresh_token
4445
self._token_refresh_period = datetime.timedelta(minutes=1)
4546

46-
def load_and_set(self, refresh_token=True):
47+
def load_and_set(self, client_configuration):
4748
self._load_config()
48-
self._set_config(refresh_token=refresh_token)
49+
self._set_config(client_configuration)
4950

5051
def _load_config(self):
5152
if (SERVICE_HOST_ENV_NAME not in self._environ or
@@ -75,37 +76,41 @@ def _load_config(self):
7576

7677
self.ssl_ca_cert = self._cert_filename
7778

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:
8585
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
9591

9692
def _read_token_file(self):
9793
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:
10196
raise ConfigException("Token file exists but empty.")
97+
self.token = "bearer " + content
98+
self.token_expires_at = datetime.datetime.now() + self._token_refresh_period
10299

103100

104-
def load_incluster_config(refresh_token=True):
101+
def load_incluster_config(client_configuration=None, try_refresh_token=True):
105102
"""
106103
Use the service account kubernetes gives to pods to connect to kubernetes
107104
cluster. It's intended for clients that expect to be running inside a pod
108105
running on kubernetes. It will raise an exception if called from a process
109106
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)

Diff for: config/incluster_config_test.py

+10-14
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,6 @@ def _create_file_with_temp_content(self, content=""):
5555
os.close(handler)
5656
return name
5757

58-
def _overwrite_file_with_content(self, name, content=""):
59-
handler = os.open(name, os.O_RDWR)
60-
os.truncate(name, 0)
61-
os.write(handler, str.encode(content))
62-
os.close(handler)
63-
6458
def get_test_loader(
6559
self,
6660
token_filename=None,
@@ -73,6 +67,7 @@ def get_test_loader(
7367
return InClusterConfigLoader(
7468
token_filename=token_filename,
7569
cert_filename=cert_filename,
70+
try_refresh_token=True,
7671
environ=environ)
7772

7873
def test_join_host_port(self):
@@ -87,30 +82,31 @@ def test_load_config(self):
8782
loader._load_config()
8883
self.assertEqual("https://" + _TEST_HOST_PORT, loader.host)
8984
self.assertEqual(cert_filename, loader.ssl_ca_cert)
90-
self.assertEqual(_TEST_TOKEN, loader.token)
85+
self.assertEqual('bearer '+_TEST_TOKEN, loader.token)
9186

9287
def test_refresh_token(self):
9388
loader = self.get_test_loader()
94-
loader._token_refresh_period = datetime.timedelta(seconds=5)
95-
loader.load_and_set()
9689
config = Configuration()
90+
loader.load_and_set(config)
9791

9892
self.assertEqual('bearer '+_TEST_TOKEN, config.get_api_key_with_prefix('authorization'))
99-
self.assertEqual(_TEST_TOKEN, loader.token)
93+
self.assertEqual('bearer '+_TEST_TOKEN, loader.token)
10094
self.assertIsNotNone(loader.token_expires_at)
10195

10296
old_token = loader.token
10397
old_token_expires_at = loader.token_expires_at
104-
self._overwrite_file_with_content(loader._token_filename, _TEST_NEW_TOKEN)
105-
time.sleep(5)
98+
loader._token_filename = self._create_file_with_temp_content(_TEST_NEW_TOKEN)
99+
self.assertEqual('bearer '+_TEST_TOKEN, config.get_api_key_with_prefix('authorization'))
106100

101+
loader.token_expires_at = datetime.datetime.now()
107102
self.assertEqual('bearer '+_TEST_NEW_TOKEN, config.get_api_key_with_prefix('authorization'))
108-
self.assertEqual(_TEST_NEW_TOKEN, loader.token)
103+
self.assertEqual('bearer '+_TEST_NEW_TOKEN, loader.token)
109104
self.assertGreater(loader.token_expires_at, old_token_expires_at)
110105

111106
def _should_fail_load(self, config_loader, reason):
112107
try:
113-
config_loader.load_and_set()
108+
config = Configuration()
109+
config_loader.load_and_set(config)
114110
self.fail("Should fail because %s" % reason)
115111
except ConfigException:
116112
# expected

0 commit comments

Comments
 (0)