-
Notifications
You must be signed in to change notification settings - Fork 2
/
image_loader.go
180 lines (152 loc) · 4.22 KB
/
image_loader.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package image
import (
"context"
"encoding/json"
"fmt"
"github.com/docker/distribution"
"github.com/docker/distribution/manifest/schema2"
dockerclient "github.com/docker/docker/client"
"github.com/docker/docker/image"
"github.com/docker/docker/layer"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/symlink"
"github.com/golang/glog"
"github.com/opencontainers/go-digest"
ociv1 "github.com/opencontainers/image-spec/specs-go/v1"
"io"
"io/ioutil"
"os"
"path/filepath"
)
type imageLoader struct {
imageDir string
manifests []manifestConf
}
func newDockerImageLoader(cc *dockerclient.Client, imageName string) (l *imageLoader, err error) {
l = &imageLoader{}
glog.V(3).Infof("Saving image %s", imageName)
reader, err := cc.ImageSave(context.Background(), []string{imageName})
if err != nil {
glog.V(3).Infof("tar image failed: %s", err)
return
}
defer reader.Close()
l.imageDir, err = ioutil.TempDir("", "papa-imageName-")
if err != nil {
glog.V(3).Infof("tmpdir failed: %s", err)
return
}
glog.V(3).Infof("Save image %s in tempdir %s", imageName, l.imageDir)
if err = archive.Unpack(reader, l.imageDir, &archive.TarOptions{
NoLchown: true,
IncludeSourceDir: true,
}); err != nil {
glog.V(3).Infof("untar image failed: %s", err)
return
}
manifestPath, err := safePath(l.imageDir, manifestFileName)
if err != nil {
glog.V(3).Infof("manifest failed: %s", err)
return
}
manifestFile, err := os.Open(manifestPath)
if err != nil {
glog.V(3).Infof("open manifest failed at %s: %s", manifestPath, err)
return
}
defer manifestFile.Close()
var manifests []manifestItem
if err = json.NewDecoder(manifestFile).Decode(&manifests); err != nil {
glog.V(3).Infof("parse manifest failed at %s: %s", manifestPath, err)
return
}
if len(manifests) == 0 {
err = fmt.Errorf("no manifest found in the imageName")
return
}
for i := range manifests {
mc := manifestConf{
manifest: &manifests[i],
}
var configPath string
configPath, err = safePath(l.imageDir, mc.manifest.Config)
if err != nil {
glog.V(3).Infof("manifest configuration failed: %s", err)
return
}
mc.rawConf, err = ioutil.ReadFile(configPath)
if err != nil {
glog.V(3).Infof("open manifest configuration failed at %s: %s", configPath, err)
return
}
mc.conf, err = image.NewFromJSON(mc.rawConf)
if err != nil {
glog.V(3).Infof("parse manifest configuration failed at %s: %s", configPath, err)
return
}
l.manifests = append(l.manifests, mc)
}
return
}
func (l *imageLoader) Close() error {
return os.RemoveAll(l.imageDir)
}
const (
manifestFileName = "manifest.json"
)
func safePath(base, path string) (string, error) {
return symlink.FollowSymlinkInScope(filepath.Join(base, path), base)
}
type manifestItem struct {
Config string
RepoTags []string
Layers []string
Parent image.ID `json:",omitempty"`
LayerSources map[layer.DiffID]distribution.Descriptor `json:",omitempty"`
}
type manifestConf struct {
manifest *manifestItem
conf *image.Image
rawConf []byte
}
func (l *imageLoader) GetLayers() (layers []*layerLoader, err error) {
for _, m := range l.manifests {
for i := range m.manifest.Layers {
layerPath := m.manifest.Layers[i]
layerID := m.conf.RootFS.DiffIDs[i]
layerDir := filepath.Join(l.imageDir, filepath.Dir(layerPath))
layerPath = filepath.Join(l.imageDir, layerPath)
var fi os.FileInfo
fi, err = os.Lstat(layerPath)
if err != nil {
glog.V(3).Infof("can't found layer at %s: %s", layerPath, err)
return
}
layers = append(layers, &layerLoader{
layerDir: layerDir,
layerPath: layerPath,
descriptor: distribution.Descriptor{
MediaType: schema2.MediaTypeUncompressedLayer,
Size: fi.Size(),
Digest: digest.Digest(layerID),
Platform: &ociv1.Platform{
Architecture: m.conf.Architecture,
OS: m.conf.OS,
},
},
})
}
}
return
}
func (l *imageLoader) GetManifests() (manifests []*manifestConf, err error) {
return
}
type layerLoader struct {
layerDir string
layerPath string
descriptor distribution.Descriptor
}
func (l *layerLoader) OpenReader() (r io.ReadCloser, err error) {
return os.Open(l.layerPath)
}