-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
94 lines (83 loc) · 2.67 KB
/
image.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
package daemon
import (
"fmt"
"runtime"
"github.com/docker/distribution/reference"
"github.com/docker/docker/image"
"github.com/docker/docker/pkg/stringid"
)
// errImageDoesNotExist is error returned when no image can be found for a reference.
type errImageDoesNotExist struct {
ref reference.Reference
}
func (e errImageDoesNotExist) Error() string {
ref := e.ref
if named, ok := ref.(reference.Named); ok {
ref = reference.TagNameOnly(named)
}
return fmt.Sprintf("No such image: %s", reference.FamiliarString(ref))
}
func (e errImageDoesNotExist) NotFound() {}
// GetImageIDAndPlatform returns an image ID and platform corresponding to the image referred to by
// refOrID.
func (daemon *Daemon) GetImageIDAndPlatform(refOrID string) (image.ID, string, error) {
ref, err := reference.ParseAnyReference(refOrID)
if err != nil {
return "", "", validationError{err}
}
namedRef, ok := ref.(reference.Named)
if !ok {
digested, ok := ref.(reference.Digested)
if !ok {
return "", "", errImageDoesNotExist{ref}
}
id := image.IDFromDigest(digested.Digest())
for platform := range daemon.stores {
if _, err = daemon.stores[platform].imageStore.Get(id); err == nil {
return id, platform, nil
}
}
return "", "", errImageDoesNotExist{ref}
}
if digest, err := daemon.referenceStore.Get(namedRef); err == nil {
// Search the image stores to get the platform, defaulting to host OS.
imagePlatform := runtime.GOOS
id := image.IDFromDigest(digest)
for platform := range daemon.stores {
if img, err := daemon.stores[platform].imageStore.Get(id); err == nil {
imagePlatform = img.Platform()
break
}
}
return id, imagePlatform, nil
}
// deprecated: repo:shortid https://github.com/docker/docker/pull/799
if tagged, ok := namedRef.(reference.Tagged); ok {
if tag := tagged.Tag(); stringid.IsShortID(stringid.TruncateID(tag)) {
for platform := range daemon.stores {
if id, err := daemon.stores[platform].imageStore.Search(tag); err == nil {
for _, storeRef := range daemon.referenceStore.References(id.Digest()) {
if storeRef.Name() == namedRef.Name() {
return id, platform, nil
}
}
}
}
}
}
// Search based on ID
for platform := range daemon.stores {
if id, err := daemon.stores[platform].imageStore.Search(refOrID); err == nil {
return id, platform, nil
}
}
return "", "", errImageDoesNotExist{ref}
}
// GetImage returns an image corresponding to the image referred to by refOrID.
func (daemon *Daemon) GetImage(refOrID string) (*image.Image, error) {
imgID, platform, err := daemon.GetImageIDAndPlatform(refOrID)
if err != nil {
return nil, err
}
return daemon.stores[platform].imageStore.Get(imgID)
}