@@ -19,24 +19,19 @@ package util
19
19
import (
20
20
"archive/tar"
21
21
"compress/gzip"
22
- "context"
23
22
"encoding/json"
24
23
"errors"
25
24
"io/ioutil"
26
25
"os"
27
26
"path/filepath"
28
- "regexp"
29
27
"strings"
30
28
31
- "github.com/containers/image/docker"
32
- "github.com/containers/image/docker/tarfile"
33
- "github.com/docker/docker/client"
34
-
29
+ "github.com/containers/image/types"
35
30
"github.com/golang/glog"
36
31
)
37
32
38
33
var sourceToPrepMap = map [string ]func (ip ImagePrepper ) Prepper {
39
- "ID" : func (ip ImagePrepper ) Prepper { return IDPrepper {ImagePrepper : ip } },
34
+ "ID" : func (ip ImagePrepper ) Prepper { return DaemonPrepper {ImagePrepper : ip } },
40
35
"URL" : func (ip ImagePrepper ) Prepper { return CloudPrepper {ImagePrepper : ip } },
41
36
"tar" : func (ip ImagePrepper ) Prepper { return TarPrepper {ImagePrepper : ip } },
42
37
}
@@ -66,79 +61,22 @@ type ConfigSchema struct {
66
61
History []ImageHistoryItem `json:"history"`
67
62
}
68
63
69
- type ImagePrepper struct {
70
- Source string
71
- Client * client.Client
72
- }
73
-
74
- type Prepper interface {
75
- getFileSystem () (string , error )
76
- getConfig () (ConfigSchema , error )
77
- }
78
-
79
- func (p ImagePrepper ) GetImage () (Image , error ) {
80
- glog .Infof ("Starting prep for image %s" , p .Source )
81
- img := p .Source
82
-
83
- var prepper Prepper
84
- for source , check := range sourceCheckMap {
85
- if check (img ) {
86
- prepper = sourceToPrepMap [source ](p )
87
- break
88
- }
89
- }
90
- if prepper == nil {
91
- return Image {}, errors .New ("Could not retrieve image from source" )
92
- }
93
-
94
- imgPath , err := prepper .getFileSystem ()
95
- if err != nil {
96
- return Image {}, err
97
- }
98
-
99
- config , err := prepper .getConfig ()
100
- if err != nil {
101
- glog .Error ("Error retrieving History: " , err )
102
- }
103
-
104
- glog .Infof ("Finished prepping image %s" , p .Source )
105
- return Image {
106
- Source : img ,
107
- FSPath : imgPath ,
108
- Config : config ,
109
- }, nil
110
- }
111
-
112
64
func getImageFromTar (tarPath string ) (string , error ) {
113
65
glog .Info ("Extracting image tar to obtain image file system" )
114
66
path := strings .TrimSuffix (tarPath , filepath .Ext (tarPath ))
115
67
err := unpackDockerSave (tarPath , path )
116
68
return path , err
117
69
}
118
70
119
- // CloudPrepper prepares images sourced from a Cloud registry
120
- type CloudPrepper struct {
121
- ImagePrepper
122
- }
123
-
124
- func (p CloudPrepper ) getFileSystem () (string , error ) {
125
- // The regexp when passed a string creates a list of the form
126
- // [repourl/image:tag, image:tag, tag] (the tag may or may not be present)
127
- URLPattern := regexp .MustCompile ("^.+/(.+(:.+){0,1})$" )
128
- URLMatch := URLPattern .FindStringSubmatch (p .Source )
129
- // Removing the ":" so that the image path name can be <image><tag>
130
- sanitizedName := strings .Replace (URLMatch [1 ], ":" , "" , - 1 )
71
+ func getFileSystemFromReference (ref types.ImageReference , imageName string ) (string , error ) {
72
+ sanitizedName := strings .Replace (imageName , ":" , "" , - 1 )
73
+ sanitizedName = strings .Replace (sanitizedName , "/" , "" , - 1 )
131
74
132
75
path , err := ioutil .TempDir ("" , sanitizedName )
133
76
if err != nil {
134
77
return "" , err
135
78
}
136
79
137
- ref , err := docker .ParseReference ("//" + p .Source )
138
- if err != nil {
139
- return "" , err
140
- }
141
-
142
80
img , err := ref .NewImage (nil )
143
81
if err != nil {
144
82
glog .Error (err )
@@ -170,121 +108,26 @@ func (p CloudPrepper) getFileSystem() (string, error) {
170
108
return path , nil
171
109
}
172
110
173
- func (p CloudPrepper ) getConfig () (ConfigSchema , error ) {
174
- ref , err := docker .ParseReference ("//" + p .Source )
175
- if err != nil {
176
- return ConfigSchema {}, err
177
- }
178
-
111
+ func getConfigFromReference (ref types.ImageReference , source string ) (ConfigSchema , error ) {
179
112
img , err := ref .NewImage (nil )
180
113
if err != nil {
181
- glog .Errorf ("Error referencing image %s from registry: %s" , p . Source , err )
114
+ glog .Errorf ("Error referencing image %s from registry: %s" , source , err )
182
115
return ConfigSchema {}, errors .New ("Could not obtain image config" )
183
116
}
184
117
defer img .Close ()
185
118
186
119
configBlob , err := img .ConfigBlob ()
187
120
if err != nil {
188
- glog .Errorf ("Error obtaining config blob for image %s from registry: %s" , p . Source , err )
121
+ glog .Errorf ("Error obtaining config blob for image %s from registry: %s" , source , err )
189
122
return ConfigSchema {}, errors .New ("Could not obtain image config" )
190
123
}
191
124
192
125
var config ConfigSchema
193
126
err = json .Unmarshal (configBlob , & config )
194
127
if err != nil {
195
- glog .Errorf ("Error with config file struct for image %s: %s" , p .Source , err )
196
- return ConfigSchema {}, errors .New ("Could not obtain image config" )
197
- }
198
- return config , nil
199
- }
200
-
201
- type IDPrepper struct {
202
- ImagePrepper
203
- }
204
-
205
- func (p IDPrepper ) getFileSystem () (string , error ) {
206
- tarPath , err := saveImageToTar (p .Client , p .Source , p .Source )
207
- if err != nil {
208
- return "" , err
209
- }
210
-
211
- defer os .Remove (tarPath )
212
- return getImageFromTar (tarPath )
213
- }
214
-
215
- func (p IDPrepper ) getConfig () (ConfigSchema , error ) {
216
- inspect , _ , err := p .Client .ImageInspectWithRaw (context .Background (), p .Source )
217
- if err != nil {
218
- return ConfigSchema {}, err
219
- }
220
-
221
- config := ConfigObject {
222
- Env : inspect .Config .Env ,
223
- }
224
- history := p .getHistory ()
225
- return ConfigSchema {
226
- Config : config ,
227
- History : history ,
228
- }, nil
229
- }
230
-
231
- func (p IDPrepper ) getHistory () []ImageHistoryItem {
232
- history , err := p .Client .ImageHistory (context .Background (), p .Source )
233
- if err != nil {
234
- glog .Error ("Could not obtain image history for %s: %s" , p .Source , err )
235
- }
236
- historyItems := []ImageHistoryItem {}
237
- for _ , item := range history {
238
- historyItems = append (historyItems , ImageHistoryItem {CreatedBy : item .CreatedBy })
239
- }
240
- return historyItems
241
- }
242
-
243
- type TarPrepper struct {
244
- ImagePrepper
245
- }
246
-
247
- func (p TarPrepper ) getFileSystem () (string , error ) {
248
- return getImageFromTar (p .Source )
249
- }
250
-
251
- func (p TarPrepper ) getConfig () (ConfigSchema , error ) {
252
- tempDir := strings .TrimSuffix (p .Source , filepath .Ext (p .Source )) + "-config"
253
- defer os .RemoveAll (tempDir )
254
- err := UnTar (p .Source , tempDir )
255
- if err != nil {
256
- return ConfigSchema {}, err
257
- }
258
-
259
- var config ConfigSchema
260
- // First open the manifest, then find the referenced config.
261
- manifestPath := filepath .Join (tempDir , "manifest.json" )
262
- contents , err := ioutil .ReadFile (manifestPath )
263
- if err != nil {
264
- return ConfigSchema {}, err
265
- }
266
-
267
- manifests := []tarfile.ManifestItem {}
268
- if err := json .Unmarshal (contents , & manifests ); err != nil {
269
- return ConfigSchema {}, err
270
- }
271
-
272
- if len (manifests ) != 1 {
273
- return ConfigSchema {}, errors .New ("specified tar file contains multiple images" )
274
- }
275
-
276
- cfgFilename := filepath .Join (tempDir , manifests [0 ].Config )
277
- file , err := ioutil .ReadFile (cfgFilename )
278
- if err != nil {
279
- glog .Errorf ("Could not read config file %s: %s" , cfgFilename , err )
128
+ glog .Errorf ("Error with config file struct for image %s: %s" , source , err )
280
129
return ConfigSchema {}, errors .New ("Could not obtain image config" )
281
130
}
282
- err = json .Unmarshal (file , & config )
283
- if err != nil {
284
- glog .Errorf ("Could not marshal config file %s: %s" , cfgFilename , err )
285
- return ConfigSchema {}, errors .New ("Could not obtain image config" )
286
- }
287
-
288
131
return config , nil
289
132
}
290
133
0 commit comments