Skip to content
This repository was archived by the owner on Mar 27, 2024. It is now read-only.

Commit df97031

Browse files
authored
Merge pull request #67 from dlorenc/reflect
Stop using reflect in image_prep_utils.
2 parents 8be7997 + 1d26787 commit df97031

File tree

15 files changed

+1076
-1141
lines changed

15 files changed

+1076
-1141
lines changed

Godeps/Godeps.json

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

utils/image_prep_utils.go

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ import (
99
"io/ioutil"
1010
"os"
1111
"path/filepath"
12-
"reflect"
1312
"regexp"
1413
"strings"
1514

1615
"github.com/containers/image/docker"
16+
"github.com/containers/image/docker/tarfile"
1717
"github.com/containers/image/types"
1818
"github.com/docker/docker/client"
1919

2020
"github.com/golang/glog"
2121
)
2222

23-
var sourceToPrepMap = map[string]Prepper{
24-
"ID": IDPrepper{},
25-
"URL": CloudPrepper{},
26-
"tar": TarPrepper{},
23+
var sourceToPrepMap = map[string]func(ip ImagePrepper) Prepper{
24+
"ID": func(ip ImagePrepper) Prepper { return IDPrepper{ImagePrepper: ip} },
25+
"URL": func(ip ImagePrepper) Prepper { return CloudPrepper{ImagePrepper: ip} },
26+
"tar": func(ip ImagePrepper) Prepper { return TarPrepper{ImagePrepper: ip} },
2727
}
2828

2929
var sourceCheckMap = map[string]func(string) bool{
@@ -68,9 +68,7 @@ func (p ImagePrepper) GetImage() (Image, error) {
6868
var prepper Prepper
6969
for source, check := range sourceCheckMap {
7070
if check(img) {
71-
typePrepper := reflect.TypeOf(sourceToPrepMap[source])
72-
prepper = reflect.New(typePrepper).Interface().(Prepper)
73-
reflect.ValueOf(prepper).Elem().Field(0).Set(reflect.ValueOf(p))
71+
prepper = sourceToPrepMap[source](p)
7472
break
7573
}
7674
}
@@ -258,38 +256,35 @@ func (p TarPrepper) getConfig() (ConfigSchema, error) {
258256
if err != nil {
259257
return ConfigSchema{}, err
260258
}
261-
contents, err := ioutil.ReadDir(tempDir)
259+
260+
var config ConfigSchema
261+
// First open the manifest, then find the referenced config.
262+
manifestPath := filepath.Join(tempDir, "manifest.json")
263+
contents, err := ioutil.ReadFile(manifestPath)
262264
if err != nil {
263-
glog.Errorf("Could not read image tar contents: %s", err)
264-
return ConfigSchema{}, errors.New("Could not obtain image config")
265+
return ConfigSchema{}, err
265266
}
266267

267-
var config ConfigSchema
268-
configList := []string{}
269-
for _, item := range contents {
270-
if filepath.Ext(item.Name()) == ".json" && item.Name() != "manifest.json" {
271-
if len(configList) != 0 {
272-
// Another <image>.json file has already been processed and the image config obtained is uncertain.
273-
glog.Error("Multiple possible config sources detected for image at " + p.Source + ". Multiple images likely contained in tar. Choosing first one, but diff results may not be completely accurate.")
274-
break
275-
}
276-
fileName := filepath.Join(tempDir, item.Name())
277-
file, err := ioutil.ReadFile(fileName)
278-
if err != nil {
279-
glog.Errorf("Could not read config file %s: %s", fileName, err)
280-
return ConfigSchema{}, errors.New("Could not obtain image config")
281-
}
282-
err = json.Unmarshal(file, &config)
283-
if err != nil {
284-
glog.Errorf("Could not marshal config file %s: %s", fileName, err)
285-
return ConfigSchema{}, errors.New("Could not obtain image config")
286-
}
287-
configList = append(configList, fileName)
288-
}
268+
manifests := []tarfile.ManifestItem{}
269+
if err := json.Unmarshal(contents, &manifests); err != nil {
270+
return ConfigSchema{}, err
271+
}
272+
273+
if len(manifests) != 1 {
274+
return ConfigSchema{}, errors.New("specified tar file contains multiple images")
275+
}
276+
277+
cfgFilename := filepath.Join(tempDir, manifests[0].Config)
278+
file, err := ioutil.ReadFile(cfgFilename)
279+
if err != nil {
280+
glog.Errorf("Could not read config file %s: %s", cfgFilename, err)
281+
return ConfigSchema{}, errors.New("Could not obtain image config")
289282
}
290-
if reflect.DeepEqual(ConfigSchema{}, config) {
291-
glog.Warningf("No image config found in tar source %s. Pip differ may be incomplete due to missing PYTHONPATH information.")
292-
return config, nil
283+
err = json.Unmarshal(file, &config)
284+
if err != nil {
285+
glog.Errorf("Could not marshal config file %s: %s", cfgFilename, err)
286+
return ConfigSchema{}, errors.New("Could not obtain image config")
293287
}
288+
294289
return config, nil
295290
}

0 commit comments

Comments
 (0)