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

Commit 7b0ac08

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

File tree

4 files changed

+41
-12
lines changed

4 files changed

+41
-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: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ 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+
// TODO(nkubala): remove this when we can set compression level in containerregistry
165+
noCache = true // force noCache if image is in daemon
164166
} else {
165167
// either has remote prefix or has no prefix, in which case we force remote
166168
imageName = strings.Replace(imageName, RemotePrefix, "", -1)
@@ -191,9 +193,8 @@ func getImageForName(imageName string) (pkgutil.Image, error) {
191193
}
192194
for _, layer := range imgLayers {
193195
layerStart := time.Now()
194-
diffID, err := layer.DiffID()
195-
logrus.Infof("layer digest: %s", diffID.String())
196-
path, err := getExtractPathForName(diffID.String())
196+
digest, err := layer.Digest()
197+
path, err := getExtractPathForName(digest.String())
197198
if err != nil {
198199
return pkgutil.Image{
199200
Layers: layers,
@@ -214,7 +215,7 @@ func getImageForName(imageName string) (pkgutil.Image, error) {
214215
logrus.Infof("time elapsed retrieving image layers: %fs", elapsed.Seconds())
215216
}
216217

217-
path, err := getExtractPathForName(imageName)
218+
path, err := getExtractPathForImage(imageName, img)
218219
// extract fs into provided dir
219220
if err := pkgutil.GetFileSystemForImage(img, path, nil); err != nil {
220221
return pkgutil.Image{
@@ -230,6 +231,20 @@ func getImageForName(imageName string) (pkgutil.Image, error) {
230231
}, nil
231232
}
232233

234+
func getExtractPathForImage(imageName string, image v1.Image) (string, error) {
235+
if noCache {
236+
return getExtractPathForName(imageName)
237+
}
238+
start := time.Now()
239+
digest, err := image.Digest()
240+
if err != nil {
241+
return "", err
242+
}
243+
elapsed := time.Now().Sub(start)
244+
logrus.Infof("time elapsed retrieving image digest: %fs", elapsed.Seconds())
245+
return getExtractPathForName(pkgutil.RemoveTag(imageName) + "@" + digest.String())
246+
}
247+
233248
func getExtractPathForName(name string) (string, error) {
234249
var path string
235250
var err error
@@ -244,10 +259,11 @@ func getExtractPathForName(name string) (string, error) {
244259
if err != nil {
245260
return "", err
246261
}
247-
logrus.Infof("Image fs cached at %s", path)
262+
logrus.Infof("caching filesystem at %s", path)
248263
}
249264
} else {
250265
// otherwise, create tempdir
266+
logrus.Infof("skipping caching")
251267
path, err = ioutil.TempDir("", strings.Replace(name, "/", "", -1))
252268
if err != nil {
253269
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)