Repository navigation
feat(image): include image OS version in layer cache key - #11770
Open
srinivasKandukuri wants to merge 1 commit into
Open
srinivasKandukuri wants to merge 1 commit into
srinivasKandukuri wants to merge 1 commit into
Conversation
srinivasKandukuri
requested review from
DmitriyLewen and
knqyf263
as code owners
November 27, 2026 09:22
Add artifact.Option.ImageOSVersion and include it in the blob cache key. Post-handlers can write data derived from the image config into each layer blob, for example a Windows kernel package built from the image's os.version. The layer cache key is calculated from the diffID, analyzer and handler versions, and options, but not from the image config. Two images that share an identical layer but were built on different OS versions then share that layer's blob, and the second image inherits the first image's config-derived data. Setting ImageOSVersion gives such layers a separate cache key per OS version. The field is omitted from the key when empty, so all existing cache keys stay the same.
srinivasKandukuri
force-pushed
the
feat/cache-key-image-os-version
branch
from
November 27, 2026 10:10
f01d29a to
861c848
Compare
This branch has not been deployed
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.
Description
Add
artifact.Option.ImageOSVersionand include it in the layer (blob) cache key. It is opt-in: when the field is empty, every cache key is exactly the same as today.Problem
Post-handlers (
handler.RegisterPostHandlerInit) can write data that comes from the image config into every layer blob. For Windows images, the main example is the OS build: it isn't in any layer's files (unlike/etc/os-releaseon Linux), only in the config'sos.version, for example10.0.20348.5499. A scanner therefore turnsos.versioninto a package to match cumulative updates (KBs) against it.The layer cache key (
cache.CalcKey) is built from the diffID, analyzer and handler versions, and a few options. It doesn't include the image config. So two images that contain the same layer but were built on different Windows builds share one cached blob for that layer. Whichever image is scanned second inherits the first image's build. It's reported as more patched than it is, and applicable KBs are missed.This happens in practice when Windows images are re-based onto the monthly patched base without rebuilding the app layers, for example with
crane rebase,crane append/mutate, or builders that create layers independently of the base. The app layers stay byte-identical (same diffID) whileos.versionchanges.Example with public images
Two images built on Microsoft's nanoserver bases, with the same app layer appended:
os.versiondocker.io/srinivaskand/trivy-windows-shared-layer:5622mcr.microsoft.com/windows/nanoserver:10.0.20348.5622e1f0ac6f…,c1a3d9be…docker.io/srinivaskand/trivy-windows-shared-layer:5499mcr.microsoft.com/windows/nanoserver:10.0.20348.549991307a1f…,c1a3d9be…(The appended layer contains only
app/hello.txt. Built withcrane append --platform windows/amd64 -b <base> -f app.tar.)Both images were scanned into one cache with
pkg/fanal/artifact/image, with a post-handler that writesos.versioninto each layer blob as anos-buildpackage (code below).:5622was scanned first:Without the field, both images get the same key for the shared top layer, so
:5499reads the blob written for:5622. With the field set, that layer gets one key per OS version, and each image gets its own value.Repro code (handler + scan loop)
Change
pkg/fanal/artifact/artifact.go: newImageOSVersion stringfield onOption(json:",omitempty").pkg/cache/key.go:CalcKeyincludes it in the hashed struct (json:",omitempty").pkg/cache/key_test.go:TestCalcKey_ImageOSVersion.Compatibility
TestCalcKeyexpected hashes are unchanged, and the new test checks that an empty value gives exactly the existing "happy path" key.Design notes (alternatives considered)
os.versionis the narrowest value that separates only the cases that matter.Version(): it's a fixedintper handler type and can't hold a per-image value.ArtifactInfofields, applier merging, remote-cache transport, and an image-level hook.configFile.OSVersionfor Windows images (only Windows keys would change, a one-time re-analysis). Happy to add that here or in a follow-up.Testing
go test ./pkg/cache/ ./pkg/fanal/artifact/image/passes.Checklist