Support automatic service account token refresh for in-cluster configurations#141
Merged
Conversation
Extend the currently available token auth mechanism which already reads a token from file in the from_service_account function by a mechanism which re-reads the token in case the token is expired (HTTP 401). Extending existing unit tests to also cover the new functionality.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When running lightkube inside a Kubernetes pod, the service account token is mounted as a projected volume at
/var/run/secrets/kubernetes.io/serviceaccount/token.Starting with Kubernetes 1.22+, these tokens are bound and have a limited lifetime (1h by default, configurable via
--service-account-max-token-expiration).Previously,
from_service_account()read the token file once at startup and stored it as a static string. When the token expired, all subsequent API calls failed with HTTP 401 — requiring a full client restart to recover. This is a problem when lightkube is for example used in a long running Kubernetes operator.This is a well-known problem that Go's client-go solves via its
fileTokenSource/cachingTokenSource/tokenSourceTransportmechanism, which re-reads the token file and retries on 401.Solution
This PR adds a
BearerTokenFileAuthclass that follows in general the same pattern:Backward compatibility
Fully backward compatible:
user.tokenis still populated byfrom_service_account(), so existing code that readsconfig.get().user.tokencontinues to work.token_fileisNone), behavior is completely unchanged — the existingBearerAuthwith static tokens is used.token_fileis only set programmatically byfrom_service_account(); it does not interfere with YAML kubeconfig parsing.I also tried to adapt the documentation accordingly. If I missed something or changes are necessary from your pov, please let me know and I will adapt the PR.