-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
92 lines (76 loc) · 2.27 KB
/
cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package images // import "github.com/docker/docker/daemon/images"
import (
"context"
"encoding/json"
"fmt"
"github.com/containerd/log"
"github.com/docker/docker/api/types/backend"
"github.com/docker/docker/builder"
"github.com/docker/docker/image"
"github.com/docker/docker/image/cache"
"github.com/docker/docker/layer"
)
type cacheAdaptor struct {
is *ImageService
}
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)
}
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
}
// MakeImageCache creates a stateful image cache.
func (i *ImageService) MakeImageCache(ctx context.Context, sourceRefs []string) (builder.ImageCache, error) {
return cache.New(ctx, cacheAdaptor{i}, sourceRefs)
}