Refresh token/api-key periodically
Currently, authorization token/api-key is only initialized on loading config: https://github.com/kubernetes-client/python-base/blob/bd9a8525e9215f7f01c32a321beb9a605cf0402b/config/kube_config.py#L420 https://github.com/kubernetes-client/python-base/blob/bd9a8525e9215f7f01c32a321beb9a605cf0402b/config/kube_config.py#L510 When working with Amazon EKS via aws-iam-authenticator though, token/api-key expires relatively quickly.
It is proposed to introduce a configurable option to specify token/api-key time-to-live. On API call the time should be checked, and if expired the token/api-key should be refreshed by calling https://github.com/kubernetes-client/python-base/blob/bd9a8525e9215f7f01c32a321beb9a605cf0402b/config/kube_config.py#L350 again.
Alternatively, API client could check for 401 Unauthorized return code and refresh token (at most once per API call).
This issue has been affecting us quite a lot - @roycaihw could please assign someone to look further into it? We just need an approximate timeline on what it could be addressed, so we can plan ahead if we need to come up with a workaround in the near term or just to wait on the fix (and others, please chime in if you have a workaround already!)
@dinvlad i have pushed my workaround into Airflow at https://github.com/apache/airflow/blob/master/airflow/kubernetes/kube_client.py. We have been running Airflow with this custom client in production for more than a year now without any issue.
We have a similiar workaround as well. Works fine for now.
@houqp by looking at your code, it uses the default load_incluster_config() when running inside the cluster. Does it mean you only experience this issue with the default load_kube_config()? We're seeing it even with load_incluster_config(), if I'm reading that right.
Or maybe our "hanging" issue is actually not due to expired creds, but instead because of the TCP keepalive problem you mentioned there as well (thanks for indirectly bringing this to my attention!) 🤔
To more specifically expand on our issue (which might be slightly different from others' use of config loader here), we use load_incluster_config() for in-cluster cleanup operation, which in our case is the following pseudo-code:
from kubernetes.watch import Watch
def cleanup(namespace: str):
"""
Watches for and deletes terminated Jobs.
This function would not normally be needed if `ttlSecondsAfterFinished` worked.
However, that feature is currently hidden behind an `alpha` flag in GKE.
"""
load_incluster_config()
api = BatchV1Api()
events = Watch().stream(api.list_namespaced_job, namespace)
for event in events:
# cleanup logic
So our issue (if I'm not mistaken) is that no matter how credentials are fetched (either in-cluster or not), they seem to be "cached" in the config, and then stream() generator expires. I wonder if there's a more general way to implement a config client that refreshes credentials automatically.
Please correct me if that's what's already happening in load_incluster_config(), and our issue (watcher stops working after a while) is caused by something else (like the keepalive problem etc.)
@dinvlad my fix was only for out of cluster communication where auth goes through iam-authenticator. IIRC, in cluster communication uses builtin K8S auth, which doesn't have this problem.
Hi there! Any updates on this?
is anyone actively working on this issue? if not, my team at Twitter might try to take a stab at it soon.
The Kubernetes project currently lacks enough contributors to adequately respond to all issues and PRs.
This bot triages issues and PRs according to the following rules:
- After 90d of inactivity,
lifecycle/staleis applied - After 30d of inactivity since
lifecycle/stalewas applied,lifecycle/rottenis applied - After 30d of inactivity since
lifecycle/rottenwas applied, the issue is closed
You can:
- Mark this issue or PR as fresh with
/remove-lifecycle stale - Mark this issue or PR as rotten with
/lifecycle rotten - Close this issue or PR with
/close - Offer to help out with Issue Triage
Please send feedback to sig-contributor-experience at kubernetes/community.
/lifecycle stale
/remove-lifecycle stale