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

refresh GCP tokens if <55 mins of life left #72

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions config/kube_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
from .config_exception import ConfigException
from .dateutil import UTC, format_rfc3339, parse_rfc3339

EXPIRY_SKEW_PREVENTION_DELAY = datetime.timedelta(minutes=5)
EXPIRY_TIME_SKEW = datetime.timedelta(minutes=5)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: EXPIRY_SKEW_PREVENTION_DELAY sounds more clear to me in explaining the purpose of the timedelta. Maybe document these constants and update

# should be less than kube_config.EXPIRY_SKEW_PREVENTION_DELAY
if we want to change the name.

MINIMUM_GCP_TOKEN_TIME_REMAINING = datetime.timedelta(minutes=55)
KUBE_CONFIG_DEFAULT_LOCATION = os.environ.get('KUBECONFIG', '~/.kube/config')
_temp_files = {}

Expand Down Expand Up @@ -62,8 +63,16 @@ def _create_temp_file_with_content(content):
return name


def _is_stale(expiry):
return _has_min_lifespan(expiry, MINIMUM_GCP_TOKEN_TIME_REMAINING)


def _is_expired(expiry):
return ((parse_rfc3339(expiry) - EXPIRY_SKEW_PREVENTION_DELAY) <=
return _has_min_lifespan(expiry, EXPIRY_TIME_SKEW)


def _has_min_lifespan(expiry, min_lifespan):
return ((parse_rfc3339(expiry) - min_lifespan) <=
datetime.datetime.utcnow().replace(tzinfo=UTC))


Expand Down Expand Up @@ -198,7 +207,7 @@ def _load_gcp_token(self):
if (('config' not in provider) or
('access-token' not in provider['config']) or
('expiry' in provider['config'] and
_is_expired(provider['config']['expiry']))):
_is_stale(provider['config']['expiry']))):
# token is not available or expired, refresh it
self._refresh_gcp_token()

Expand Down