python icon indicating copy to clipboard operation
python copied to clipboard

Refresh token/api-key periodically

Open aparamon opened this issue 7 years ago • 30 comments

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).

aparamon avatar Jan 30 '19 13:01 aparamon

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 avatar Mar 09 '21 17:03 dinvlad

@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.

houqp avatar Mar 09 '21 23:03 houqp

We have a similiar workaround as well. Works fine for now.

Uznick avatar Mar 10 '21 11:03 Uznick

@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!) 🤔

dinvlad avatar Mar 10 '21 18:03 dinvlad

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 avatar Mar 10 '21 18:03 dinvlad

@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.

houqp avatar Mar 10 '21 19:03 houqp

Hi there! Any updates on this?

alexcristi avatar Apr 27 '21 15:04 alexcristi

is anyone actively working on this issue? if not, my team at Twitter might try to take a stab at it soon.

kwlzn avatar Jul 20 '21 02:07 kwlzn

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/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was 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

k8s-triage-robot avatar Oct 18 '21 02:10 k8s-triage-robot

/remove-lifecycle stale

dinvlad avatar Oct 18 '21 17:10 dinvlad