-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_history.go
161 lines (135 loc) · 3.81 KB
/
image_history.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package containerd
import (
"context"
containerdimages "github.com/containerd/containerd/images"
"github.com/containerd/containerd/platforms"
"github.com/containerd/log"
"github.com/distribution/reference"
imagetype "github.com/docker/docker/api/types/image"
"github.com/opencontainers/go-digest"
"github.com/opencontainers/image-spec/identity"
"github.com/pkg/errors"
)
// ImageHistory returns a slice of HistoryResponseItem structures for the
// specified image name by walking the image lineage.
func (i *ImageService) ImageHistory(ctx context.Context, name string) ([]*imagetype.HistoryResponseItem, error) {
img, err := i.resolveImage(ctx, name)
if err != nil {
return nil, err
}
// TODO: pass platform in from the CLI
platform := matchAllWithPreference(platforms.Default())
presentImages, err := i.presentImages(ctx, img, name, platform)
if err != nil {
return nil, err
}
ociimage := presentImages[0]
var (
history []*imagetype.HistoryResponseItem
sizes []int64
)
s := i.client.SnapshotService(i.snapshotter)
diffIDs := ociimage.RootFS.DiffIDs
for i := range diffIDs {
chainID := identity.ChainID(diffIDs[0 : i+1]).String()
use, err := s.Usage(ctx, chainID)
if err != nil {
return nil, err
}
sizes = append(sizes, use.Size)
}
for _, h := range ociimage.History {
size := int64(0)
if !h.EmptyLayer {
if len(sizes) == 0 {
return nil, errors.New("unable to find the size of the layer")
}
size = sizes[0]
sizes = sizes[1:]
}
var created int64
if h.Created != nil {
created = h.Created.Unix()
}
history = append([]*imagetype.HistoryResponseItem{{
ID: "<missing>",
Comment: h.Comment,
CreatedBy: h.CreatedBy,
Created: created,
Size: size,
Tags: nil,
}}, history...)
}
findParents := func(img containerdimages.Image) []containerdimages.Image {
imgs, err := i.getParentsByBuilderLabel(ctx, img)
if err != nil {
log.G(ctx).WithFields(log.Fields{
"error": err,
"image": img,
}).Warn("failed to list parent images")
return nil
}
return imgs
}
is := i.client.ImageService()
currentImg := img
for _, h := range history {
dgst := currentImg.Target.Digest.String()
h.ID = dgst
imgs, err := is.List(ctx, "target.digest=="+dgst)
if err != nil {
return nil, err
}
tags := getImageTags(ctx, imgs)
h.Tags = append(h.Tags, tags...)
parents := findParents(currentImg)
foundNext := false
for _, img := range parents {
_, hasLabel := img.Labels[imageLabelClassicBuilderParent]
if !foundNext || hasLabel {
currentImg = img
foundNext = true
}
}
if !foundNext {
break
}
}
return history, nil
}
func getImageTags(ctx context.Context, imgs []containerdimages.Image) []string {
var tags []string
for _, img := range imgs {
if isDanglingImage(img) {
continue
}
name, err := reference.ParseNamed(img.Name)
if err != nil {
log.G(ctx).WithFields(log.Fields{
"name": name,
"error": err,
}).Warn("image with a name that's not a valid named reference")
continue
}
tags = append(tags, reference.FamiliarString(name))
}
return tags
}
// getParentsByBuilderLabel finds images that were a base for the given image
// by an image label set by the legacy builder.
// NOTE: This only works for images built with legacy builder (not Buildkit).
func (i *ImageService) getParentsByBuilderLabel(ctx context.Context, img containerdimages.Image) ([]containerdimages.Image, error) {
parent, ok := img.Labels[imageLabelClassicBuilderParent]
if !ok || parent == "" {
return nil, nil
}
dgst, err := digest.Parse(parent)
if err != nil {
log.G(ctx).WithFields(log.Fields{
"error": err,
"value": parent,
}).Warnf("invalid %s label value", imageLabelClassicBuilderParent)
return nil, nil
}
return i.client.ImageService().List(ctx, "target.digest=="+dgst.String())
}