Repository navigation
Refresh exec-plugin tokens in the async Kubernetes client - #74535
AnushreeKasturi wants to merge 2 commits into
Conversation
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
|
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
|
seanghaeli
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
|
Thanks for the review, @seanghaeli. You're right: a token without 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. |
|
Thanks. Still need to bump the kubernetes_asyncio version as per #74535 (review), then this should be good to merge. |
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
kubernetespackage installs arefresh_api_key_hookon its configuration.kubernetes_asynciodoes not, so this PR registers an equivalent hook inget_async_kube_client:load_kube_config, so exec-plugin credentials are re-run when they expire.asyncio.Lock, so concurrent requests trigger at most one refresh at a time.verify_sslandssl_ca_certoverrides 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_retriedchecks that a failed refresh raises and that a later request retries the refresh instead of getting stuck.Both new tests fail on
mainand pass with this change.test_kube_client.pyand theexecutors/test suite pass locally.closes: #74306
Was generative AI tooling used to co-author this PR?
Generated-by: AI coding assistant following the guidelines