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

Commit f4271db

Browse files
committed
only cache remote images
1 parent 158af30 commit f4271db

File tree

4 files changed

+40
-12
lines changed

4 files changed

+40
-12
lines changed

cmd/analyze.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ func analyzeImage(imageName string, analyzerArgs []string) error {
7979
return fmt.Errorf("Error performing image analysis: %s", err)
8080
}
8181

82-
output.PrintToStdErr("Retrieving analyses\n")
82+
logrus.Info("retrieving analyses")
8383
outputResults(analyses)
8484

8585
if noCache && save {
86-
logrus.Infof("Image was saved at %s", image.FSPath)
86+
logrus.Infof("image was saved at %s", image.FSPath)
8787
}
8888

8989
return nil

cmd/diff.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func diffImages(image1Arg, image2Arg string, diffArgs []string) error {
7878
var wg sync.WaitGroup
7979
wg.Add(2)
8080

81-
output.PrintToStdErr("Starting diff on images %s and %s, using differs: %s\n", image1Arg, image2Arg, diffArgs)
81+
logrus.Infof("starting diff on images %s and %s, using differs: %s\n", image1Arg, image2Arg, diffArgs)
8282

8383
imageMap := map[string]*pkgutil.Image{
8484
image1Arg: {},
@@ -102,7 +102,7 @@ func diffImages(image1Arg, image2Arg string, diffArgs []string) error {
102102
defer pkgutil.CleanupImage(*imageMap[image2Arg])
103103
}
104104

105-
output.PrintToStdErr("Computing diffs\n")
105+
logrus.Info("computing diffs")
106106
req := differs.DiffRequest{
107107
Image1: *imageMap[image1Arg],
108108
Image2: *imageMap[image2Arg],
@@ -114,7 +114,7 @@ func diffImages(image1Arg, image2Arg string, diffArgs []string) error {
114114
outputResults(diffs)
115115

116116
if filename != "" {
117-
output.PrintToStdErr("Computing filename diffs\n")
117+
logrus.Info("computing filename diffs")
118118
err := diffFile(imageMap[image1Arg], imageMap[image2Arg])
119119
if err != nil {
120120
return err

cmd/root.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ func getImageForName(imageName string) (pkgutil.Image, error) {
161161
}
162162
elapsed := time.Now().Sub(start)
163163
logrus.Infof("retrieving image from daemon took %f seconds", elapsed.Seconds())
164+
noCache = true // force noCache if we have daemon image. TODO(nkubala): remove this
164165
} else {
165166
// either has remote prefix or has no prefix, in which case we force remote
166167
imageName = strings.Replace(imageName, RemotePrefix, "", -1)
@@ -191,9 +192,8 @@ func getImageForName(imageName string) (pkgutil.Image, error) {
191192
}
192193
for _, layer := range imgLayers {
193194
layerStart := time.Now()
194-
diffID, err := layer.DiffID()
195-
logrus.Infof("layer digest: %s", diffID.String())
196-
path, err := getExtractPathForName(diffID.String())
195+
digest, err := layer.Digest()
196+
path, err := getExtractPathForName(digest.String())
197197
if err != nil {
198198
return pkgutil.Image{
199199
Layers: layers,
@@ -214,7 +214,7 @@ func getImageForName(imageName string) (pkgutil.Image, error) {
214214
logrus.Infof("time elapsed retrieving image layers: %fs", elapsed.Seconds())
215215
}
216216

217-
path, err := getExtractPathForName(imageName)
217+
path, err := getExtractPathForImage(imageName, img)
218218
// extract fs into provided dir
219219
if err := pkgutil.GetFileSystemForImage(img, path, nil); err != nil {
220220
return pkgutil.Image{
@@ -230,6 +230,20 @@ func getImageForName(imageName string) (pkgutil.Image, error) {
230230
}, nil
231231
}
232232

233+
func getExtractPathForImage(imageName string, image v1.Image) (string, error) {
234+
if noCache {
235+
return getExtractPathForName(imageName)
236+
}
237+
start := time.Now()
238+
digest, err := image.Digest()
239+
if err != nil {
240+
return "", err
241+
}
242+
elapsed := time.Now().Sub(start)
243+
logrus.Infof("time elapsed retrieving image digest: %fs", elapsed.Seconds())
244+
return getExtractPathForName(pkgutil.RemoveTag(imageName) + "@" + digest.String())
245+
}
246+
233247
func getExtractPathForName(name string) (string, error) {
234248
var path string
235249
var err error
@@ -244,10 +258,11 @@ func getExtractPathForName(name string) (string, error) {
244258
if err != nil {
245259
return "", err
246260
}
247-
logrus.Infof("Image fs cached at %s", path)
261+
logrus.Infof("caching filesystem at %s", path)
248262
}
249263
} else {
250264
// otherwise, create tempdir
265+
logrus.Infof("skipping caching")
251266
path, err = ioutil.TempDir("", strings.Replace(name, "/", "", -1))
252267
if err != nil {
253268
return "", err

pkg/util/image_utils.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import (
3333
"github.com/sirupsen/logrus"
3434
)
3535

36+
const tagRegexStr = ".*:([^/]+$)"
37+
3638
type Layer struct {
3739
FSPath string
3840
}
@@ -90,7 +92,7 @@ func GetFileSystemForImage(image v1.Image, root string, whitelist []string) erro
9092
return err
9193
}
9294
if !empty {
93-
logrus.Infof("Using cached filesystem in %s", root)
95+
logrus.Infof("using cached filesystem in %s", root)
9496
return nil
9597
}
9698
if err := unpackTar(tar.NewReader(mutate.Extract(image)), root, whitelist); err != nil {
@@ -143,6 +145,17 @@ func copyToFile(outfile string, r io.Reader) error {
143145

144146
// checks to see if an image string contains a tag.
145147
func HasTag(image string) bool {
146-
tagRegex := regexp.MustCompile(".*:[^/]+$")
148+
tagRegex := regexp.MustCompile(tagRegexStr)
147149
return tagRegex.MatchString(image)
148150
}
151+
152+
// returns a raw image name with the tag removed
153+
func RemoveTag(image string) string {
154+
if !HasTag(image) {
155+
return image
156+
}
157+
tagRegex := regexp.MustCompile(tagRegexStr)
158+
parts := tagRegex.FindStringSubmatch(image)
159+
tag := parts[len(parts)-1]
160+
return image[0 : len(image)-len(tag)-1]
161+
}

0 commit comments

Comments
 (0)