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

Commit 7978ef8

Browse files
author
Jake Sanders
committed
Refresh GCP tokens if <55 mins of life left
Signed-off-by: Jake Sanders <[email protected]>
1 parent 78472de commit 7978ef8

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

config/kube_config.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
from .config_exception import ConfigException
3333
from .dateutil import UTC, format_rfc3339, parse_rfc3339
3434

35-
EXPIRY_SKEW_PREVENTION_DELAY = datetime.timedelta(minutes=5)
35+
EXPIRY_TIME_SKEW = datetime.timedelta(minutes=5)
36+
MINIMUM_GCP_TOKEN_TIME_REMAINING = datetime.timedelta(minutes=55)
3637
KUBE_CONFIG_DEFAULT_LOCATION = os.environ.get('KUBECONFIG', '~/.kube/config')
3738
_temp_files = {}
3839

@@ -62,8 +63,16 @@ def _create_temp_file_with_content(content):
6263
return name
6364

6465

66+
def _is_stale(expiry):
67+
return _has_min_lifespan(expiry, MINIMUM_GCP_TOKEN_TIME_REMAINING)
68+
69+
6570
def _is_expired(expiry):
66-
return ((parse_rfc3339(expiry) - EXPIRY_SKEW_PREVENTION_DELAY) <=
71+
return _has_min_lifespan(expiry, EXPIRY_TIME_SKEW)
72+
73+
74+
def _has_min_lifespan(expiry, min_lifespan):
75+
return ((parse_rfc3339(expiry) - min_lifespan) <=
6776
datetime.datetime.utcnow().replace(tzinfo=UTC))
6877

6978

@@ -198,7 +207,7 @@ def _load_gcp_token(self):
198207
if (('config' not in provider) or
199208
('access-token' not in provider['config']) or
200209
('expiry' in provider['config'] and
201-
_is_expired(provider['config']['expiry']))):
210+
_is_stale(provider['config']['expiry']))):
202211
# token is not available or expired, refresh it
203212
self._refresh_gcp_token()
204213

0 commit comments

Comments
 (0)