-
Notifications
You must be signed in to change notification settings - Fork 3
feat(build): Cache pulled artifacts #216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: staging
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,132 @@ | ||||||||||||||||||||||||||||||||||||||||||||||
| // SPDX-License-Identifier: BSD-3-Clause | ||||||||||||||||||||||||||||||||||||||||||||||
| // Copyright (c) 2026, Unikraft GmbH and The Unikraft CLI Authors. | ||||||||||||||||||||||||||||||||||||||||||||||
| // Licensed under the BSD-3-Clause License (the "License"). | ||||||||||||||||||||||||||||||||||||||||||||||
| // You may not use this file except in compliance with the License. | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| package imagespec | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||||||||||||||||
| "context" | ||||||||||||||||||||||||||||||||||||||||||||||
| "os" | ||||||||||||||||||||||||||||||||||||||||||||||
| "path/filepath" | ||||||||||||||||||||||||||||||||||||||||||||||
| "sync" | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/containerd/containerd/v2/core/content" | ||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/containerd/containerd/v2/plugins/content/local" | ||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/containerd/errdefs" | ||||||||||||||||||||||||||||||||||||||||||||||
| ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||||||||||||||||||||||||||||||||||||||||||||||
| spec "unikraft.com/x/image-spec" | ||||||||||||||||||||||||||||||||||||||||||||||
| "unikraft.com/x/log" | ||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // WrapCached wraps a File with caching support. If the file has a backing | ||||||||||||||||||||||||||||||||||||||||||||||
| // provider and descriptor, it returns a new ContentStoreFile that uses a | ||||||||||||||||||||||||||||||||||||||||||||||
| // pull-through cache at ~/.cache/unikraft. Otherwise, returns the original file. | ||||||||||||||||||||||||||||||||||||||||||||||
| func WrapCached(ctx context.Context, file spec.File) spec.File { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, we should try and only cache if it's fetched from a remote registry. If it's a local content store, then probably not. |
||||||||||||||||||||||||||||||||||||||||||||||
| if file == nil { | ||||||||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| desc, provider := file.Source() | ||||||||||||||||||||||||||||||||||||||||||||||
| if desc.Digest == "" || provider == nil { | ||||||||||||||||||||||||||||||||||||||||||||||
| return file | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| store, err := cacheStore() | ||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||
| log.G(ctx).Debug().Err(err).Msg("failed to initialize content cache") | ||||||||||||||||||||||||||||||||||||||||||||||
| return file | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| return spec.NewContentStoreFile( | ||||||||||||||||||||||||||||||||||||||||||||||
| pullThroughProvider{cache: store, upstream: provider}, | ||||||||||||||||||||||||||||||||||||||||||||||
| desc, | ||||||||||||||||||||||||||||||||||||||||||||||
| file.Path(), | ||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| type pullThroughProvider struct { | ||||||||||||||||||||||||||||||||||||||||||||||
| cache content.Store | ||||||||||||||||||||||||||||||||||||||||||||||
| upstream content.Provider | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| func (p pullThroughProvider) ReaderAt(ctx context.Context, desc ocispec.Descriptor) (content.ReaderAt, error) { | ||||||||||||||||||||||||||||||||||||||||||||||
| if p.cache != nil { | ||||||||||||||||||||||||||||||||||||||||||||||
| ra, err := p.cache.ReaderAt(ctx, desc) | ||||||||||||||||||||||||||||||||||||||||||||||
| if err == nil { | ||||||||||||||||||||||||||||||||||||||||||||||
| log.G(ctx).Debug(). | ||||||||||||||||||||||||||||||||||||||||||||||
| Str("digest", desc.Digest.String()). | ||||||||||||||||||||||||||||||||||||||||||||||
| Msg("content cache hit") | ||||||||||||||||||||||||||||||||||||||||||||||
| return ra, nil | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| if !errdefs.IsNotFound(err) { | ||||||||||||||||||||||||||||||||||||||||||||||
| log.G(ctx).Debug(). | ||||||||||||||||||||||||||||||||||||||||||||||
| Err(err). | ||||||||||||||||||||||||||||||||||||||||||||||
| Str("digest", desc.Digest.String()). | ||||||||||||||||||||||||||||||||||||||||||||||
| Msg("content cache read failed") | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| ra, err := p.upstream.ReaderAt(ctx, desc) | ||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||
| return nil, err | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| if p.cache != nil { | ||||||||||||||||||||||||||||||||||||||||||||||
| if err := cacheBlob(ctx, p.cache, ra, desc); err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||
| log.G(ctx).Debug(). | ||||||||||||||||||||||||||||||||||||||||||||||
| Err(err). | ||||||||||||||||||||||||||||||||||||||||||||||
| Str("digest", desc.Digest.String()). | ||||||||||||||||||||||||||||||||||||||||||||||
| Msg("failed to write content to cache") | ||||||||||||||||||||||||||||||||||||||||||||||
| return ra, nil | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| // Close the upstream reader and return from the cache instead | ||||||||||||||||||||||||||||||||||||||||||||||
| ra.Close() | ||||||||||||||||||||||||||||||||||||||||||||||
| return p.cache.ReaderAt(ctx, desc) | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+83
to
+85
|
||||||||||||||||||||||||||||||||||||||||||||||
| // Close the upstream reader and return from the cache instead | |
| ra.Close() | |
| return p.cache.ReaderAt(ctx, desc) | |
| // Try to open a new reader from the cache after writing. | |
| cachedRA, err := p.cache.ReaderAt(ctx, desc) | |
| if err == nil { | |
| // Successfully opened from cache: close upstream and return cached reader. | |
| ra.Close() | |
| log.G(ctx).Debug(). | |
| Str("digest", desc.Digest.String()). | |
| Msg("content cache read after write") | |
| return cachedRA, nil | |
| } | |
| // If the cache read after write fails for any reason, fall back to the | |
| // still-open upstream reader instead of returning an error. | |
| log.G(ctx).Debug(). | |
| Err(err). | |
| Str("digest", desc.Digest.String()). | |
| Msg("content cache read after write failed; using upstream reader") | |
| return ra, nil |
Copilot
AI
Mar 20, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cacheBlob returns nil even when it decides it cannot write anything (e.g., missing/unknown size). Callers currently interpret nil as "cached successfully". Consider returning a sentinel error or an additional boolean indicating whether a blob was actually written, so callers can safely decide whether to switch to the cache.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it okay to have no automatic cache cleanup? Probably not.