github.com/moby/docker@v26.1.3+incompatible/daemon/images/cache.go (about) 1 package images // import "github.com/docker/docker/daemon/images" 2 3 import ( 4 "context" 5 "encoding/json" 6 "fmt" 7 8 "github.com/containerd/log" 9 "github.com/docker/docker/api/types/backend" 10 "github.com/docker/docker/builder" 11 "github.com/docker/docker/image" 12 "github.com/docker/docker/image/cache" 13 "github.com/docker/docker/layer" 14 ) 15 16 type cacheAdaptor struct { 17 is *ImageService 18 } 19 20 func (c cacheAdaptor) Get(id image.ID) (*image.Image, error) { 21 return c.is.imageStore.Get(id) 22 } 23 24 func (c cacheAdaptor) GetByRef(ctx context.Context, refOrId string) (*image.Image, error) { 25 return c.is.GetImage(ctx, refOrId, backend.GetImageOpts{}) 26 } 27 28 func (c cacheAdaptor) SetParent(target, parent image.ID) error { 29 return c.is.imageStore.SetParent(target, parent) 30 } 31 32 func (c cacheAdaptor) GetParent(target image.ID) (image.ID, error) { 33 return c.is.imageStore.GetParent(target) 34 } 35 36 func (c cacheAdaptor) IsBuiltLocally(target image.ID) (bool, error) { 37 return c.is.imageStore.IsBuiltLocally(target) 38 } 39 40 func (c cacheAdaptor) Children(imgID image.ID) []image.ID { 41 // Not FROM scratch 42 if imgID != "" { 43 return c.is.imageStore.Children(imgID) 44 } 45 images := c.is.imageStore.Map() 46 47 var siblings []image.ID 48 for id, img := range images { 49 if img.Parent != "" { 50 continue 51 } 52 53 builtLocally, err := c.is.imageStore.IsBuiltLocally(id) 54 if err != nil { 55 log.G(context.TODO()).WithFields(log.Fields{ 56 "error": err, 57 "id": id, 58 }).Warn("failed to check if image was built locally") 59 continue 60 } 61 if !builtLocally { 62 continue 63 } 64 65 siblings = append(siblings, id) 66 } 67 return siblings 68 } 69 70 func (c cacheAdaptor) Create(parent *image.Image, image image.Image, _ layer.DiffID) (image.ID, error) { 71 data, err := json.Marshal(image) 72 if err != nil { 73 return "", fmt.Errorf("failed to marshal image config: %w", err) 74 } 75 imgID, err := c.is.imageStore.Create(data) 76 if err != nil { 77 return "", err 78 } 79 80 if parent != nil { 81 if err := c.is.imageStore.SetParent(imgID, parent.ID()); err != nil { 82 return "", fmt.Errorf("failed to set parent for %v to %v: %w", imgID, parent.ID(), err) 83 } 84 } 85 86 return imgID, err 87 } 88 89 // MakeImageCache creates a stateful image cache. 90 func (i *ImageService) MakeImageCache(ctx context.Context, sourceRefs []string) (builder.ImageCache, error) { 91 return cache.New(ctx, cacheAdaptor{i}, sourceRefs) 92 }