imageService: Extract common code from MakeImageCache
Both containerd and graphdriver image service use the same code to create the cache - they only supply their own `cacheAdaptor` struct. Extract the shared code to `cache.New`. Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
parent
d66177591e
commit
e8496b1ee4
3 changed files with 35 additions and 53 deletions
|
@ -23,26 +23,7 @@ import (
|
|||
|
||||
// MakeImageCache creates a stateful image cache.
|
||||
func (i *ImageService) MakeImageCache(ctx context.Context, sourceRefs []string) (builder.ImageCache, error) {
|
||||
adaptor := cacheAdaptor{i}
|
||||
if len(sourceRefs) == 0 {
|
||||
return cache.NewLocal(adaptor), nil
|
||||
}
|
||||
|
||||
cache := cache.New(adaptor)
|
||||
|
||||
for _, ref := range sourceRefs {
|
||||
img, err := i.GetImage(ctx, ref, backend.GetImageOpts{})
|
||||
if err != nil {
|
||||
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
|
||||
return nil, err
|
||||
}
|
||||
log.G(ctx).Warnf("Could not look up %s for cache resolution, skipping: %+v", ref, err)
|
||||
continue
|
||||
}
|
||||
cache.Populate(img)
|
||||
}
|
||||
|
||||
return cache, nil
|
||||
return cache.New(ctx, cacheAdaptor{i}, sourceRefs)
|
||||
}
|
||||
|
||||
type cacheAdaptor struct {
|
||||
|
@ -128,6 +109,10 @@ func (c cacheAdaptor) Get(id image.ID) (*image.Image, error) {
|
|||
return outImg, nil
|
||||
}
|
||||
|
||||
func (c cacheAdaptor) GetByRef(ctx context.Context, refOrId string) (*image.Image, error) {
|
||||
return c.is.GetImage(ctx, refOrId, backend.GetImageOpts{})
|
||||
}
|
||||
|
||||
func (c cacheAdaptor) SetParent(target, parent image.ID) error {
|
||||
ctx := context.TODO()
|
||||
_, imgs, err := c.is.resolveAllReferences(ctx, target.String())
|
||||
|
|
|
@ -3,7 +3,6 @@ package images // import "github.com/docker/docker/daemon/images"
|
|||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/containerd/log"
|
||||
|
@ -22,6 +21,10 @@ func (c cacheAdaptor) Get(id image.ID) (*image.Image, error) {
|
|||
return c.is.imageStore.Get(id)
|
||||
}
|
||||
|
||||
func (c cacheAdaptor) GetByRef(ctx context.Context, refOrId string) (*image.Image, error) {
|
||||
return c.is.GetImage(ctx, refOrId, backend.GetImageOpts{})
|
||||
}
|
||||
|
||||
func (c cacheAdaptor) SetParent(target, parent image.ID) error {
|
||||
return c.is.imageStore.SetParent(target, parent)
|
||||
}
|
||||
|
@ -85,24 +88,5 @@ func (c cacheAdaptor) Create(parent *image.Image, image image.Image, _ layer.Dif
|
|||
|
||||
// MakeImageCache creates a stateful image cache.
|
||||
func (i *ImageService) MakeImageCache(ctx context.Context, sourceRefs []string) (builder.ImageCache, error) {
|
||||
adaptor := cacheAdaptor{i}
|
||||
if len(sourceRefs) == 0 {
|
||||
return cache.NewLocal(adaptor), nil
|
||||
}
|
||||
|
||||
cache := cache.New(adaptor)
|
||||
|
||||
for _, ref := range sourceRefs {
|
||||
img, err := i.GetImage(ctx, ref, backend.GetImageOpts{})
|
||||
if err != nil {
|
||||
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
|
||||
return nil, err
|
||||
}
|
||||
log.G(ctx).Warnf("Could not look up %s for cache resolution, skipping: %+v", ref, err)
|
||||
continue
|
||||
}
|
||||
cache.Populate(img)
|
||||
}
|
||||
|
||||
return cache, nil
|
||||
return cache.New(ctx, cacheAdaptor{i}, sourceRefs)
|
||||
}
|
||||
|
|
37
image/cache/cache.go
vendored
37
image/cache/cache.go
vendored
|
@ -8,6 +8,7 @@ import (
|
|||
|
||||
"github.com/containerd/log"
|
||||
containertypes "github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/docker/builder"
|
||||
"github.com/docker/docker/dockerversion"
|
||||
"github.com/docker/docker/image"
|
||||
"github.com/docker/docker/layer"
|
||||
|
@ -17,6 +18,7 @@ import (
|
|||
|
||||
type ImageCacheStore interface {
|
||||
Get(image.ID) (*image.Image, error)
|
||||
GetByRef(ctx context.Context, refOrId string) (*image.Image, error)
|
||||
SetParent(target, parent image.ID) error
|
||||
GetParent(target image.ID) (image.ID, error)
|
||||
Create(parent *image.Image, image image.Image, extraLayer layer.DiffID) (image.ID, error)
|
||||
|
@ -24,11 +26,30 @@ type ImageCacheStore interface {
|
|||
Children(id image.ID) []image.ID
|
||||
}
|
||||
|
||||
// NewLocal returns a local image cache, based on parent chain
|
||||
func NewLocal(store ImageCacheStore) *LocalImageCache {
|
||||
return &LocalImageCache{
|
||||
store: store,
|
||||
func New(ctx context.Context, store ImageCacheStore, cacheFrom []string) (builder.ImageCache, error) {
|
||||
local := &LocalImageCache{store: store}
|
||||
if len(cacheFrom) == 0 {
|
||||
return local, nil
|
||||
}
|
||||
|
||||
cache := &ImageCache{
|
||||
store: store,
|
||||
localImageCache: local,
|
||||
}
|
||||
|
||||
for _, ref := range cacheFrom {
|
||||
img, err := store.GetByRef(ctx, ref)
|
||||
if err != nil {
|
||||
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
|
||||
return nil, err
|
||||
}
|
||||
log.G(ctx).Warnf("Could not look up %s for cache resolution, skipping: %+v", ref, err)
|
||||
continue
|
||||
}
|
||||
cache.Populate(img)
|
||||
}
|
||||
|
||||
return cache, nil
|
||||
}
|
||||
|
||||
// LocalImageCache is cache based on parent chain.
|
||||
|
@ -41,14 +62,6 @@ func (lic *LocalImageCache) GetCache(imgID string, config *containertypes.Config
|
|||
return getImageIDAndError(getLocalCachedImage(lic.store, image.ID(imgID), config, platform))
|
||||
}
|
||||
|
||||
// New returns an image cache, based on history objects
|
||||
func New(store ImageCacheStore) *ImageCache {
|
||||
return &ImageCache{
|
||||
store: store,
|
||||
localImageCache: NewLocal(store),
|
||||
}
|
||||
}
|
||||
|
||||
// ImageCache is cache based on history objects. Requires initial set of images.
|
||||
type ImageCache struct {
|
||||
sources []*image.Image
|
||||
|
|
Loading…
Reference in a new issue