feat: add in-memory LRU+TTL cache for validation results#1933
feat: add in-memory LRU+TTL cache for validation results#1933nissessenap wants to merge 5 commits intosigstore:mainfrom
Conversation
|
I need to perform some validation, of this before changing over from draft. I will also create a PR that builds on this which implements some basic metrics make the cach visaiable, but that is a sperate PR since it's the first time we use custom metrics in this repo, but I wanted to showcase that I was working on the issues with this PR at least. |
3038a32 to
8013c95
Compare
Add tests for the upcoming LRU+TTL cache implementation (sigstore#647). Unit tests cover set/get, TTL expiry, eviction, key isolation, error skipping, and resource version invalidation. Integration tests verify ValidatePolicy cache hit/miss behavior. All tests currently fail to compile (NewLRUCache undefined), confirming the TDD Red phase. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Signed-off-by: Edvin Norling <edvin.norling@kognic.com>
Add LRUCache implementing ResultCache using hashicorp/golang-lru/v2 expirable. Only successful validations (PolicyResult non-nil) are cached; failed validations are skipped to allow immediate retries. Fix cache key mismatch bug: ref.Name() in Set vs ref.String() in Get caused cache to never hit. Both now use ref.String(). Move cache Set into ValidatePolicy so caching is self-contained regardless of call path. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Signed-off-by: Edvin Norling <edvin.norling@kognic.com>
Wire the LRU+TTL cache into the validating webhook via --enable-cache, --cache-size, and --cache-ttl flags. Cache is off by default and only injected into the validating admission controller context. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Signed-off-by: Edvin Norling <edvin.norling@kognic.com>
- Copy CacheResult.Errors slice in LRUCache.Set to prevent callers from mutating cached entries through the shared backing array - Update copyright year to 2026 on new files (lrucache.go, lrucache_test.go) - Extract cacheTestFixtures helper to reduce boilerplate in cache integration tests Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Signed-off-by: Edvin Norling <edvin.norling@kognic.com>
Move cache observability into LRUCache.Get() so the implementation
owns its own logging
Signed-off-by: Edvin Norling <edvin.norling@kognic.com>
70790b3 to
e870c6c
Compare
e870c6c to
85c8ec6
Compare
|
I have ran this locally and it seams to works as intended. I removed third_party/VENDOR-LICENSE/github.com/hashicorp/golang-lru in one commit, but saw on my local PR that it made a CI fail. I hope you think this is a sufisant start to get this PR merged and we can build uppon it to create two seperate PR
|
Summary
ResultCacheinterface with an LRU+TTL cache usinghashicorp/golang-lru/v2/expirable, opt-in via--enable-cacheCLI flagref.Name()inSetvsref.String()inGetcaused the cache to never hit — both now useref.String()cache.Setcall fromvalidatePoliciesintoValidatePolicyso caching is self-contained regardless of call path (previously it was in the goroutine caller, usingref.Name()which strips the digest)Closes #647, closes #1887
Details
New files:
pkg/webhook/lrucache.go—LRUCachestruct wrappingexpirable.LRU[string, *CacheResult]pkg/webhook/lrucache_test.go— 8 unit tests (set/get, miss, skip errors, partial success, TTL expiry, eviction, key isolation, resourceVersion invalidation)Modified files:
pkg/webhook/validator.go— Fixref.Name()→ref.String()bug, movecache.SetintoValidatePolicy(was invalidatePoliciesgoroutine)pkg/webhook/validator_test.go— 4 integration tests (cache hit, skip errors, partial success, no-cache default)cmd/webhook/main.go— Add--enable-cache,--cache-size,--cache-ttlflags; inject cache into validating webhook contextgo.mod— Promotehashicorp/golang-lru/v2from indirect to direct dependencyCLI flags (all opt-in, cache disabled by default):
--enable-cachefalse--cache-size1024--cache-ttl1hTest plan
go test ./pkg/webhook/... -run TestLRUCachego test ./pkg/webhook/... -run TestValidatePolicyCachego test $(go list ./... | grep -v third_party/)go build ./cmd/webhook/...--enable-cache,--cache-size,--cache-ttl--enable-cacheand verify repeated admissions use cache--enable-cachebehavior is identical to before🤖 Generated with Claude Code