2018-02-07 20:52:47 +00:00
|
|
|
package images // import "github.com/docker/docker/daemon/images"
|
2016-09-22 21:38:00 +00:00
|
|
|
|
|
|
|
import (
|
2022-08-09 12:42:50 +00:00
|
|
|
"context"
|
2024-01-09 17:00:26 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-08-09 12:42:50 +00:00
|
|
|
|
2023-09-13 15:41:45 +00:00
|
|
|
"github.com/containerd/log"
|
2024-01-20 15:01:43 +00:00
|
|
|
"github.com/docker/docker/api/types/backend"
|
2016-09-22 21:38:00 +00:00
|
|
|
"github.com/docker/docker/builder"
|
2024-01-09 17:00:26 +00:00
|
|
|
"github.com/docker/docker/image"
|
2017-01-03 18:19:27 +00:00
|
|
|
"github.com/docker/docker/image/cache"
|
2024-01-09 17:00:26 +00:00
|
|
|
"github.com/docker/docker/layer"
|
2016-09-22 21:38:00 +00:00
|
|
|
)
|
|
|
|
|
2024-01-09 17:00:26 +00:00
|
|
|
type cacheAdaptor struct {
|
|
|
|
is *ImageService
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c cacheAdaptor) Get(id image.ID) (*image.Image, error) {
|
|
|
|
return c.is.imageStore.Get(id)
|
|
|
|
}
|
|
|
|
|
2024-03-07 14:39:42 +00:00
|
|
|
func (c cacheAdaptor) GetByRef(ctx context.Context, refOrId string) (*image.Image, error) {
|
|
|
|
return c.is.GetImage(ctx, refOrId, backend.GetImageOpts{})
|
|
|
|
}
|
|
|
|
|
2024-01-09 17:00:26 +00:00
|
|
|
func (c cacheAdaptor) SetParent(target, parent image.ID) error {
|
|
|
|
return c.is.imageStore.SetParent(target, parent)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c cacheAdaptor) GetParent(target image.ID) (image.ID, error) {
|
|
|
|
return c.is.imageStore.GetParent(target)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c cacheAdaptor) IsBuiltLocally(target image.ID) (bool, error) {
|
|
|
|
return c.is.imageStore.IsBuiltLocally(target)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c cacheAdaptor) Children(imgID image.ID) []image.ID {
|
|
|
|
// Not FROM scratch
|
|
|
|
if imgID != "" {
|
|
|
|
return c.is.imageStore.Children(imgID)
|
|
|
|
}
|
|
|
|
images := c.is.imageStore.Map()
|
|
|
|
|
|
|
|
var siblings []image.ID
|
|
|
|
for id, img := range images {
|
|
|
|
if img.Parent != "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
builtLocally, err := c.is.imageStore.IsBuiltLocally(id)
|
|
|
|
if err != nil {
|
|
|
|
log.G(context.TODO()).WithFields(log.Fields{
|
|
|
|
"error": err,
|
|
|
|
"id": id,
|
|
|
|
}).Warn("failed to check if image was built locally")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !builtLocally {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
siblings = append(siblings, id)
|
|
|
|
}
|
|
|
|
return siblings
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c cacheAdaptor) Create(parent *image.Image, image image.Image, _ layer.DiffID) (image.ID, error) {
|
|
|
|
data, err := json.Marshal(image)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to marshal image config: %w", err)
|
|
|
|
}
|
|
|
|
imgID, err := c.is.imageStore.Create(data)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if parent != nil {
|
|
|
|
if err := c.is.imageStore.SetParent(imgID, parent.ID()); err != nil {
|
|
|
|
return "", fmt.Errorf("failed to set parent for %v to %v: %w", imgID, parent.ID(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return imgID, err
|
|
|
|
}
|
|
|
|
|
2016-09-22 21:38:00 +00:00
|
|
|
// MakeImageCache creates a stateful image cache.
|
2022-10-26 16:13:17 +00:00
|
|
|
func (i *ImageService) MakeImageCache(ctx context.Context, sourceRefs []string) (builder.ImageCache, error) {
|
2024-03-07 14:39:42 +00:00
|
|
|
return cache.New(ctx, cacheAdaptor{i}, sourceRefs)
|
2016-09-22 21:38:00 +00:00
|
|
|
}
|