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

Commit 9e17aeb

Browse files
committed
refactor preppers to their own files, and consolidate util methods
1 parent 52e25f1 commit 9e17aeb

File tree

5 files changed

+263
-166
lines changed

5 files changed

+263
-166
lines changed

pkg/util/cloud_prepper.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Copyright 2017 Google, Inc. All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package util
18+
19+
import (
20+
"github.com/containers/image/docker"
21+
)
22+
23+
// CloudPrepper prepares images sourced from a Cloud registry
24+
type CloudPrepper struct {
25+
ImagePrepper
26+
}
27+
28+
func (p CloudPrepper) getFileSystem() (string, error) {
29+
ref, err := docker.ParseReference("//" + p.Source)
30+
if err != nil {
31+
return "", err
32+
}
33+
34+
return getFileSystemFromReference(ref, p.Source)
35+
}
36+
37+
func (p CloudPrepper) getConfig() (ConfigSchema, error) {
38+
ref, err := docker.ParseReference("//" + p.Source)
39+
if err != nil {
40+
return ConfigSchema{}, err
41+
}
42+
43+
return getConfigFromReference(ref, p.Source)
44+
}

pkg/util/daemon_prepper.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Copyright 2017 Google, Inc. All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package util
18+
19+
import (
20+
"context"
21+
"os"
22+
23+
"github.com/golang/glog"
24+
)
25+
26+
type DaemonPrepper struct {
27+
ImagePrepper
28+
}
29+
30+
func (p DaemonPrepper) getFileSystem() (string, error) {
31+
tarPath, err := saveImageToTar(p.Client, p.Source, p.Source)
32+
if err != nil {
33+
return "", err
34+
}
35+
36+
defer os.Remove(tarPath)
37+
return getImageFromTar(tarPath)
38+
}
39+
40+
func (p DaemonPrepper) getConfig() (ConfigSchema, error) {
41+
inspect, _, err := p.Client.ImageInspectWithRaw(context.Background(), p.Source)
42+
if err != nil {
43+
return ConfigSchema{}, err
44+
}
45+
46+
config := ConfigObject{
47+
Env: inspect.Config.Env,
48+
}
49+
history := p.getHistory()
50+
return ConfigSchema{
51+
Config: config,
52+
History: history,
53+
}, nil
54+
}
55+
56+
func (p DaemonPrepper) getHistory() []ImageHistoryItem {
57+
history, err := p.Client.ImageHistory(context.Background(), p.Source)
58+
if err != nil {
59+
glog.Error("Could not obtain image history for %s: %s", p.Source, err)
60+
}
61+
historyItems := []ImageHistoryItem{}
62+
for _, item := range history {
63+
historyItems = append(historyItems, ImageHistoryItem{CreatedBy: item.CreatedBy})
64+
}
65+
return historyItems
66+
}

pkg/util/image_prep_utils.go

Lines changed: 9 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,19 @@ package util
1919
import (
2020
"archive/tar"
2121
"compress/gzip"
22-
"context"
2322
"encoding/json"
2423
"errors"
2524
"io/ioutil"
2625
"os"
2726
"path/filepath"
28-
"regexp"
2927
"strings"
3028

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"
3530
"github.com/golang/glog"
3631
)
3732

3833
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} },
4035
"URL": func(ip ImagePrepper) Prepper { return CloudPrepper{ImagePrepper: ip} },
4136
"tar": func(ip ImagePrepper) Prepper { return TarPrepper{ImagePrepper: ip} },
4237
}
@@ -66,79 +61,22 @@ type ConfigSchema struct {
6661
History []ImageHistoryItem `json:"history"`
6762
}
6863

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-
11264
func getImageFromTar(tarPath string) (string, error) {
11365
glog.Info("Extracting image tar to obtain image file system")
11466
path := strings.TrimSuffix(tarPath, filepath.Ext(tarPath))
11567
err := unpackDockerSave(tarPath, path)
11668
return path, err
11769
}
11870

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)
13174

13275
path, err := ioutil.TempDir("", sanitizedName)
13376
if err != nil {
13477
return "", err
13578
}
13679

137-
ref, err := docker.ParseReference("//" + p.Source)
138-
if err != nil {
139-
return "", err
140-
}
141-
14280
img, err := ref.NewImage(nil)
14381
if err != nil {
14482
glog.Error(err)
@@ -170,121 +108,26 @@ func (p CloudPrepper) getFileSystem() (string, error) {
170108
return path, nil
171109
}
172110

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) {
179112
img, err := ref.NewImage(nil)
180113
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)
182115
return ConfigSchema{}, errors.New("Could not obtain image config")
183116
}
184117
defer img.Close()
185118

186119
configBlob, err := img.ConfigBlob()
187120
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)
189122
return ConfigSchema{}, errors.New("Could not obtain image config")
190123
}
191124

192125
var config ConfigSchema
193126
err = json.Unmarshal(configBlob, &config)
194127
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)
280129
return ConfigSchema{}, errors.New("Could not obtain image config")
281130
}
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-
288131
return config, nil
289132
}
290133

0 commit comments

Comments
 (0)