Skip to content

Refresh exec-plugin tokens in the async Kubernetes client - #74535

Open
AnushreeKasturi wants to merge 2 commits into
apache:mainfrom
AnushreeKasturi:fix/74306-async-exec-token-refresh
Open

AnushreeKasturi wants to merge 2 commits into
apache:mainfrom
AnushreeKasturi:fix/74306-async-exec-token-refresh

Conversation

@AnushreeKasturi

Copy link
Copy Markdown

The async Kubernetes client used for pod creation (async_pod_creation = True) is built once and cached. When the kubeconfig authenticates through an exec credential plugin (e.g. EKS), the token expires, but the client keeps sending the first token, so pod creation fails with 401 until the scheduler restarts.

The sync client is not affected because the kubernetes package installs a refresh_api_key_hook on its configuration. kubernetes_asyncio does not, so this PR registers an equivalent hook in get_async_kube_client:

  • The hook reloads the kubeconfig through the loader returned by load_kube_config, so exec-plugin credentials are re-run when they expire.
  • Reloads are serialized with an asyncio.Lock, so concurrent requests trigger at most one refresh at a time.
  • The client's verify_ssl and ssl_ca_cert overrides are preserved across reloads, since reloading the kubeconfig would otherwise overwrite them.

Tests:

  • test_refreshes_exec_token (parametrized with 1 and 5 concurrent requests) checks that an expired exec token is refreshed on the next request, that concurrent requests share one refresh, and that the TLS overrides survive it.
  • test_refresh_failure_can_be_retried checks that a failed refresh raises and that a later request retries the refresh instead of getting stuck.

Both new tests fail on main and pass with this change. test_kube_client.py and the executors/ test suite pass locally.

closes: #74306


Was generative AI tooling used to co-author this PR?
  • Yes — AI coding assistant

Generated-by: AI coding assistant following the guidelines

The async client used for pod creation is built once and cached. When the
kubeconfig authenticates through an exec credential plugin (e.g. EKS),
its token expires and the client kept sending the first token, so pod
creation failed with 401 until the scheduler restarted.

The sync client gets a refresh_api_key_hook from the kubernetes package.
kubernetes_asyncio does not install one, so register an equivalent hook
that reloads the kubeconfig. Serialize reloads with a lock, and keep the
client's TLS overrides when reloading.

closes: apache#74306
@boring-cyborg

boring-cyborg Bot commented Oct 10, 2026

Copy link
Copy Markdown

Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contributors' Guide
Here are some useful points:

  • Pay attention to the quality of your code (ruff, mypy and type annotations). Our prek-hooks will help you with that.
  • In case of a new feature add useful documentation (in docstrings or in docs/ directory). Adding a new operator? Check this short guide Consider adding an example Dag that shows how users should use it.
  • Consider using Breeze environment for testing locally, it's a heavy docker but it ships with a working Airflow and a lot of integrations.
  • Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
  • Please follow ASF Code of Conduct for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
  • Be sure to read the Airflow Coding style.
  • Always keep your Pull Requests rebased, otherwise your build might fail due to changes not related to your commits.
    Apache Airflow is a community-driven project and together we are making it better 🚀.
    In case of doubts contact the developers at:
    Mailing List: [email protected]
    Slack: https://s.apache.org/airflow-slack

@seanghaeli seanghaeli left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In the case a token does not come with an expiry, we should not be re-minting it on every call. We should treat it as a token with no expiry (as the sync path is already doing). This way we can always keep re-using a cached token.

@seanghaeli seanghaeli left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We will have to raise the minimum version of kubernetes_asyncio to >=33.3.0. This is because the package does not await this hook on earlier versions, and warning is raised with message "coroutine was never awaited", and the refresh does not go through. Tested this is indeed the case on 32.3.2

A token without an expirationTimestamp was treated as expired, so every request
re-ran the exec plugin. Skip the reload in the refresh hook for such tokens, as the
sync client does, and cover it with a test.
@AnushreeKasturi

Copy link
Copy Markdown
Author

Thanks for the review, @seanghaeli. You're right: a token without expirationTimestamp was being re-minted on every call because the loader only records an expiry when one is returned.

The new commit skips the reload in the refresh hook for exec tokens that have no expiry, so the token loaded at startup is reused, matching the sync client. Tokens that do have an expiry still refresh once they expire. I added a test that checks the exec plugin runs once across concurrent requests.

@seanghaeli

Copy link
Copy Markdown
Contributor

Thanks. Still need to bump the kubernetes_asyncio version as per #74535 (review), then this should be good to merge.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:providers provider:cncf-kubernetes Kubernetes (k8s) provider related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

KubernetesExecutor async pod creation never refreshes exec plugin tokens

3 participants