[k8s] Fix logical race conditions in kubernetes_secrets provider #6623
+1,475
−755
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.
What does this PR do?
This PR refactors the implementation to eliminate logical race conditions of the
kubernetes_secrets
provider alongside with brand new unit-tests.Initially, the issue seemed to stem from misuse or lack of synchronisation primitives, but after deeper analysis, it became evident that the "race" conditions were logical rather than concurrency-related. The existing implementation was structured in a way that led to inconsistencies due to overlapping responsibilities of different actors managing the secret lifecycle.
To address this, I restructured the logic while keeping in mind the constraints of the existing provider, specifically:
With this in mind, the provider behaviour is now as follows:
Cache Disabled Mode:
Cache Enabled Mode:
Cache Actor Responsibilities:
lastAccess
:lastAccess
to prevent premature expiration and give the fetch actor time to pick up the new value.lastAccess
and let the entry "age" as it should.Fetch Actor Responsibilities:
lastAccess
when an entry is accessed to prevent unintended expiration.Considerations:
ExpirationCache
fromk8s.io/client-go/tools/cache
does not suit our needs, as it lacks the aforementioned conditional insertion required to handle these interactions correctly.PS: as the main changes of this PR are captured by the commit a549728, I consider this PR to be aligned with the Pull Requests policy
Why is it important?
This refactor significantly improves the correctness of the
kubernetes_secrets
provider by ensuring:Checklist
./changelog/fragments
using the change-log toolDisruptive User Impact
This change does not introduce breaking changes but ensures that the
kubernetes_secrets
provider operates correctly in cache-enabled mode. Users relying on cache behaviour may notice improved stability in secret retrieval.How to test this PR locally
go test ./internal/pkg/composable/providers/kubernetessecrets/...
Related issues